From d32b2a58fc7968872c3eedabbef29df6b5432c15 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Sat, 6 May 2017 12:14:59 +0000 Subject: [PATCH] Improved the behavior of accepting/handling byte-sequence data types in Python code. --- python/qrcodegen.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/python/qrcodegen.py b/python/qrcodegen.py index 666d658..d61297b 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -81,8 +81,8 @@ class QrCode(object): This function always encodes using the binary segment mode, not any text mode. The maximum number of bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.""" - if not isinstance(data, bytes): - raise TypeError("Binary array expected") + if not isinstance(data, (bytes, bytearray)): + raise TypeError("Byte string/list expected") return QrCode.encode_segments([QrSegment.make_bytes(data)], ecl) @@ -613,10 +613,14 @@ class QrSegment(object): @staticmethod def make_bytes(data): """Returns a segment representing the given binary data encoded in byte mode.""" - bb = _BitBuffer() py3 = sys.version_info.major >= 3 + if (py3 and isinstance(data, str)) or (not py3 and isinstance(data, unicode)): + raise TypeError("Byte string/list expected") + if not py3 and isinstance(data, str): + data = bytearray(data) + bb = _BitBuffer() for b in data: - bb.append_bits((b if py3 else ord(b)), 8) + bb.append_bits(b, 8) return QrSegment(QrSegment.Mode.BYTE, len(data), bb.get_bits())