Repository navigation
Make the math module an extension package #140824
Description
Activity
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.
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.
I created #141363 to solve this problem: implement the math package in Python.
cc @skirpichev
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.
math.integer is missing from sys.stdlib_module_names:
>>> import sys
>>> 'math.integer' in sys.stdlib_module_names
False
math.integeris missing fromsys.stdlib_module_names
FTR, this is expected per the doc.
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.
_Py_DumpExtensionModules() should be update to ignore math.integer.
This also will be solved, if we remove math.integer from sys.stdlib_module_names.
I wrote a fix for _Py_DumpExtensionModules(): #144339 ignores math.integer since math is part of sys.stdlib_module_names.
In #81313 a new module
math.integerwas added. It was implemented as the_math_integerextension module, modified to look like amathsubmodule. This required assigning theintegerattribute of themathmodule, settingsys.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 themath.integermodule automatically when themathmodule 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
mathmodule should be compiled intomath/__init__.*.soinstead ofmath.*.so, andmath.integercorrespondingly intomath/integer.*.so. I believe this is possible.Linked PRs