Skip to content

Commit 12307eb

Browse files
committed
Ensure that JAR for ZXing version detection matches classpath
Thank you @muued for clarifying this bug in #26 (comment)
1 parent 0512c7a commit 12307eb

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

test/test_all.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ def test_bad_java():
137137

138138

139139
def test_bad_classpath():
140-
test_reader = zxing.BarCodeReader(classpath=mkdtemp())
141140
with helper.assertRaises(zxing.BarCodeReaderException):
142-
test_reader.decode(test_barcodes[0][0])
141+
test_reader = zxing.BarCodeReader(classpath=mkdtemp())
143142

144143

145144
@with_setup(setup_reader)

zxing/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# library: http://code.google.com/p/zxing/
77
#
88

9+
import glob
910
import os
1011
import pathlib
1112
import re
@@ -14,6 +15,7 @@
1415
import zipfile
1516
from enum import Enum
1617
from io import IOBase
18+
from itertools import chain
1719

1820
try:
1921
from PIL.Image import Image
@@ -50,12 +52,17 @@ def __init__(self, classpath=None, java=None):
5052
else:
5153
self.classpath = os.path.join(os.path.dirname(__file__), 'java', '*')
5254

53-
with zipfile.ZipFile(os.path.join(os.path.dirname(__file__), 'java', 'core.jar')) as c:
54-
for line in c.open('META-INF/MANIFEST.MF'):
55-
if line.startswith(b'Bundle-Version: '):
56-
self.zxing_version = line.split(b' ', 1)[1].strip().decode()
57-
self.zxing_version_info = tuple(int(n) for n in self.zxing_version.split('.'))
58-
break
55+
for fn in chain.from_iterable(glob.glob(cp) for cp in self.classpath.split(':')):
56+
if os.path.basename(fn) == 'core.jar':
57+
self.core_jar = fn
58+
with zipfile.ZipFile(self.core_jar) as c:
59+
for line in c.open('META-INF/MANIFEST.MF'):
60+
if line.startswith(b'Bundle-Version: '):
61+
self.zxing_version = line.split(b' ', 1)[1].strip().decode()
62+
self.zxing_version_info = tuple(int(n) for n in self.zxing_version.split('.'))
63+
break
64+
return
65+
raise BarCodeReaderException("Java JARs not found in classpath (%s)" % self.classpath, self.classpath)
5966

6067
def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcode=False, products_only=False):
6168
possible_formats = (possible_formats,) if isinstance(possible_formats, str) else possible_formats

0 commit comments

Comments
 (0)