Remove sys.version_info checks for python2 and clean some imports.

This commit is contained in:
Emmanuel Gil Peyrot 2014-08-16 22:37:25 +02:00 committed by Florent Le Coz
parent b92dac72f3
commit 0e95015410
13 changed files with 23 additions and 114 deletions

View file

@ -12,9 +12,6 @@
:license: MIT, see LICENSE for more details
"""
from __future__ import with_statement, unicode_literals
import sys
import logging
import threading
@ -38,13 +35,6 @@ from slixmpp.plugins import PluginManager, load_plugin
log = logging.getLogger(__name__)
# In order to make sure that Unicode is handled properly
# in Python 2.x, reset the default encoding.
if sys.version_info < (3, 0):
from slixmpp.util.misc_ops import setdefaultencoding
setdefaultencoding('utf8')
class BaseXMPP(XMLStream):
"""

View file

@ -12,8 +12,6 @@
:license: MIT, see LICENSE for more details
"""
from __future__ import absolute_import, unicode_literals
import logging
from slixmpp.stanza import StreamFeatures

View file

@ -12,10 +12,7 @@
:license: MIT, see LICENSE for more details
"""
from __future__ import absolute_import
import logging
import sys
import hashlib
from slixmpp.basexmpp import BaseXMPP
@ -136,10 +133,7 @@ class ComponentXMPP(BaseXMPP):
# Construct a hash of the stream ID and the component secret.
sid = xml.get('id', '')
pre_hash = '%s%s' % (sid, self.secret)
if sys.version_info >= (3, 0):
# Handle Unicode byte encoding in Python 3.
pre_hash = bytes(pre_hash, 'utf-8')
pre_hash = bytes('%s%s' % (sid, self.secret), 'utf-8')
handshake = ET.Element('{jabber:component:accept}handshake')
handshake.text = hashlib.sha1(pre_hash).hexdigest().lower()

View file

@ -19,10 +19,6 @@ import logging
import threading
if sys.version_info >= (3, 0):
unicode = str
log = logging.getLogger(__name__)
@ -89,7 +85,7 @@ def load_plugin(name, module=None):
module = 'slixmpp.features.%s' % name
__import__(module)
mod = sys.modules[module]
elif isinstance(module, (str, unicode)):
elif isinstance(module, str):
__import__(module)
mod = sys.modules[module]
else:

View file

@ -10,10 +10,6 @@ from slixmpp.xmlstream import ET
import base64
import logging
import time
import sys
if sys.version_info > (3, 0):
unicode = str
log = logging.getLogger(__name__)
@ -58,7 +54,7 @@ def _py2xml(*args):
boolean = ET.Element("{%s}boolean" % _namespace)
boolean.text = str(int(x))
val.append(boolean)
elif type(x) in (str, unicode):
elif type(x) is str:
string = ET.Element("{%s}string" % _namespace)
string.text = x
val.append(string)
@ -156,7 +152,7 @@ class rpctime(object):
def __init__(self,data=None):
#assume string data is in iso format YYYYMMDDTHH:MM:SS
if type(data) in (str, unicode):
if type(data) is str:
self.timestamp = time.strptime(data,"%Y%m%dT%H:%M:%S")
elif type(data) is time.struct_time:
self.timestamp = data

View file

@ -9,8 +9,6 @@
import uuid
import logging
import hashlib
import random
import sys
from slixmpp.jid import JID
from slixmpp.exceptions import IqError, IqTimeout
@ -105,12 +103,8 @@ class XEP_0078(BasePlugin):
if 'digest' in resp['auth']['fields']:
log.debug('Authenticating via jabber:iq:auth Digest')
if sys.version_info < (3, 0):
stream_id = bytes(self.xmpp.stream_id)
password = bytes(self.xmpp.password)
else:
stream_id = bytes(self.xmpp.stream_id, encoding='utf-8')
password = bytes(self.xmpp.password, encoding='utf-8')
stream_id = bytes(self.xmpp.stream_id, encoding='utf-8')
password = bytes(self.xmpp.password, encoding='utf-8')
digest = hashlib.sha1(b'%s%s' % (stream_id, password)).hexdigest()
iq['auth']['digest'] = digest

View file

@ -12,7 +12,7 @@
from slixmpp.util.misc_ops import bytes, unicode, hashes, hash, \
num_to_bytes, bytes_to_num, quote, \
XOR, safedict
XOR
# =====================================================================

View file

