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 <lukasz@langa.pl>
This commit is contained in:
Irit Katriel 2021-09-20 16:10:30 +01:00 committed by GitHub
parent 4d2957c1b9
commit f71300cb04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View file

@ -0,0 +1 @@
Interpreter no longer attempts to open files with names in angle brackets (like "<string>" or "<stdin>") when formatting an exception.

View file

@ -396,6 +396,15 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent, i
if (filename == NULL)
return 0;
/* Do not attempt to open things like <string> or <stdin> */
assert(PyUnicode_Check(filename));
if (PyUnicode_READ_CHAR(filename, 0) == '<') {
Py_ssize_t len = PyUnicode_GET_LENGTH(filename);
if (len > 0 && PyUnicode_READ_CHAR(filename, len - 1) == '>') {
return 0;
}
}
io = PyImport_ImportModuleNoBlock("io");
if (io == NULL)
return -1;