Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81f241a

Browse files
committed
Issue #23571: If io.TextIOWrapper constructor fails in _Py_DisplaySourceLine(),
close the binary file to fix a resource warning.
1 parent 84092ac commit 81f241a

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

Python/traceback.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,20 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
309309
}
310310
fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding);
311311
Py_DECREF(io);
312-
Py_DECREF(binary);
313312
PyMem_FREE(found_encoding);
314313

315314
if (fob == NULL) {
316315
PyErr_Clear();
316+
317+
res = _PyObject_CallMethodId(binary, &PyId_close, "");
318+
Py_DECREF(binary);
319+
if (res)
320+
Py_DECREF(res);
321+
else
322+
PyErr_Clear();
317323
return 0;
318324
}
325+
Py_DECREF(binary);
319326

320327
/* get the line number lineno */
321328
for (i = 0; i < lineno; i++) {

0 commit comments

Comments
 (0)