-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
ResourceWarning in GzipFile (write mode) if constructor raises (3.14 only) #131461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Want to separate out a couple pieces for clarity here, new to this code example, want to validate my reading of it:
I think all three of these should definitely be improved, just that they're somewhat distinct. 1 and 3 should definitely be improved in CPython itself. Looks like your PR helps primarily with 1. This warning was added in 3.14 after code inspection around user reports in Python 3.12 For 2, I think the sample code would be more robust by using |
for 2 you shouldn't need to close a GzipFile if it raises in the constructor, we use self.assertRaises so we know the tarfile.open is responsible for closing its resources before raising, so we don't need to use |
Agreed, 1 should mean you don't get a warning in the sample code. It's important for |
Tests / samples can (and often do) reduce code to increase clarity and make sure that catch errors expect. As a test case, I think the sample code is reasonable. In general code which uses |
… owning resources (#131462) Co-authored-by: Victor Stinner <[email protected]>
…ructor while owning resources (pythonGH-131462) (cherry picked from commit ce79274) Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
…ructor while owning resources (pythonGH-131462) (cherry picked from commit ce79274) Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
…r while owning resources (GH-131462) (#131518) (cherry picked from commit ce79274) Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
…r while owning resources (GH-131462) (#131519) (cherry picked from commit ce79274) Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
Fixed by the change ce79274. |
Oh, I still get a ResourceWarning when I run the reproducer script:
I reopen the issue. |
apologies, the demo code was slightly off, it should have been import io
import gzip
class BadFile(io.BytesIO):
first = False
def write(self, data):
if not self.first:
self.first = False
raise OSError
def main():
try:
gzip.GzipFile(fileobj=BadFile(), mode="w")
except OSError:
pass
else:
assert False, "did not raise OSError!"
main() |
Aha, with the adjusted reproducer, I don't get a ResourceWarning anymore. I close again the issue :-) |
…ructor while owning resources (python#131462) Co-authored-by: Victor Stinner <[email protected]>
Bug report
Bug description:
when run produces:
This warning is also raised by the test suite, in test_tarfile.WriteTestBase.test_open_nonwritable_fileobj
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: