mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
gh-128911: Use PyImport_ImportModuleAttr() function (#129657)
* Replace PyImport_ImportModule() + PyObject_GetAttr() with PyImport_ImportModuleAttr(). * Replace PyImport_ImportModule() + PyObject_GetAttrString() with PyImport_ImportModuleAttrString().
This commit is contained in:
parent
fb5d1c9236
commit
dc804ffb2f
7 changed files with 32 additions and 84 deletions
|
@ -314,25 +314,19 @@ done:
|
|||
static int
|
||||
pymain_run_module(const wchar_t *modname, int set_argv0)
|
||||
{
|
||||
PyObject *module, *runpy, *runmodule, *runargs, *result;
|
||||
PyObject *module, *runmodule, *runargs, *result;
|
||||
if (PySys_Audit("cpython.run_module", "u", modname) < 0) {
|
||||
return pymain_exit_err_print();
|
||||
}
|
||||
runpy = PyImport_ImportModule("runpy");
|
||||
if (runpy == NULL) {
|
||||
fprintf(stderr, "Could not import runpy module\n");
|
||||
return pymain_exit_err_print();
|
||||
}
|
||||
runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
|
||||
runmodule = PyImport_ImportModuleAttrString("runpy",
|
||||
"_run_module_as_main");
|
||||
if (runmodule == NULL) {
|
||||
fprintf(stderr, "Could not access runpy._run_module_as_main\n");
|
||||
Py_DECREF(runpy);
|
||||
fprintf(stderr, "Could not import runpy._run_module_as_main\n");
|
||||
return pymain_exit_err_print();
|
||||
}
|
||||
module = PyUnicode_FromWideChar(modname, wcslen(modname));
|
||||
if (module == NULL) {
|
||||
fprintf(stderr, "Could not convert module name to unicode\n");
|
||||
Py_DECREF(runpy);
|
||||
Py_DECREF(runmodule);
|
||||
return pymain_exit_err_print();
|
||||
}
|
||||
|
@ -340,7 +334,6 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
|
|||
if (runargs == NULL) {
|
||||
fprintf(stderr,
|
||||
"Could not create arguments for runpy._run_module_as_main\n");
|
||||
Py_DECREF(runpy);
|
||||
Py_DECREF(runmodule);
|
||||
Py_DECREF(module);
|
||||
return pymain_exit_err_print();
|
||||
|
@ -350,7 +343,6 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
|
|||
if (!result && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
|
||||
_PyRuntime.signals.unhandled_keyboard_interrupt = 1;
|
||||
}
|
||||
Py_DECREF(runpy);
|
||||
Py_DECREF(runmodule);
|
||||
Py_DECREF(module);
|
||||
Py_DECREF(runargs);
|
||||
|
@ -497,24 +489,22 @@ error:
|
|||
static int
|
||||
pymain_run_interactive_hook(int *exitcode)
|
||||
{
|
||||
PyObject *sys, *hook, *result;
|
||||
sys = PyImport_ImportModule("sys");
|
||||
if (sys == NULL) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
hook = PyObject_GetAttrString(sys, "__interactivehook__");
|
||||
Py_DECREF(sys);
|
||||
PyObject *hook = PyImport_ImportModuleAttrString("sys",
|
||||
"__interactivehook__");
|
||||
if (hook == NULL) {
|
||||
PyErr_Clear();
|
||||
return 0;
|
||||
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
||||
// no sys.__interactivehook__ attribute
|
||||
PyErr_Clear();
|
||||
return 0;
|
||||
}
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
result = _PyObject_CallNoArgs(hook);
|
||||
PyObject *result = _PyObject_CallNoArgs(hook);
|
||||
Py_DECREF(hook);
|
||||
if (result == NULL) {
|
||||
goto error;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue