From 0e950154103ba9385b13f76548befa87509995f3 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 16 Aug 2014 22:37:25 +0200 Subject: Remove sys.version_info checks for python2 and clean some imports. --- slixmpp/basexmpp.py | 10 -------- slixmpp/clientxmpp.py | 2 -- slixmpp/componentxmpp.py | 8 +------ slixmpp/plugins/base.py | 6 +---- slixmpp/plugins/xep_0009/binding.py | 8 ++----- slixmpp/plugins/xep_0078/legacyauth.py | 10 ++------ slixmpp/util/__init__.py | 2 +- slixmpp/util/misc_ops.py | 44 +++++++++------------------------- slixmpp/util/sasl/mechanisms.py | 4 +--- slixmpp/xmlstream/tostring.py | 12 ---------- slixmpp/xmlstream/xmlstream.py | 22 +++-------------- tests/test_overall.py | 5 +--- tests/test_stanza_xep_0009.py | 4 ---- 13 files changed, 23 insertions(+), 114 deletions(-) diff --git a/slixmpp/basexmpp.py b/slixmpp/basexmpp.py index 0b133f6d..b15354c9 100644 --- a/slixmpp/basexmpp.py +++ b/slixmpp/basexmpp.py @@ -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): """ diff --git a/slixmpp/clientxmpp.py b/slixmpp/clientxmpp.py index 66a846d3..86d764f4 100644 --- a/slixmpp/clientxmpp.py +++ b/slixmpp/clientxmpp.py @@ -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 diff --git a/slixmpp/componentxmpp.py b/slixmpp/componentxmpp.py index 52829dfa..b00e084e 100644 --- a/slixmpp/componentxmpp.py +++ b/slixmpp/componentxmpp.py @@ -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() diff --git a/slixmpp/plugins/base.py b/slixmpp/plugins/base.py index 9694a414..d3f751fb 100644 --- a/slixmpp/plugins/base.py +++ b/slixmpp/plugins/base.py @@ -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: diff --git a/slixmpp/plugins/xep_0009/binding.py b/slixmpp/plugins/xep_0009/binding.py index 5416fc41..d922dfc7 100644 --- a/slixmpp/plugins/xep_0009/binding.py +++ b/slixmpp/plugins/xep_0009/binding.py @@ -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 diff --git a/slixmpp/plugins/xep_0078/legacyauth.py b/slixmpp/plugins/xep_0078/legacyauth.py index 9c49d346..0bcfb3d0 100644 --- a/slixmpp/plugins/xep_0078/legacyauth.py +++ b/slixmpp/plugins/xep_0078/legacyauth.py @@ -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 diff --git a/slixmpp/util/__init__.py b/slixmpp/util/__init__.py index 0a57baf3..b4243e83 100644 --- a/slixmpp/util/__init__.py +++ b/slixmpp/util/__init__.py @@ -12,7 +12,7 @@ from slixmpp.util.misc_ops import bytes, unicode, hashes, hash, \ num_to_bytes, bytes_to_num, quote, \ - XOR, safedict + XOR # ===================================================================== diff --git a/slixmpp/util/misc_ops.py b/slixmpp/util/misc_ops.py index 18c919a8..2e661045 100644 --- a/slixmpp/util/misc_ops.py +++ b/slixmpp/util/misc_ops.py @@ -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 diff --git a/slixmpp/util/sasl/mechanisms.py b/slixmpp/util/sasl/mechanisms.py index b417fb46..de0203c0 100644 --- a/slixmpp/util/sasl/mechanisms.py +++ b/slixmpp/util/sasl/mechanisms.py @@ -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(): diff --git a/slixmpp/xmlstream/tostring.py b/slixmpp/xmlstream/tostring.py index c1e4911a..2d99e323 100644 --- a/slixmpp/xmlstream/tostring.py +++ b/slixmpp/xmlstream/tostring.py @@ -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 = {'&': '&', '<': '<', '>': '>', diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index 13e88428..92f5c6d3 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -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 diff --git a/tests/test_overall.py b/tests/test_overall.py index 411ebcdf..3f32913c 100644 --- a/tests/test_overall.py +++ b/tests/test_overall.py @@ -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): diff --git a/tests/test_stanza_xep_0009.py b/tests/test_stanza_xep_0009.py index 4392ca14..f1a61b80 100644 --- a/tests/test_stanza_xep_0009.py +++ b/tests/test_stanza_xep_0009.py @@ -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): -- cgit v1.2.3