GeezerVison is showing github.com — Make the math module an extension package · Issue #140824 · python/cpython
Make the math module an extension package #140824DescriptionIn #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.
Linked PRs
Activityitamaro 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. vstinner commented on Nov 10, 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.
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 skirpichev commented on Nov 11, 2025
I created #141363 to solve this problem
Sorry, I don't think it's a good alternative to the current hack. It's just a different hack. Reacted by Serhiy Storchaka vstinner commented on Jan 22, 2026 math.integer is missing from sys.stdlib_module_names:
>>> import sys
>>> 'math.integer' in sys.stdlib_module_names
False
StanFromIreland commented on Jan 28, 2026
math.integer is missing from sys.stdlib_module_names
FTR, this is expected per the doc. vstinner commented on Jan 28, 2026
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.
skirpichev commented on Jan 28, 2026
_Py_DumpExtensionModules() should be update to ignore math.integer.
This also will be solved, if we remove math.integer from sys.stdlib_module_names. 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.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|