This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PEP 573: Module State Access from C Extension Methods
Type: enhancement Stage: resolved
Components: Extension Modules Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Dormouse759, miss-islington, ncoghlan, petr.viktorin, scoder, shihai1991, terry.reedy, vstinner
Priority: normal Keywords: patch

Created on 2019-11-13 14:30 by Dormouse759, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17145 closed Dormouse759, 2019-11-13 14:34
PR 19936 merged petr.viktorin, 2020-05-05 16:47
PR 19980 merged shihai1991, 2020-05-07 16:01
PR 20024 merged scoder, 2020-05-10 14:47
PR 20074 merged vstinner, 2020-05-13 22:03
PR 20215 merged petr.viktorin, 2020-05-19 12:20
PR 20344 merged shihai1991, 2020-05-24 03:56
PR 21984 merged miss-islington, 2020-08-27 13:37
Messages (18)
msg356533 - (view) Author: Marcel Plch (Dormouse759) * Date: 2019-11-13 14:30
Currently, there is not way to access per-module state from the methods of types defined in extension modules.

This PEP provides a fix to this issue.

PEP 573 - https://www.python.org/dev/peps/pep-0573/

Reference implementation - https://github.com/Dormouse759/cpython/tree/pep-c-rebase_newer
msg367552 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2020-04-28 18:18
What can we do to move this forward? I see that the original PR was closed in January. Is there or will there be a new one? Can I help with anything?
msg367569 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-04-28 21:54
The PEP is listed as accepted, with you as the BDFL-delegate, but it lacks a link to the public acceptance message (usually on pydev list).

The PR was not rejected by a core dev but was closed without explanation by the author on the same day the last commits were added.  Perhaps OP just gave up getting tests to pass.  But they disappear on closes.  

However, the contribution had been made under the CLA and you or Petr or any other core dev should be able to download it to a local branch, try to whip it into shape, and make a new PR before beta 1, in about a month.  Or start from scratch.

I cannot test downloading at the moment.  Apparently, OP's fork branch has been altered, so that merely reopening is not an option.  The OP used force-pushes rather than update merges, and this often creates problems.
msg367636 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2020-04-29 09:39
I'm working on the implementation as my time allows, here: https://github.com/encukou/cpython/pull/3 .

Would it help to create a proper PR for CPython now? That would mean I could no longer do rebases/force-pushes, so the commits would be less readable. But happy to do it if it would help you.
msg368180 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-05 16:57
See also bpo-40137: TODO list when PEP 573 "Module State Access from C Extension Methods" will be implemented.
msg368337 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-07 13:40
New changeset e1becf46b4e3ba6d7d32ebf4bbd3e0804766a423 by Petr Viktorin in branch 'master':
bpo-38787: C API for module state access from extension methods (PEP 573) (GH-19936)
https://github.com/python/cpython/commit/e1becf46b4e3ba6d7d32ebf4bbd3e0804766a423
msg368356 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-07 17:16
New changeset c068b53a0ca6ebf740d98e422569d2f705e54f93 by Hai Shi in branch 'master':
bpo-38787: Update structures.rst docs (PEP 573) (GH-19980)
https://github.com/python/cpython/commit/c068b53a0ca6ebf740d98e422569d2f705e54f93
msg368577 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-10 09:38
See also bpo-40574: segfault causing regression from PEP 573 implementation (PyQt5).
msg368580 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-10 09:55
The posix/nt and _abc modules now use the PEP 573 APIs:

* posix/nt: commit 1c2fa781560608aa4be50c748d4b3f403cfa5035 (bpo-40549)
* _abc: commit 77c614624b6bf2145bef69830d0f499d8b55ec0c (bpo-40566)
msg368733 - (view) Author: miss-islington (miss-islington) Date: 2020-05-12 14:12
New changeset 4c9ea093cd752a6687864674d34250653653f743 by scoder in branch 'master':
bpo-38787: Add PyCFunction_CheckExact() macro for exact type checks (GH-20024)
https://github.com/python/cpython/commit/4c9ea093cd752a6687864674d34250653653f743
msg368735 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2020-05-12 15:36
In PR19982, petr have mentioned that `PyType_GetModule` can not worked if the type is the subtype.

I try to use pep573 in functools, I get the error info from testcases:
======================================================================
ERROR: test_arg_combinations (test.test_functools.TestPartialCSubclass)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/temp/shihai/cpython/Lib/test/test_functools.py", line 109, in test_arg_combinations
    p = self.partial(capture)
TypeError: PyType_GetModule: Type 'CPartialSubclass' has no associated module

