Skip to content

Commit 92260e3

Browse files
vstinnersrinivasreddy
authored andcommitted
pythongh-128911: Use PyImport_ImportModuleAttr() function (python#129657)
* Replace PyImport_ImportModule() + PyObject_GetAttr() with PyImport_ImportModuleAttr(). * Replace PyImport_ImportModule() + PyObject_GetAttrString() with PyImport_ImportModuleAttrString().
1 parent 637383e commit 92260e3

File tree

7 files changed

+32
-84
lines changed

7 files changed

+32
-84
lines changed

Modules/_ctypes/callbacks.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -551,31 +551,19 @@ STDAPI DllGetClassObject(REFCLSID rclsid,
551551

552552
long Call_CanUnloadNow(void)
553553
{
554-
PyObject *mod, *func, *result;
555-
long retval;
556-
557-
mod = PyImport_ImportModule("ctypes");
558-
if (!mod) {
559-
/* OutputDebugString("Could not import ctypes"); */
560-
/* We assume that this error can only occur when shutting
561-
down, so we silently ignore it */
562-
PyErr_Clear();
563-
return E_FAIL;
564-
}
565-
/* Other errors cannot be raised, but are printed to stderr */
566-
func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
567-
Py_DECREF(mod);
554+
PyObject *func = PyImport_ImportModuleAttrString("ctypes",
555+
"DllCanUnloadNow");
568556
if (!func) {
569557
goto error;
570558
}
571559

572-
result = _PyObject_CallNoArgs(func);
560+
PyObject *result = _PyObject_CallNoArgs(func);
573561
Py_DECREF(func);
574562
if (!result) {
575563
goto error;
576564
}
577565

578-
retval = PyLong_AsLong(result);
566+
long retval = PyLong_AsLong(result);
579567
if (PyErr_Occurred()) {
580568
Py_DECREF(result);
581569
goto error;

Modules/_ctypes/stgdict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
258258
}
259259

260260
PyObject *layout_func = PyImport_ImportModuleAttrString("ctypes._layout",
261-
"get_layout");
261+
"get_layout");
262262
if (!layout_func) {
263263
goto error;
264264
}

Modules/_testcapi/code.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ static PyObject *
4747
test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable))
4848
{
4949
PyObject *result = NULL;
50-
PyObject *test_module = NULL;
5150
PyObject *test_func = NULL;
5251

5352
// Get or initialize interpreter-specific code object storage index
@@ -62,11 +61,8 @@ test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable))
6261

6362
// Get a function to test with
6463
// This can be any Python function. Use `test.test_misc.testfunction`.
65-
test_module = PyImport_ImportModule("test.test_capi.test_misc");
66-
if (!test_module) {
67-
goto finally;
68-
}
69-
test_func = PyObject_GetAttrString(test_module, "testfunction");
64+
test_func = PyImport_ImportModuleAttrString("test.test_capi.test_misc",
65+
"testfunction");
7066
if (!test_func) {
7167
goto finally;
7268
}
@@ -102,7 +98,6 @@ test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable))
10298
}
10399
result = Py_NewRef(Py_None);
104100
finally:
105-
Py_XDECREF(test_module);
106101
Py_XDECREF(test_func);
107102
return result;
108103
}

Modules/_testcapimodule.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,15 +1057,10 @@ test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored))
10571057
if (ret != -1 || match == 0)
10581058
goto error;
10591059

1060-
PyObject *mod_io = PyImport_ImportModule("_io");
1061-
if (mod_io == NULL) {
1062-
return NULL;
1063-
}
1064-
10651060
/* bytesiobuf_getbuffer() */
1066-
PyTypeObject *type = (PyTypeObject *)PyObject_GetAttrString(
1067-
mod_io, "_BytesIOBuffer");
1068-
Py_DECREF(mod_io);
1061+
PyTypeObject *type = (PyTypeObject *)PyImport_ImportModuleAttrString(
1062+
"_io",
1063+
"_BytesIOBuffer");
10691064
if (type == NULL) {
10701065
return NULL;
10711066
}

