diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 29d98c8092..31c83987ab 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -1546,6 +1546,12 @@ class OtherTests(unittest.TestCase): zinfo.flag_bits |= 0x08 # Include an extended local header. orig_zip.writestr(zinfo, data) + def test_writestr_pathlike_issue40506(self): + with zipfile.ZipFile(TESTFN2, 'w') as orig_zip: + path = '/foo/bar.txt' + orig_zip.writestr(pathlib.PurePath(path), '1234') + self.assertEqual(orig_zip.open(path).read(4), b'1234') + def test_close(self): """Check that the zipfile is closed after the 'with' block.""" with zipfile.ZipFile(TESTFN2, "w") as zipfp: diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 8903d6a42e..44b3ee8e63 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -341,6 +341,8 @@ class ZipInfo (object): ) def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)): + if isinstance(filename, os.PathLike): + filename = os.fspath(filename) self.orig_filename = filename # Original file name in archive # Terminate the file name at the first null byte. Null bytes in file