diff options
author | Victor Stinner <vstinner@python.org> | 2025-02-05 10:03:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-05 10:03:58 (GMT) |
commit | dc804ffb2f7cfaf60916b36f3d5cac9c00e4f1ea (patch) | |
tree | 3eb3cce29397c318c1eba574c0cb41efe3b2f37e /Python/pythonrun.c | |
parent | fb5d1c923677e7982360bad934d70cf9ad3366ca (diff) | |
download | cpython-dc804ffb2f7cfaf60916b36f3d5cac9c00e4f1ea.zip cpython-dc804ffb2f7cfaf60916b36f3d5cac9c00e4f1ea.tar.gz cpython-dc804ffb2f7cfaf60916b36f3d5cac9c00e4f1ea.tar.bz2 |
gh-128911: Use PyImport_ImportModuleAttr() function (#129657)
* Replace PyImport_ImportModule() + PyObject_GetAttr() with
PyImport_ImportModuleAttr().
* Replace PyImport_ImportModule() + PyObject_GetAttrString() with
PyImport_ImportModuleAttrString().
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r-- | Python/pythonrun.c | 29 |
1 files changed, 6 insertions, 23 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 0da26ad..ae0df96 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1108,22 +1108,15 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb) int unhandled_keyboard_interrupt = _PyRuntime.signals.unhandled_keyboard_interrupt; // Try first with the stdlib traceback module - PyObject *traceback_module = PyImport_ImportModule("traceback"); - - if (traceback_module == NULL) { - goto fallback; - } - - PyObject *print_exception_fn = PyObject_GetAttrString(traceback_module, "_print_exception_bltin"); - + PyObject *print_exception_fn = PyImport_ImportModuleAttrString( + "traceback", + "_print_exception_bltin"); if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) { - Py_DECREF(traceback_module); goto fallback; } PyObject* result = PyObject_CallOneArg(print_exception_fn, value); - Py_DECREF(traceback_module); Py_XDECREF(print_exception_fn); if (result) { Py_DECREF(result); @@ -1371,27 +1364,18 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals, } if (interactive_src) { - PyObject *linecache_module = PyImport_ImportModule("linecache"); - - if (linecache_module == NULL) { - Py_DECREF(co); - Py_DECREF(interactive_filename); - return NULL; - } - - PyObject *print_tb_func = PyObject_GetAttrString(linecache_module, "_register_code"); - + PyObject *print_tb_func = PyImport_ImportModuleAttrString( + "linecache", + "_register_code"); if (print_tb_func == NULL) { Py_DECREF(co); Py_DECREF(interactive_filename); - Py_DECREF(linecache_module); return NULL; } if (!PyCallable_Check(print_tb_func)) { Py_DECREF(co); Py_DECREF(interactive_filename); - Py_DECREF(linecache_module); Py_DECREF(print_tb_func); PyErr_SetString(PyExc_ValueError, "linecache._register_code is not callable"); return NULL; @@ -1406,7 +1390,6 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals, Py_DECREF(interactive_filename); - Py_DECREF(linecache_module); Py_XDECREF(print_tb_func); Py_XDECREF(result); if (!result) { |