Issue 31444: ResourceWarning in Python/traceback.c in case of a bad io.TextIOWrapper (original) (raw)

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:52 admin set github: 75625
2017-09-13 10:11:25 vstinner set status: open -> closedresolution: not a bugmessages: + stage: resolved
2017-09-13 10:09:35 serhiy.storchaka set messages: +
2017-09-13 09:30:22 vstinner set nosy: + vstinner, serhiy.storchaka, pitroumessages: +
2017-09-13 09:12:50 Oren Milman create