8000 gh-67224: Show source lines in tracebacks when using the -c option when running Python by pablogsal · Pull Request #111200 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-67224: Show source lines in tracebacks when using the -c option when running Python #111200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! fixup! fixup! gh-67224: Show source lines in tracebacks when u…
…sing the -c option when running Python
  • Loading branch information
pablogsal committed Oct 25, 2023
commit 7c04b729e4c256513736165e10f622c22883fba5
2 changes: 1 addition & 1 deletion Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ def test_source_lines_are_shown_when_running_source(self):
_, _, stderr = assert_python_failure("-c", "1/0")
expected_lines = [
b'Traceback (most recent call last):',
b' File "<stdin>", line 1, in <module>',
b' File "<string>", line 1, in <module>',
b' 1/0',
b' ~^~',
b'ZeroDivisionError: division by zero']
Expand Down
22 changes: 11 additions & 11 deletions Lib/test/test_faulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

def expected_traceback(lineno1, lineno2, header, min_count=1):
regex = header
regex += ' File "<string>-0", line %s in func\n' % lineno1
regex += ' File "<string>-0", line %s in <module>' % lineno2
regex += ' File "<string>", line %s in func\n' % lineno1
regex += ' File "<string>", line %s in <module>' % lineno2
if 1 < min_count:
return '^' + (regex + '\n') * (min_count - 1) + regex
else:
Expand Down Expand Up @@ -114,7 +114,7 @@ def check_error(self, code, lineno, fatal_error, *,
regex.append(fr'{header} \(most recent call first\):')
if garbage_collecting:
regex.append(' Garbage-collecting')
regex.append(fr' File "<string>-0", line {lineno} in {function}')
regex.append(fr' File "<string>", line {lineno} in {function}')
regex = '\n'.join(regex)

if other_regex:
Expand Down Expand Up @@ -485,9 +485,9 @@ def funcA():
lineno = 14
expected = [
'Stack (most recent call first):',
' File "<string>-0", line %s in funcB' % lineno,
' File "<string>-0", line 17 in funcA',
' File "<string>-0", line 19 in <module>'
' File "<string>", line %s in funcB' % lineno,
' File "<string>", line 17 in funcA',
' File "<string>", line 19 in <module>'
]
trace, exitcode = self.get_output(code, filename, fd)
self.assertEqual(trace, expected)
Expand Down Expand Up @@ -523,8 +523,8 @@ def {func_name}():
)
expected = [
'Stack (most recent call first):',
' File "<string>-0", line 4 in %s' % truncated,
' File "<string>-0", line 6 in <module>'
' File "<string>", line 4 in %s' % truncated,
' File "<string>", line 6 in <module>'
]
trace, exitcode = self.get_output(code)
self.assertEqual(trace, expected)
Expand Down Expand Up @@ -577,13 +577,13 @@ def run(self):
regex = r"""
^Thread 0x[0-9a-f]+ \(most recent call first\):
(?: File ".*threading.py", line [0-9]+ in [_a-z]+
){{1,3}} File "<string>-0", line 23 in run
){{1,3}} File "<string>", line 23 in run
File ".*threading.py", line [0-9]+ in _bootstrap_inner
File ".*threading.py", line [0-9]+ in _bootstrap

Current thread 0x[0-9a-f]+ \(most recent call first\):
File "<string>-0", line {lineno} in dump
File "<string>-0", line 28 in <module>$
File "<string>", line {lineno} in dump
File "<string>", line 28 in <module>$
"""
regex = dedent(regex.format(lineno=lineno)).strip()
self.assertRegex(output, regex)
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4398,9 +4398,9 @@ def test_check_encoding_warning(self):
warnings = proc.err.splitlines()
self.assertEqual(len(warnings), 4)
self.assertTrue(
warnings[0].startswith(b"<string>-0:5: EncodingWarning: "))
warnings[0].startswith(b"<string>:5: EncodingWarning: "))
self.assertTrue(
warnings[2].startswith(b"<string>-0:8: EncodingWarning: "))
warnings[2].startswith(b"<string>:8: EncodingWarning: "))

def test_text_encoding(self):
# PEP 597, bpo-47000. io.text_encoding() returns "locale" or "utf-8"
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1770,8 +1770,8 @@ def test_encoding_warning(self):
capture_output=True)
lines = cp.stderr.splitlines()
self.assertEqual(len(lines), 4, lines)
self.assertTrue(lines[0].startswith(b"<string>-0:2: EncodingWarning: "))
self.assertTrue(lines[2].startswith(b"<string>-0:3: EncodingWarning: "))
self.assertTrue(lines[0].startswith(b"<string>:2: EncodingWarning: "))
self.assertTrue(lines[2].startswith(b"<string>:3: EncodingWarning: "))


