classification
Title: Using read_mime_types function from mimetypes module gives resource warning
Type: resource usage Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, martin.panter, r.david.murray, serhiy.storchaka, vajrasky
Priority: normal Keywords: patch

Created on 2013-08-06 15:31 by vajrasky, last changed 2020-06-20 08:55 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
fix_resource_warning_read_mime_types.patch vajrasky, 2013-08-06 15:31 review
fix_resource_warning_read_mime_types_v2.patch vajrasky, 2013-08-11 16:00 review
fix_resource_warning_read_mime_types_v3.patch vajrasky, 2014-01-24 09:32 review
Messages (6)
msg194557 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-08-06 15:31
[sky@localhost cpython]$ cat /tmp/a.txt
x-application/mimea mimea
application/mimeb mimeb
[sky@localhost cpython]$ cat /tmp/a.py
import warnings
warnings.simplefilter('default')
import mimetypes

mimetypes.read_mime_types('/tmp/a.txt')
[sky@localhost cpython]$ python3 /tmp/a.py
/tmp/a.py:7: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/a.txt' mode='r' encoding='UTF-8'>
  mimetypes.read_mime_types('/tmp/a.txt')



Attached the patch to fix the resource warning problem.
msg194751 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-08-09 16:28
Since the TESTFN file is created only in this test, rather than use a tearDown method, it would be better to use addCleanup in the test method itself.
msg194898 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-08-11 16:00
Attached the second patch to use addCleanup rather than tear down method. Also, I added the non-existent file case.
msg209054 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2014-01-24 09:32
Here is the updated patch based on Ezio's review. Sorry, for a long time, I didn't notice you had reviewed my code, Ezio. Thanks for the review!
msg305139 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-10-27 22:09
The patches would mask an OSError raised by the “readfp” call, which would be a change in behaviour. But moving the call does not seem to be necessary; why not leave it outside the “try” statement?
msg371924 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-06-20 08:55
It was fixed in issue20331.
History
Date User Action Args
2020-06-20 08:55:45serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg371924

resolution: out of date
stage: patch review -> resolved
2017-10-27 22:09:33martin.pantersetnosy: + martin.panter
messages: + msg305139
2014-01-24 09:32:43vajraskysetfiles: + fix_resource_warning_read_mime_types_v3.patch

messages: + msg209054
2013-08-11 16:20:07ezio.melottisetnosy: + ezio.melotti
stage: patch review

versions: + Python 3.3
2013-08-11 16:00:13vajraskysetfiles: + fix_resource_warning_read_mime_types_v2.patch

messages: + msg194898
2013-08-09 16:28:52r.david.murraysetnosy: + r.david.murray
messages: + msg194751
2013-08-06 15:31:44vajraskycreate