Skip to content

Make the math module an extension package #140824

Description

@serhiy-storchaka

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.

Linked PRs

Activity

added
buildThe build process and cross-build
3.15pre-release feature fixes, bugs and security fixes
on Oct 31, 2025

itamaro commented on Nov 9, 2025

@itamaro
Contributor

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.

added 2 commits that reference this issue on Nov 10, 2025

vstinner commented on Nov 10, 2025

@vstinner
Member

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

@vstinner
Member

I created #141363 to solve this problem: implement the math package in Python.

vstinner commented on Nov 10, 2025

@vstinner
Member

skirpichev commented on Nov 11, 2025

@skirpichev
Member

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.

vstinner commented on Jan 22, 2026

@vstinner
Member

math.integer is missing from sys.stdlib_module_names:

>>> import sys
>>> 'math.integer' in sys.stdlib_module_names
False
added a commit that references this issue on Jan 28, 2026

StanFromIreland commented on Jan 28, 2026

@StanFromIreland
Member

math.integer is missing from sys.stdlib_module_names

FTR, this is expected per the doc.

vstinner commented on Jan 28, 2026

@vstinner
Member

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

@skirpichev
Member

_Py_DumpExtensionModules() should be update to ignore math.integer.

This also will be solved, if we remove math.integer from sys.stdlib_module_names.

added 2 commits that reference this issue on Jan 30, 2026

vstinner commented on Jan 30, 2026

@vstinner
Member

I wrote a fix for _Py_DumpExtensionModules(): #144339 ignores math.integer since math is part of sys.stdlib_module_names.

added a commit that references this issue on Feb 4, 2026
added a commit that references this issue on Feb 5, 2026
added a commit that references this issue on Feb 15, 2026
added a commit that references this issue on Apr 25, 2026
added a commit that references this issue on Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15pre-release feature fixes, bugs and security fixesbuildThe build process and cross-buildextension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions