Skip to content

gh-157384: Refuse inittab names in PyImport_CreateModuleFromInitfunc() - #157388

Closed
itamaro wants to merge 1 commit into
python:mainfrom
itamaro:gh-116146-initfunc-refuse-inittab
Closed

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

Conversation

@itamaro

@itamaro itamaro commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Modules created by PyImport_CreateModuleFromInitfunc() share the extensions cache and sys.modules with built-in modules. Passing a spec whose name is registered in PyImport_Inittab either silently ignored the init function (for an already loaded single-phase builtin, or for "sys" and "builtins") or replaced the built-in module in sys.modules (for a multi-phase builtin). Raise ImportError instead.

AI assisted with Claude Fable 5.1

…tfunc()

Modules created by PyImport_CreateModuleFromInitfunc() share the
extensions cache and sys.modules with built-in modules.  Passing a
spec whose name is registered in PyImport_Inittab either silently
ignored the init function (for an already loaded single-phase builtin,
or for "sys" and "builtins") or replaced the built-in module in
sys.modules (for a multi-phase builtin).  Raise ImportError instead.

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 #34530618 | 📁 Comparing a0de16a against main (2cd6d4b)

  🔍 Preview build  

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

@encukou

encukou commented Sep 14, 2026

Copy link
Copy Markdown
Member

Looking at your post (and not checking the code in detail): at least for multi-phase modules, this looks like it's is working as expected. For multi-phase case, sys.modules shouldn't be involved; you're free to create any module object you wish. (Replacing existing sys.modules entries would be questionable of course, but, that's not in scope for PyImport_CreateModuleFromInitfunc.)

For single-phase, things are less clear, but I think the documented quirk applies: “For subsequent imports, Python does not call the initialization function again.”
We should be clearer about what the lookup key for “subsequent imports”, but, I don't think this needs a behaviour change. (as in: I don't know off the top of my head if you get a new module or the cached one, but either is fine; we should probably document what happens.)

Bottom line, I thing scanning inittab is the users' responsibility.

@itamaro

itamaro commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

I agree with you, I think this PR can be closed, I'd fold the docs clarification into gh-157389

@itamaro itamaro closed this Sep 14, 2026
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