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:
Victor Stinner 2025-02-05 11:03:58 +01:00 committed by GitHub
parent fb5d1c9236
commit dc804ffb2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 32 additions and 84 deletions

View file

@ -551,31 +551,19 @@ STDAPI DllGetClassObject(REFCLSID rclsid,
long Call_CanUnloadNow(void) long Call_CanUnloadNow(void)
{ {
PyObject *mod, *func, *result; PyObject *func = PyImport_ImportModuleAttrString("ctypes",
long retval; "DllCanUnloadNow");
mod = PyImport_ImportModule("ctypes");
if (!mod) {
/* OutputDebugString("Could not import ctypes"); */
/* We assume that this error can only occur when shutting
down, so we silently ignore it */
PyErr_Clear();
return E_FAIL;
}
/* Other errors cannot be raised, but are printed to stderr */
func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
Py_DECREF(mod);
if (!func) { if (!func) {
goto error; goto error;
} }
result = _PyObject_CallNoArgs(func); PyObject *result = _PyObject_CallNoArgs(func);
Py_DECREF(func); Py_DECREF(func);
if (!result) { if (!result) {
goto error; goto error;
} }
retval = PyLong_AsLong(result); long retval = PyLong_AsLong(result);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
Py_DECREF(result); Py_DECREF(result);
goto error; goto error;

View file

@ -258,7 +258,7 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
} }
PyObject *layout_func = PyImport_ImportModuleAttrString("ctypes._layout", PyObject *layout_func = PyImport_ImportModuleAttrString("ctypes._layout",
"get_layout"); "get_layout");
if (!layout_func) { if (!layout_func) {
goto error; goto error;
} }

View file

@ -47,7 +47,6 @@ static PyObject *
test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable)) test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable))
{ {
PyObject *result = NULL; PyObject *result = NULL;
PyObject *test_module = NULL;
PyObject *test_func = NULL; PyObject *test_func = NULL;
// Get or initialize interpreter-specific code object storage index // Get or initialize interpreter-specific code object storage index
@ -62,11 +61,8 @@ test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable))
// Get a function to test with // Get a function to test with
// This can be any Python function. Use `test.test_misc.testfunction`. // This can be any Python function. Use `test.test_misc.testfunction`.
test_module = PyImport_ImportModule("test.test_capi.test_misc"); test_func = PyImport_ImportModuleAttrString("test.test_capi.test_misc",
if (!test_module) { "testfunction");
goto finally;
}
test_func = PyObject_GetAttrString(test_module, "testfunction");
if (!test_func) { if (!test_func) {
goto finally; goto finally;
} }
@ -102,7 +98,6 @@ test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable))
} }
result = Py_NewRef(Py_None); result = Py_NewRef(Py_None);
finally: finally:
Py_XDECREF(test_module);
Py_XDECREF(test_func); Py_XDECREF(test_func);
return result; return result;
} }

View file

@ -1057,15 +1057,10 @@ test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored))
if (ret != -1 || match == 0) if (ret != -1 || match == 0)
goto error; goto error;
PyObject *mod_io = PyImport_ImportModule("_io");
if (mod_io == NULL) {
return NULL;
}
/* bytesiobuf_getbuffer() */ /* bytesiobuf_getbuffer() */
PyTypeObject *type = (PyTypeObject *)PyObject_GetAttrString( PyTypeObject *type = (PyTypeObject *)PyImport_ImportModuleAttrString(
mod_io, "_BytesIOBuffer"); "_io",
Py_DECREF(mod_io); "_BytesIOBuffer");
if (type == NULL) { if (type == NULL) {
return NULL; return NULL;
} }

View file

@ -314,25 +314,19 @@ done:
static int static int
pymain_run_module(const wchar_t *modname, int set_argv0) 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) { if (PySys_Audit("cpython.run_module", "u", modname) < 0) {
return pymain_exit_err_print(); return pymain_exit_err_print();
} }
runpy = PyImport_ImportModule("runpy"); runmodule = PyImport_ImportModuleAttrString("runpy",
if (runpy == NULL) { "_run_module_as_main");
fprintf(stderr, "Could not import runpy module\n");
return pymain_exit_err_print();
}
runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
if (runmodule == NULL) { if (runmodule == NULL) {
fprintf(stderr, "Could not access runpy._run_module_as_main\n"); fprintf(stderr, "Could not import runpy._run_module_as_main\n");
Py_DECREF(runpy);
return pymain_exit_err_print(); return pymain_exit_err_print();
} }
module = PyUnicode_FromWideChar(modname, wcslen(modname)); module = PyUnicode_FromWideChar(modname, wcslen(modname));
if (module == NULL) { if (module == NULL) {
fprintf(stderr, "Could not convert module name to unicode\n"); fprintf(stderr, "Could not convert module name to unicode\n");
Py_DECREF(runpy);
Py_DECREF(runmodule); Py_DECREF(runmodule);
return pymain_exit_err_print(); return pymain_exit_err_print();
} }
@ -340,7 +334,6 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
if (runargs == NULL) { if (runargs == NULL) {
fprintf(stderr, fprintf(stderr,
"Could not create arguments for runpy._run_module_as_main\n"); "Could not create arguments for runpy._run_module_as_main\n");
Py_DECREF(runpy);
Py_DECREF(runmodule); Py_DECREF(runmodule);
Py_DECREF(module); Py_DECREF(module);
return pymain_exit_err_print(); 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) { if (!result && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
_PyRuntime.signals.unhandled_keyboard_interrupt = 1; _PyRuntime.signals.unhandled_keyboard_interrupt = 1;
} }
Py_DECREF(runpy);
Py_DECREF(runmodule); Py_DECREF(runmodule);
Py_DECREF(module); Py_DECREF(module);
Py_DECREF(runargs); Py_DECREF(runargs);
@ -497,24 +489,22 @@ error:
static int static int
pymain_run_interactive_hook(int *exitcode) pymain_run_interactive_hook(int *exitcode)
{ {
PyObject *sys, *hook, *result; PyObject *hook = PyImport_ImportModuleAttrString("sys",
sys = PyImport_ImportModule("sys"); "__interactivehook__");
if (sys == NULL) {
goto error;
}
hook = PyObject_GetAttrString(sys, "__interactivehook__");
Py_DECREF(sys);
if (hook == NULL) { if (hook == NULL) {
PyErr_Clear(); if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
return 0; // no sys.__interactivehook__ attribute
PyErr_Clear();
return 0;
}
goto error;
} }
if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) { if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) {
goto error; goto error;
} }
result = _PyObject_CallNoArgs(hook); PyObject *result = _PyObject_CallNoArgs(hook);
Py_DECREF(hook); Py_DECREF(hook);
if (result == NULL) { if (result == NULL) {
goto error; goto error;

View file

@ -368,12 +368,9 @@ _convert_exc_to_TracebackException(PyObject *exc, PyObject **p_tbexc)
PyObject *create = NULL; PyObject *create = NULL;
// This is inspired by _PyErr_Display(). // This is inspired by _PyErr_Display().
PyObject *tbmod = PyImport_ImportModule("traceback"); PyObject *tbexc_type = PyImport_ImportModuleAttrString(
if (tbmod == NULL) { "traceback",
return -1; "TracebackException");
}
PyObject *tbexc_type = PyObject_GetAttrString(tbmod, "TracebackException");
Py_DECREF(tbmod);
if (tbexc_type == NULL) { if (tbexc_type == NULL) {
return -1; return -1;
} }

View file

@ -1108,22 +1108,15 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
int unhandled_keyboard_interrupt = _PyRuntime.signals.unhandled_keyboard_interrupt; int unhandled_keyboard_interrupt = _PyRuntime.signals.unhandled_keyboard_interrupt;
// Try first with the stdlib traceback module // Try first with the stdlib traceback module
PyObject *traceback_module = PyImport_ImportModule("traceback"); PyObject *print_exception_fn = PyImport_ImportModuleAttrString(
"traceback",
if (traceback_module == NULL) { "_print_exception_bltin");
goto fallback;
}
PyObject *print_exception_fn = PyObject_GetAttrString(traceback_module, "_print_exception_bltin");
if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) { if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) {
Py_DECREF(traceback_module);
goto fallback; goto fallback;
} }
PyObject* result = PyObject_CallOneArg(print_exception_fn, value); PyObject* result = PyObject_CallOneArg(print_exception_fn, value);
Py_DECREF(traceback_module);
Py_XDECREF(print_exception_fn); Py_XDECREF(print_exception_fn);
if (result) { if (result) {
Py_DECREF(result); Py_DECREF(result);
@ -1371,27 +1364,18 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
} }
if (interactive_src) { if (interactive_src) {
PyObject *linecache_module = PyImport_ImportModule("linecache"); PyObject *print_tb_func = PyImport_ImportModuleAttrString(
"linecache",
if (linecache_module == NULL) { "_register_code");
Py_DECREF(co);
Py_DECREF(interactive_filename);
return NULL;
}
PyObject *print_tb_func = PyObject_GetAttrString(linecache_module, "_register_code");
if (print_tb_func == NULL) { if (print_tb_func == NULL) {
Py_DECREF(co); Py_DECREF(co);
Py_DECREF(interactive_filename); Py_DECREF(interactive_filename);
Py_DECREF(linecache_module);
return NULL; return NULL;
} }
if (!PyCallable_Check(print_tb_func)) { if (!PyCallable_Check(print_tb_func)) {
Py_DECREF(co); Py_DECREF(co);
Py_DECREF(interactive_filename); Py_DECREF(interactive_filename);
Py_DECREF(linecache_module);
Py_DECREF(print_tb_func); Py_DECREF(print_tb_func);
PyErr_SetString(PyExc_ValueError, "linecache._register_code is not callable"); PyErr_SetString(PyExc_ValueError, "linecache._register_code is not callable");
return NULL; return NULL;
@ -1406,7 +1390,6 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
Py_DECREF(interactive_filename); Py_DECREF(interactive_filename);
Py_DECREF(linecache_module);
Py_XDECREF(print_tb_func); Py_XDECREF(print_tb_func);
Py_XDECREF(result); Py_XDECREF(result);
if (!result) { if (!result) {