summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_import
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-06-20 06:48:14 (GMT)
committerGitHub <noreply@github.com>2023-06-20 06:48:14 (GMT)
commit03f1a132eeb34c738812161947ef171b21d58c25 (patch)
treeefb632b4c885e58425bfd563c92137ef60f7d72d /Lib/test/test_import
parent7f97c8e367869e2aebe9f28bc5f8d4ce36448878 (diff)
downloadcpython-03f1a132eeb34c738812161947ef171b21d58c25.zip
cpython-03f1a132eeb34c738812161947ef171b21d58c25.tar.gz
cpython-03f1a132eeb34c738812161947ef171b21d58c25.tar.bz2
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).
Diffstat (limited to 'Lib/test/test_import')
-rw-r--r--Lib/test/test_import/__init__.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index 71a50bc..f2726da 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -2621,6 +2621,30 @@ class SinglephaseInitTests(unittest.TestCase):
# * module's global state was initialized, not reset
+@cpython_only
+class CAPITests(unittest.TestCase):
+ def test_pyimport_addmodule(self):
+ # gh-105922: Test PyImport_AddModuleRef(), PyImport_AddModule()
+ # and PyImport_AddModuleObject()
+ import _testcapi
+ for name in (
+ 'sys', # frozen module
+ 'test', # package
+ __name__, # package.module
+ ):
+ _testcapi.check_pyimport_addmodule(name)
+
+ def test_pyimport_addmodule_create(self):
+ # gh-105922: Test PyImport_AddModuleRef(), create a new module
+ import _testcapi
+ name = 'dontexist'
+ self.assertNotIn(name, sys.modules)
+ self.addCleanup(unload, name)
+
+ mod = _testcapi.check_pyimport_addmodule(name)
+ self.assertIs(mod, sys.modules[name])
+
+
if __name__ == '__main__':
# Test needs to be a package, so we can do relative imports.
unittest.main()