In #81313 a new module math.integer was added. It was implemented as the _math_integer extension module, modified to look like a math submodule. This required assigning the integer attribute of the math module, setting sys.modules['math.integer'], patching the __name__ attribute of the new module and the __module__ attribute of its functions (to support help and pickling). Still, some implementation details are visible: sys.modules['_math_integer'], the __spec__ attribute of the new module. Also, it is difficult to stop importing the math.integer module automatically when the math module is imported.
Import machinery in principle supports extension packages and extension submodules, but there is no precedence of this in the stdlib, and it is not trivial to build them. The math module should be compiled into math/__init__.*.so instead of math.*.so, and math.integer correspondingly into math/integer.*.so. I believe this is possible.
math.integer is missing from sys.stdlib_module_names
The problem is that the _Py_DumpExtensionModules() function considers that math.integer is not part of the stdlib. Example with faulthandler on a crash:
$ ./python -X faulthandler
>>> import math.integer
>>> import ctypes
>>> ctypes.string_at(0)
Fatal Python error: Segmentation fault
Current thread 0x00007f01b0b2a780 [python] (most recent call first):
File "/home/vstinner/python/main/Lib/ctypes/__init__.py", line 607 in string_at
...
Current thread's C stack trace (most recent call first):
Binary file "./python", at _Py_DumpStack+0x31 [0x674654]
...
Extension modules: math.integer (total: 1)
FTR, this is expected per the doc.
_Py_DumpExtensionModules() should be update to ignore math.integer.
Activity
itamaro commented on Nov 9, 2025
it might be simpler to convert the _math and _math_integer extensions to builtins. not sure it would actually solve what you want to solve though.
pythongh-140824: _math extension
pythongh-140824: Implement the math package in Python
vstinner commented on Nov 10, 2025
I don't think that converting these extensions to built-in extensions would solve the problem of functions __module__ set to the wrong module.
vstinner commented on Nov 10, 2025
I created #141363 to solve this problem: implement the math package in Python.
vstinner commented on Nov 10, 2025
cc @skirpichev
skirpichev commented on Nov 11, 2025
Sorry, I don't think it's a good alternative to the current hack. It's just a different hack.
vstinner commented on Jan 22, 2026
math.integer is missing from sys.stdlib_module_names:
pythongh-140824: workaround math.integer module name for sys.stdlib_m…
StanFromIreland commented on Jan 28, 2026
FTR, this is expected per the doc.
vstinner commented on Jan 28, 2026
The problem is that the _Py_DumpExtensionModules() function considers that math.integer is not part of the stdlib. Example with faulthandler on a crash:
_Py_DumpExtensionModules() should be update to ignore math.integer.
skirpichev commented on Jan 28, 2026
This also will be solved, if we remove math.integer from sys.stdlib_module_names.
pythongh-140824: Fix _Py_DumpExtensionModules() to ignore sub-modules
pythongh-140824: Fix _Py_DumpExtensionModules() to ignore sub-modules
vstinner commented on Jan 30, 2026
I wrote a fix for _Py_DumpExtensionModules(): #144339 ignores math.integer since math is part of sys.stdlib_module_names.
gh-140824: Fix _Py_DumpExtensionModules() to ignore sub-modules (#144339
pythongh-140824: Fix _Py_DumpExtensionModules() to ignore sub-modules (…
pythongh-140824: Fix _Py_DumpExtensionModules() to ignore sub-modules (…
pythongh-140824: Fix _Py_DumpExtensionModules() to ignore sub-modules (…
pythongh-140824: Build the math module as an extension package