[issue40506] add support for os.Pathlike filenames in zipfile.ZipFile.writestr

2021-09-07 Thread Ethan Furman


Change by Ethan Furman :


--
nosy: +ethan.furman

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40506] add support for os.Pathlike filenames in zipfile.ZipFile.writestr

2020-05-22 Thread Cheryl Sabella


Change by Cheryl Sabella :


--
nosy: +serhiy.storchaka, twouters
versions: +Python 3.10 -Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40506] add support for os.Pathlike filenames in zipfile.ZipFile.writestr

2020-05-08 Thread Domenico Ragusa


Change by Domenico Ragusa :


--
pull_requests: +19314
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/20002

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40506] add support for os.Pathlike filenames in zipfile.ZipFile.writestr

2020-05-06 Thread Domenico Ragusa

Domenico Ragusa  added the comment:

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  wrote:
>
>
> New submission from Domenico Ragusa :
>
> 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 "", line 1, in 
>   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 
> 
> ___

--
keywords: +patch
Added file: https://bugs.python.org/file49132/pathlike_writestr.patch

___
Python tracker 

___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
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40506] add support for os.Pathlike filenames in zipfile.ZipFile.writestr

2020-05-04 Thread Domenico Ragusa


New submission from Domenico Ragusa :

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 "", line 1, in 
  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com