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-116146: Add C-API to create module from spec and initfunc by itamaro · Pull Request #139196 · python/cpython

Conversation

@itamaro

@itamaro itamaro commented Sep 21, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

C-API WG decision: capi-workgroup/decisions#77

  • Issue: Expose a C-API function to allow custom importers to create a module using an init function #116146

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Sep 21, 2025
Expose a C-API function to allow custom importers to create a module using an init function #116146
Closed
@itamaro
itamaro force-pushed the gh-116146-create-builtin-initfunc branch from f9cd705 to 4363e5a Compare September 21, 2025 19:25
@encukou encukou mentioned this pull request Sep 23, 2025
_PyImport_SwapPackageContext would be helpful if exposed #139227
Closed
encukou
encukou reviewed Sep 23, 2025
View reviewed changes

@encukou encukou left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/import.c Resolved

Uh oh!

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

@itamaro

itamaro commented Sep 23, 2025

Copy link
Copy Markdown
Contributor Author

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?

All reactions

Sorry, something went wrong.

Uh oh!

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

@encukou

encukou commented Sep 24, 2025

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 :)

All reactions

Sorry, something went wrong.

Uh oh!

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

@itamaro itamaro mentioned this pull request Sep 24, 2025
Expose a C-API to allow custom importers to create a module directly from spec & initfunc capi-workgroup/decisions#77
Closed
3 of 6 tasks
@itamaro

itamaro commented Sep 24, 2025

Copy link
Copy Markdown
Contributor Author

Just go to the WG :)

done :) capi-workgroup/decisions#77

All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner reviewed Sep 26, 2025
View reviewed changes
Comment thread Python/import.c Resolved

Uh oh!

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

itamaro added 5 commits November 8, 2025 08:09
@itamaro
pythongh-116146: Add new C-API to create builtin from spec and initfunc
403c7e2
@itamaro
Apply review feedback and CAPI WG decision
074a510
- use `PyImport_CreateModuleFromInitfunc` for the new API
- no need to introduce `create_builtin_ex` - update `create_builtin` and all callers
- update exception message
@itamaro
Add tests for the PyImport_CreateModuleFromInitfunc API
d38c0e4
@itamaro
Document the new PyImport_CreateModuleFromInitfunc C-API
d5be6e5
@itamaro
Add news entry
781a730
@itamaro
itamaro force-pushed the gh-116146-create-builtin-initfunc branch from 4363e5a to 781a730 Compare November 8, 2025 18:52
itamaro added 2 commits November 8, 2025 10:54
@itamaro
Fix lint
7af34eb
@itamaro
fix CI
35e9e78
- remove unused `found` variable
- use `my_test_extension` instead of `embedded_ext` (the former is free-threading-ready, the latter prints a warning)
@itamaro itamaro changed the title gh-116146: Add new C-API to create builtin from spec and initfunc gh-116146: Add C-API to create module from spec and initfunc Nov 9, 2025
@itamaro
itamaro marked this pull request as ready for review November 9, 2025 01:01
@itamaro
itamaro requested review from brettcannon, ericsnowcurrently, kumaraditya303, ncoghlan and warsaw as code owners November 9, 2025 01:01
@bedevere-app bedevere-app Bot added the awaiting review label Nov 9, 2025
kumaraditya303
kumaraditya303 reviewed Nov 9, 2025
View reviewed changes
Comment thread Python/import.c Resolved

Uh oh!

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

@itamaro @kumaraditya303
Apply Kumar's review suggestion
6aa2fb2
Co-authored-by: Kumar Aditya <[email protected]>
@encukou

encukou commented Nov 11, 2025

Copy link
Copy Markdown
Member

I left my suggestions as a PR: itamaro#27

itamaro reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

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