======================================================================
ERROR: test_attributes (test.test_functools.TestPartialCSubclass)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/temp/shihai/cpython/Lib/test/test_functools.py", line 71, in test_attributes
    p = self.partial(capture, 1, 2, a=10, b=20)
TypeError: PyType_GetModule: Type 'CPartialSubclass' has no associated module
msg368768 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2020-05-13 08:41
I guess I'll need to clarify the documentation here.
Call `PyType_GetModule` on the *defining class*, not on type(self). See the PEP for details.
The associated module is not inherited; each class in the MRO can be defined in a different module.
msg368789 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2020-05-13 17:24
> I guess I'll need to clarify the documentation here.
> Call `PyType_GetModule` on the *defining class*, not on type(self).

+1, I didn't notice this detail until I got the error.

> The associated module is not inherited; each class in the MRO can be > defined in a different module.

Thanks, pep have enough info, I need spend sometime to make sure I understand it clearly, Lol~.
msg368804 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-13 22:31
New changeset 97e1568325e4d8eff2fc80eeb174b3f3e5d1c350 by Victor Stinner in branch 'master':
bpo-38787: Fix Argument Clinic defining_class_converter (GH-20074)
https://github.com/python/cpython/commit/97e1568325e4d8eff2fc80eeb174b3f3e5d1c350
msg375991 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2020-08-27 13:37
New changeset d9a966ae08258da2ce2a432c943d8194760f09c4 by Petr Viktorin in branch 'master':
bpo-38787: Clarify docs for PyType_GetModule and warn against common mistake (GH-20215)
https://github.com/python/cpython/commit/d9a966ae08258da2ce2a432c943d8194760f09c4
msg376411 - (view) Author: miss-islington (miss-islington) Date: 2020-09-04 22:32
New changeset fbb9ee0a903fb9b7b4b807f85aed1de754da09e6 by Miss Islington (bot) in branch '3.9':
[3.9] bpo-38787: Clarify docs for PyType_GetModule and warn against common mistake (GH-20215) (GH-21984)
https://github.com/python/cpython/commit/fbb9ee0a903fb9b7b4b807f85aed1de754da09e6
msg379107 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2020-10-20 08:56
Thanks Victor, Hai Shi, Pablo, Stefan, Dong-hee Na and Mariatta for reviews!

I did not put all the details in the documentation because there are still feature gaps. So, a full write-up is now in the informational PEP 630.
msg381853 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-11-25 17:08
FYI Cython doesn't support this PEP yet: https://github.com/cython/cython/issues/3917
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 82968
2020-11-25 17:08:25vstinnersetmessages: + msg381853
2020-10-20 08:56:18petr.viktorinsetstatus: open -> closed
resolution: fixed
messages: + msg379107

stage: patch review -> resolved
2020-09-04 22:32:32miss-islingtonsetmessages: + msg376411
2020-08-27 13:37:13miss-islingtonsetpull_requests: + pull_request21093
2020-08-27 13:37:05petr.viktorinsetmessages: + msg375991
2020-05-24 03:56:42shihai1991setpull_requests: + pull_request19609
2020-05-19 12:20:49petr.viktorinsetpull_requests: + pull_request19510
2020-05-13 22:31:38vstinnersetmessages: + msg368804
2020-05-13 22:03:44vstinnersetpull_requests: + pull_request19380
2020-05-13 17:24:05shihai1991setmessages: + msg368789
2020-05-13 08:41:11petr.viktorinsetmessages: + msg368768
2020-05-12 15:36:41shihai1991setmessages: + msg368735
2020-05-12 14:12:45miss-islingtonsetnosy: + miss-islington
messages: + msg368733
2020-05-10 14:47:20scodersetpull_requests: + pull_request19334
2020-05-10 09:55:40vstinnersetmessages: + msg368580
2020-05-10 09:38:27vstinnersetmessages: + msg368577
2020-05-07 17:16:08vstinnersetmessages: + msg368356
2020-05-07 16:01:16shihai1991setpull_requests: + pull_request19296
2020-05-07 13:40:06vstinnersetmessages: + msg368337
2020-05-06 15:34:35shihai1991setnosy: + shihai1991
2020-05-05 16:57:43vstinnersetnosy: + vstinner
messages: + msg368180
2020-05-05 16:47:32petr.viktorinsetpull_requests: + pull_request19251
2020-04-29 09:39:14petr.viktorinsetmessages: + msg367636
2020-04-28 21:54:40terry.reedysetmessages: + msg367569
2020-04-28 18:18:30scodersetmessages: + msg367552
2019-11-13 14:34:10Dormouse759setkeywords: + patch
stage: patch review
pull_requests: + pull_request16655
2019-11-13 14:30:40Dormouse759create