lincolnloop / python-qrcode

Python QR Code image generator
https://pypi.python.org/pypi/qrcode
Other
4.25k stars 660 forks source link

qrcode fails tests with Pillow 10.3.0 #361

Open doko42 opened 2 months ago

doko42 commented 2 months ago

qrcode fails tests with Pillow 10.3.0 (and Python 3.12.x)

39s =================================== FAILURES =================================== 39s ____ ScriptTest.testpiped ____ 39s 39s self = 39s mock_stdout = 39s 39s @mock.patch("os.isatty", lambda *args: False) 39s @mock.patch("sys.stdout") 39s @unittest.skipIf(not Image, "Requires PIL") 39s def test_piped(self, mock_stdout): 39s > main(["testtext"]) 39s 39s tests/testscript.py:33: 39s _ 39s /usr/lib/python3/dist-packages/qrcode/console_scripts.py:139: in main 39s img.save(sys.stdout.buffer) 39s /usr/lib/python3/dist-packages/qrcode/image/pil.py:51: in save 39s self.img.save(stream, format=format, **kwargs) 39s _ 39s 39s self = <PIL.Image.Image image mode=1 size=290x290 at 0x7FDF4DD57530> 39s fp = , format = 'PNG' 39s params = {} 39s filename = '/tmp/autopkgtest-lxc.tzwg_onb/downtmp/autopkgtest_tmp/MagicMock/stdout.buffer/140597060286944' 39s open_fp = True, save_all = False, filename_ext = '', ext = '' 39s 39s def save(self, fp, format=None, **params) -> None: 39s """ 39s Saves this image under the given filename. If no format is 39s specified, the format to use is determined from the filename 39s extension, if possible. 39s
39s Keyword options can be used to provide additional instructions 39s to the writer. If a writer doesn't recognise an option, it is 39s silently ignored. The available options are described in the 39s :doc:image format documentation 39s <../handbook/image-file-formats> for each writer. 39s
39s You can use a file object instead of a filename. In this case, 39s you must always specify the format. The file object must 39s implement the seek, tell, and write 39s methods, and be opened in binary mode. 39s
39s :param fp: A filename (string), os.PathLike object or file object. 39s :param format: Optional format override. If omitted, the 39s format to use is determined from the filename extension. 39s If a file object was used instead of a filename, this 39s parameter should always be used. 39s :param params: Extra parameters to the image writer. 39s :returns: None 39s :exception ValueError: If the output format could not be determined 39s from the file name. Use the format option to solve this. 39s :exception OSError: If the file could not be written. The file 39s may have been created, and may contain partial data. 39s """ 39s
39s filename: str | bytes = "" 39s open_fp = False 39s if is_path(fp): 39s filename = os.path.realpath(os.fspath(fp)) 39s open_fp = True 39s elif fp == sys.stdout: 39s try: 39s fp = sys.stdout.buffer 39s except AttributeError: 39s pass 39s if not filename and hasattr(fp, "name") and is_path(fp.name): 39s # only set the name for metadata purposes 39s filename = os.path.realpath(os.fspath(fp.name)) 39s
39s # may mutate self! 39s self._ensure_mutable() 39s
39s save_all = params.pop("save_all", False) 39s self.encoderinfo = params 39s self.encoderconfig = () 39s
39s preinit() 39s
39s filename_ext = os.path.splitext(filename)[1].lower() 39s ext = filename_ext.decode() if isinstance(filename_ext, bytes) else filename_ext 39s
39s if not format: 39s if ext not in EXTENSION: 39s init() 39s try: 39s format = EXTENSION[ext] 39s except KeyError as e: 39s msg = f"unknown file extension: {ext}" 39s raise ValueError(msg) from e 39s
39s if format.upper() not in SAVE: 39s init() 39s if save_all: 39s save_handler = SAVE_ALL[format.upper()] 39s else: 39s save_handler = SAVE[format.upper()] 39s
39s created = False 39s if open_fp: 39s created = not os.path.exists(filename) 39s if params.get("append", False): 39s # Open also for reading ("+"), because TIFF save_all 39s # writer needs to go back and edit the written data. 39s fp = builtins.open(filename, "r+b") 39s else: 39s > fp = builtins.open(filename, "w+b") 39s E FileNotFoundError: [Errno 2] No such file or directory: '/tmp/autopkgtest-lxc.tzwg_onb/downtmp/autopkgtest_tmp/MagicMock/stdout.buffer/140597060286944' 39s 39s /usr/lib/python3/dist-packages/PIL/Image.py:2456: FileNotFoundError 39s =============================== warnings summary =============================== 39s tests/test_script.py::ScriptTest::test_bad_factory 39s /usr/lib/python3/dist-packages/qrcode/console_scripts.py:43: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html 39s from pkg_resources import get_distribution 39s 39s -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html 39s =========================== short test summary info ============================ 39s FAILED tests/test_script.py::ScriptTest::test_piped - FileNotFoundError: [Err... 39s =================== 1 failed, 78 passed, 1 warning in 3.33s ====================

arkamar commented 3 weeks ago

I have reproduced the same error with Pillow 10.2.0.