mirror of
https://github.com/python/cpython.git
synced 2025-07-08 03:45:36 +00:00
gh-67224: Show source lines in tracebacks when using the -c option when running Python (#111200)
This commit is contained in:
parent
3f84a19e62
commit
90a1b2859f
13 changed files with 104 additions and 36 deletions
|
@ -40,14 +40,17 @@
|
|||
/* 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 *);
|
||||
static PyObject* pyrun_file(FILE *fp, PyObject *filename, int start,
|
||||
PyObject *globals, PyObject *locals, int closeit,
|
||||
PyCompilerFlags *flags);
|
||||
|
||||
static PyObject *
|
||||
_PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
|
||||
PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
|
||||
int generate_new_source);
|
||||
|
||||
int
|
||||
_PyRun_AnyFileObject(FILE *fp, PyObject *filename, int closeit,
|
||||
|
@ -281,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) {
|
||||
|
@ -499,16 +502,25 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
|
|||
|
||||
|
||||
int
|
||||
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
|
||||
{
|
||||
_PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompilerFlags *flags) {
|
||||
PyObject *main_module = PyImport_AddModuleRef("__main__");
|
||||
if (main_module == NULL) {
|
||||
return -1;
|
||||
}
|
||||
PyObject *dict = PyModule_GetDict(main_module); // borrowed ref
|
||||
|
||||
PyObject *res = PyRun_StringFlags(command, Py_file_input,
|
||||
dict, dict, flags);
|
||||
PyObject *res = NULL;
|
||||
if (name == NULL) {
|
||||
res = PyRun_StringFlags(command, Py_file_input, dict, dict, flags);
|
||||
} else {
|
||||
PyObject* the_name = PyUnicode_FromString(name);
|
||||
if (!the_name) {
|
||||
PyErr_Print();
|
||||
return -1;
|
||||
}
|
||||
res = _PyRun_StringFlagsWithName(command, the_name, Py_file_input, dict, dict, flags, 0);
|
||||
Py_DECREF(the_name);
|
||||
}
|
||||
Py_DECREF(main_module);
|
||||
if (res == NULL) {
|
||||
PyErr_Print();
|
||||
|
@ -519,6 +531,12 @@ PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
|
||||
{
|
||||
return _PyRun_SimpleStringFlagsWithName(command, NULL, flags);
|
||||
}
|
||||
|
||||
int
|
||||
_Py_HandleSystemExit(int *exitcode_p)
|
||||
{
|
||||
|
@ -1131,9 +1149,10 @@ void PyErr_DisplayException(PyObject *exc)
|
|||
PyErr_Display(NULL, exc, NULL);
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyRun_StringFlags(const char *str, int start, PyObject *globals,
|
||||
PyObject *locals, PyCompilerFlags *flags)
|
||||
static PyObject *
|
||||
_PyRun_StringFlagsWithName(const char *str, PyObject* name, int start,
|
||||
PyObject *globals, PyObject *locals, PyCompilerFlags *flags,
|
||||
int generate_new_source)
|
||||
{
|
||||
PyObject *ret = NULL;
|
||||
mod_ty mod;
|
||||
|
@ -1143,17 +1162,36 @@ PyRun_StringFlags(const char *str, int start, PyObject *globals,
|
|||
if (arena == NULL)
|
||||
return NULL;
|
||||
|
||||
PyObject* source = NULL;
|
||||
_Py_DECLARE_STR(anon_string, "<string>");
|
||||
mod = _PyParser_ASTFromString(
|
||||
str, &_Py_STR(anon_string), start, flags, arena);
|
||||
|
||||
if (mod != NULL)
|
||||
ret = run_mod(mod, &_Py_STR(anon_string), globals, locals, flags, arena, NULL);
|
||||
if (name) {
|
||||
source = PyUnicode_FromString(str);
|
||||
if (!source) {
|
||||
PyErr_Clear();
|
||||
}
|
||||
} else {
|
||||
name = &_Py_STR(anon_string);
|
||||
}
|
||||
|
||||
mod = _PyParser_ASTFromString(str, name, start, flags, arena);
|
||||
|
||||
if (mod != NULL) {
|
||||
ret = run_mod(mod, name, globals, locals, flags, arena, source, generate_new_source);
|
||||
}
|
||||
Py_XDECREF(source);
|
||||
_PyArena_Free(arena);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PyObject *
|
||||
PyRun_StringFlags(const char *str, int start, PyObject *globals,
|
||||
PyObject *locals, PyCompilerFlags *flags) {
|
||||
|
||||
return _PyRun_StringFlagsWithName(str, NULL, start, globals, locals, flags, 0);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
pyrun_file(FILE *fp, PyObject *filename, int start, PyObject *globals,
|
||||
PyObject *locals, int closeit, PyCompilerFlags *flags)
|
||||
|
@ -1173,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;
|
||||
|
@ -1261,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(
|
||||
"<python-input-%d>", 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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue