Skip to content

gh-157384: Set package context in PyImport_CreateModuleFromInitfunc() - #157387

Open
itamaro wants to merge 1 commit into
python:mainfrom
itamaro:gh-116146-initfunc-pkgcontext
Open

itamaro wants to merge 1 commit into
python:mainfrom
itamaro:gh-116146-initfunc-pkgcontext

Conversation

@itamaro

@itamaro itamaro commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

A single-phase init function that creates its module using only the last component of a dotted name (as pybind11 does for submodules) got a module whose name was the short name. Set the package context while calling the init function, as the dynamic loader does, so the module gets the full name from the spec.

AI assisted with Claude Fable 5.1

…func()

A single-phase init function that creates its module using only the
last component of a dotted name (as pybind11 does for submodules) got
a module whose __name__ was the short name.  Set the package context
while calling the init function, as the dynamic loader does, so the
module gets the full name from the spec.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HoZE3WdRWQshvinfBZ137N
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34530612 | 📁 Comparing 238cdb7 against main (2cd6d4b)

  🔍 Preview build  

2 files changed
± c-api/import.html
± whatsnew/changelog.html

@itamaro itamaro added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 13, 2026
@encukou

encukou commented Sep 14, 2026

Copy link
Copy Markdown
Member

Would it be possible to fix the binding generator instead?

@itamaro

itamaro commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Would it be possible to fix the binding generator instead?

In what way?

afaik pybind11 3+ already defaults to multi-phase init, so this issue is mostly with older pybind11 extensions (2.13 and older), or hand-written C extensions.

this fix makes this API honor the documented cpython contract for single-phase init extensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants