[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2021-04-24 Thread Jason R. Coombs


Jason R. Coombs  added the comment:

Thanks Andrei

--

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2021-04-24 Thread Jason R. Coombs


Change by Jason R. Coombs :


--
resolution:  -> duplicate
stage: patch review -> resolved
status: open -> closed
superseder:  -> Using zipfile.Path with several files prematurely closes zip

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2021-04-24 Thread Andrei Kulakov


Andrei Kulakov  added the comment:

Looks like a duplicate of https://bugs.python.org/issue40564 , which was fixed 
and closed so this can also be closed.

--
nosy: +andrei.avk

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-24 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Not sure if it's related to this issue there is an existing issue with 
ZipFile.__del__ : issue37773

--
nosy: +xtreak

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-24 Thread Angel Siblani


Change by Angel Siblani :


--
components: +Demos and Tools, XML, email -Library (Lib)
nosy: +angelsb, barry, r.david.murray
type: behavior -> resource usage
versions:  -Python 3.8, Python 3.9

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-24 Thread Jason R. Coombs


Jason R. Coombs  added the comment:

This routine will repro the issue without relying on garbage collection to 
trigger the error:

```
import io
import zipfile
buf = io.BytesIO()
zf = zipfile.ZipFile(buf, mode='w')
zp = zipfile.Path(zf)
with zp.joinpath('zile-a').open('w') as fp:
fp.write('contents of file-a')
zf.close()
buf.close()
zp.root.close()
```

--

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-24 Thread Jason R. Coombs


Jason R. Coombs  added the comment:

I'm a little surprised from Nick's original post that the behavior is 
triggered. After all, the code [already has a protection for a 
previously-closed 
zipfile](https://github.com/python/cpython/blob/0dd98c2d00a75efbec19c2ed942923981bc06683/Lib/zipfile.py#L1812-L1813)
 and that value is [unconditionally set during 
close](https://github.com/python/cpython/blob/0dd98c2d00a75efbec19c2ed942923981bc06683/Lib/zipfile.py#L1828).
 So how is it that Zipfile.__del__ is being called?

Jeffrey suggests that

> a copy of the zip_file object is getting created, probably by the Path 
> constructor

And indeed, I can confirm the ZipFile [state is copied into a new object 
here](https://github.com/python/cpython/blob/0dd98c2d00a75efbec19c2ed942923981bc06683/Lib/zipfile.py#L2202).
 The FastLookup and CompleteDirs functionality is for performance optimizations.

> I created a simpler test case that exercises the buggy code.

Although the simpler test does trigger the error, I'd argue that the error is 
_expected_ in this case and that the zip file will be invalid because 
`self._write_end_record` will never get called.

I expected this example to more accurately capture the failure:

```
import io
import zipfile
buf = io.BytesIO()
zf = zipfile.ZipFile(buf, mode='w')
zp = zipfile.Path(zf)
with zp.joinpath('zile-a').open('w') as fp:
fp.write(b'contents of file-a')
zf.close()
zp.root.close()
```

But it does not. I'll need to do more investigation.

One option here is for `Path` to document that any zipfile passed to it is no 
longer valid and enforce that fact by disabling the original object passed to 
it.

Another approach could be to try to use an adapter pattern to adapt the 
original ZipFile rather than clone it into a subclass.

--

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-24 Thread Jeffrey Kintscher


Change by Jeffrey Kintscher :


--
keywords: +patch
pull_requests: +20745
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/21604

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-24 Thread Rajarishi Devarajan


Rajarishi Devarajan  added the comment:

Hi all, I have a working patch for this. Could I submit it for review ?

--
nosy: +rishi93

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-24 Thread Jeffrey Kintscher


Change by Jeffrey Kintscher :


--
versions: +Python 3.10

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-24 Thread Jeffrey Kintscher


Jeffrey Kintscher  added the comment:

I created a simpler test case that exercises the buggy code.  The problem can 
be reproduced by passing ZipFile.__init__() a file-like object with the write 
flag (mode "w") and then closing the file-like object before calling 
ZipFile.close().

The exception being triggered by the call to ZipFile.__del__() is a side-effect 
because all ZipFile.__del__() does is call ZipFile.close().



import io
from zipfile import ZipFile

bytes_io = io.BytesIO()
zip_file = ZipFile(bytes_io, mode="w")
bytes_io.close()
zip_file.close()  # throws ValueError

--
Added file: https://bugs.python.org/file49336/zipfile_close_bug.py

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-23 Thread Jeffrey Kintscher


Jeffrey Kintscher  added the comment:

It looks like a copy of the zip_file object is getting created, probably by the 
Path constructor:

zip_path = Path(zip_file, "file-a")

When return is invoked, the copy still has a reference to the now closed 
bytes_io object which causes the ValueError exception when ZipFile.__del__() 
calls ZipFile.close().  This is definitely a bug.  I'll take a crack at fixing 
it.

--

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-23 Thread Jeffrey Kintscher


Change by Jeffrey Kintscher :


--
nosy: +Jeffrey.Kintscher

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-20 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +jaraco

___
Python tracker 

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



[issue41350] Use of zipfile.Path causes attempt to write after ZipFile is closed

2020-07-20 Thread Nick Henderson


New submission from Nick Henderson :

In both Python 3.8.3 and 3.9.0b3, using zipfile.Path to write a file in a 
context manager results in an attempt to write to the zip file after it is 
closed.

In Python 3.9.0b3:

import io
from zipfile import ZipFile, Path

def make_zip():
"""Make zip file and return bytes."""
bytes_io = io.BytesIO()
zip_file = ZipFile(bytes_io, mode="w")
zip_path = Path(zip_file, "file-a")

# use zipp.Path.open
with zip_path.open(mode="wb") as fp:
fp.write(b"contents of file-a")

zip_file.close()

data = bytes_io.getvalue()

bytes_io.close()

return data

zip_data = make_zip()
# Exception ignored in: 
# Traceback (most recent call last):
#   File "/Users/nick/.pyenv/versions/3.9.0b3/lib/python3.9/zipfile.py", line 
1807, in __del__
# self.close()
#   File "/Users/nick/.pyenv/versions/3.9.0b3/lib/python3.9/zipfile.py", line 
1824, in close
# self.fp.seek(self.start_dir)
# ValueError: I/O operation on closed file.


In Python 3.8.3:

import io
from zipfile import ZipFile, Path

def make_zip():
"""Make zip file and return bytes."""
bytes_io = io.BytesIO()
zip_file = ZipFile(bytes_io, mode="w")
zip_path = Path(zip_file, "file-a")

# use zipp.Path.open
with zip_path.open(mode="w") as fp:
fp.write(b"contents of file-a")

zip_file.close()

data = bytes_io.getvalue()

bytes_io.close()

return data

zip_data = make_zip()
# Exception ignored in: 
# Traceback (most recent call last):
#   File "/Users/nick/.pyenv/versions/3.8.3/lib/python3.8/zipfile.py", line 
1820, in __del__
# self.close()
#   File "/Users/nick/.pyenv/versions/3.8.3/lib/python3.8/zipfile.py", line 
1837, in close
# self.fp.seek(self.start_dir)
# ValueError: I/O operation on closed file.

In the Python 3.8 example, mode="w" is used in the open method on zip_path.

--
components: Library (Lib)
files: zippath_bug_39.py
messages: 374015
nosy: Nick Henderson
priority: normal
severity: normal
status: open
title: Use of zipfile.Path causes attempt to write after ZipFile is closed
type: behavior
versions: Python 3.8, Python 3.9
Added file: https://bugs.python.org/file49329/zippath_bug_39.py

___
Python tracker 

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