FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Make the math module an extension package · Issue #140824 · python/cpython · GitHub

Repository navigation

Make the math module an extension package #140824

Open

Description

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

  1. itamaro commented on Nov 9, 2025

    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.

  2. added 2 commits that reference this issue on Nov 10, 2025
  3. vstinner commented on Nov 10, 2025

    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.

  4. vstinner commented on Nov 10, 2025

    Member

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

  5. vstinner commented on Nov 10, 2025

    Member
  6. skirpichev commented on Nov 11, 2025

    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.

  7. vstinner commented on Jan 22, 2026

    Member

    math.integer is missing from sys.stdlib_module_names:

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

    Member

    math.integer is missing from sys.stdlib_module_names

    FTR, this is expected per the doc.

  10. vstinner commented on Jan 28, 2026

    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.

  11. skirpichev commented on Jan 28, 2026

    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.

  12. added 2 commits that reference this issue on Jan 30, 2026
  13. vstinner commented on Jan 30, 2026

    Member

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

  14. added a commit that references this issue on Feb 4, 2026
  15. added a commit that references this issue on Feb 5, 2026
  16. added a commit that references this issue on Feb 15, 2026
  17. added a commit that references this issue on Apr 25, 2026
  18. 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.15bugs 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


      Back | FazBrowse Home | New Git URL