diff --git a/test/test_all.py b/test/test_all.py index 044c026..4d6159e 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -1,5 +1,7 @@ import logging import os +from operator import attrgetter +from pathlib import Path from tempfile import mkdtemp from PIL import Image @@ -174,3 +176,33 @@ def test_bad_file_format_error(): global test_reader with helper.assertRaises(zxing.BarCodeReaderException): test_reader.decode(os.path.join(test_barcode_dir, 'bad_format.png')) + + +@with_setup(setup_reader) +def _check_filenames_type(filenames, expected_raw): + result = test_reader.decode(filenames) + if isinstance(filenames, set): + # Might be in another order. + assert set(map(attrgetter('raw'), result)) == set(expected_raw) + elif isinstance(expected_raw, list): + assert list(map(attrgetter('raw'), result)) == expected_raw + else: + assert result.raw == expected_raw + + +def test_filenames_types(): + multi_paths = [ + Path(test_barcode_dir, test_barcodes[0][0]), + os.path.join(test_barcode_dir, test_barcodes[1][0]) + ] + multi_expected = [test_barcodes[0][2], test_barcodes[1][2]] + yield from ( + (_check_filenames_type, filenames, expected_raw) for filenames, expected_raw in [ + (os.path.join(test_barcode_dir, test_barcodes[0][0]), test_barcodes[0][2]), + (Path(test_barcode_dir, test_barcodes[0][0]), test_barcodes[0][2]), + (multi_paths, multi_expected), + (set(multi_paths), multi_expected), + (tuple(multi_paths), multi_expected), + ((x for x in multi_paths), multi_expected), # Generator, inline version of `yield`. + ] + ) diff --git a/zxing/__init__.py b/zxing/__init__.py index 27421a4..508fea9 100644 --- a/zxing/__init__.py +++ b/zxing/__init__.py @@ -17,6 +17,7 @@ from enum import Enum from io import IOBase from itertools import chain +from types import GeneratorType try: from PIL.Image import Image @@ -69,11 +70,11 @@ def __init__(self, classpath=None, java=None): def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcode=False, products_only=False): possible_formats = (possible_formats,) if isinstance(possible_formats, str) else possible_formats - if isinstance(filenames, (str, IOBase, Image) if have_pil else (str, IOBase)): + if isinstance(filenames, (list, set, tuple, GeneratorType)): + one_file = False + else: one_file = True filenames = filenames, - else: - one_file = False file_uris = [] temp_files = []