Skip to content

Commit 80b9509

Browse files
committed
gh-117174: Rename <string> to <__interactive_string__> for interactive code objects
Using <string> leads to confusion when linecache it's populated with <string> entries as the inspect module heavily relies on it. An example: ./python -c "import inspect;x=eval('lambda x: x');print(inspect.getsource(x))" The code above should trigger an exception because we should not be able to get the source of x (it's dynamically generated) but if we use <string> as name for interactive code it will return gives the full string. Signed-off-by: Pablo Galindo <[email protected]>
1 parent 1dc1521 commit 80b9509

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

Lib/traceback.py

+2
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ def format_frame_summary(self, frame_summary, **kwargs):
552552
filename = frame_summary.filename
553553
if frame_summary.filename.startswith("<stdin>-"):
554554
filename = "<stdin>"
555+
if frame_summary.filename == "<__interactive_string__>":
556+
filename = "<string>"
555557
if colorize:
556558
row.append(' File {}"{}"{}, line {}{}{}, in {}{}{}\n'.format(
557559
_ANSIColors.MAGENTA,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Rename ``<string>`` to ``<__interactive_string__>`` for interactive source
2+
lines to avoid unwanted collisions with other dynamically generated code.
3+
Patch by Pablo Galindo

Modules/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ pymain_run_command(wchar_t *command)
249249

250250
PyCompilerFlags cf = _PyCompilerFlags_INIT;
251251
cf.cf_flags |= PyCF_IGNORE_COOKIE;
252-
ret = _PyRun_SimpleStringFlagsWithName(PyBytes_AsString(bytes), "<string>", &cf);
252+
ret = _PyRun_SimpleStringFlagsWithName(PyBytes_AsString(bytes), "<__interactive_string__>", &cf);
253253
Py_DECREF(bytes);
254254
return (ret != 0);
255255

0 commit comments

Comments
 (0)