gh-140824: Fix _Py_DumpExtensionModules() to ignore sub-modules (#144339)

Ignore "math.integer" extension if "math" is in
sys.stdlib_module_names.
This commit is contained in:
Victor Stinner 2026-02-04 16:06:35 +01:00 committed by GitHub
parent 0bb4ecafcb
commit 2aea861fe0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 7 deletions

View file

@ -3393,11 +3393,25 @@ _Py_DumpExtensionModules(int fd, PyInterpreterState *interp)
Py_hash_t hash;
// if stdlib_module_names is not NULL, it is always a frozenset.
while (_PySet_NextEntry(stdlib_module_names, &i, &item, &hash)) {
if (PyUnicode_Check(item)
&& PyUnicode_Compare(key, item) == 0)
{
is_stdlib_ext = 1;
break;
if (!PyUnicode_Check(item)) {
continue;
}
Py_ssize_t len = PyUnicode_GET_LENGTH(item);
if (PyUnicode_Tailmatch(key, item, 0, len, -1) == 1) {
Py_ssize_t key_len = PyUnicode_GET_LENGTH(key);
if (key_len == len) {
is_stdlib_ext = 1;
break;
}
assert(key_len > len);
// Ignore sub-modules of stdlib packages. For example,
// ignore "math.integer" if key starts with "math.".
Py_UCS4 ch = PyUnicode_ReadChar(key, len);
if (ch == '.') {
is_stdlib_ext = 1;
break;
}
}
}
if (is_stdlib_ext) {