This is an automated email from the ASF dual-hosted git repository. jdanek pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-python.git
commit bd2a9f1bba2809a6429b1b66202c68f0f14287c4 Author: Jiri Daněk <jda...@redhat.com> AuthorDate: Tue Apr 11 10:21:54 2023 +0200 QPID-8631: use io.BytesIO for bytes --- qpid/codec.py | 13 +++++-------- qpid/connection08.py | 13 +++++-------- qpid/tests/codec.py | 17 ++++++++--------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/qpid/codec.py b/qpid/codec.py index 01c4309..4ee38ef 100644 --- a/qpid/codec.py +++ b/qpid/codec.py @@ -29,10 +29,7 @@ The unit test for this module is located in tests/codec.py from __future__ import absolute_import import re, qpid, os from . import spec08 -try: - from cStringIO import StringIO -except ImportError: - from io import StringIO +from io import BytesIO from struct import * from .reference import ReferenceId from logging import getLogger @@ -438,7 +435,7 @@ class Codec: """ encodes a table data structure in network byte order """ - enc = StringIO() + enc = BytesIO() codec = Codec(enc, self.spec) if tbl: for key, value in tbl.items(): @@ -573,7 +570,7 @@ class Codec: def encode_struct(self, type, s): if type.size: - enc = StringIO() + enc = BytesIO() codec = Codec(enc, self.spec) codec.encode_struct_body(type, s) codec.flush() @@ -636,7 +633,7 @@ class Codec: return s def encode_long_struct(self, s): - enc = StringIO() + enc = BytesIO() codec = Codec(enc, self.spec) type = s.type codec.encode_short(type.type) @@ -644,7 +641,7 @@ class Codec: self.encode_longstr(enc.getvalue()) def decode_long_struct(self): - codec = Codec(StringIO(self.decode_longstr()), self.spec) + codec = Codec(BytesIO(self.decode_longstr()), self.spec) type = self.spec.structs[codec.decode_short()] return codec.decode_struct_body(type) diff --git a/qpid/connection08.py b/qpid/connection08.py index 338e79f..ecd4eb0 100644 --- a/qpid/connection08.py +++ b/qpid/connection08.py @@ -26,10 +26,7 @@ server, or even a proxy implementation. from __future__ import absolute_import import socket, errno, qpid from . import codec -try: - from cStringIO import StringIO -except ImportError: - from io import StringIO +from io import BytesIO from .codec import EOF from .compat import SHUT_RDWR from .exceptions import VersionError @@ -189,7 +186,7 @@ class Connection: c = self.codec c.encode_octet(self.spec.constants.byname[frame.type].id) c.encode_short(frame.channel) - body = StringIO() + body = BytesIO() enc = codec.Codec(body, self.spec) frame.encode(enc) enc.flush() @@ -211,7 +208,7 @@ class Connection: try: channel = c.decode_short() body = c.decode_longstr() - dec = codec.Codec(StringIO(body), self.spec) + dec = codec.Codec(BytesIO(body), self.spec) frame = Frame.DECODERS[type].decode(self.spec, dec, len(body)) frame.channel = channel end = c.decode_octet() @@ -250,7 +247,7 @@ class Connection: c.encode_octet(flags) # TODO: currently fixed at ver=0, B=E=b=e=1 c.encode_octet(self.spec.constants.byname[frame.type].id) - body = StringIO() + body = BytesIO() enc = codec.Codec(body, self.spec) frame.encode(enc) enc.flush() @@ -282,7 +279,7 @@ class Connection: raise FramingError("frame error: reserved bits not all zero") body_size = frame_size - 12 # TODO: Magic number (frame header size) body = c.read(body_size) - dec = codec.Codec(StringIO(body), self.spec) + dec = codec.Codec(BytesIO(body), self.spec) try: frame = Frame.DECODERS[type].decode(self.spec, dec, len(body)) except EOF: diff --git a/qpid/tests/codec.py b/qpid/tests/codec.py index 6520e0e..9d77b22 100644 --- a/qpid/tests/codec.py +++ b/qpid/tests/codec.py @@ -23,10 +23,7 @@ from __future__ import print_function import unittest from qpid.codec import Codec from qpid.spec08 import load -try: - from cStringIO import StringIO -except ImportError: - from io import StringIO +from io import BytesIO from qpid.reference import ReferenceId __doc__ = """ @@ -75,7 +72,7 @@ class BaseDataTypes(unittest.TestCase): """ standard setUp for unitetest (refer unittest documentation for details) """ - self.codec = Codec(StringIO(), SPEC) + self.codec = Codec(BytesIO(), SPEC) # ------------------ def tearDown(self): @@ -100,7 +97,7 @@ class BaseDataTypes(unittest.TestCase): helper function - creates a input stream and then calls the function with arguments as have been supplied """ - self.codec.stream = StringIO(args[0]) + self.codec.stream = BytesIO(args[0]) return getattr(self.codec, functionName)() @@ -212,7 +209,7 @@ class IntegerTestCase(BaseDataTypes): # ----------------------- def test_ulong_int(self): """ - testing unsigned long iteger + testing unsigned long integer """ self.failUnlessEqual(self.callFunc('encode_long', self.const_integer), self.const_integer_long_encoded, 'long encoding FAILED...') @@ -633,19 +630,21 @@ def test(type, value): values = value else: values = [value] - stream = StringIO() + stream = BytesIO() codec = Codec(stream, SPEC) for v in values: codec.encode(type, v) codec.flush() enc = stream.getvalue() - stream.reset() + stream.seek(0) dup = [] for i in range(len(values)): dup.append(codec.decode(type)) if values != dup: raise AssertionError("%r --> %r --> %r" % (values, enc, dup)) +test.__test__ = False # tells pytest to not try run this as a test function + # ----------------------- def dotest(type, value): """ --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org