classification
Title: add support for os.Pathlike filenames in zipfile.ZipFile.writestr
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: d.ragusa, serhiy.storchaka, twouters
Priority: normal Keywords: patch

Created on 2020-05-05 02:12 by d.ragusa, last changed 2020-05-22 20:09 by cheryl.sabella.

Files
File name Uploaded Description Edit
pathlike_writestr.patch d.ragusa, 2020-05-06 18:31
Pull Requests
URL Status Linked Edit
PR 20002 open d.ragusa, 2020-05-08 13:44
Messages (2)
msg368098 - (view) Author: Domenico Ragusa (d.ragusa) * Date: 2020-05-05 02:12
ZipFile seems to support Pathlike objects pretty well, except in ZipFile.writestr.
For example:

>>> a = ZipFile(Path('test.zip'), 'w') # this works ok
>>> a.write(Path('./foo.jpeg'), arcname=PurePath('/some/thing.jpeg')) # this works as well
>>> a.writestr(PurePath('/test.txt'), 'idk') # this doesn't
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/zipfile.py", line 1788, in writestr
    zinfo = ZipInfo(filename=zinfo_or_arcname,
  File "/usr/lib/python3.8/zipfile.py", line 349, in __init__
    null_byte = filename.find(chr(0))
AttributeError: 'PurePosixPath' object has no attribute 'find'

I think it would be more consistent if it accepted any kind of paths, it would suffice to call os.fspath in ZipInfo.__init__ when the filename is a Pathlike-object, it's just 2 lines (+ tests, of course).

Can I go ahead and prepare a patch for this?
msg368289 - (view) Author: Domenico Ragusa (d.ragusa) * Date: 2020-05-06 18:31
Here's a small patch to do this. Everything seems to work fine.
I don't know if where I placed the test (in OtherTests) is the most appropriate.

On Tue, May 5, 2020 at 4:12 AM Domenico Ragusa <report@bugs.python.org> wrote:
>
>
> New submission from Domenico Ragusa <domenicoragusa@gmail.com>:
>
> ZipFile seems to support Pathlike objects pretty well, except in ZipFile.writestr.
> For example:
>
> >>> a = ZipFile(Path('test.zip'), 'w') # this works ok
> >>> a.write(Path('./foo.jpeg'), arcname=PurePath('/some/thing.jpeg')) # this works as well
> >>> a.writestr(PurePath('/test.txt'), 'idk') # this doesn't
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python3.8/zipfile.py", line 1788, in writestr
>     zinfo = ZipInfo(filename=zinfo_or_arcname,
>   File "/usr/lib/python3.8/zipfile.py", line 349, in __init__
>     null_byte = filename.find(chr(0))
> AttributeError: 'PurePosixPath' object has no attribute 'find'
>
> I think it would be more consistent if it accepted any kind of paths, it would suffice to call os.fspath in ZipInfo.__init__ when the filename is a Pathlike-object, it's just 2 lines (+ tests, of course).
>
> Can I go ahead and prepare a patch for this?
>
> ----------
> components: Library (Lib)
> messages: 368098
> nosy: d.ragusa
> priority: normal
> severity: normal
> status: open
> title: add support for os.Pathlike filenames in zipfile.ZipFile.writestr
> type: enhancement
> versions: Python 3.9
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue40506>
> _______________________________________
History
Date User Action Args
2020-05-22 20:09:05cheryl.sabellasetnosy: + twouters, serhiy.storchaka

versions: + Python 3.10, - Python 3.9
2020-05-08 13:44:00d.ragusasetstage: patch review
pull_requests: + pull_request19314
2020-05-06 18:31:13d.ragusasetfiles: + pathlike_writestr.patch
keywords: + patch
messages: + msg368289
2020-05-05 02:12:03d.ragusacreate