@encukou @itamaro
PyImport_CreateModuleFromInitfunc: Doc & test update (#27)
f652399
* Test single-phase init as well; don't use private APIs in test
* Doc update

---------

Co-authored-by: Itamar Oren <[email protected]>
vstinner
vstinner reviewed Nov 12, 2025
View reviewed changes
Comment thread Doc/c-api/import.rst Resolved

Uh oh!

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

Comment thread Include/cpython/import.h Resolved

Uh oh!

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

Comment thread Programs/_testembed.c Resolved

Uh oh!

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

Comment thread Programs/_testembed.c Resolved

Uh oh!

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

Comment thread Programs/_testembed.c Resolved

Uh oh!

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

Comment thread Python/import.c Resolved

Uh oh!

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

@itamaro @vstinner
Apply Victor's review suggestions
c4f1cc8
Co-authored-by: Victor Stinner <[email protected]>
@python-cla-bot

python-cla-bot Bot commented Nov 12, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

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

itamaro added 2 commits November 12, 2025 08:17
@itamaro
Set error if initfunc is NULL
29b1b68
@itamaro
Account for the RuntimeWarning when import a singlephase init extensi…
1165950
…on under the free-threaded build
@itamaro
itamaro force-pushed the gh-116146-create-builtin-initfunc branch from 8f16533 to 1165950 Compare November 12, 2025 16:18
@itamaro

itamaro commented Nov 12, 2025

Copy link
Copy Markdown
Contributor Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner reviewed Nov 12, 2025
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

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

itamaro reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Misc/NEWS.d/next/C_API/2025-11-08-10-51-50.gh-issue-116146.pCmx6L.rst Resolved

Uh oh!

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

encukou
encukou reviewed Nov 13, 2025
View reviewed changes
Comment thread Doc/c-api/import.rst Resolved

Uh oh!

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

kumaraditya303
kumaraditya303 reviewed Nov 13, 2025
View reviewed changes
Comment thread Include/cpython/import.h Resolved

Uh oh!

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

kumaraditya303
kumaraditya303 reviewed Nov 13, 2025
View reviewed changes
Comment thread Programs/_testembed.c Resolved

Uh oh!

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

kumaraditya303
kumaraditya303 reviewed Nov 13, 2025
View reviewed changes
Comment thread Python/import.c Resolved

Uh oh!

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

@itamaro @encukou
@kumaraditya303 @vstinner
Apply suggestions from code review
79e5d0e
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@itamaro
itamaro requested a review from AA-Turner as a code owner November 13, 2025 15:31
@itamaro
Mention new C-API in whatsnew
fbfde0a
@itamaro
itamaro force-pushed the gh-116146-create-builtin-initfunc branch from fabafb5 to fbfde0a Compare November 13, 2025 15:32
vstinner
vstinner approved these changes Nov 13, 2025
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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!

Sorry, something went wrong.

Uh oh!

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

itamaro reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Nov 13, 2025
kumaraditya303
kumaraditya303 approved these changes Nov 13, 2025
View reviewed changes
@itamaro
Merge branch 'main' into pythongh-116146-create-builtin-initfunc
f485fcb
@encukou
encukou merged commit 1e4e59b into python:main Nov 14, 2025
46 checks passed

Uh oh!

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

@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 14, 2025
encukou added a commit to encukou/cpython that referenced this pull request Nov 14, 2025
@encukou
Link reference to PyModule_Exec added in python#139196
816023d
@vstinner

vstinner commented Nov 14, 2025

Copy link
Copy Markdown
Member

Congrats @itamaro!

itamaro reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

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

@itamaro
itamaro deleted the gh-116146-create-builtin-initfunc branch November 15, 2025 23:40
@itamaro itamaro mentioned this pull request Dec 1, 2025
Assertion failures from calling create_builtin with invalid object #142029
Closed
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@itamaro @kumaraditya303
@encukou @vstinner @StanFromIreland
pythongh-116146: Add C-API to create module from spec and initfunc (p…
d8c42b4
…ythonGH-139196)

Co-authored-by: Kumar Aditya <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@itamaro itamaro mentioned this pull request Dec 11, 2025
gh-142029: Raise ModuleNotFoundError instead of crashing on nonexsistent module name given to create_builtin() #142054
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou left review comments
@vstinner vstinner vstinner approved these changes
@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes
@brettcannon brettcannon Awaiting requested review from brettcannon brettcannon 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
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

4 participants

@itamaro @encukou @vstinner @kumaraditya303
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.