Skip to content

Commit

Permalink
mangled 'binary' barcode (see #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlenski committed Aug 25, 2021
1 parent 938ce86 commit aff3dde
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
Binary file added test/barcodes/QR_CODE-binary-80.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/barcodes/QR_CODE-binary-8081fe01008200.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' ),
]
Expand Down Expand Up @@ -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))

Expand Down
20 changes: 14 additions & 6 deletions zxing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

0 comments on commit aff3dde

Please sign in to comment.