]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-1514420: Do not attempt to open files with names in <>s when formatting an except...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Mon, 20 Sep 2021 15:10:30 +0000 (16:10 +0100)
committerGitHub <noreply@github.com>
Mon, 20 Sep 2021 15:10:30 +0000 (17:10 +0200)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Misc/NEWS.d/next/Core and Builtins/2021-09-03-16-18-10.bpo-1514420.2Lumpj.rst [new file with mode: 0644]
Python/traceback.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-09-03-16-18-10.bpo-1514420.2Lumpj.rst b/Misc/NEWS.d/next/Core and Builtins/2021-09-03-16-18-10.bpo-1514420.2Lumpj.rst
new file mode 100644 (file)
index 0000000..fdd5cd7
--- /dev/null
@@ -0,0 +1 @@
+Interpreter no longer attempts to open files with names in angle brackets (like "<string>" or "<stdin>") when formatting an exception.
\ No newline at end of file
index cdabd2900acf492e902aa9346f63d63d013db9b9..76280a35e3a5f020be55e4b042b70a3271c5b86c 100644 (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;