From 193a2b2eaab9890f2cb16128c95bb63cb49307f1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 23 Jun 2023 00:04:39 +0200 Subject: [PATCH] gh-105922: Use PyImport_AddModuleRef() function (#105999) Replace PyImport_AddModuleObject() + Py_XNewRef() with PyImport_AddModuleRef() to get directly a strong reference. --- Python/import.c | 3 +-- Python/pythonrun.c | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Python/import.c b/Python/import.c index 9b1ad87b84f649..05da9506cc0b64 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1382,8 +1382,7 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec) if (_PyUnicode_EqualToASCIIString(name, p->name)) { if (p->initfunc == NULL) { /* Cannot re-init internal module ("sys" or "builtins") */ - mod = PyImport_AddModuleObject(name); - return Py_XNewRef(mod); + return import_add_module(tstate, name); } mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc); if (mod == NULL) { diff --git a/Python/pythonrun.c b/Python/pythonrun.c index af82fa491511c1..2986622a8b0dcf 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -276,7 +276,7 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename, return parse_res; } - PyObject *main_module = Py_XNewRef(PyImport_AddModuleObject(&_Py_ID(__main__))); + PyObject *main_module = PyImport_AddModuleRef("__main__"); if (main_module == NULL) { _PyArena_Free(arena); return -1;