Skip to content

gh-116146: Add C-API to create module from spec and initfunc - #139196

Merged
encukou merged 15 commits into
python:mainfrom
itamaro:gh-116146-create-builtin-initfunc
Nov 14, 2025
Merged

gh-116146: Add C-API to create module from spec and initfunc#139196
encukou merged 15 commits into
python:mainfrom
itamaro:gh-116146-create-builtin-initfunc

Conversation

@itamaro

@itamaroitamaro commented Sep 21, 2025

Copy link
Copy Markdown
Contributor

@encukouencukou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable.

I'm proposing a replacement for Init functions in PEP-793, but that PEP adds PyModule_FromSlotsAndSpec and PyModule_Exec which should work for similar use cases.
Would it make sense to switch the spec & initfunc arguments to match that proposal?

I'm not sure if the word Builtin in the name is necessary -- is that to match the internal create_builtin? Would something like PyImport_LoadModuleFromInitfuncAndSpec work better?

Do you want to submit this to the C API WG?

Comment threadPython/import.c Outdated
@itamaro

Copy link
Copy Markdown
ContributorAuthor

Would it make sense to switch the spec & initfunc arguments to match that proposal?

I think it should be fine! Is the PEP already implemented on main?

I'm not sure if the word Builtin in the name is necessary -- is that to match the internal create_builtin? Would something like PyImport_LoadModuleFromInitfuncAndSpec work better?

Yes, I used it to reflect what it does internally. PyImport_LoadModuleFromInitfuncAndSpec sounds great to me!

Do you want to submit this to the C API WG?

Will do. Should we first decide whether to change this to match PEP 793, or go to the WP with options and get their input?

@encukou

Copy link
Copy Markdown
Member

Is the PEP already implemented on main?

No, it's not even accepted :)
I have a slight preference to align the two proposals, in case both make it. I'm checking if you have strong reasons for the order here. No need to update the PR now.

Will do. Should we first decide whether to change this to match PEP 793, or go to the WP with options and get their input?

Just go to the WG :)

@itamaro

Copy link
Copy Markdown
ContributorAuthor

Just go to the WG :)

done :) capi-workgroup/decisions#77

Comment threadPython/import.c Outdated
@itamaro
itamaroforce-pushed the gh-116146-create-builtin-initfunc branch from 4363e5a to 781a730CompareNovember 8, 2025 18:52
- remove unused `found` variable
- use `my_test_extension` instead of `embedded_ext` (the former is free-threading-ready, the latter prints a warning)
@itamaroitamaro changed the title gh-116146: Add new C-API to create builtin from spec and initfuncgh-116146: Add C-API to create module from spec and initfuncNov 9, 2025
@itamaro
itamaro marked this pull request as ready for review November 9, 2025 01:01
Comment threadPython/import.c Outdated
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
@encukou

Copy link
Copy Markdown
Member

I left my suggestions as a PR: itamaro#27

* Test single-phase init as well; don't use private APIs in test
* Doc update
---------
Co-authored-by: Itamar Oren <itamarost@gmail.com>
Comment threadDoc/c-api/import.rst Outdated
Comment threadInclude/cpython/import.h Outdated
Comment threadPrograms/_testembed.c Outdated
Comment threadPrograms/_testembed.c Outdated
Comment threadPrograms/_testembed.c Outdated
Comment threadPython/import.c
Co-authored-by: Victor Stinner <vstinner@python.org>
@python-cla-bot

python-cla-botBot commented Nov 12, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@itamaro
itamaroforce-pushed the gh-116146-create-builtin-initfunc branch from 8f16533 to 1165950CompareNovember 12, 2025 16:18
@itamaro

Copy link
Copy Markdown
ContributorAuthor

thank you @encukou@vstinner@kumaraditya303 for the review and suggestions!

@encukou are you planning to merge gh-141197 soon? until that is merged, this PR has a docs failure due to referring to the function that you document in gh-141197.

@vstinnervstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new function PyImport_CreateModuleFromInitfunc() should be documented in Doc/whatsnew/3.15.rst.

Comment threadMisc/NEWS.d/next/C_API/2025-11-08-10-51-50.gh-issue-116146.pCmx6L.rst Outdated
Comment threadDoc/c-api/import.rst Outdated
Comment threadInclude/cpython/import.h Outdated
Comment threadPrograms/_testembed.c Outdated
Comment threadPython/import.c Outdated
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
@itamaro
itamaroforce-pushed the gh-116146-create-builtin-initfunc branch from fabafb5 to fbfde0aCompareNovember 13, 2025 15:32

@vstinnervstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the updates!

@encukou
encukou merged commit 1e4e59b into python:mainNov 14, 2025
46 checks passed
encukou added a commit to encukou/cpython that referenced this pull request Nov 14, 2025
@vstinner

Copy link
Copy Markdown
Member

Congrats @itamaro!

@itamaro
itamaro deleted the gh-116146-create-builtin-initfunc branch November 15, 2025 23:40
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
…ythonGH-139196)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@itamaro@encukou@vstinner@kumaraditya303