GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-140824: Build the math module as an extension package by serhiy-storchaka · Pull Request #152352 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jun 26, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Build math as a real extension package (math/__init__ and math/integer), so that math.integer is a genuine extension submodule instead of the _math_integer extension module patched at runtime to look like a submodule.

This removes the leftover sys.modules['_math_integer'], gives math.integer's functions a correct __module__, and drops the __name__/__module__ and _PyImport_SetModuleString workarounds.

How

  • Modules/makesetup now accepts dotted module names and decouples three attributes that were previously tied to a single Setup token: the registration name (math.integer), the on-disk path (math/integer), and the C init symbol. A package initializer is written with an explicit __init__ leaf (math.__init__).
  • Makefile.pre.in preserves sub-directories when staging into the build-tree dynload dir and when installing into lib-dynload.
  • Tools/build/check_extension_modules.py and Tools/build/generate_stdlib_module_names.py understand package and dotted submodule names (the submodule is excluded from sys.stdlib_module_names).
  • configure.ac/Setup.stdlib.in build the package through the normal module-detection machinery (MODULE_MATH_INTEGER_*).

Shared vs. builtin

Works both for the usual shared build and for builds where math is compiled into the interpreter as a builtin (Windows, WebAssembly):

  • A builtin package gets no __path__ from the import machinery, so math's exec slot sets an empty __path__ to mark it as a package and let the builtin math.integer be found.
  • A shared submodule must export PyInit_<last component> (PyInit_integer) for the dynamic loader, but in an all-static build every PyInit_* shares one symbol namespace where a bare last-component name would clash across packages. So the static inittab uses a symbol derived from the full dotted name (PyInit_math_integer), selected with Py_BUILD_CORE_BUILTIN; only one of the two symbols is compiled per build mode. PC/config.c registers the Windows builtin accordingly.

Testing

  • Default shared build: make, checksharedmods, and test_math/test_cmath/test_importlib/test_sys all pass.
  • All-static (builtin math) build: verified that import math.integer works and that only the unique PyInit_math_integer symbol is present in the binary.
  • Not built on Windows from my environment; PC/config.c mirrors the verified builtin path.

🤖 Generated with Claude Code

https://claude.ai/code/session_01R4YFmYjuT6XeiobFzeQjDp

  • Issue: Make the math module an extension package #140824

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
serhiy-storchaka requested review from a team, AA-Turner, corona10, emmatyping, erlend-aasland and itamaro as code owners June 26, 2026 23:43
@bedevere-app bedevere-app Bot mentioned this pull request Jun 26, 2026
Make the math module an extension package #140824
Open
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 26, 2026
@serhiy-storchaka @claude
pythongh-140824: Build the math module as an extension package
9dc3148
Build math as an extension package (math/__init__ and math/integer) so
that math.integer is a genuine extension submodule instead of the
_math_integer extension module patched at runtime to look like one.  This
drops the sys.modules['_math_integer'] leak and gives math.integer's
functions a correct __module__.

Modules/makesetup now accepts dotted module names, mapping each to its
registration name, on-disk path and init symbol; a package initializer is
written with an explicit __init__ leaf (math.__init__).  Makefile.pre.in
keeps sub-directories when staging and installing shared modules, and
check_extension_modules.py and generate_stdlib_module_names.py handle
package and dotted submodule names.

The package works both for shared builds and for builds where math is a
builtin (Windows, WebAssembly): math's exec slot sets __path__ when the
import machinery does not, and the static inittab uses a symbol derived
from the full dotted name (PyInit_math_integer) to stay unique, selected
with Py_BUILD_CORE_BUILTIN.

ExtensionFileLoader.is_package() now mirrors FileLoader by returning False
for the "__init__" name itself, so that modulefinder (used by
Tools/freeze) does not recurse on an extension package.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@serhiy-storchaka
serhiy-storchaka force-pushed the math-extpkg-proto branch from 00db9e8 to 9dc3148 Compare June 27, 2026 05:48
@serhiy-storchaka
serhiy-storchaka requested review from FFY00, brettcannon, ericsnowcurrently, ncoghlan and warsaw as code owners June 27, 2026 05:48
@serhiy-storchaka
serhiy-storchaka requested a review from vstinner June 29, 2026 16:54
@serhiy-storchaka

serhiy-storchaka commented Jun 29, 2026

Copy link
Copy Markdown
Member Author

@hugovk , it would be nice to land this in 3.15, where math.integer was introduced, so we will no longer use hacks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brettcannon
brettcannon removed their request for review June 29, 2026 17:54
@serhiy-storchaka
Merge branch 'main' into math-extpkg-proto
f30e815
@hugovk

hugovk commented Jul 28, 2026

Copy link
Copy Markdown
Member

I think this should have testing during an alpha phase, so let's not backport to 3.15.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Oct 28, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
@emmatyping emmatyping Awaiting requested review from emmatyping emmatyping is a code owner
@itamaro itamaro Awaiting requested review from itamaro itamaro is a code owner
@corona10 corona10 Awaiting requested review from corona10 corona10 is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner
@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 is a code owner
@vstinner vstinner Awaiting requested review from vstinner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@serhiy-storchaka @hugovk
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.