From a8ff3586d3852fc70b1932a579cf8a28ce920d5c Mon Sep 17 00:00:00 2001 From: Nathan Fritz Date: Fri, 8 Jan 2010 06:03:02 +0000 Subject: * python 2.6 compatibility --- sleekxmpp/xmlstream/handler/waiter.py | 5 ++++- sleekxmpp/xmlstream/matcher/xmlmask.py | 2 +- sleekxmpp/xmlstream/xmlstream.py | 37 ++++++++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 10 deletions(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/handler/waiter.py b/sleekxmpp/xmlstream/handler/waiter.py index ba296386..f2303cd5 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 a1610489..e8e4df02 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 cf61ba31..51786935 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]) -- cgit v1.2.3