mirror of
https://github.com/python/cpython.git
synced 2025-10-28 09:10:36 +00:00
gh-105922: Add PyImport_AddModuleRef() function (#105923)
* Add tests on PyImport_AddModuleRef(), PyImport_AddModule() and PyImport_AddModuleObject(). * pythonrun.c: Replace Py_XNewRef(PyImport_AddModule(name)) with PyImport_AddModuleRef(name).
This commit is contained in:
parent
7f97c8e367
commit
03f1a132ee
13 changed files with 150 additions and 25 deletions
|
|
@ -3325,6 +3325,53 @@ test_atexit(PyObject *self, PyObject *Py_UNUSED(args))
|
|||
}
|
||||
|
||||
|
||||
static PyObject *
|
||||
check_pyimport_addmodule(PyObject *self, PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
if (!PyArg_ParseTuple(args, "s", &name)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// test PyImport_AddModuleRef()
|
||||
PyObject *module = PyImport_AddModuleRef(name);
|
||||
if (module == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
assert(PyModule_Check(module));
|
||||
// module is a strong reference
|
||||
|
||||
// test PyImport_AddModule()
|
||||
PyObject *module2 = PyImport_AddModule(name);
|
||||
if (module2 == NULL) {
|
||||
goto error;
|
||||
}
|
||||
assert(PyModule_Check(module2));
|
||||
assert(module2 == module);
|
||||
// module2 is a borrowed ref
|
||||
|
||||
// test PyImport_AddModuleObject()
|
||||
PyObject *name_obj = PyUnicode_FromString(name);
|
||||
if (name_obj == NULL) {
|
||||
goto error;
|
||||
}
|
||||
PyObject *module3 = PyImport_AddModuleObject(name_obj);
|
||||
Py_DECREF(name_obj);
|
||||
if (module3 == NULL) {
|
||||
goto error;
|
||||
}
|
||||
assert(PyModule_Check(module3));
|
||||
assert(module3 == module);
|
||||
// module3 is a borrowed ref
|
||||
|
||||
return module;
|
||||
|
||||
error:
|
||||
Py_DECREF(module);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static PyMethodDef TestMethods[] = {
|
||||
{"set_errno", set_errno, METH_VARARGS},
|
||||
{"test_config", test_config, METH_NOARGS},
|
||||
|
|
@ -3468,6 +3515,7 @@ static PyMethodDef TestMethods[] = {
|
|||
{"function_get_kw_defaults", function_get_kw_defaults, METH_O, NULL},
|
||||
{"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
|
||||
{"test_atexit", test_atexit, METH_NOARGS},
|
||||
{"check_pyimport_addmodule", check_pyimport_addmodule, METH_VARARGS},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue