From 0d326383799a7d7bb69fec9dcd1eaf9e1a64eab8 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 11 Feb 2011 15:20:26 -0500 Subject: XMPPError exceptions can keep a stanza's contents. This allows exceptions to include the original content of a stanza in the error response by including the parameter clear=False when raising the exception. --- sleekxmpp/xmlstream/stanzabase.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index 3937a7a9..1f229cea 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -1161,12 +1161,17 @@ class StanzaBase(ElementBase): self.clear() return self - def reply(self): + def reply(self, clear=True): """ - Reset the stanza and swap its 'from' and 'to' attributes to prepare - for sending a reply stanza. + Swap the 'from' and 'to' attributes to prepare the stanza for + sending a reply. If clear=True, then also remove the stanza's + contents to make room for the reply content. For client streams, the 'from' attribute is removed. + + Arguments: + clear -- Indicates if the stanza's contents should be + removed. Defaults to True """ # if it's a component, use from if self.stream and hasattr(self.stream, "is_component") and \ @@ -1175,7 +1180,8 @@ class StanzaBase(ElementBase): else: self['to'] = self['from'] del self['from'] - self.clear() + if clear: + self.clear() return self def error(self): -- cgit v1.2.3 From 70af52d74c3f4253791120be4016f108aa966b4d Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 13 Feb 2011 16:28:06 -0500 Subject: Make one-off Callbacks ready for deletion after the prerun step. Waiting until the actual run step means that the handler is not marked for deletion when checked in the __spawn_event() thread, causing the callback to stay in the handler list. --- sleekxmpp/xmlstream/handler/callback.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/handler/callback.py b/sleekxmpp/xmlstream/handler/callback.py index f0a72853..cfd3c43c 100644 --- a/sleekxmpp/xmlstream/handler/callback.py +++ b/sleekxmpp/xmlstream/handler/callback.py @@ -62,6 +62,8 @@ class Callback(BaseHandler): payload -- The matched stanza object. """ BaseHandler.prerun(self, payload) + if self._once: + self._destroy = True if self._instream: self.run(payload, True) -- cgit v1.2.3 From 8b5511c7ec036aef6a243c3c5e801cdaa7a4863a Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 13 Feb 2011 16:40:04 -0500 Subject: Simplification when removing a deletable handler. --- sleekxmpp/xmlstream/xmlstream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 1cd23fba..560b80a0 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -845,7 +845,7 @@ class XMLStream(object): self.event_queue.put(('stanza', handler, stanza_copy)) try: if handler.check_delete(): - self.__handlers.pop(self.__handlers.index(handler)) + self.__handlers.remove(handler) except: pass # not thread safe unhandled = False -- cgit v1.2.3 From 9004e8bbf2c29d23fad20467791d9a8224d4bdb3 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 14 Feb 2011 11:13:41 -0500 Subject: Break references that can prevent garbage collection. --- sleekxmpp/xmlstream/handler/callback.py | 1 + 1 file changed, 1 insertion(+) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/handler/callback.py b/sleekxmpp/xmlstream/handler/callback.py index cfd3c43c..f1fda805 100644 --- a/sleekxmpp/xmlstream/handler/callback.py +++ b/sleekxmpp/xmlstream/handler/callback.py @@ -84,3 +84,4 @@ class Callback(BaseHandler): self._pointer(payload) if self._once: self._destroy = True + del self._pointer -- cgit v1.2.3 From e0f9025e7c6fe51243222aeb684b94eda1a2be5f Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 14 Feb 2011 11:30:04 -0500 Subject: More attempts at fixing garbage collection. Don't keep a reference to stanzas in Callback objects. --- sleekxmpp/xmlstream/handler/callback.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/handler/callback.py b/sleekxmpp/xmlstream/handler/callback.py index f1fda805..7fadab43 100644 --- a/sleekxmpp/xmlstream/handler/callback.py +++ b/sleekxmpp/xmlstream/handler/callback.py @@ -61,7 +61,6 @@ class Callback(BaseHandler): Arguments: payload -- The matched stanza object. """ - BaseHandler.prerun(self, payload) if self._once: self._destroy = True if self._instream: @@ -80,7 +79,6 @@ class Callback(BaseHandler): Defaults to False. """ if not self._instream or instream: - BaseHandler.run(self, payload) self._pointer(payload) if self._once: self._destroy = True -- cgit v1.2.3 From 75584d7ad74b284d30164cde0b5efec2c845d207 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 14 Feb 2011 13:49:43 -0500 Subject: Remap old method names in a better way. This should prevent some reference cycles that will cause garbage collection issues. --- sleekxmpp/xmlstream/handler/base.py | 7 ++++-- sleekxmpp/xmlstream/matcher/xmlmask.py | 3 ++- sleekxmpp/xmlstream/scheduler.py | 3 ++- sleekxmpp/xmlstream/stanzabase.py | 45 +++++++++++++++++----------------- sleekxmpp/xmlstream/xmlstream.py | 29 ++++++++++++---------- 5 files changed, 47 insertions(+), 40 deletions(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/handler/base.py b/sleekxmpp/xmlstream/handler/base.py index 9c704ec6..6ec9b6a3 100644 --- a/sleekxmpp/xmlstream/handler/base.py +++ b/sleekxmpp/xmlstream/handler/base.py @@ -42,8 +42,6 @@ class BaseHandler(object): this handler. stream -- The XMLStream instance the handler should monitor. """ - self.checkDelete = self.check_delete - self.name = name self.stream = stream self._destroy = False @@ -87,3 +85,8 @@ class BaseHandler(object): handlers. """ return self._destroy + + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +BaseHandler.checkDelete = BaseHandler.check_delete diff --git a/sleekxmpp/xmlstream/matcher/xmlmask.py b/sleekxmpp/xmlstream/matcher/xmlmask.py index 60e19495..53ccc9ba 100644 --- a/sleekxmpp/xmlstream/matcher/xmlmask.py +++ b/sleekxmpp/xmlstream/matcher/xmlmask.py @@ -117,7 +117,8 @@ class MatchXMLMask(MatcherBase): return False # If the mask includes text, compare it. - if mask.text and source.text and source.text.strip() != mask.text.strip(): + if mask.text and source.text and \ + source.text.strip() != mask.text.strip(): return False # Compare attributes. The stanza must include the attributes diff --git a/sleekxmpp/xmlstream/scheduler.py b/sleekxmpp/xmlstream/scheduler.py index 14359102..0e711b4b 100644 --- a/sleekxmpp/xmlstream/scheduler.py +++ b/sleekxmpp/xmlstream/scheduler.py @@ -140,7 +140,8 @@ class Scheduler(object): """Process scheduled tasks.""" self.run = True try: - while self.run and (self.parentstop is None or not self.parentstop.isSet()): + while self.run and (self.parentstop is None or \ + not self.parentstop.isSet()): wait = 1 updated = False if self.schedule: diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index 1f229cea..753977c1 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -218,18 +218,6 @@ class ElementBase(object): xml -- Initialize the stanza with optional existing XML. parent -- Optional stanza object that contains this stanza. """ - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.initPlugin = self.init_plugin - self._getAttr = self._get_attr - self._setAttr = self._set_attr - self._delAttr = self._del_attr - self._getSubText = self._get_sub_text - self._setSubText = self._set_sub_text - self._delSub = self._del_sub - self.getStanzaValues = self._get_stanza_values - self.setStanzaValues = self._set_stanza_values - self.xml = xml self.plugins = OrderedDict() self.iterables = [] @@ -1076,17 +1064,6 @@ class StanzaBase(ElementBase): sfrom -- Optional string or JID object of the sender's JID. sid -- Optional ID value for the stanza. """ - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.setType = self.set_type - self.getTo = self.get_to - self.setTo = self.set_to - self.getFrom = self.get_from - self.setFrom = self.set_from - self.getPayload = self.get_payload - self.setPayload = self.set_payload - self.delPayload = self.del_payload - self.stream = stream if stream is not None: self.namespace = stream.default_ns @@ -1224,3 +1201,25 @@ class StanzaBase(ElementBase): return tostring(self.xml, xmlns='', stanza_ns=self.namespace, stream=self.stream) + + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +ElementBase.initPlugin = ElementBase.init_plugin +ElementBase._getAttr = ElementBase._get_attr +ElementBase._setAttr = ElementBase._set_attr +ElementBase._delAttr = ElementBase._del_attr +ElementBase._getSubText = ElementBase._get_sub_text +ElementBase._setSubText = ElementBase._set_sub_text +ElementBase._delSub = ElementBase._del_sub +ElementBase.getStanzaValues = ElementBase._get_stanza_values +ElementBase.setStanzaValues = ElementBase._set_stanza_values + +StanzaBase.setType = StanzaBase.set_type +StanzaBase.getTo = StanzaBase.get_to +StanzaBase.setTo = StanzaBase.set_to +StanzaBase.getFrom = StanzaBase.get_from +StanzaBase.setFrom = StanzaBase.set_from +StanzaBase.getPayload = StanzaBase.get_payload +StanzaBase.setPayload = StanzaBase.set_payload +StanzaBase.delPayload = StanzaBase.del_payload diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 560b80a0..87771adf 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -149,19 +149,6 @@ class XMLStream(object): port -- The port to use for the connection. Defaults to 0. """ - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.startTLS = self.start_tls - self.registerStanza = self.register_stanza - self.removeStanza = self.remove_stanza - self.registerHandler = self.register_handler - self.removeHandler = self.remove_handler - self.setSocket = self.set_socket - self.sendRaw = self.send_raw - self.getId = self.get_id - self.getNewId = self.new_id - self.sendXML = self.send_xml - self.ssl_support = SSL_SUPPORT self.ssl_version = ssl.PROTOCOL_TLSv1 self.ca_certs = None @@ -970,9 +957,11 @@ class XMLStream(object): is not caught. """ init_old = threading.Thread.__init__ + def init(self, *args, **kwargs): init_old(self, *args, **kwargs) run_old = self.run + def run_with_except_hook(*args, **kw): try: run_old(*args, **kw) @@ -982,3 +971,17 @@ class XMLStream(object): sys.excepthook(*sys.exc_info()) self.run = run_with_except_hook threading.Thread.__init__ = init + + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +XMLStream.startTLS = XMLStream.start_tls +XMLStream.registerStanza = XMLStream.register_stanza +XMLStream.removeStanza = XMLStream.remove_stanza +XMLStream.registerHandler = XMLStream.register_handler +XMLStream.removeHandler = XMLStream.remove_handler +XMLStream.setSocket = XMLStream.set_socket +XMLStream.sendRaw = XMLStream.send_raw +XMLStream.getId = XMLStream.get_id +XMLStream.getNewId = XMLStream.new_id +XMLStream.sendXML = XMLStream.send_xml -- cgit v1.2.3 From d709f8db657aa1d1314082d842dd29e2546739c4 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 14 Feb 2011 13:50:59 -0500 Subject: Use the _build_stanza method. --- sleekxmpp/xmlstream/xmlstream.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 87771adf..a5151d7b 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -813,13 +813,7 @@ class XMLStream(object): # Convert the raw XML object into a stanza object. If no registered # stanza type applies, a generic StanzaBase stanza will be used. - stanza_type = StanzaBase - for stanza_class in self.__root_stanza: - if xml.tag == "{%s}%s" % (self.default_ns, stanza_class.name) or \ - xml.tag == stanza_class.tag_name(): - stanza_type = stanza_class - break - stanza = stanza_type(self, xml) + stanza = self._build_stanza(xml) # Match the stanza against registered handlers. Handlers marked # to run "in stream" will be executed immediately; the rest will @@ -827,7 +821,7 @@ class XMLStream(object): unhandled = True for handler in self.__handlers: if handler.match(stanza): - stanza_copy = stanza_type(self, copy.deepcopy(xml)) + stanza_copy = copy.copy(stanza) handler.prerun(stanza_copy) self.event_queue.put(('stanza', handler, stanza_copy)) try: -- cgit v1.2.3