[Python-checkins] bpo-1514420: Do not attempt to open files with names in <>s when formatting an exception (GH-28143)

ambv webhook-mailer at python.org
Mon Sep 20 11:10:38 EDT 2021


https://github.com/python/cpython/commit/f71300cb0442f16ee9abc938e12537aec1eb5979
commit: f71300cb0442f16ee9abc938e12537aec1eb5979
branch: main
author: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2021-09-20T17:10:30+02:00
summary:

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 at langa.pl>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-09-03-16-18-10.bpo-1514420.2Lumpj.rst
M 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
index 0000000000000..fdd5cd70c5c2f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-09-03-16-18-10.bpo-1514420.2Lumpj.rst	
@@ -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
diff --git a/Python/traceback.c b/Python/traceback.c
index cdabd2900acf4..76280a35e3a5f 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -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;



More information about the Python-checkins mailing list