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.
> 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.
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.