From a8ff3586d3852fc70b1932a579cf8a28ce920d5c Mon Sep 17 00:00:00 2001 From: Nathan Fritz Date: Fri, 8 Jan 2010 06:03:02 +0000 Subject: [PATCH] * python 2.6 compatibility --- sleekxmpp/__init__.py | 7 +++-- sleekxmpp/basexmpp.py | 2 +- sleekxmpp/xmlstream/handler/waiter.py | 5 +++- sleekxmpp/xmlstream/matcher/xmlmask.py | 2 +- sleekxmpp/xmlstream/xmlstream.py | 37 ++++++++++++++++++++------ 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/sleekxmpp/__init__.py b/sleekxmpp/__init__.py index 91dffa0..b81e8a1 100644 --- a/sleekxmpp/__init__.py +++ b/sleekxmpp/__init__.py @@ -19,7 +19,7 @@ along with SleekXMPP; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """ -from __future__ import absolute_import +from __future__ import absolute_import, unicode_literals from . basexmpp import basexmpp from xml.etree import cElementTree as ET from . xmlstream.xmlstream import XMLStream @@ -201,7 +201,10 @@ class ClientXMPP(basexmpp, XMLStream): for sasl_mech in sasl_mechs: self.features.append("sasl:%s" % sasl_mech.text) if 'sasl:PLAIN' in self.features: - self.send("""%s""" % base64.b64encode(b'\x00' + bytes(self.username, 'utf-8') + b'\x00' + bytes(self.password, 'utf-8')).decode('utf-8')) + if sys.version_info < (3,0): + self.send("""%s""" % base64.b64encode(b'\x00' + bytes(self.username) + b'\x00' + bytes(self.password)).decode('utf-8')) + else: + self.send("""%s""" % base64.b64encode(b'\x00' + bytes(self.username, 'utf-8') + b'\x00' + bytes(self.password, 'utf-8')).decode('utf-8')) else: logging.error("No appropriate login method.") self.disconnect() diff --git a/sleekxmpp/basexmpp.py b/sleekxmpp/basexmpp.py index dd77bfb..7460594 100644 --- a/sleekxmpp/basexmpp.py +++ b/sleekxmpp/basexmpp.py @@ -17,7 +17,7 @@ along with SleekXMPP; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """ -from __future__ import with_statement +from __future__ import with_statement, unicode_literals from xml.etree import cElementTree as ET diff --git a/sleekxmpp/xmlstream/handler/waiter.py b/sleekxmpp/xmlstream/handler/waiter.py index ba29638..f2303cd 100644 --- a/sleekxmpp/xmlstream/handler/waiter.py +++ b/sleekxmpp/xmlstream/handler/waiter.py @@ -1,5 +1,8 @@ from . import base -import queue +try: + import queue +except ImportError: + import Queue as queue import logging from .. stanzabase import StanzaBase diff --git a/sleekxmpp/xmlstream/matcher/xmlmask.py b/sleekxmpp/xmlstream/matcher/xmlmask.py index a161048..e8e4df0 100644 --- a/sleekxmpp/xmlstream/matcher/xmlmask.py +++ b/sleekxmpp/xmlstream/matcher/xmlmask.py @@ -25,7 +25,7 @@ class MatchXMLMask(base.MatcherBase): #TODO require namespaces if source == None: #if element not found (happens during recursive check below) return False - if type(maskobj) == type(str()): #if the mask is a string, make it an xml obj + if not hasattr(maskobj, 'attrib'): #if the mask is a string, make it an xml obj try: maskobj = cElementTree.fromstring(maskobj) except ExpatError: diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index cf61ba3..5178693 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -1,5 +1,8 @@ -from __future__ import with_statement -import queue +from __future__ import with_statement, unicode_literals +try: + import queue +except ImportError: + import Queue as queue from . import statemachine from . stanzabase import StanzaBase from xml.etree import cElementTree @@ -15,10 +18,15 @@ import xml.sax.saxutils HANDLER_THREADS = 1 ssl_support = True -try: - import ssl -except ImportError: - ssl_support = False +#try: +import ssl +#except ImportError: +# ssl_support = False +import sys +if sys.version_info < (3, 0): + #monkey patch broken filesocket object + from . import filesocket + socket._fileobject = filesocket.filesocket class RestartStream(Exception): @@ -89,11 +97,13 @@ class XMLStream(object): self.use_tls = use_tls self.state.set('is client', True) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.socket.settimeout(None) if self.use_ssl and self.ssl_support: logging.debug("Socket Wrapped for SSL") self.socket = ssl.wrap_socket(self.socket) try: self.socket.connect(self.address) + #self.filesocket = self.socket.makefile('rb', 0) self.filesocket = self.socket.makefile('rb', 0) self.state.set('connected', True) return True @@ -111,7 +121,11 @@ class XMLStream(object): self.realsocket = self.socket self.socket = ssl.wrap_socket(self.socket, ssl_version=ssl.PROTOCOL_TLSv1, do_handshake_on_connect=False) self.socket.do_handshake() - self.filesocket = self.socket.makefile('rb', 0) + if sys.version_info < (3,0): + from . filesocket import filesocket + self.filesocket = filesocket(self.socket) + else: + self.filesocket = self.socket.makefile('rb', 0) return True else: logging.warning("Tried to enable TLS, but ssl module not found.") @@ -180,6 +194,7 @@ class XMLStream(object): "Parses the incoming stream, adding to xmlin queue as it goes" #build cElementTree object from expat was we go #self.filesocket = self.socket.makefile('rb', 0) + #print self.filesocket.read(1024) #self.filesocket._sock.recv(1024) edepth = 0 root = None for (event, xmlobj) in cElementTree.iterparse(self.filesocket, (b'end', b'start')): @@ -208,11 +223,14 @@ class XMLStream(object): logging.debug("SEND: %s" % data) try: self.socket.send(bytes(data, "utf-8")) + except TypeError: + self.socket.send(bytes(data)) #except socket.error,(errno, strerror): except: self.state.set('connected', False) if self.state.reconnect: logging.error("Disconnected. Socket Error.") + traceback.print_exc() self.disconnect(reconnect=True) return False return True @@ -277,7 +295,10 @@ class XMLStream(object): except queue.Empty: event = None if event is not None: - etype, handler, *args = event + etype = event[0] + handler = event[1] + args = event[2:] + #etype, handler, *args = event #python 3.x way if etype == 'stanza': try: handler.run(args[0])