FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-134160: Use multi-phase init in documentation examples by neonene · Pull Request #134296 · python/cpython · GitHub

/ cpython Public

gh-134160: Use multi-phase init in documentation examples - #134296

Merged
AA-Turner merged 24 commits into
python:mainfrom
neonene:multi
May 26, 2025
Merged

gh-134160: Use multi-phase init in documentation examples#134296
AA-Turner merged 24 commits into
python:mainfrom
neonene:multi

Conversation

neonene commented May 19, 2025
edited by github-actions Bot
Loading

Copy link
Copy Markdown
Contributor

Replaces PyModule_Create() with PyModuleDef_Init().

Static types still remain unchanged in the doc.


📚 Documentation preview 📚: https://cpython-previews--134296.org.readthedocs.build/

neonene commented May 20, 2025

Copy link
Copy Markdown
Contributor Author

cc @erlend-aasland @encukou Just in case you're interested.

AA-Turner changed the title gh-134160: Docs: Ensure single-phase init is no longer used in examples gh-134160: Use multi-phase init in documentation examples May 21, 2025
AA-Turner added the docs Documentation in the Doc dir label May 21, 2025
github-project-automation Bot moved this to Todo in Docs PRs May 21, 2025
AA-Turner added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels May 21, 2025
Comment thread Doc/c-api/intro.rst
Comment thread Doc/extending/extending.rst Outdated
Comment thread Doc/extending/extending.rst Outdated
Comment thread Doc/extending/extending.rst Outdated
Comment thread Doc/extending/extending.rst Outdated
Comment thread Doc/extending/extending.rst Outdated
Comment thread Doc/extending/extending.rst Outdated
Comment thread Doc/extending/extending.rst Outdated
Comment thread Doc/extending/extending.rst Outdated
Comment thread Doc/extending/embedding.rst Outdated
Comment on lines +254 to +258
static PyModuleDef_Slot emb_module_slots[] = {
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
{0, NULL}
};

AA-Turner May 22, 2025
edited
Loading

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Having done trial conversions of NumPy to multi-phase, I think we should include the relevant PY_VERSION_HEX checks. Both Py_mod_multiple_interpreters and Py_mod_gil were added very recently.

If we agree on adding these checks, we should add them for all relevant PyModuleDef_Slot structs. It is verbose, but aids in the copy-and-paste scenario.

Suggested change
static PyModuleDef_Slot emb_module_slots[] = {
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
{0, NULL}
};
static PyModuleDef_Slot emb_module_slots[] = {
#if PY_VERSION_HEX >= 0x030C00F0 // Python 3.12+
// signal that this module can be imported in isolated subinterpreters
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
#endif
#if PY_VERSION_HEX >= 0x030D00F0 // Python 3.13+
// signal that this module supports running without an active GIL
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
#endif
{0, NULL}
};

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Alternative:

Suggested change
static PyModuleDef_Slot emb_module_slots[] = {
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
{0, NULL}
};
static PyModuleDef_Slot emb_module_slots[] = {
#ifdef Py_mod_multiple_interpreters
// signal that this module can be imported in isolated subinterpreters
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
#endif
#ifdef Py_mod_gil
// signal that this module supports running without an active GIL
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
#endif
{0, NULL}
};

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can this PR omit these slots entirely?

IMO, they should be introduced later, after you compile your first module, along with a longer discussion about isolated state & thread safety.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Agree better to keep simple. @neonene perhaps remove the gil and interpreter slots from where we've added them in this PR unless they were already present?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Removed, but not all. I can complete.

Copy link
Copy Markdown
Member

Other places to update:

neonene and others added 3 commits May 22, 2025 16:05
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>

neonene commented May 22, 2025

Copy link
Copy Markdown
Contributor Author

AA-Turner enabled auto-merge (squash) May 26, 2025 21:39
AA-Turner merged commit 96905bd into python:main May 26, 2025
github-project-automation Bot moved this from Todo to Done in Docs PRs May 26, 2025

Copy link
Copy Markdown

Thanks @neonene for the PR, and @AA-Turner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 26, 2025
…onGH-134296)

(cherry picked from commit 96905bd)

Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 26, 2025
…onGH-134296)

(cherry picked from commit 96905bd)

Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>

bedevere-app Bot commented May 26, 2025

Copy link
Copy Markdown

GH-134753 is a backport of this pull request to the 3.14 branch.

bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label May 26, 2025

bedevere-app Bot commented May 26, 2025

Copy link
Copy Markdown

GH-134754 is a backport of this pull request to the 3.13 branch.

bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label May 26, 2025
AA-Turner added a commit that referenced this pull request May 26, 2025
…134296) (#134753)

gh-134160: Use multi-phase init in documentation examples (GH-134296)
(cherry picked from commit 96905bd)

Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
AA-Turner added a commit that referenced this pull request May 26, 2025
…134296) (#134754)

gh-134160: Use multi-phase init in documentation examples (GH-134296)
(cherry picked from commit 96905bd)

Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Comment on lines +206 to +208
You can also define a new exception that is unique to your module.
For this, you can declare a static global object variable at the beginning
of the file::

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is not correct for multi-phase initialization, without preventing the exec function from other module instances from overriding this pointer.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@encukou I added but then reverted (6e5b1c5) module state for this error type -- should we un-revert that commit? Sorry for my mistake here.

A

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think it's fine -- module state deserves a better explanation a bit later in the tutorial. And the ImportError being added in the next PR is a good thing to show.

The main thing I'd do differently is not backporting to 3.13 -- that's like pushing directly to production. 3.14 is out in a few months; that sounds like a good amount of time for this all to spend in prerelease.

neonene deleted the multi branch May 27, 2025 05:48
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
…on#134296)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…on#134296)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
gostak-dd pushed a commit to gostak-dd/cpython that referenced this pull request Jun 2, 2026
…on#134296)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL