Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .coverage
Binary file not shown.
2 changes: 1 addition & 1 deletion barcode/codex.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class PZN7(Code39):

def __init__(self, pzn: str, writer: BaseWriter | None = None) -> None:
pzn = pzn[: self.digits]
if not pzn.isdigit():
if not (pzn.isascii() and pzn.isdigit()):
raise IllegalCharacterError("PZN can only contain numbers.")
if len(pzn) != self.digits:
raise NumberOfDigitsError(
Expand Down
6 changes: 4 additions & 2 deletions barcode/ean.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def __init__(
no_checksum: bool = False,
guardbar: bool = False,
) -> None:
if not ean[: self.digits].isdigit():
base_candidate = ean[: self.digits]
if not (base_candidate.isascii() and base_candidate.isdigit()):
raise IllegalCharacterError(f"EAN code can only contain numbers {ean}.")

if len(ean) < self.digits:
Expand All @@ -65,7 +66,8 @@ def __init__(
base = ean[: self.digits]
if no_checksum:
# Use the thirteenth digit if given in parameter, otherwise pad with zero
if len(ean) > self.digits and ean[self.digits].isdigit():
check_digit = ean[self.digits : self.digits + 1]
if check_digit.isascii() and check_digit.isdigit():
last = int(ean[self.digits])
else:
last = 0
Expand Down
2 changes: 1 addition & 1 deletion barcode/itf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(
narrow: int = 2,
wide: int = 5,
) -> None:
if not code.isdigit():
if not (code.isascii() and code.isdigit()):
raise IllegalCharacterError("ITF code can only contain numbers.")
# Length must be even, prepend 0 if necessary
if len(code) % 2 != 0:
Expand Down
2 changes: 1 addition & 1 deletion barcode/upc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(
"""
self.ean = make_ean
upc = upc[: self.digits]
if not upc.isdigit():
if not (upc.isascii() and upc.isdigit()):
raise IllegalCharacterError("UPC code can only contain numbers.")
if len(upc) != self.digits:
raise NumberOfDigitsError(
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ v0.17.0
barcodes. (#251)
* Fix ISBN-10 and ISSN barcodes being truncated to 10 and 8 digits.
* Fix Code128 emitting different (but valid) output depending on invocation.
* Reject non-ASCII Unicode digits (such as ``²`` or Arabic-Indic digits) in
numeric barcodes with ``IllegalCharacterError`` instead of crashing with a
``ValueError`` or silently producing a corrupt barcode.

v0.16.1
~~~~~~~
Expand Down
18 changes: 18 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,21 @@ def test_generate_with_bytesio() -> None:
barcode.generate("ean13", "123455559121112", output=bio)
# XXX: File is not 100% deterministic; needs to be addressed at some point.
# assert len(bio.getvalue()) == 6127 # noqa: ERA001


# Non-ASCII Unicode characters for which ``str.isdigit()`` is True but which are
# not ASCII 0-9: superscript two and the Arabic-Indic digit three.
NON_ASCII_DIGITS = ["\N{SUPERSCRIPT TWO}", "\N{ARABIC-INDIC DIGIT THREE}"]


@pytest.mark.parametrize(
("code_type", "length"),
[("ean13", 12), ("ean8", 7), ("ean14", 13), ("upca", 11), ("itf", 6), ("pzn", 6)],
)
@pytest.mark.parametrize("char", NON_ASCII_DIGITS)
def test_non_ascii_digits_rejected(code_type: str, length: int, char: str) -> None:
# ``str.isdigit()`` accepts non-ASCII Unicode digits, so these used to either
# crash with a raw ValueError or silently produce a corrupt barcode instead
# of raising the documented IllegalCharacterError.
with pytest.raises(barcode.errors.IllegalCharacterError):
barcode.get_barcode(code_type, char * length)