summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-05-27 19:11:29 (GMT)
committerGitHub <noreply@github.com>2024-05-27 19:11:29 (GMT)
commitbd9983cab89cc42eecdbb4556cca0b6d7a7c529c (patch)
tree3a93e60e20dce025da387b100bae243fb825eb2d /Modules
parent0a4a3184f56a1e4e8cb4d3466cee20388bf7f00d (diff)
downloadcpython-bd9983cab89cc42eecdbb4556cca0b6d7a7c529c.zip
cpython-bd9983cab89cc42eecdbb4556cca0b6d7a7c529c.tar.gz
cpython-bd9983cab89cc42eecdbb4556cca0b6d7a7c529c.tar.bz2
[3.13] gh-119560: Drop an Invalid Assert in PyState_FindModule() (gh-119561) (gh-119632)
The assertion was added in gh-118532 but was based on the invalid assumption that PyState_FindModule() would only be called with an already-initialized module def. I've added a test to make sure we don't make that assumption again. (cherry picked from commit 0c5ebe13e9937c446e9947c44f2570737ecca135) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testsinglephase.c91
1 files changed, 89 insertions, 2 deletions
diff --git a/Modules/_testsinglephase.c b/Modules/_testsinglephase.c
index 448be50..bcdb5ba 100644
--- a/Modules/_testsinglephase.c
+++ b/Modules/_testsinglephase.c
@@ -1,7 +1,7 @@
/* Testing module for single-phase initialization of extension modules
-This file contains 5 distinct modules, meaning each as its own name
+This file contains 8 distinct modules, meaning each as its own name
and its own init function (PyInit_...). The default import system will
only find the one matching the filename: _testsinglephase. To load the
others you must do so manually. For example:
@@ -14,7 +14,7 @@ spec = importlib.util.spec_from_file_location(name, filename, loader=loader)
mod = importlib._bootstrap._load(spec)
```
-Here are the 5 modules:
+Here are the 8 modules:
* _testsinglephase
* def: _testsinglephase_basic,
@@ -136,6 +136,32 @@ Here are the 5 modules:
5. increment <global>.initialized_count
* functions: see common functions below
* import system: same as _testsinglephase_basic_copy
+* _testsinglephase_check_cache_first
+ * def: _testsinglepahse_check_cache_first
+ * m_name: "_testsinglephase_check_cache_first"
+ * m_size: -1
+ * state: none
+ * init function:
+ * tries PyState_FindModule() first
+ * otherwise creates empty module
+ * functions: none
+ * import system: same as _testsinglephase
+* _testsinglephase_with_reinit_check_cache_first
+ * def: _testsinglepahse_with_reinit_check_cache_first
+ * m_name: "_testsinglephase_with_reinit_check_cache_first"
+ * m_size: 0
+ * state: none
+ * init function: same as _testsinglephase_check_cache_first
+ * functions: none
+ * import system: same as _testsinglephase_with_reinit
+* _testsinglephase_with_state_check_cache_first
+ * def: _testsinglepahse_with_state_check_cache_first
+ * m_name: "_testsinglephase_with_state_check_cache_first"
+ * m_size: 42
+ * state: none
+ * init function: same as _testsinglephase_check_cache_first
+ * functions: none
+ * import system: same as _testsinglephase_with_state
Module state:
@@ -650,3 +676,64 @@ PyInit__testsinglephase_with_state(void)
finally:
return module;
}
+
+
+/****************************************************/
+/* the _testsinglephase_*_check_cache_first modules */
+/****************************************************/
+
+static struct PyModuleDef _testsinglephase_check_cache_first = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = "_testsinglephase_check_cache_first",
+ .m_doc = PyDoc_STR("Test module _testsinglephase_check_cache_first"),
+ .m_size = -1, // no module state
+};
+
+PyMODINIT_FUNC
+PyInit__testsinglephase_check_cache_first(void)
+{
+ assert(_testsinglephase_check_cache_first.m_base.m_index == 0);
+ PyObject *mod = PyState_FindModule(&_testsinglephase_check_cache_first);
+ if (mod != NULL) {
+ return Py_NewRef(mod);
+ }
+ return PyModule_Create(&_testsinglephase_check_cache_first);
+}
+
+
+static struct PyModuleDef _testsinglephase_with_reinit_check_cache_first = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = "_testsinglephase_with_reinit_check_cache_first",
+ .m_doc = PyDoc_STR("Test module _testsinglephase_with_reinit_check_cache_first"),
+ .m_size = 0, // no module state
+};
+
+PyMODINIT_FUNC
+PyInit__testsinglephase_with_reinit_check_cache_first(void)
+{
+ assert(_testsinglephase_with_reinit_check_cache_first.m_base.m_index == 0);
+ PyObject *mod = PyState_FindModule(&_testsinglephase_with_reinit_check_cache_first);
+ if (mod != NULL) {
+ return Py_NewRef(mod);
+ }
+ return PyModule_Create(&_testsinglephase_with_reinit_check_cache_first);
+}
+
+
+static struct PyModuleDef _testsinglephase_with_state_check_cache_first = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = "_testsinglephase_with_state_check_cache_first",
+ .m_doc = PyDoc_STR("Test module _testsinglephase_with_state_check_cache_first"),
+ .m_size = 42, // not used
+};
+
+PyMODINIT_FUNC
+PyInit__testsinglephase_with_state_check_cache_first(void)
+{
+ assert(_testsinglephase_with_state_check_cache_first.m_base.m_index == 0);
+ PyObject *mod = PyState_FindModule(&_testsinglephase_with_state_check_cache_first);
+ if (mod != NULL) {
+ return Py_NewRef(mod);
+ }
+ return PyModule_Create(&_testsinglephase_with_state_check_cache_first);
+}