Skip to content

Commit

Permalink
Return 1 if exists, 0 if created
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Nov 9, 2023
1 parent c12b080 commit af8a9df
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Doc/c-api/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ Importing Modules
- If the module name is already present in :data:`sys.modules`,
set *\*module* to a :term:`strong reference` to the existing module, and
return 0.
return 1.
- If the module does not exist in :data:`sys.modules`, create a new empty
module, store it in :data:`sys.modules`, set *\*module* to a :term:`strong
reference` to the module, and return 1.
reference` to the module, and return 0.
- On error, raise an exception, set *\*module* to NULL, and return -1.
The *name* argument may be of the form ``package.module``. Package
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,7 @@ def test_pyimport_addmodule(self):
__name__, # package.module
):
self.assertIn(name, sys.modules)
_testcapi.check_pyimport_addmodule(name, False)
_testcapi.check_pyimport_addmodule(name, True)

def test_pyimport_addmodule_create(self):
# gh-105922: Test PyImport_ImportOrAddModule(): create a new module.
Expand All @@ -2687,7 +2687,7 @@ def test_pyimport_addmodule_create(self):
self.assertNotIn(name, sys.modules)
self.addCleanup(unload, name)

mod = _testcapi.check_pyimport_addmodule(name, True)
mod = _testcapi.check_pyimport_addmodule(name, False)
self.assertIs(mod, sys.modules[name])


Expand Down
6 changes: 3 additions & 3 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3036,8 +3036,8 @@ static PyObject *
check_pyimport_addmodule(PyObject *self, PyObject *args)
{
const char *name;
int is_new;
if (!PyArg_ParseTuple(args, "si", &name, &is_new)) {
int expected;
if (!PyArg_ParseTuple(args, "si", &name, &expected)) {
return NULL;
}

Expand All @@ -3047,7 +3047,7 @@ check_pyimport_addmodule(PyObject *self, PyObject *args)
if (res < 0) {
return NULL;
}
assert(res == is_new);
assert(res == expected);
assert(PyModule_Check(module));
// module is a strong reference

Expand Down
16 changes: 8 additions & 8 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ PyImport_GetModule(PyObject *name)
First check the modules dictionary if there's one there,
if not, create a new one and insert it in the modules dictionary. */
static PyObject *
import_import_or_add_module(PyThreadState *tstate, PyObject *name, int *is_new)
import_import_or_add_module(PyThreadState *tstate, PyObject *name, int *exists)
{
PyObject *modules = MODULES(tstate->interp);
if (modules == NULL) {
Expand All @@ -304,8 +304,8 @@ import_import_or_add_module(PyThreadState *tstate, PyObject *name, int *is_new)
return NULL;
}
if (m != NULL && PyModule_Check(m)) {
if (is_new) {
*is_new = 0;
if (exists) {
*exists = 1;
}
return m;
}
Expand All @@ -320,8 +320,8 @@ import_import_or_add_module(PyThreadState *tstate, PyObject *name, int *is_new)
return NULL;
}

if (is_new) {
*is_new = 1;
if (exists) {
*exists = 0;
}
return m;
}
Expand All @@ -343,16 +343,16 @@ PyImport_ImportOrAddModule(const char *name, PyObject **pmodule)
return -1;
}
PyThreadState *tstate = _PyThreadState_GET();
int is_new;
PyObject *module = import_import_or_add_module(tstate, name_obj, &is_new);
int exists;
PyObject *module = import_import_or_add_module(tstate, name_obj, &exists);
Py_DECREF(name_obj);

*pmodule = module;
if (module == NULL) {
assert(PyErr_Occurred());
return -1;
}
return is_new;
return exists;
}


Expand Down

0 comments on commit af8a9df

Please sign in to comment.