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
NamedTemporaryFile does not generate a ResourceWarning for unclosed files (unlike TemporaryFile) #73053
Comments
When using unittest, I'll frequently enable -Wall to help catch code smells and potential bugs. One feature of this, I'm told when files aren't explicitly closed with an error like: "ResourceWarning: unclosed file <...>". I've noticed this warning is generated for an unclosed tempfile.TemporaryFile (good), but not for an unclosed tempfile.NamedTemporaryFile (possible bug). I've verified this on Python 3.5 and 3.6. Example test:
Actual output:
Expected: I expect both tests to generate a ResourceWarning, as neither test explicitly closes the file. |
Looking at the code it seems NamedTemporaryFile is closed on destruction of the object, so there is no need for ResourceWarning, because the file descriptor is not leaking. You also don't need to involve unittest here. Just running python with -Wall would generate a warning if there is a need for one. |
The point of ResourceWarning it tell you when you are depending on destructors for cleanup, so this does look like a bug. |
|
__del__ is a destructor. If the file is open when it is called, it should emit a ResourceWarning. |
The ResourceWarning that you've introduced need to be "fixed" in a few place in test_urllib2 |
Thanks for the review. I have updated the patch. Now all warnings during tests handled. Please let me know if there are any other concerns with the changes. |
Hmm. That e.close() looks like there may be a deeper problem here. I don't have time to investigate myself, unfortunately. |
Just for some context, the e.close() is handling this bit of code: Lines 739 to 754 in d8132c4
When there is no error, http_error_302() will close the passed fp, on error, it will not. The following comment indicates this is intentional: # Don't close the fp until we are sure that we won't use it But I agree, this may pose a problem when enforcing deterministic resource handling. |
I've taken a new approach to resolve the urllib issues. I believe HTTPError _should not_ warn when __del__ is called as HTTPError wraps an existing resource instead of generating its own. IIUC, in this case, I believe it falls to the responsibility of code generating the resource to properly close and manage the resource (or pass that responsibility along). If I misunderstood, please let me know. All feedback on the patch is welcome, thanks. |
I decided to try a new direction. Instead of modifying _TemporaryFileCloser to handle urllib, I've changed urllib classes to not inherit from _TemporaryFileCloser. The urllib classes are not temporary files as built by tempfile, so I believe this makes more sense. There is a small amount of code copied to the existing class, but I believe this cleans up the inheritance in a way that makes more sense for the given objects and requires fewer workarounds. All feedback on this alternative approach is welcome. Thanks. |
Just to bump this because it's been inactive for 7 years ... similar to #103070, |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: