diff --git a/test/barcodes/QR_CODE-binary-80.png b/test/barcodes/QR_CODE-binary-80.png new file mode 100644 index 0000000..5f378ae Binary files /dev/null and b/test/barcodes/QR_CODE-binary-80.png differ diff --git a/test/barcodes/QR_CODE-binary-8081fe01008200.png b/test/barcodes/QR_CODE-binary-8081fe01008200.png new file mode 100644 index 0000000..c5f9bd1 Binary files /dev/null and b/test/barcodes/QR_CODE-binary-8081fe01008200.png differ diff --git a/test/test_all.py b/test/test_all.py index f508efd..77a4129 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -16,6 +16,8 @@ ( 'QR CODE (¡filenáme törture test! 😉).png', 'QR_CODE', 'This should be QR_CODE' ), ( 'QR_CODE-png-but-wrong-extension.bmp', 'QR_CODE', 'This should be QR_CODE' ), ( 'QR_CODE-fun-with-whitespace.png', 'QR_CODE', '\n\r\t\r\r\r\n ' ), + ( 'QR_CODE-binary-8081fe01008200.png', 'QR_CODE', b'\x80\x81\xfe\x01\x00\x82\x00' ), + ( 'QR_CODE-binary-80.png', 'QR_CODE', b'\x80' ), ( 'QR_CODE-screen_scraping_torture_test.png', 'QR_CODE', '\n\\n¡Atención ☹! UTF-8 characters,\n\r embedded newlines,\r &&am&p;& trailing whitespace\t \r ' ), ] @@ -44,8 +46,11 @@ def _check_decoding(filename, expected_format, expected_raw, extra={}): if dec is not None: raise AssertionError('Expected failure, but got result in {} format'.format(expected_format, dec.format)) else: - if dec.raw != expected_raw: + if isinstance(expected_raw, bytes) and dec.raw_bytes != expected_raw: + raise AssertionError('Expected {!r} but got {!r}'.format(expected_raw, dec.raw_bytes)) + elif dec.raw != expected_raw: raise AssertionError('Expected {!r} but got {!r}'.format(expected_raw, dec.raw)) + if dec.format != expected_format: raise AssertionError('Expected {!r} but got {!r}'.format(expected_format, dec.format)) diff --git a/zxing/__init__.py b/zxing/__init__.py index 9a95e12..e6256fa 100644 --- a/zxing/__init__.py +++ b/zxing/__init__.py @@ -140,11 +140,19 @@ def parse(cls, zxing_output): if m: points.append((float(m.group(1)), float(m.group(2)))) - raw = raw[:-1].decode() - parsed = parsed[:-1].decode() - return cls(uri, format, type, raw, parsed, points) + raw_bytes = raw[:-1] + try: + raw = raw[:-1].decode() + except UnicodeDecodeError: + raw = None + try: + parsed = parsed[:-1].decode() + except UnicodeDecodeError: + parsed = None + return cls(uri, format, type, raw_bytes, raw, parsed, points) - def __init__(self, uri, format, type, raw, parsed, points): + def __init__(self, uri, format, type, raw_bytes, raw, parsed, points): + self.raw_bytes = raw_bytes self.raw = raw self.parsed = parsed self.uri = uri @@ -153,5 +161,5 @@ def __init__(self, uri, format, type, raw, parsed, points): self.points = points def __repr__(self): - return '{}(raw={!r}, parsed={!r}, uri={!r}, format={!r}, type={!r}, points={!r})'.format( - self.__class__.__name__, self.raw, self.parsed, self.uri, self.format, self.type, self.points) + return '{}(raw={!r}, parsed={!r}, uri={!r}, format={!r}, type={!r}, points={!r}, raw_bytes={!r})'.format( + self.__class__.__name__, self.raw, self.parsed, self.uri, self.format, self.type, self.points, self.raw_bytes)