@ -3,12 +3,7 @@ import hashlib
def unicode(text):
if sys.version_info < (3, 0):
if isinstance(text, str):
text = text.decode('utf-8')
import __builtin__
return __builtin__.unicode(text)
elif not isinstance(text, str):
if not isinstance(text, str):
return text.decode('utf-8')
else:
return text
@ -27,20 +22,16 @@ def bytes(text):
if text is None:
return b''
if sys.version_info < (3, 0):
import __builtin__
return __builtin__.bytes(text)
import builtins
if isinstance(text, builtins.bytes):
# We already have bytes, so do nothing
return text
if isinstance(text, list):
# Convert a list of integers to bytes
return builtins.bytes(text)
else:
import builtins
if isinstance(text, builtins.bytes):
# We already have bytes, so do nothing
return text
if isinstance(text, list):
# Convert a list of integers to bytes
return builtins.bytes(text)
else:
# Convert UTF-8 text to bytes
return builtins.bytes(text, encoding='utf-8')
# Convert UTF-8 text to bytes
return builtins.bytes(text, encoding='utf-8')
def quote(text):
@ -91,10 +82,7 @@ def XOR(x, y):
"""
result = b''
for a, b in zip(x, y):
if sys.version_info < (3, 0):
result += chr((ord(a) ^ ord(b)))
else:
result += bytes([a ^ b])
result += bytes([a ^ b])
return result
@ -153,13 +141,3 @@ def setdefaultencoding(encoding):
raise RuntimeError("Could not find setdefaultencoding")
sys.setdefaultencoding = func
return func(encoding)
def safedict(data):
if sys.version_info < (2, 7):
safe = {}
for key in data:
safe[key.encode('utf8')] = data[key]
return safe
else:
return data

View file

@ -15,7 +15,6 @@
:license: MIT, see LICENSE for more details
"""
import sys
import hmac
import random
@ -365,8 +364,7 @@ class DIGEST(Mech):
for char in challenge:
if sys.version_info >= (3, 0):
char = bytes([char])
char = bytes([char])
if state == 'var':
if char.isspace():

View file

@ -13,14 +13,6 @@
:license: MIT, see LICENSE for more details
"""
from __future__ import unicode_literals
import sys
if sys.version_info < (3, 0):
import types
XML_NS = 'http://www.w3.org/XML/1998/namespace'
@ -145,10 +137,6 @@ def escape(text, use_cdata=False):
:param string text: The XML text to convert.
:rtype: Unicode string
"""
if sys.version_info < (3, 0):
if type(text) != types.UnicodeType:
text = unicode(text, 'utf-8', 'ignore')
escapes = {'&': '&amp;',
'<': '&lt;',
'>': '&gt;',

View file

@ -12,32 +12,19 @@
:license: MIT, see LICENSE for more details
"""
from __future__ import with_statement, unicode_literals
import asyncio
import functools
import base64
import copy
import logging
import signal
import socket as Socket
import ssl
import sys
import time
import random
import weakref
import uuid
import errno
from xml.parsers.expat import ExpatError
import xml.etree.ElementTree
import slixmpp
from slixmpp.util import Queue, QueueEmpty, safedict
from slixmpp.xmlstream import tostring, cert
from slixmpp.xmlstream.stanzabase import StanzaBase, ET, ElementBase
from slixmpp.xmlstream.handler import Waiter, XMLCallback
from slixmpp.xmlstream.matcher import MatchXMLMask
from slixmpp.xmlstream import tostring
from slixmpp.xmlstream.stanzabase import StanzaBase, ElementBase
from slixmpp.xmlstream.resolver import resolve, default_resolver
#: The time in seconds to wait before timing out waiting for response stanzas.
@ -584,10 +571,7 @@ class XMLStream(object):
if not self.dns_answers:
self.dns_answers = self.get_dns_records(domain, port)
if sys.version_info < (3, 0):
return self.dns_answers.next()
else:
return next(self.dns_answers)
return next(self.dns_answers)
def add_event_handler(self, name, pointer, disposable=False):
"""Add a custom event handler that will be executed whenever

View file

@ -15,10 +15,7 @@ class TestOverall(unittest.TestCase):
def testModules(self):
"""Testing all modules by compiling them"""
src = '.%sslixmpp' % os.sep
if sys.version_info < (3, 0):
rx = re.compile('/[.]svn')
else:
rx = re.compile('/[.]svn|.*26.*')
rx = re.compile('/[.]svn|.*26.*')
self.failUnless(compileall.compile_dir(src, rx=rx, quiet=True))
def testTabNanny(self):

View file

@ -24,10 +24,6 @@ from slixmpp.xmlstream.tostring import tostring
import unittest
if sys.version_info > (3, 0):
unicode = str
class TestJabberRPC(SlixTest):
def setUp(self):