From 5fabf445b2a4a08e0ddc5df7feb49edc4c53496d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Bruguera=20Mic=C3=B3?= Date: Sat, 6 Jul 2024 23:35:23 +0000 Subject: [PATCH] Remove workaround for fixed Python zipfile issue. The issue that was worked around has been fixed in upstream CPython: https://github.com/python/cpython/commit/36ff513f82e372ed3cea0bf7cbdf15a1ef6dab9e Since the fix is included in CPython v3.8.4+ and v3.9+, which includes all supported versions, we can get rid of this ugly workaround. --- full_offline_backup_for_todoist/virtual_fs.py | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/full_offline_backup_for_todoist/virtual_fs.py b/full_offline_backup_for_todoist/virtual_fs.py index dabba7c..9fb5d2c 100644 --- a/full_offline_backup_for_todoist/virtual_fs.py +++ b/full_offline_backup_for_todoist/virtual_fs.py @@ -34,8 +34,6 @@ def write_file(self, file_path: str, file_data: bytes) -> None: class ZipVirtualFs(VirtualFs): """ Represents a virtual filesystem over a ZIP file """ - __ZIP_FLAG_BITS_UTF8 = 0x800 - src_path: Optional[str] dst_path: Optional[str] _zip_file: Optional[zipfile.ZipFile] @@ -88,28 +86,3 @@ def read_file(self, file_path: str) -> bytes: def write_file(self, file_path: str, file_data: bytes) -> None: assert self._zip_file self._zip_file.writestr(file_path, file_data) - - # WORKAROUND FOR A PYTHON BUG in Python's zipfile (at least in Python 3.6.5) - # There's a bug in the zipfile module, where if a file with a non-ASCII name - # is first writen to the ZIP, and then immediately read before saving the ZIP, - # then it will crash with something like: - - # zipfile.BadZipFile: File name in directory '🦋' and header b'\xf0\x9f\xa6\x8b' differ. - # Sample reproduction: - # with zipfile.ZipFile(io.BytesIO(), "a") as zip: - # zip.writestr("🦋test", b'010203') - # print(zip.read("🦋test")) - # This happens because the flag_bits specifying whether the filename is UTF-8 or not - # are set when saving the ZIP, not when creating the in-memory ZipInfo instance, - # so when reading the file again in-memory, it gets confused and thinks the filename - # in the ZipInfo is not UTF-8 and tries to decode it with another encoding - - # This isn't really surprising because encoding in zipfile.py is a mess currently... - # See also: https://bugs.python.org/issue12048 https://bugs.python.org/issue10614 - - # As a workaround, we set the in-memory ZipInfo flag when the filename is UTF-8 - try: - file_path.encode('ascii') - except UnicodeEncodeError: - file_info = self._zip_file.getinfo(file_path) - file_info.flag_bits = file_info.flag_bits | self.__ZIP_FLAG_BITS_UTF8