Modules/main.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -314,33 +314,26 @@ pymain_start_pyrepl_no_main(void)
314314
static int
315315
pymain_run_module(const wchar_t *modname, int set_argv0)
316316
{
317-
PyObject *module, *runpy, *runmodule, *runargs, *result;
317+
PyObject *module, *runmodule, *runargs, *result;
318318
if (PySys_Audit("cpython.run_module", "u", modname) < 0) {
319319
return pymain_exit_err_print();
320320
}
321-
runpy = PyImport_ImportModule("runpy");
322-
if (runpy == NULL) {
323-
fprintf(stderr, "Could not import runpy module\n");
324-
return pymain_exit_err_print();
325-
}
326-
runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
321+
runmodule = PyImport_ImportModuleAttrString("runpy",
322+
"_run_module_as_main");
327323
if (runmodule == NULL) {
328-
fprintf(stderr, "Could not access runpy._run_module_as_main\n");
329-
Py_DECREF(runpy);
324+
fprintf(stderr, "Could not import runpy._run_module_as_main\n");
330325
return pymain_exit_err_print();
331326
}
332327
module = PyUnicode_FromWideChar(modname, wcslen(modname));
333328
if (module == NULL) {
334329
fprintf(stderr, "Could not convert module name to unicode\n");
335-
Py_DECREF(runpy);
336330
Py_DECREF(runmodule);
337331
return pymain_exit_err_print();
338332
}
339333
runargs = PyTuple_Pack(2, module, set_argv0 ? Py_True : Py_False);
340334
if (runargs == NULL) {
341335
fprintf(stderr,
342336
"Could not create arguments for runpy._run_module_as_main\n");
343-
Py_DECREF(runpy);
344337
Py_DECREF(runmodule);
345338
Py_DECREF(module);
346339
return pymain_exit_err_print();
@@ -350,7 +343,6 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
350343
if (!result && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
351344
_PyRuntime.signals.unhandled_keyboard_interrupt = 1;
352345
}
353-
Py_DECREF(runpy);
354346
Py_DECREF(runmodule);
355347
Py_DECREF(module);
356348
Py_DECREF(runargs);
@@ -497,24 +489,22 @@ pymain_run_startup(PyConfig *config, int *exitcode)
497489
static int
498490
pymain_run_interactive_hook(int *exitcode)
499491
{
500-
PyObject *sys, *hook, *result;
501-
sys = PyImport_ImportModule("sys");
502-
if (sys == NULL) {
503-
goto error;
504-
}
505-
506-
hook = PyObject_GetAttrString(sys, "__interactivehook__");
507-
Py_DECREF(sys);
492+
PyObject *hook = PyImport_ImportModuleAttrString("sys",
493+
"__interactivehook__");
508494
if (hook == NULL) {
509-
PyErr_Clear();
510-
return 0;
495+
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
496+
// no sys.__interactivehook__ attribute
497+
PyErr_Clear();
498+
return 0;
499+
}
500+
goto error;
511501
}
512502

513503
if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) {
514504
goto error;
515505
}
516506

517-
result = _PyObject_CallNoArgs(hook);
507+
PyObject *result = _PyObject_CallNoArgs(hook);
518508
Py_DECREF(hook);
519509
if (result == NULL) {
520510
goto error;

Python/crossinterp.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,9 @@ _convert_exc_to_TracebackException(PyObject *exc, PyObject **p_tbexc)
368368
PyObject *create = NULL;
369369

370370
// This is inspired by _PyErr_Display().
371-
PyObject *tbmod = PyImport_ImportModule("traceback");
372-
if (tbmod == NULL) {
373-
return -1;
374-
}
375-
PyObject *tbexc_type = PyObject_GetAttrString(tbmod, "TracebackException");
376-
Py_DECREF(tbmod);
371+
PyObject *tbexc_type = PyImport_ImportModuleAttrString(
372+
"traceback",
373+
"TracebackException");
377374
if (tbexc_type == NULL) {
378375
return -1;
379376
}

Python/pythonrun.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,22 +1108,15 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
11081108
int unhandled_keyboard_interrupt = _PyRuntime.signals.unhandled_keyboard_interrupt;
11091109

11101110
// Try first with the stdlib traceback module
1111-
PyObject *traceback_module = PyImport_ImportModule("traceback");
1112-
1113-
if (traceback_module == NULL) {
1114-
goto fallback;
1115-
}
1116-
1117-
PyObject *print_exception_fn = PyObject_GetAttrString(traceback_module, "_print_exception_bltin");
1118-
1111+
PyObject *print_exception_fn = PyImport_ImportModuleAttrString(
1112+
"traceback",
1113+
"_print_exception_bltin");
11191114
if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) {
1120-
Py_DECREF(traceback_module);
11211115
goto fallback;
11221116
}
11231117

11241118
PyObject* result = PyObject_CallOneArg(print_exception_fn, value);
11251119

1126-
Py_DECREF(traceback_module);
11271120
Py_XDECREF(print_exception_fn);
11281121
if (result) {
11291122
Py_DECREF(result);
@@ -1371,27 +1364,18 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
13711364
}
13721365

13731366
if (interactive_src) {
1374-
PyObject *linecache_module = PyImport_ImportModule("linecache");
1375-
1376-
if (linecache_module == NULL) {
1377-
Py_DECREF(co);
1378-
Py_DECREF(interactive_filename);
1379-
return NULL;
1380-
}
1381-
1382-
PyObject *print_tb_func = PyObject_GetAttrString(linecache_module, "_register_code");
1383-
1367+
PyObject *print_tb_func = PyImport_ImportModuleAttrString(
1368+
"linecache",
1369+
"_register_code");
13841370
if (print_tb_func == NULL) {
13851371
Py_DECREF(co);
13861372
Py_DECREF(interactive_filename);
1387-
Py_DECREF(linecache_module);
13881373
return NULL;
13891374
}
13901375

13911376
if (!PyCallable_Check(print_tb_func)) {
13921377
Py_DECREF(co);
13931378
Py_DECREF(interactive_filename);
1394-
Py_DECREF(linecache_module);
13951379
Py_DECREF(print_tb_func);
13961380
PyErr_SetString(PyExc_ValueError, "linecache._register_code is not callable");
13971381
return NULL;
@@ -1406,7 +1390,6 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
14061390

14071391
Py_DECREF(interactive_filename);
14081392

1409-
Py_DECREF(linecache_module);
14101393
Py_XDECREF(print_tb_func);
14111394
Py_XDECREF(result);
14121395
if (!result) {

0 commit comments

Comments
 (0)