From 628e81eb332af6e2fc4bbadbc66b5910d8709e14 Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Thu, 3 Dec 2020 20:50:53 +0100 Subject: [PATCH] Fix crash when using EAN without checksums Fixes #43 --- barcode/ean.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/barcode/ean.py b/barcode/ean.py index 0b4ab5b..cd23672 100755 --- a/barcode/ean.py +++ b/barcode/ean.py @@ -44,6 +44,8 @@ class EuropeanArticleNumber13(Barcode): digits = 12 def __init__(self, ean, writer=None, no_checksum=False): + self.digits = 13 if no_checksum else 12 + ean = ean[: self.digits] if not ean.isdigit(): raise IllegalCharacterError("EAN code can only contain numbers.") @@ -54,14 +56,9 @@ def __init__(self, ean, writer=None, no_checksum=False): len(ean), ) ) - self.ean = ean - # If no checksum + if no_checksum: - # Add a thirteen char if given in parameter, - # otherwise pad with zero - self.ean = "{}{}".format( - ean, ean[self.digits] if len(ean) > self.digits else 0 - ) + self.ean = ean else: self.ean = "{}{}".format(ean, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()