Change in osmo-gsm-tester[master]: esme: fix smpplib imports

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

pespin gerrit-no-reply at lists.osmocom.org
Mon May 11 09:56:41 UTC 2020


pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-gsm-tester/+/18199 )


Change subject: esme: fix smpplib imports
......................................................................

esme: fix smpplib imports

smpplib modules are used in several places, so it's not enough to import
them in the constructor since they are only imported for the function
scope.

Change-Id: I6511e28c7ef6aa86e85a7e03aa10a67d87896588
---
M src/osmo_gsm_tester/obj/esme.py
1 file changed, 33 insertions(+), 18 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/99/18199/1

diff --git a/src/osmo_gsm_tester/obj/esme.py b/src/osmo_gsm_tester/obj/esme.py
index e23e88c..13e1bba 100644
--- a/src/osmo_gsm_tester/obj/esme.py
+++ b/src/osmo_gsm_tester/obj/esme.py
@@ -27,6 +27,25 @@
 MAX_SYS_ID_LEN = 16
 MAX_PASSWD_LEN = 16
 
+smpplib_gsm = None
+smpplib_client = None
+smpplib_command = None
+smpplib_consts = None
+smpplib_exceptions = None
+def _import_smpplib_modules():
+    global smpplib_gsm, smpplib_client, smpplib_command, smpplib_consts, smpplib_exceptions
+    if smpplib_exceptions is None:
+        import smpplib.gsm
+        import smpplib.client
+        import smpplib.command
+        import smpplib.consts
+        import smpplib.exceptions
+        smpplib_gsm = smpplib.gsm
+        smpplib_client = smpplib.client
+        smpplib_command = smpplib.command
+        smpplib_consts = smpplib.consts
+        smpplib_exceptions = smpplib.exceptions
+
 class Esme(log.Origin):
 
     def __init__(self, msisdn):
@@ -42,13 +61,9 @@
         self.listening = False
         self.references_pending_receipt = []
         self.next_user_message_reference = 1
-        import smpplib.gsm
-        import smpplib.client
-        import smpplib.command
-        import smpplib.consts
-        import smpplib.exceptions
-        self.MSGMODE_TRANSACTION = smpplib.consts.SMPP_MSGMODE_FORWARD
-        self.MSGMODE_STOREFORWARD = smpplib.consts.SMPP_MSGMODE_STOREFORWARD
+        _import_smpplib_modules()
+        self.MSGMODE_TRANSACTION = smpplib_consts.SMPP_MSGMODE_FORWARD
+        self.MSGMODE_STOREFORWARD = smpplib_consts.SMPP_MSGMODE_STOREFORWARD
 
     def __del__(self):
         self.cleanup()
@@ -56,7 +71,7 @@
     def cleanup(self):
         try:
             self.disconnect()
-        except smpplib.exceptions.ConnectionError:
+        except smpplib_exceptions.ConnectionError:
             pass
 
     def set_smsc(self, smsc):
@@ -95,7 +110,7 @@
         host, port = self.smsc.addr_port
         if self.client:
             self.disconnect()
-        self.client = smpplib.client.Client(host, port, timeout=None)
+        self.client = smpplib_client.Client(host, port, timeout=None)
         self.client.set_message_sent_handler(
             lambda pdu: self.dbg('Unhandled submit_sm_resp message:', pdu.sequence) )
         self.client.set_message_received_handler(self._message_received_handler)
@@ -117,9 +132,9 @@
 
     def _message_received_handler(self, pdu, *args):
         self.dbg('message received:', seq=pdu.sequence)
-        if isinstance(pdu, smpplib.command.AlertNotification):
+        if isinstance(pdu, smpplib_command.AlertNotification):
             self.dbg('message received:  AlertNotification:', ms_availability_status=pdu.ms_availability_status)
-        elif isinstance(pdu, smpplib.command.DeliverSM):
+        elif isinstance(pdu, smpplib_command.DeliverSM):
             umref = int(pdu.user_message_reference)
             self.dbg('message received: DeliverSM', references_pending_receipt=self.references_pending_receipt, user_message_reference=umref)
             self.references_pending_receipt.remove(umref)
@@ -131,25 +146,25 @@
         try:
             method(*args)
             #it should not succeed, raise an exception:
-            raise log.Error('SMPP Failure: %s should have failed with SMPP error %d (%s) but succeeded.' % (method, errcode, smpplib.consts.DESCRIPTIONS[errcode]))
-        except smpplib.exceptions.PDUError as e:
+            raise log.Error('SMPP Failure: %s should have failed with SMPP error %d (%s) but succeeded.' % (method, errcode, smpplib_consts.DESCRIPTIONS[errcode]))
+        except smpplib_exceptions.PDUError as e:
             if e.args[1] != errcode:
                 raise e
             self.dbg('Expected failure triggered: %d' % errcode)
 
     def sms_send(self, sms_obj, mode, receipt=False):
-        parts, encoding_flag, msg_type_flag = smpplib.gsm.make_parts(str(sms_obj))
+        parts, encoding_flag, msg_type_flag = smpplib_gsm.make_parts(str(sms_obj))
         seqs = []
         self.log('Sending SMS "%s" to %s' % (str(sms_obj), sms_obj.dst_msisdn()))
         umref = self.next_user_message_reference
         self.next_user_message_reference = (self.next_user_message_reference + 1) % (1 << 8)
         for part in parts:
             pdu = self.client.send_message(
-                source_addr_ton=smpplib.consts.SMPP_TON_INTL,
-                source_addr_npi=smpplib.consts.SMPP_NPI_ISDN,
+                source_addr_ton=smpplib_consts.SMPP_TON_INTL,
+                source_addr_npi=smpplib_consts.SMPP_NPI_ISDN,
                 source_addr=sms_obj.src_msisdn(),
-                dest_addr_ton=smpplib.consts.SMPP_TON_INTL,
-                dest_addr_npi=smpplib.consts.SMPP_NPI_ISDN,
+                dest_addr_ton=smpplib_consts.SMPP_TON_INTL,
+                dest_addr_npi=smpplib_consts.SMPP_NPI_ISDN,
                 destination_addr=sms_obj.dst_msisdn(),
                 short_message=part,
                 data_coding=encoding_flag,

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-gsm-tester/+/18199
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Change-Id: I6511e28c7ef6aa86e85a7e03aa10a67d87896588
Gerrit-Change-Number: 18199
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200511/b6194b8b/attachment.htm>


More information about the gerrit-log mailing list