Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-38159: Clarify documentation of PyState_AddModule #16101

Merged
merged 4 commits into from
Nov 1, 2019

Conversation

encukou
Copy link
Member

@encukou encukou commented Sep 13, 2019

This was never intented to be called manually from PyInit_*.

Also, clarify PyState_RemoveModule return value.

https://bugs.python.org/issue38159

Automerge-Triggered-By: @encukou

Automerge-Triggered-By: @encukou

This was never intented to be called manually from PyInit_*.

Also, clarify PyState_RemoveModule return value.
@eduardo-elizondo
Copy link
Contributor

eduardo-elizondo commented Sep 13, 2019

Just to clarify "implementing an alternative import mechanism" meant someone changing __import__ right? Otherwise, could you expand a bit more on what you meant by that? :)

Btw for context, these were added since the documentation currently specifies that PyState_FindModule needs to be paired up with PyState_AddModule to work. Since the import documentation does not offer a guarantee that your module will be attached to the interpreter state (through PyState_FindModule), it seemed like the right call to have that in the init functions.

As a matter of fact, in our system we do rely on this signal to correctly work with PyState_FindModule. Our import mechanism does not do it by default.

Maybe an alternative can be to update the import documentation that it should offer this guarantee? On the other hand, it seems like that should be an implementation detail and it's best to let the module themselves call the right APIs. Thoughts?

@encukou
Copy link
Member Author

encukou commented Sep 13, 2019

Changing __import__, creating module objects dynamically, things like that. I'm not 100% sure on this expected behavior myself, unfortunately. But it's currently redundant in the common case.

encukou added a commit to eduardo-elizondo/cpython that referenced this pull request Sep 13, 2019
I'm proposing a PyState_AddModule doc update to make things clearer:
python#16101
@encukou
Copy link
Member Author

encukou commented Sep 13, 2019

I asked over at at capi-sig.

@matrixise
Copy link
Member

@encukou maybe you could add "DO-NOT-MERGE"?

@encukou
Copy link
Member Author

encukou commented Sep 24, 2019

Note to reviewer: See the capi-sig discussion

@encukou
Copy link
Member Author

encukou commented Sep 24, 2019

cc @ncoghlan

Copy link
Contributor

@eduardo-elizondo eduardo-elizondo left a comment

Choose a reason for hiding this comment

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

LGTM with a small comment

@@ -485,10 +485,19 @@ since multiple such modules can be created from a single definition.

Only effective on modules created using single-phase initialization.

Python calls ``PyState_AddModule`` automatically after importing a module,
Copy link
Contributor

Choose a reason for hiding this comment

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

An alternate implementation might not necessarily call PyState_AddModule but still achieve the same end result. Perhaps just say that Python attaches the module to the interpreter state and should be accessible through PyState_FindModule. How they achieve that should be up to the implementation

Copy link
Member Author

Choose a reason for hiding this comment

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

All ways I can find of wording this more precisely make it significantly harder to understand.

I think it's understood that alternate implementations are free to do an equivalent of calling PyState_AddModule, instead of actually calling it. They should still implement the PyState_AddModule semantics (unless they decide to differ, of course).

Another thing that's implied here is that Python only calls this on single-phase-init modules. And another is that it's idempotent.

Should we document all that? How much hand-holding do authors of alternative import mechanisms need? (This is a real question, which you are in a good position to answer.)

Copy link
Member Author

Choose a reason for hiding this comment

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

But my main issue is:

All ways I can find of wording this more precisely make it significantly harder to understand.

If you can think of a better way, let me know! Otherwise I can merge this.

Copy link
Contributor

@ncoghlan ncoghlan left a comment

Choose a reason for hiding this comment

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

Added a couple of specific suggestions that I think improve precision without making it too much harder to read.

encukou and others added 2 commits October 22, 2019 13:04
Co-Authored-By: Nick Coghlan <ncoghlan@gmail.com>
@encukou
Copy link
Member Author

encukou commented Nov 1, 2019

Azure Pipelines didn't start, I'll try closing & reopening.

@encukou encukou closed this Nov 1, 2019
@encukou encukou reopened this Nov 1, 2019
@encukou encukou merged commit 9bc94ec into python:master Nov 1, 2019
@encukou encukou deleted the PyState_AddModule-doc branch November 1, 2019 15:47
@miss-islington
Copy link
Contributor

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7, 3.8.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-17026 is a backport of this pull request to the 3.8 branch.

@bedevere-bot
Copy link

GH-17027 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 1, 2019
This was never intented to be called manually from PyInit_*.

Also, clarify PyState_RemoveModule return value.
(cherry picked from commit 9bc94ec)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
miss-islington added a commit that referenced this pull request Nov 5, 2019
GH-17026)

This was never intented to be called manually from PyInit_*.

Also, clarify PyState_RemoveModule return value.
(cherry picked from commit 9bc94ec)


Co-authored-by: Petr Viktorin <encukou@gmail.com>


https://bugs.python.org/issue38159



Automerge-Triggered-By: @encukou
miss-islington added a commit that referenced this pull request Nov 5, 2019
GH-17027)

This was never intented to be called manually from PyInit_*.

Also, clarify PyState_RemoveModule return value.
(cherry picked from commit 9bc94ec)


Co-authored-by: Petr Viktorin <encukou@gmail.com>


https://bugs.python.org/issue38159



Automerge-Triggered-By: @encukou
jacobneiltaylor pushed a commit to jacobneiltaylor/cpython that referenced this pull request Dec 5, 2019
This was never intented to be called manually from PyInit_*.

Also, clarify PyState_RemoveModule return value.
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
This was never intented to be called manually from PyInit_*.

Also, clarify PyState_RemoveModule return value.
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
None yet
Development

Successfully merging this pull request may close these issues.

7 participants