def _get_test_grp_name():
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ def __del__(self):
"""
rc, out, err = assert_python_ok("-c", code)
self.assertEqual(err.decode().rstrip(),
'<string>-0:7: UserWarning: test')
'<string>:7: UserWarning: test')

def test_late_resource_warning(self):
# Issue #21925: Emitting a ResourceWarning late during the Python
Expand Down
2 changes: 0 additions & 2 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,6 @@ def format_frame_summary(self, frame_summary):
filename = frame_summary.filename
if frame_summary.filename.startswith("<stdin>-"):
filename = "<stdin>"
elif frame_summary.filename.startswith("<string>-"):
filename = "<string>"
row.append(' File "{}", line {}, in {}\n'.format(
filename, frame_summary.lineno, frame_summary.name))
if frame_summary.line:
Expand Down
30 changes: 18 additions & 12 deletions Python/pythonrun.c
638C
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/* Forward */
static void flush_io(void);
static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
PyCompilerFlags *, PyArena *, PyObject*);
PyCompilerFlags *, PyArena *, PyObject*, int);
static PyObject *run_pyc_file(FILE *, PyObject *, PyObject *,
PyCompilerFlags *);
static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *);
Expand All @@ -49,7 +49,8 @@ static PyObject* pyrun_file(FILE *fp, PyObject *filename, int start,
PyCompilerFlags *flags);
static PyObject *
_PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
PyObject *globals, PyObject *locals, PyCompilerFlags *flags);
PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
int generate_new_source);

int
_PyRun_AnyFileObject(FILE *fp, PyObject *filename, int closeit,
Expand Down Expand Up @@ -283,7 +284,7 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
}
PyObject *main_dict = PyModule_GetDict(main_module); // borrowed ref

PyObject *res = run_mod(mod, filename, main_dict, main_dict, flags, arena, interactive_src);
PyObject *res = run_mod(mod, filename, main_dict, main_dict, flags, arena, interactive_src, 1);
_PyArena_Free(arena);
Py_DECREF(main_module);
if (res == NULL) {
Expand Down Expand Up @@ -517,7 +518,7 @@ _PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompil
PyErr_Print();
return -1;
}
res = _PyRun_StringFlagsWithName(command, the_name, Py_file_input, dict, dict, flags);
res = _PyRun_StringFlagsWithName(command, the_name, Py_file_input, dict, dict, flags, 0);
Py_DECREF(the_name);
}
Py_DECREF(main_module);
Expand Down Expand Up @@ -1150,7 +1151,8 @@ void PyErr_DisplayException(PyObject *exc)

static PyObject *
_PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
int generate_new_source)
{
PyObject *ret = NULL;
mod_ty mod;
Expand All @@ -1175,7 +1177,7 @@ _PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
mod = _PyParser_ASTFromString(str, name, start, flags, arena);

if (mod != NULL) {
ret = run_mod(mod, name, globals, locals, flags, arena, source);
ret = run_mod(mod, name, globals, locals, flags, arena, source, generate_new_source);
}
Py_XDECREF(source);
_PyArena_Free(arena);
Expand All @@ -1187,7 +1189,7 @@ PyObject *
PyRun_StringFlags(const char *str, int start, PyObject *globals,
PyObject *locals, PyCompilerFlags *flags) {

return _PyRun_StringFlagsWithName(str, NULL, start, globals, locals, flags);
return _PyRun_StringFlagsWithName(str, NULL, start, globals, locals, flags, 0);
}

static PyObject *
Expand All @@ -1209,7 +1211,7 @@ pyrun_file(FILE *fp, PyObject *filename, int start, PyObject *globals,

PyObject *ret;
if (mod != NULL) {
ret = run_mod(mod, filename, globals, locals, flags, arena, NULL);
ret = run_mod(mod, filename, globals, locals, flags, arena, NULL, 0);
}
else {
ret = NULL;
Expand Down Expand Up @@ -1297,15 +1299,19 @@ run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, Py

static PyObject *
run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
PyCompilerFlags *flags, PyArena *arena, PyObject* interactive_src)
PyCompilerFlags *flags, PyArena *arena, PyObject* interactive_src,
int generate_new_source)
{
PyThreadState *tstate = _PyThreadState_GET();
PyObject* interactive_filename = filename;
if (interactive_src) {
PyInterpreterState *interp = tstate->interp;
interactive_filename = PyUnicode_FromFormat(
"%U-%d", filename, interp->_interactive_src_count++
);
if (generate_new_source) {
interactive_filename = PyUnicode_FromFormat(
"%U-%d", filename, interp->_interactive_src_count++);
} else {
Py_INCREF(interactive_filename);
}
if (interactive_filename == NULL) {
return NULL;
}
Expand Down
0