[issue19829] _pyio.BufferedReader and _pyio.TextIOWrapper destructor don't emit ResourceWarning if the file is not closed

STINNER Victor report at bugs.python.org
Thu Mar 24 10:32:26 EDT 2016


STINNER Victor added the comment:

Try attached res_warn.py script to test manually all ResourceWarning warnings.

Ouput of python3.6 -Wd res_warn.py:
--------------------------
text
<_pyio.TextIOWrapper name='/etc/issue' mode='r' encoding='UTF-8'>
res_warn.py:9: ResourceWarning: unclosed file <_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
  f = None

buffered
<_pyio.BufferedReader name='/etc/issue'>
res_warn.py:15: ResourceWarning: unclosed file <_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
  f=None

raw
<_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
res_warn.py:21: ResourceWarning: unclosed file <_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
  f=None

fileio
<_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
res_warn.py:27: ResourceWarning: unclosed file <_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
  f=None

--------------------------


Ouput of python3.6 -X tracemalloc=2 -Wd res_warn.py:
--------------------------
text
<_pyio.TextIOWrapper name='/etc/issue' mode='r' encoding='UTF-8'>
res_warn.py:9: ResourceWarning: unclosed file <_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
  f = None
Object allocated at (most recent call first):
  File "/home/haypo/prog/python/default/Lib/_pyio.py", lineno 209
    closefd, opener=opener)
  File "res_warn.py", lineno 7
    f = _pyio.open(fn, "r")

buffered
<_pyio.BufferedReader name='/etc/issue'>
res_warn.py:15: ResourceWarning: unclosed file <_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
  f=None
Object allocated at (most recent call first):
  File "/home/haypo/prog/python/default/Lib/_pyio.py", lineno 209
    closefd, opener=opener)
  File "res_warn.py", lineno 13
    f = _pyio.open(fn, "rb")

raw
<_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
res_warn.py:21: ResourceWarning: unclosed file <_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
  f=None
Object allocated at (most recent call first):
  File "/home/haypo/prog/python/default/Lib/_pyio.py", lineno 209
    closefd, opener=opener)
  File "res_warn.py", lineno 19
    f = _pyio.open(fn, "rb", 0)

fileio
<_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
res_warn.py:27: ResourceWarning: unclosed file <_pyio.FileIO name='/etc/issue' mode='rb' closefd=True>
  f=None
Object allocated at (most recent call first):
  File "res_warn.py", lineno 25
    f = _pyio.FileIO(fn, "rb")

--------------------------

----------
Added file: http://bugs.python.org/file42274/res_warn.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19829>
_______________________________________


More information about the Python-bugs-list mailing list