Skip to content

Commit f71300c

Browse files
iritkatrielblurb-it[bot]ambv
authored
bpo-1514420: Do not attempt to open files with names in <>s when formatting an exception (GH-28143)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Łukasz Langa <[email protected]>
1 parent 4d2957c commit f71300c

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Interpreter no longer attempts to open files with names in angle brackets (like "<string>" or "<stdin>") when formatting an exception.

Python/traceback.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,15 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent, i
396396
if (filename == NULL)
397397
return 0;
398398

399+
/* Do not attempt to open things like <string> or <stdin> */
400+
assert(PyUnicode_Check(filename));
401+
if (PyUnicode_READ_CHAR(filename, 0) == '<') {
402+
Py_ssize_t len = PyUnicode_GET_LENGTH(filename);
403+
if (len > 0 && PyUnicode_READ_CHAR(filename, len - 1) == '>') {
404+
return 0;
405+
}
406+
}
407+
399408
io = PyImport_ImportModuleNoBlock("io");
400409
if (io == NULL)
401410
return -1;

0 commit comments

Comments
 (0)