PROTON-1848: [Python] Remove Python 2.5 and earlier compatibility - Remove need for most compatibility hacks - Fix some seemingly odd conversion functions - Hidden all compatibility code in _compat module - will probably get rid of raise_ when rewriting reactor - can get rid of string_type by doing unicode type hack in reactor.py - leaving iteritems & unichr
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/51934030 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/51934030 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/51934030 Branch: refs/heads/go1 Commit: 51934030922396feeca796863c58f9e43691638f Parents: b6b29ec Author: Andrew Stitcher <[email protected]> Authored: Tue Apr 24 10:38:57 2018 -0400 Committer: Andrew Stitcher <[email protected]> Committed: Wed May 23 16:27:10 2018 -0400 ---------------------------------------------------------------------- python/proton/__init__.py | 178 ++++++++++------------------ python/proton/_compat.py | 57 +++------ python/proton/reactor.py | 20 ++-- tests/python/proton_tests/codec.py | 34 +++--- tests/python/proton_tests/engine.py | 68 +++++------ tests/python/proton_tests/interop.py | 5 +- tests/python/proton_tests/message.py | 21 ++-- tests/python/proton_tests/sasl.py | 51 ++++---- tests/python/proton_tests/transport.py | 57 +++++---- 9 files changed, 203 insertions(+), 288 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51934030/python/proton/__init__.py ---------------------------------------------------------------------- diff --git a/python/proton/__init__.py b/python/proton/__init__.py index 60f7323..6ee0d68 100644 --- a/python/proton/__init__.py +++ b/python/proton/__init__.py @@ -32,94 +32,43 @@ from __future__ import absolute_import from cproton import * from .wrapper import Wrapper -from proton import _compat - -import logging, weakref, socket, sys, threading - -try: - handler = logging.NullHandler() -except AttributeError: - class NullHandler(logging.Handler): - def handle(self, record): - pass +from . import _compat + +import logging +import socket +import sys +import threading +import uuid +import weakref + +# This private NullHandler is required for Python 2.6, +# when we no longer support 2.6 this replace NullHandler class definition and assignment with: +# handler = logging.NullHandler() +class NullHandler(logging.Handler): + def handle(self, record): + pass - def emit(self, record): - pass + def emit(self, record): + pass - def createLock(self): - self.lock = None + def createLock(self): + self.lock = None - handler = NullHandler() +handler = NullHandler() log = logging.getLogger("proton") log.addHandler(handler) -try: - import uuid - - def generate_uuid(): - return uuid.uuid4() - -except ImportError: - """ - No 'native' UUID support. Provide a very basic UUID type that is a compatible subset of the uuid type provided by more modern python releases. - """ - import struct - class uuid: - class UUID: - def __init__(self, hex=None, bytes=None): - if [hex, bytes].count(None) != 1: - raise TypeError("need one of hex or bytes") - if bytes is not None: - self.bytes = bytes - elif hex is not None: - fields=hex.split("-") - fields[4:5] = [fields[4][:4], fields[4][4:]] - self.bytes = struct.pack("!LHHHHL", *[int(x,16) for x in fields]) - - def __cmp__(self, other): - if isinstance(other, uuid.UUID): - return cmp(self.bytes, other.bytes) - else: - return -1 - - def __str__(self): - return "%08x-%04x-%04x-%04x-%04x%08x" % struct.unpack("!LHHHHL", self.bytes) - - def __repr__(self): - return "UUID(%r)" % str(self) - - def __hash__(self): - return self.bytes.__hash__() - - import os, random, time - rand = random.Random() - rand.seed((os.getpid(), time.time(), socket.gethostname())) - def random_uuid(): - data = [rand.randint(0, 255) for i in xrange(16)] - - # From RFC4122, the version bits are set to 0100 - data[6] &= 0x0F - data[6] |= 0x40 - - # From RFC4122, the top two bits of byte 8 get set to 01 - data[8] &= 0x3F - data[8] |= 0x80 - return "".join(map(chr, data)) - - def uuid4(): - return uuid.UUID(bytes=random_uuid()) - - def generate_uuid(): - return uuid4() +def generate_uuid(): + return uuid.uuid4() # # Hacks to provide Python2 <---> Python3 compatibility # -try: - bytes() -except NameError: - bytes = str +# The results are +# | |long|unicode| +# |python2|long|unicode| +# |python3| int| str| try: long() except NameError: @@ -241,10 +190,15 @@ class Message(object): def _check_property_keys(self): for k in self.properties.keys(): - if not isinstance(k, (bytes, str, unicode)): - raise MessageException('Application property key is not unicode string: key=%s %s' % (str(k), type(k))) - if isinstance(k, bytes): - self.properties[_compat.bin2str(k)] = self.properties.pop(k) + if isinstance(k, unicode): + # py2 unicode, py3 str (via hack definition) + continue + # If key is binary then change to string + elif isinstance(k, str): + # py2 str + self.properties[k.encode('utf-8')] = self.properties.pop(k) + else: + raise MessageException('Application property key is not string type: key=%s %s' % (str(k), type(k))) def _pre_encode(self): inst = Data(pn_message_instructions(self._msg)) @@ -376,7 +330,7 @@ The number of delivery attempts made for this message. def _get_id(self): return self._id.get_object() def _set_id(self, value): - if type(value) in _compat.INT_TYPES: + if type(value) in (int, long): value = ulong(value) self._id.rewind() self._id.put_object(value) @@ -432,7 +386,7 @@ The reply-to address for the message. def _get_correlation_id(self): return self._correlation_id.get_object() def _set_correlation_id(self, value): - if type(value) in _compat.INT_TYPES: + if type(value) in (int, long): value = ulong(value) self._correlation_id.rewind() self._correlation_id.put_object(value) @@ -908,7 +862,7 @@ class Data: def type_name(type): return Data.type_names[type] def __init__(self, capacity=16): - if type(capacity) in _compat.INT_TYPES: + if type(capacity) in (int, long): self._data = pn_data(capacity) self._free = True else: @@ -1419,7 +1373,7 @@ class Data: If the current node is a char, returns its value, returns 0 otherwise. """ - return char(_compat.unichar(pn_data_get_char(self._data))) + return char(_compat.unichr(pn_data_get_char(self._data))) def get_ulong(self): """ @@ -1827,37 +1781,33 @@ def millis2timeout(millis): return millis2secs(millis) def unicode2utf8(string): - """Some Proton APIs expect a null terminated string. Convert python text - types to UTF8 to avoid zero bytes introduced by other multi-byte encodings. - This method will throw if the string cannot be converted. - """ - if string is None: - return None - if _compat.IS_PY2: - if isinstance(string, unicode): - return string.encode('utf-8') - elif isinstance(string, str): - return string - else: - # decoding a string results in bytes - if isinstance(string, str): - string = string.encode('utf-8') - # fall through - if isinstance(string, bytes): - return string.decode('utf-8') - raise TypeError("Unrecognized string type: %r (%s)" % (string, type(string))) + """Some Proton APIs expect a null terminated string. Convert python text + types to UTF8 to avoid zero bytes introduced by other multi-byte encodings. + This method will throw if the string cannot be converted. + """ + if string is None: + return None + elif isinstance(string, str): + # Must be py2 or py3 str + # The swig binding converts py3 str -> utf8 char* and back sutomatically + return string + elif isinstance(string, unicode): + # This must be python2 unicode as we already detected py3 str above + return string.encode('utf-8') + # Anything else illegal - specifically python3 bytes + raise TypeError("Unrecognized string type: %r (%s)" % (string, type(string))) def utf82unicode(string): - """Covert C strings returned from proton-c into python unicode""" - if string is None: - return None - if isinstance(string, _compat.TEXT_TYPES): - # already unicode - return string - elif isinstance(string, _compat.BINARY_TYPES): - return string.decode('utf8') - else: - raise TypeError("Unrecognized string type") + """Convert C strings returned from proton-c into python unicode""" + if string is None: + return None + elif isinstance(string, unicode): + # py2 unicode, py3 str (via hack definition) + return string + elif isinstance(string, bytes): + # py2 str (via hack definition), py3 bytes + return string.decode('utf8') + raise TypeError("Unrecognized string type") class Connection(Wrapper, Endpoint): """ http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51934030/python/proton/_compat.py ---------------------------------------------------------------------- diff --git a/python/proton/_compat.py b/python/proton/_compat.py index c8815f4..afd82e3 100644 --- a/python/proton/_compat.py +++ b/python/proton/_compat.py @@ -22,16 +22,17 @@ Utilities to help Proton support both python2 and python3. """ import sys -import types -IS_PY2 = sys.version_info[0] == 2 -IS_PY3 = sys.version_info[0] == 3 -if IS_PY3: - INT_TYPES = (int,) - TEXT_TYPES = (str,) - STRING_TYPES = (str,) - BINARY_TYPES = (bytes,) - CLASS_TYPES = (type,) +# bridge between py2 Queue renamed as py3 queue +try: + import Queue as queue +except ImportError: + import queue + +PY3 = sys.version_info[0] == 3 + +if PY3: + string_types = (str,) def raise_(t, v=None, tb=None): """Mimic the old 2.x raise behavior: @@ -44,29 +45,13 @@ if IS_PY3: else: raise v.with_traceback(tb) - def bin2str(s, encoding='utf-8'): - return s - - def iteritems(d): - return iter(d.items()) - - def unichar(i): - return chr(i) - - def str2bin(s, encoding='latin-1'): - """Convert str to binary type""" - return s.encode(encoding) - - def str2unicode(s): - return s + def iteritems(d, **kw): + return iter(d.items(**kw)) + unichr = chr else: - INT_TYPES = (int, long) - TEXT_TYPES = (unicode,) # includes both unicode and non-unicode strings: - STRING_TYPES = (basestring,) - BINARY_TYPES = (str,) - CLASS_TYPES = (type, types.ClassType) + string_types = (basestring,) # the raise syntax will cause a parse error in Py3, so 'sneak' in a # definition that won't cause the parser to barf @@ -74,17 +59,9 @@ else: raise t, v, tb """) - def bin2str(s, encoding='utf-8'): - return s.decode(encoding) - def iteritems(d, **kw): - return d.iteritems() - - def unichar(i): - return unichr(i) + return d.iteritems(**kw) - def str2bin(s, encoding='latin-1'): - return s + unichr = unichr - def str2unicode(s): - return unicode(s, "unicode_escape") +__all__ = [ 'PY3', 'queue', 'string_types', 'raise_', 'iteritems', 'unichr'] \ No newline at end of file http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51934030/python/proton/reactor.py ---------------------------------------------------------------------- diff --git a/python/proton/reactor.py b/python/proton/reactor.py index 0fa1c4b..d5d5183 100644 --- a/python/proton/reactor.py +++ b/python/proton/reactor.py @@ -19,6 +19,9 @@ from __future__ import absolute_import # import logging, os, socket, time, types from heapq import heappush, heappop, nsmallest + +import traceback + from proton import Collector, Connection, ConnectionException, Delivery, Described, dispatch from proton import Endpoint, Event, EventBase, EventType, generate_uuid, Handler, Link, Message from proton import ProtonException, PN_ACCEPTED, PN_PYREF, SASL, Session, SSL, SSLDomain, SSLUnavailable, symbol @@ -27,16 +30,13 @@ from select import select from proton.handlers import OutgoingMessageHandler from proton import unicode2utf8, utf82unicode -import traceback from proton import WrappedHandler, _chandler, secs2millis, millis2secs, timeout2millis, millis2timeout, Selectable from .wrapper import Wrapper, PYCTX from cproton import * + from . import _compat -try: - import Queue -except ImportError: - import queue as Queue +from ._compat import queue log = logging.getLogger("proton") @@ -259,7 +259,7 @@ class EventInjector(object): needed, to allow the event loop to end if needed. """ def __init__(self): - self.queue = Queue.Queue() + self.queue = queue.Queue() self.pipe = os.pipe() self._closed = False @@ -269,7 +269,7 @@ class EventInjector(object): of the reactor to which this EventInjector was added. """ self.queue.put(event) - os.write(self.pipe[1], _compat.str2bin("!")) + os.write(self.pipe[1], b"!") def close(self): """ @@ -278,7 +278,7 @@ class EventInjector(object): then this will be removed from the set of interest. """ self._closed = True - os.write(self.pipe[1], _compat.str2bin("!")) + os.write(self.pipe[1], b"!") def fileno(self): return self.pipe[0] @@ -806,7 +806,7 @@ class Container(Reactor): Various LinkOptions can be specified to further control the attachment. """ - if isinstance(context, _compat.STRING_TYPES): + if isinstance(context, _compat.string_types): context = Url(context) if isinstance(context, Url) and not target: target = context.path @@ -847,7 +847,7 @@ class Container(Reactor): Various LinkOptions can be specified to further control the attachment. """ - if isinstance(context, _compat.STRING_TYPES): + if isinstance(context, _compat.string_types): context = Url(context) if isinstance(context, Url) and not source: source = context.path http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51934030/tests/python/proton_tests/codec.py ---------------------------------------------------------------------- diff --git a/tests/python/proton_tests/codec.py b/tests/python/proton_tests/codec.py index 27e70cf..e13dcfe 100644 --- a/tests/python/proton_tests/codec.py +++ b/tests/python/proton_tests/codec.py @@ -20,11 +20,8 @@ import os, sys from . import common from proton import * -from proton._compat import raise_, str2unicode, unichar, str2bin -try: - from uuid import uuid4 -except ImportError: - from proton import uuid4 +from proton._compat import raise_ +from uuid import uuid4 class Test(common.Test): @@ -281,8 +278,7 @@ class DataTest(Test): self._test("double", 0, 1, 2, 3, 0.1, 0.2, 0.3, -1, -2, -3, -0.1, -0.2, -0.3) def testBinary(self): - self._test("binary", str2bin("this"), str2bin("is"), str2bin("a"), str2bin("test"), - str2bin("of" "b\x00inary")) + self._test("binary", b"this", b"is", b"a", b"test",b"of" b"b\x00inary") def testSymbol(self): self._test("symbol", symbol("this is a symbol test"), symbol("bleh"), symbol("blah")) @@ -291,7 +287,7 @@ class DataTest(Test): self._test("timestamp", timestamp(0), timestamp(12345), timestamp(1000000)) def testChar(self): - self._test("char", char('a'), char('b'), char('c'), char(unichar(0x20AC))) + self._test("char", char('a'), char('b'), char('c'), char(u'\u20AC')) def testUUID(self): self._test("uuid", uuid4(), uuid4(), uuid4()) @@ -303,7 +299,7 @@ class DataTest(Test): self._test("decimal64", decimal64(0), decimal64(1), decimal64(2), decimal64(3), decimal64(4), decimal64(2**60)) def testDecimal128(self): - self._test("decimal128", decimal128(str2bin("fdsaasdf;lkjjkl;")), decimal128(str2bin("x"*16))) + self._test("decimal128", decimal128(b"fdsaasdf;lkjjkl;"), decimal128(b"x"*16)) def testCopy(self): self.data.put_described() @@ -344,10 +340,10 @@ class DataTest(Test): obj = {symbol("key"): timestamp(1234), ulong(123): "blah", char("c"): "bleh", - str2unicode("desc"): Described(symbol("url"), str2unicode("http://example.org")), - str2unicode("array"): Array(UNDESCRIBED, Data.INT, 1, 2, 3), - str2unicode("list"): [1, 2, 3, None, 4], - str2unicode("boolean"): True} + u"desc": Described(symbol("url"), u"http://example.org"), + u"array": Array(UNDESCRIBED, Data.INT, 1, 2, 3), + u"list": [1, 2, 3, None, 4], + u"boolean": True} self.data.put_object(obj) enc = self.data.encode() data = Data() @@ -359,7 +355,7 @@ class DataTest(Test): def testBuffer(self): try: - self.data.put_object(buffer(str2bin("foo"))) + self.data.put_object(buffer(b"foo")) except NameError: # python >= 3.0 does not have `buffer` return @@ -368,11 +364,11 @@ class DataTest(Test): data.rewind() assert data.next() assert data.type() == Data.BINARY - assert data.get_object() == str2bin("foo") + assert data.get_object() == b"foo" def testMemoryView(self): try: - self.data.put_object(memoryview(str2bin("foo"))) + self.data.put_object(memoryview(b"foo")) except NameError: # python <= 2.6 does not have `memoryview` return @@ -381,10 +377,10 @@ class DataTest(Test): data.rewind() assert data.next() assert data.type() == Data.BINARY - assert data.get_object() == str2bin("foo") + assert data.get_object() == b"foo" def testLookup(self): - obj = {symbol("key"): str2unicode("value"), + obj = {symbol("key"): u"value", symbol("pi"): 3.14159, symbol("list"): [1, 2, 3, 4]} self.data.put_object(obj) @@ -396,7 +392,7 @@ class DataTest(Test): assert self.data.get_object() == 3.14159 self.data.rewind() assert self.data.lookup("key") - assert self.data.get_object() == str2unicode("value") + assert self.data.get_object() == u"value" self.data.rewind() assert self.data.lookup("list") assert self.data.get_object() == [1, 2, 3, 4] http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51934030/tests/python/proton_tests/engine.py ---------------------------------------------------------------------- diff --git a/tests/python/proton_tests/engine.py b/tests/python/proton_tests/engine.py index 38a3b4d..c0ea31d 100644 --- a/tests/python/proton_tests/engine.py +++ b/tests/python/proton_tests/engine.py @@ -25,7 +25,6 @@ from time import time, sleep from proton import * from .common import pump, Skipped from proton.reactor import Reactor -from proton._compat import str2bin # older versions of gc do not provide the garbage list @@ -50,7 +49,7 @@ try: bytearray() except: def bytearray(x): - return str2bin('\x00') * x + return b'\x00' * x OUTPUT_SIZE = 10*1024 @@ -836,7 +835,7 @@ class TransferTest(Test): assert tag == "tag", tag assert d.writable - n = self.snd.send(str2bin("this is a test")) + n = self.snd.send(b"this is a test") assert self.snd.advance() assert self.c1.work_head is None @@ -849,7 +848,7 @@ class TransferTest(Test): def test_multiframe(self): self.rcv.flow(1) self.snd.delivery("tag") - msg = str2bin("this is a test") + msg = b"this is a test" n = self.snd.send(msg) assert n == len(msg) @@ -864,9 +863,9 @@ class TransferTest(Test): assert binary == msg, (binary, msg) binary = self.rcv.recv(1024) - assert binary == str2bin("") + assert binary == b"" - msg = str2bin("this is more") + msg = b"this is more" n = self.snd.send(msg) assert n == len(msg) assert self.snd.advance() @@ -885,7 +884,7 @@ class TransferTest(Test): self.pump() sd = self.snd.delivery("tag") - msg = str2bin("this is a test") + msg = b"this is a test" n = self.snd.send(msg) assert n == len(msg) assert self.snd.advance() @@ -984,7 +983,7 @@ class TransferTest(Test): for x in range(10): self.snd.delivery("tag%d" % x) - msg = str2bin("this is a test") + msg = b"this is a test" n = self.snd.send(msg) assert n == len(msg) assert self.snd.advance() @@ -1567,7 +1566,7 @@ class CreditTest(Test): sd = self.snd.delivery("tagA") assert sd - n = self.snd.send(str2bin("A")) + n = self.snd.send(b"A") assert n == 1 self.pump() self.snd.advance() @@ -1584,7 +1583,7 @@ class CreditTest(Test): assert self.rcv.credit == 10, self.rcv.credit data = self.rcv.recv(10) - assert data == str2bin("A"), data + assert data == b"A", data self.rcv.advance() self.pump() assert self.snd.credit == 9, self.snd.credit @@ -1605,7 +1604,7 @@ class CreditTest(Test): sd = self.snd.delivery("tagB") assert sd - n = self.snd.send(str2bin("B")) + n = self.snd.send(b"B") assert n == 1 self.snd.advance() self.pump() @@ -1619,7 +1618,7 @@ class CreditTest(Test): sd = self.snd.delivery("tagC") assert sd - n = self.snd.send(str2bin("C")) + n = self.snd.send(b"C") assert n == 1 self.snd.advance() self.pump() @@ -1634,10 +1633,10 @@ class CreditTest(Test): assert self.rcv.credit == 2, self.rcv.credit data = self.rcv.recv(10) - assert data == str2bin("B"), data + assert data == b"B", data self.rcv.advance() data = self.rcv.recv(10) - assert data == str2bin("C"), data + assert data == b"C", data self.rcv.advance() self.pump() assert self.snd.credit == 0, self.snd.credit @@ -1971,8 +1970,9 @@ class PipelineTest(Test): snd.open() for i in range(10): - d = snd.delivery("delivery-%s" % i) - snd.send(str2bin("delivery-%s" % i)) + t = "delivery-%s" % i + d = snd.delivery(t) + snd.send(t.encode('ascii')) d.settle() snd.close() @@ -2402,7 +2402,7 @@ class EventTest(CollectorTest): self.expect(Event.CONNECTION_INIT, Event.SESSION_INIT, Event.LINK_INIT, Event.LINK_LOCAL_OPEN, Event.TRANSPORT) snd.delivery("delivery") - snd.send(str2bin("Hello World!")) + snd.send(b"Hello World!") snd.advance() self.pump() self.expect() @@ -2418,7 +2418,7 @@ class EventTest(CollectorTest): snd, rcv = self.testFlowEvents() snd.open() dlv = snd.delivery("delivery") - snd.send(str2bin("Hello World!")) + snd.send(b"Hello World!") assert snd.advance() self.expect(Event.LINK_LOCAL_OPEN, Event.TRANSPORT) self.pump() @@ -2449,7 +2449,7 @@ class EventTest(CollectorTest): t.bind(c) self.expect(Event.CONNECTION_BOUND) assert t.condition is None - t.push(str2bin("asdf")) + t.push(b"asdf") self.expect(Event.TRANSPORT_ERROR, Event.TRANSPORT_TAIL_CLOSED) assert t.condition is not None assert t.condition.name == "amqp:connection:framing-error" @@ -2646,9 +2646,9 @@ class SaslEventTest(CollectorTest): transport.bind(conn) self.expect(Event.CONNECTION_INIT, Event.CONNECTION_BOUND) - transport.push(str2bin('AMQP\x03\x01\x00\x00\x00\x00\x00 \x02\x01\x00\x00\x00SA' - '\xd0\x00\x00\x00\x10\x00\x00\x00\x02\xa3\tANONYMOUS@' - 'AMQP\x00\x01\x00\x00')) + transport.push(b'AMQP\x03\x01\x00\x00\x00\x00\x00 \x02\x01\x00\x00\x00SA' + b'\xd0\x00\x00\x00\x10\x00\x00\x00\x02\xa3\tANONYMOUS@' + b'AMQP\x00\x01\x00\x00') self.expect(Event.TRANSPORT) for i in range(1024): p = transport.pending() @@ -2664,16 +2664,16 @@ class SaslEventTest(CollectorTest): s.allowed_mechs("ANONYMOUS PLAIN") transport.bind(conn) self.expect(Event.CONNECTION_INIT, Event.CONNECTION_BOUND) - transport.push(str2bin( + transport.push( # SASL - 'AMQP\x03\x01\x00\x00' + b'AMQP\x03\x01\x00\x00' # @sasl-mechanisms(64) [sasl-server-mechanisms=@PN_SYMBOL[:ANONYMOUS]] - '\x00\x00\x00\x1c\x02\x01\x00\x00\x00S@\xc0\x0f\x01\xe0\x0c\x01\xa3\tANONYMOUS' + b'\x00\x00\x00\x1c\x02\x01\x00\x00\x00S@\xc0\x0f\x01\xe0\x0c\x01\xa3\tANONYMOUS' # @sasl-outcome(68) [code=0] - '\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00' + b'\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00' # AMQP - 'AMQP\x00\x01\x00\x00' - )) + b'AMQP\x00\x01\x00\x00' + ) self.expect(Event.TRANSPORT) p = transport.pending() bytes = transport.peek(p) @@ -2695,16 +2695,16 @@ class SaslEventTest(CollectorTest): bytes = transport.peek(p) transport.pop(p) self.expect(Event.CONNECTION_INIT, Event.CONNECTION_BOUND) - transport.push(str2bin( + transport.push( # SASL - 'AMQP\x03\x01\x00\x00' + b'AMQP\x03\x01\x00\x00' # @sasl-mechanisms(64) [sasl-server-mechanisms=@PN_SYMBOL[:ANONYMOUS]] - '\x00\x00\x00\x1c\x02\x01\x00\x00\x00S@\xc0\x0f\x01\xe0\x0c\x01\xa3\tANONYMOUS' + b'\x00\x00\x00\x1c\x02\x01\x00\x00\x00S@\xc0\x0f\x01\xe0\x0c\x01\xa3\tANONYMOUS' # @sasl-outcome(68) [code=0] - '\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00' + b'\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00' # AMQP - 'AMQP\x00\x01\x00\x00' - )) + b'AMQP\x00\x01\x00\x00' + ) self.expect(Event.TRANSPORT) p = transport.pending() bytes = transport.peek(p) http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51934030/tests/python/proton_tests/interop.py ---------------------------------------------------------------------- diff --git a/tests/python/proton_tests/interop.py b/tests/python/proton_tests/interop.py index b330f22..2825fe4 100644 --- a/tests/python/proton_tests/interop.py +++ b/tests/python/proton_tests/interop.py @@ -20,7 +20,6 @@ from proton import * import os from . import common -from proton._compat import str2bin def find_test_interop_dir(): @@ -102,10 +101,10 @@ class InteropTest(common.Test): def test_strings(self): self.decode_data_file("strings") - self.assert_next(Data.BINARY, str2bin("abc\0defg")) + self.assert_next(Data.BINARY, b"abc\0defg") self.assert_next(Data.STRING, "abcdefg") self.assert_next(Data.SYMBOL, "abcdefg") - self.assert_next(Data.BINARY, str2bin("")) + self.assert_next(Data.BINARY, b"") self.assert_next(Data.STRING, "") self.assert_next(Data.SYMBOL, "") assert self.data.next() is None http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51934030/tests/python/proton_tests/message.py ---------------------------------------------------------------------- diff --git a/tests/python/proton_tests/message.py b/tests/python/proton_tests/message.py index 199f932..05a067c 100644 --- a/tests/python/proton_tests/message.py +++ b/tests/python/proton_tests/message.py @@ -20,11 +20,7 @@ import os from . import common from proton import * -from proton._compat import str2bin -try: - from uuid import uuid4 -except ImportError: - from proton import uuid4 +from uuid import uuid4 class Test(common.Test): @@ -76,8 +72,7 @@ class AccessorsTest(Test): self._test("delivery_count", 0, range(0, 1024)) def testUserId(self): - self._test("user_id", str2bin(""), (str2bin("asdf"), str2bin("fdsa"), - str2bin("asd\x00fdsa"), str2bin(""))) + self._test("user_id", b"", (b"asdf", b"fdsa", b"asd\x00fdsa", b"")) def testAddress(self): self._test_str("address") @@ -214,21 +209,21 @@ class CodecTest(Test): def testDefaultCreationExpiryDecode(self): # This is a message with everything filled explicitly as null or zero in LIST32 HEADER and PROPERTIES lists - data = str2bin('\x00\x53\x70\xd0\x00\x00\x00\x0a\x00\x00\x00\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xd0\x00\x00\x00\x12\x00\x00\x00\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x52\x00\x40') + data = b'\x00\x53\x70\xd0\x00\x00\x00\x0a\x00\x00\x00\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xd0\x00\x00\x00\x12\x00\x00\x00\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x52\x00\x40' msg2 = Message() msg2.decode(data) assert msg2.expiry_time == 0, (msg2.expiry_time) assert msg2.creation_time == 0, (msg2.creation_time) # The same message with LIST8s instead - data = str2bin('\x00\x53\x70\xc0\x07\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xc0\x0f\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x52\x00\x40') + data = b'\x00\x53\x70\xc0\x07\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xc0\x0f\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40\x52\x00\x40' msg3 = Message() msg3.decode(data) assert msg2.expiry_time == 0, (msg2.expiry_time) assert msg2.creation_time == 0, (msg2.creation_time) # Minified message with zero length HEADER and PROPERTIES lists - data = str2bin('\x00\x53\x70\x45' '\x00\x53\x73\x45') + data = b'\x00\x53\x70\x45' b'\x00\x53\x73\x45' msg4 = Message() msg4.decode(data) assert msg2.expiry_time == 0, (msg2.expiry_time) @@ -252,19 +247,19 @@ class CodecTest(Test): def testDefaultPriorityDecode(self): # This is a message with everything filled explicitly as null or zero in LIST32 HEADER and PROPERTIES lists - data = str2bin('\x00\x53\x70\xd0\x00\x00\x00\x0a\x00\x00\x00\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xd0\x00\x00\x00\x22\x00\x00\x00\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x83\x00\x00\x00\x00\x00\x00\x00\x00\x83\x00\x00\x00\x00\x00\x00\x00\x00\x40\x52\x00\x40') + data = b'\x00\x53\x70\xd0\x00\x00\x00\x0a\x00\x00\x00\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xd0\x00\x00\x00\x22\x00\x00\x00\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x83\x00\x00\x00\x00\x00\x00\x00\x00\x83\x00\x00\x00\x00\x00\x00\x00\x00\x40\x52\x00\x40' msg2 = Message() msg2.decode(data) assert msg2.priority == 4, (msg2.priority) # The same message with LIST8s instead - data = str2bin('\x00\x53\x70\xc0\x07\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xc0\x1f\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x83\x00\x00\x00\x00\x00\x00\x00\x00\x83\x00\x00\x00\x00\x00\x00\x00\x00\x40\x52\x00\x40') + data = b'\x00\x53\x70\xc0\x07\x05\x42\x40\x40\x42\x52\x00\x00\x53\x73\xc0\x1f\x0d\x40\x40\x40\x40\x40\x40\x40\x40\x83\x00\x00\x00\x00\x00\x00\x00\x00\x83\x00\x00\x00\x00\x00\x00\x00\x00\x40\x52\x00\x40' msg3 = Message() msg3.decode(data) assert msg3.priority == 4, (msg3.priority) # Minified message with zero length HEADER and PROPERTIES lists - data = str2bin('\x00\x53\x70\x45' '\x00\x53\x73\x45') + data = b'\x00\x53\x70\x45' b'\x00\x53\x73\x45' msg4 = Message() msg4.decode(data) assert msg4.priority == 4, (msg4.priority) http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51934030/tests/python/proton_tests/sasl.py ---------------------------------------------------------------------- diff --git a/tests/python/proton_tests/sasl.py b/tests/python/proton_tests/sasl.py index 804c828..68ae200 100644 --- a/tests/python/proton_tests/sasl.py +++ b/tests/python/proton_tests/sasl.py @@ -24,7 +24,6 @@ from . import engine from proton import * from .common import pump, Skipped -from proton._compat import str2bin def _sslCertpath(file): """ Return the full path to the certificate, keyfile, etc. @@ -113,20 +112,20 @@ class SaslTest(Test): assert self.s2.outcome is None # Push client bytes into server - self.t2.push(str2bin( + self.t2.push( # SASL - 'AMQP\x03\x01\x00\x00' + b'AMQP\x03\x01\x00\x00' # @sasl-init(65) [mechanism=:ANONYMOUS, initial-response=b"anonymous@fuschia"] - '\x00\x00\x002\x02\x01\x00\x00\x00SA\xd0\x00\x00\x00"\x00\x00\x00\x02\xa3\x09ANONYMOUS\xa0\x11anonymous@fuschia' + b'\x00\x00\x002\x02\x01\x00\x00\x00SA\xd0\x00\x00\x00"\x00\x00\x00\x02\xa3\x09ANONYMOUS\xa0\x11anonymous@fuschia' # SASL (again illegally) - 'AMQP\x03\x01\x00\x00' + b'AMQP\x03\x01\x00\x00' # @sasl-init(65) [mechanism=:ANONYMOUS, initial-response=b"anonymous@fuschia"] - '\x00\x00\x002\x02\x01\x00\x00\x00SA\xd0\x00\x00\x00"\x00\x00\x00\x02\xa3\x09ANONYMOUS\xa0\x11anonymous@fuschia' + b'\x00\x00\x002\x02\x01\x00\x00\x00SA\xd0\x00\x00\x00"\x00\x00\x00\x02\xa3\x09ANONYMOUS\xa0\x11anonymous@fuschia' # AMQP - 'AMQP\x00\x01\x00\x00' + b'AMQP\x00\x01\x00\x00' # @open(16) [container-id="", channel-max=1234] - '\x00\x00\x00!\x02\x00\x00\x00\x00S\x10\xd0\x00\x00\x00\x11\x00\x00\x00\x0a\xa1\x00@@`\x04\xd2@@@@@@' - )) + b'\x00\x00\x00!\x02\x00\x00\x00\x00S\x10\xd0\x00\x00\x00\x11\x00\x00\x00\x0a\xa1\x00@@`\x04\xd2@@@@@@' + ) consumeAllOuput(self.t2) @@ -144,16 +143,16 @@ class SaslTest(Test): assert self.s2.outcome is None # Push client bytes into server - self.t2.push(str2bin( + self.t2.push( # SASL - 'AMQP\x03\x01\x00\x00' + b'AMQP\x03\x01\x00\x00' # @sasl-init(65) [mechanism=:ANONYMOUS, initial-response=b"anonymous@fuschia"] - '\x00\x00\x002\x02\x01\x00\x00\x00SA\xd0\x00\x00\x00"\x00\x00\x00\x02\xa3\x09ANONYMOUS\xa0\x11anonymous@fuschia' + b'\x00\x00\x002\x02\x01\x00\x00\x00SA\xd0\x00\x00\x00"\x00\x00\x00\x02\xa3\x09ANONYMOUS\xa0\x11anonymous@fuschia' # AMQP - 'AMQP\x00\x01\x00\x00' + b'AMQP\x00\x01\x00\x00' # @open(16) [container-id="", channel-max=1234] - '\x00\x00\x00!\x02\x00\x00\x00\x00S\x10\xd0\x00\x00\x00\x11\x00\x00\x00\x0a\xa1\x00@@`\x04\xd2@@@@@@' - )) + b'\x00\x00\x00!\x02\x00\x00\x00\x00S\x10\xd0\x00\x00\x00\x11\x00\x00\x00\x0a\xa1\x00@@`\x04\xd2@@@@@@' + ) consumeAllOuput(self.t2) @@ -173,18 +172,18 @@ class SaslTest(Test): # Push server bytes into client # Commented out lines in this test are where the client input processing doesn't # run after output processing even though there is input waiting - self.t1.push(str2bin( + self.t1.push( # SASL - 'AMQP\x03\x01\x00\x00' + b'AMQP\x03\x01\x00\x00' # @sasl-mechanisms(64) [sasl-server-mechanisms=@PN_SYMBOL[:ANONYMOUS]] - '\x00\x00\x00\x1c\x02\x01\x00\x00\x00S@\xc0\x0f\x01\xe0\x0c\x01\xa3\tANONYMOUS' + b'\x00\x00\x00\x1c\x02\x01\x00\x00\x00S@\xc0\x0f\x01\xe0\x0c\x01\xa3\tANONYMOUS' # @sasl-outcome(68) [code=0] - '\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00' + b'\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00' # AMQP - 'AMQP\x00\x01\x00\x00' + b'AMQP\x00\x01\x00\x00' # @open(16) [container-id="", channel-max=1234] - '\x00\x00\x00!\x02\x00\x00\x00\x00S\x10\xd0\x00\x00\x00\x11\x00\x00\x00\x0a\xa1\x00@@`\x04\xd2@@@@@@' - )) + b'\x00\x00\x00!\x02\x00\x00\x00\x00S\x10\xd0\x00\x00\x00\x11\x00\x00\x00\x0a\xa1\x00@@`\x04\xd2@@@@@@' + ) consumeAllOuput(self.t1) @@ -217,17 +216,17 @@ class SaslTest(Test): out = self.t1.peek(1024) self.t1.pop(len(out)) - self.t1.push(str2bin("AMQP\x03\x01\x00\x00")) + self.t1.push(b"AMQP\x03\x01\x00\x00") out = self.t1.peek(1024) self.t1.pop(len(out)) - self.t1.push(str2bin("\x00\x00\x00")) + self.t1.push(b"\x00\x00\x00") out = self.t1.peek(1024) self.t1.pop(len(out)) - self.t1.push(str2bin("6\x02\x01\x00\x00\x00S@\xc0\x29\x01\xe0\x26\x04\xa3\x05PLAIN\x0aDIGEST-MD5\x09ANONYMOUS\x08CRAM-MD5")) + self.t1.push(b"6\x02\x01\x00\x00\x00S@\xc0\x29\x01\xe0\x26\x04\xa3\x05PLAIN\x0aDIGEST-MD5\x09ANONYMOUS\x08CRAM-MD5") out = self.t1.peek(1024) self.t1.pop(len(out)) - self.t1.push(str2bin("\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00")) + self.t1.push(b"\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00") out = self.t1.peek(1024) self.t1.pop(len(out)) while out: http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/51934030/tests/python/proton_tests/transport.py ---------------------------------------------------------------------- diff --git a/tests/python/proton_tests/transport.py b/tests/python/proton_tests/transport.py index 5a5cea2..5390d07 100644 --- a/tests/python/proton_tests/transport.py +++ b/tests/python/proton_tests/transport.py @@ -21,7 +21,6 @@ import os import sys from . import common from proton import * -from proton._compat import str2bin class Test(common.Test): @@ -63,16 +62,16 @@ class ClientTransportTest(Test): assert self.conn.remote_condition.name == name, self.conn.remote_condition def testEOS(self): - self.transport.push(str2bin("")) # should be a noop + self.transport.push(b"") # should be a noop self.transport.close_tail() # should result in framing error self.assert_error(u'amqp:connection:framing-error') def testPartial(self): - self.transport.push(str2bin("AMQ")) # partial header + self.transport.push(b"AMQ") # partial header self.transport.close_tail() # should result in framing error self.assert_error(u'amqp:connection:framing-error') - def testGarbage(self, garbage=str2bin("GARBAGE_")): + def testGarbage(self, garbage=b"GARBAGE_"): self.transport.push(garbage) self.assert_error(u'amqp:connection:framing-error') assert self.transport.pending() < 0 @@ -80,35 +79,35 @@ class ClientTransportTest(Test): assert self.transport.pending() < 0 def testSmallGarbage(self): - self.testGarbage(str2bin("XXX")) + self.testGarbage(b"XXX") def testBigGarbage(self): - self.testGarbage(str2bin("GARBAGE_XXX")) + self.testGarbage(b"GARBAGE_XXX") def testHeader(self): - self.transport.push(str2bin("AMQP\x00\x01\x00\x00")) + self.transport.push(b"AMQP\x00\x01\x00\x00") self.transport.close_tail() self.assert_error(u'amqp:connection:framing-error') def testHeaderBadDOFF1(self): """Verify doff > size error""" - self.testGarbage(str2bin("AMQP\x00\x01\x00\x00\x00\x00\x00\x08\x08\x00\x00\x00")) + self.testGarbage(b"AMQP\x00\x01\x00\x00\x00\x00\x00\x08\x08\x00\x00\x00") def testHeaderBadDOFF2(self): """Verify doff < 2 error""" - self.testGarbage(str2bin("AMQP\x00\x01\x00\x00\x00\x00\x00\x08\x01\x00\x00\x00")) + self.testGarbage(b"AMQP\x00\x01\x00\x00\x00\x00\x00\x08\x01\x00\x00\x00") def testHeaderBadSize(self): """Verify size > max_frame_size error""" self.transport.max_frame_size = 512 - self.testGarbage(str2bin("AMQP\x00\x01\x00\x00\x00\x00\x02\x01\x02\x00\x00\x00")) + self.testGarbage(b"AMQP\x00\x01\x00\x00\x00\x00\x02\x01\x02\x00\x00\x00") def testProtocolNotSupported(self): - self.transport.push(str2bin("AMQP\x01\x01\x0a\x00")) + self.transport.push(b"AMQP\x01\x01\x0a\x00") p = self.transport.pending() assert p >= 8, p bytes = self.transport.peek(p) - assert bytes[:8] == str2bin("AMQP\x00\x01\x00\x00") + assert bytes[:8] == b"AMQP\x00\x01\x00\x00" self.transport.pop(p) self.drain() assert self.transport.closed @@ -127,8 +126,8 @@ class ClientTransportTest(Test): trn = Transport() trn.bind(conn) out = trn.peek(1024) - assert str2bin("test-container") in out, repr(out) - assert str2bin("test-hostname") in out, repr(out) + assert b"test-container" in out, repr(out) + assert b"test-hostname" in out, repr(out) self.transport.push(out) c = Connection() @@ -185,7 +184,7 @@ class ClientTransportTest(Test): self.transport.pop(len(dat2) - len(dat1)) dat3 = self.transport.peek(1024) self.transport.pop(len(dat3)) - assert self.transport.peek(1024) == str2bin("") + assert self.transport.peek(1024) == b"" self.peer.push(dat1) self.peer.push(dat2[len(dat1):]) @@ -228,50 +227,50 @@ class ServerTransportTest(Test): # TODO: This may no longer be testing anything def testEOS(self): - self.transport.push(str2bin("")) # should be a noop + self.transport.push(b"") # should be a noop self.transport.close_tail() p = self.transport.pending() self.drain() assert self.transport.closed def testPartial(self): - self.transport.push(str2bin("AMQ")) # partial header + self.transport.push(b"AMQ") # partial header self.transport.close_tail() p = self.transport.pending() assert p >= 8, p bytes = self.transport.peek(p) - assert bytes[:8] == str2bin("AMQP\x00\x01\x00\x00") + assert bytes[:8] == b"AMQP\x00\x01\x00\x00" self.transport.pop(p) self.drain() assert self.transport.closed - def testGarbage(self, garbage="GARBAGE_"): - self.transport.push(str2bin(garbage)) + def testGarbage(self, garbage=b"GARBAGE_"): + self.transport.push(garbage) p = self.transport.pending() assert p >= 8, p bytes = self.transport.peek(p) - assert bytes[:8] == str2bin("AMQP\x00\x01\x00\x00") + assert bytes[:8] == b"AMQP\x00\x01\x00\x00" self.transport.pop(p) self.drain() assert self.transport.closed def testSmallGarbage(self): - self.testGarbage("XXX") + self.testGarbage(b"XXX") def testBigGarbage(self): - self.testGarbage("GARBAGE_XXX") + self.testGarbage(b"GARBAGE_XXX") def testHeader(self): - self.transport.push(str2bin("AMQP\x00\x01\x00\x00")) + self.transport.push(b"AMQP\x00\x01\x00\x00") self.transport.close_tail() self.assert_error(u'amqp:connection:framing-error') def testProtocolNotSupported(self): - self.transport.push(str2bin("AMQP\x01\x01\x0a\x00")) + self.transport.push(b"AMQP\x01\x01\x0a\x00") p = self.transport.pending() assert p >= 8, p bytes = self.transport.peek(p) - assert bytes[:8] == str2bin("AMQP\x00\x01\x00\x00") + assert bytes[:8] == b"AMQP\x00\x01\x00\x00" self.transport.pop(p) self.drain() assert self.transport.closed @@ -290,8 +289,8 @@ class ServerTransportTest(Test): trn = Transport() trn.bind(conn) out = trn.peek(1024) - assert str2bin("test-container") in out, repr(out) - assert str2bin("test-hostname") in out, repr(out) + assert b"test-container" in out, repr(out) + assert b"test-hostname" in out, repr(out) self.transport.push(out) c = Connection() @@ -348,7 +347,7 @@ class ServerTransportTest(Test): self.transport.pop(len(dat2) - len(dat1)) dat3 = self.transport.peek(1024) self.transport.pop(len(dat3)) - assert self.transport.peek(1024) == str2bin("") + assert self.transport.peek(1024) == b"" self.peer.push(dat1) self.peer.push(dat2[len(dat1):]) --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
