This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ResourceWarning in Python/traceback.c in case of a bad io.TextIOWrapper
Type: resource usage Stage: resolved
Components: IO Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Oren Milman, pitrou, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2017-09-13 09:12 by Oren Milman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg302039 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-09-13 09:12
the following code causes a ResourceWarning:

import io
def _bad_TextIOWrapper(*args):
    return None
io.TextIOWrapper = _bad_TextIOWrapper
1/0


this is because _Py_DisplaySourceLine() (in Python/traceback.c) assumes that
io.TextIOWrapper() returned a stream object, and tries to call its close() 
method. in case calling close() fails, _Py_DisplaySourceLine() just calls
PyErr_Clear().


maybe _Py_DisplaySourceLine() should try to call binary.close() in such cases?

I also thought about adding a check such as:
PyObject_IsInstance(fob, (PyObject*)&PyTextIOWrapper_Type);
but I am not sure whether we should use PyTextIOWrapper_Type outside of the io
module.
msg302040 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-09-13 09:30
> this is because _Py_DisplaySourceLine() (in Python/traceback.c) assumes that io.TextIOWrapper() returned a stream object, and tries to call its close()  method. in case calling close() fails, _Py_DisplaySourceLine() just calls PyErr_Clear().

I consider that _Py_DisplaySourceLine() is right to expect that io.TextIOWrapper() creates a stream object. If the TextIOWrapper creation fails, it calls binary.close() to prevent a resource warning. Here your function doesn't fail but returns None which is really not expected.


> def _bad_TextIOWrapper(*args): return None
> io.TextIOWrapper = _bad_TextIOWrapper

I don't see why Python should support such strange TextIOWrapper type. I simply suggest to close the issue as WONTFIX.
msg302049 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-13 10:09
I think it should be closed as "not a bug".

If io.TextIOWrapper() is successful, it is responsible for property closing a binary file. The bug is in user code, not in the interpreter code.

_Py_DisplaySourceLine() correctly calls binary.close() if io.TextIOWrapper() failed.
msg302050 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-09-13 10:11
Ok, thanks for the confirmation Serhiy. I close the bug.
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75625
2017-09-13 10:11:25vstinnersetstatus: open -> closed
resolution: not a bug
messages: + msg302050

stage: resolved
2017-09-13 10:09:35serhiy.storchakasetmessages: + msg302049
2017-09-13 09:30:22vstinnersetnosy: + vstinner, serhiy.storchaka, pitrou
messages: + msg302040
2017-09-13 09:12:50Oren Milmancreate