Skip to content

Commit

Permalink
Remove workaround for fixed Python zipfile issue.
Browse files Browse the repository at this point in the history
The issue that was worked around has been fixed in upstream CPython:
python/cpython@36ff513

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.
  • Loading branch information
joanbm committed Jul 6, 2024
1 parent e5cecfa commit 5fabf44
Showing 1 changed file with 0 additions and 27 deletions.
27 changes: 0 additions & 27 deletions full_offline_backup_for_todoist/virtual_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

0 comments on commit 5fabf44

Please sign in to comment.