Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2020-06-25.08:54:20.867>labels= ['extension-modules', 'expert-C-API', '3.11']
title='[C API] Convert a few stdlib extensions to the limited C API (PEP 384)'updated_at=<Date2022-02-17.16:21:09.570>user='https://github.com/vstinner'
Python stdlib has around 139 extension modules. Not all of them *need* to use internal C API. I'm interested to try to convert a bunch of them to the limited C API, as part of my work on the PEP-620. IMHO "Eating your own dog food" is a good practice to ensure that the limited C API is usable.
Currently, the stdlib has exactly one extension module using the limited C API: the xxlimited module built with Py_LIMITED_API macro defined as 0x03050000 in setup.py. By the way, maybe Py_LIMITED_API should be defined in xxlimited.c, rather than in setup.py.
xxlimited.c is not built if Python is built in debug mode. I'm not sure why.
The main limitation to use the limited C API for stdlib is Argument Clinic which attempts to always emit the most efficient code, like using the METH_FASTCALL calling convention and use private functions like _PyArg_CheckPositional() or "static _PyArg_Parser _parser".
Argument Clinic could be modified to have an option to only use C API of the limited C API. Cython is working on a similar option (restraint emitted code to the limited C API).
I already tried to convert stdlib extensions to the limited C API in bpo-39573. I found other issues:
PyTypeObject is opaque and so it's not possible to implement a deallocator function (tp_dealloc) which calls tp_free like: Py_TYPE(self)->tp_free((PyObject*)self);
Maybe we can do add new members for the 8 missing slots, especially bf_getbuffer and bf_releasebuffer?
commit f7c4e23 of bpo-40724 added Py_bf_getbuffer and Py_bf_releasebuffer slots to the C API, but these slots are still not available in the limited C API: see bpo-10181.
See also bpo-23903: "Generate PC/python3.def by scraping headers". Steve Dower suggests to convert macros to functions. I'm not sure which functions are still implemented as macro in the limited C API.
While this might make for an interesting validation experiment, I don't think the results should be checked in.
Code churn is to be avoided when there is no visible user benefit. It risks destabilizing code, introducing errors, and making the code less recognizable to the other developers who created and maintained it.
Some related changes in modules: posix, _hashopenssl, _ast (Python-ast.c), zlib, _struct, tkinter, _curses_panel.
commit b396663
Author: Eddie Elizondo <eelizondo@fb.com>
Date: Tue Nov 5 07:16:14 2019 -0800
bpo-35381 Remove all static state from posixmodule (GH-15892)
After bpo-9665, this moves the remaining types in posixmodule to be heap-allocated to make it compatible with PEP-384 as well as modifying all the type accessors to fully make the type opaque.
The original PR that got messed up a rebase: https://github.com/python/cpython/pull/10854. All the issues in that commit have now been addressed since https://github.com/python/cpython/pull/11661 got committed.
This change also removes any state from the data segment and onto the module state itself.
https://bugs.python.org/issue35381
Automerge-Triggered-By: @encukou
bpo-38142: Updated _hashopenssl.c to be PEP-384 compliant (bpo-16071)
* Updated _hashopenssl.c to be PEP-384 compliant
* Remove refleak test from test_hashlib. The updated type no longer accepts random arguments to __init__.
bpo-38113: Update the Python-ast.c generator to PEP-384 (gh-15957)
Summary: This mostly migrates Python-ast.c to PEP-384 and removes all statics from the whole file. This modifies the generator itself that generates the Python-ast.c. It leaves in the usage of _PyObject_LookupAttr even though it's not fully PEP-384 compatible (this could always be shimmed in by anyone who needs it).
bpo-38076: Make struct module PEP-384 compatible (bpo-15805)
* PEP-384 _struct
* More PEP-384 fixes for _struct
Summary: Add a couple of more fixes for `_struct` that were previously missed such as removing `tp_*` accessors and using `PyBytesWriter` instead of calling `PyBytes_FromStringAndSize` with `NULL`. Also added a test to confirm that `iter_unpack` type is still uninstantiable.
* 📜🤖 Added by blurb_it.
Activity
vstinner commented on Jun 25, 2020
Python stdlib has around 139 extension modules. Not all of them *need* to use internal C API. I'm interested to try to convert a bunch of them to the limited C API, as part of my work on the PEP-620. IMHO "Eating your own dog food" is a good practice to ensure that the limited C API is usable.
Currently, the stdlib has exactly one extension module using the limited C API: the xxlimited module built with Py_LIMITED_API macro defined as 0x03050000 in setup.py. By the way, maybe Py_LIMITED_API should be defined in xxlimited.c, rather than in setup.py.
xxlimited.c is not built if Python is built in debug mode. I'm not sure why.
The main limitation to use the limited C API for stdlib is Argument Clinic which attempts to always emit the most efficient code, like using the METH_FASTCALL calling convention and use private functions like _PyArg_CheckPositional() or "static _PyArg_Parser _parser".
Argument Clinic could be modified to have an option to only use C API of the limited C API. Cython is working on a similar option (restraint emitted code to the limited C API).
I already tried to convert stdlib extensions to the limited C API in bpo-39573. I found other issues:
https://bugs.python.org/issue39573#msg361514
vstinner commented on Jun 25, 2020
See also bpo-27499 "PY_SSIZE_T_CLEAN conflicts with Py_LIMITED_API".
vstinner commented on Jun 25, 2020
Other missing features of the limited C API:
e.g. because the entire buffer protocol is missing from it. Some bugs were
only fixed in Py3.9, time will tell if anything else is missing."
https://mail.python.org/archives/list/python-dev@python.org/message/SENQBEJCJ7NYC72ZZ7BGIEDDBTUOXLI4/
vstinner commented on Jun 25, 2020
PyType_FromSpec() doesn't support 11 slots:
https://docs.python.org/dev/c-api/type.html#c.PyType_Slot
But it is possible to define tp_weaklistoffset, tp_dictoffset and tp_vectorcall_offset via Py_tp_members slot:
https://docs.python.org/dev/c-api/structures.html#pymemberdef-offsets
Maybe we can do add new members for the 8 missing slots, especially bf_getbuffer and bf_releasebuffer?
commit f7c4e23 of bpo-40724 added Py_bf_getbuffer and Py_bf_releasebuffer slots to the C API, but these slots are still not available in the limited C API: see bpo-10181.
vstinner commented on Jun 25, 2020
See also bpo-39123: "PyThread_xxx() not available when using limited API".
vstinner commented on Jun 25, 2020
See also bpo-23903: "Generate PC/python3.def by scraping headers". Steve Dower suggests to convert macros to functions. I'm not sure which functions are still implemented as macro in the limited C API.
vstinner commented on Jun 25, 2020
See also bpo-16731: "xxlimited/xxmodule docstrings ambiguous".
vstinner commented on Jun 25, 2020
See also bpo-41073: "[C API] PyType_GetSlot() should accept static types".
vstinner commented on Jun 25, 2020
See also ./Tools/scripts/abitype.py script and bpo-10943: "abitype: Need better support to port C extension modules to the stable C API".
vstinner commented on Jun 25, 2020
See also bpo-40077: "Convert static types to PyType_FromSpec()".
vstinner commented on Jun 25, 2020
See also bpo-29086: "Document C API that is not part of the limited API".
rhettinger commented on Jun 25, 2020
While this might make for an interesting validation experiment, I don't think the results should be checked in.
Code churn is to be avoided when there is no visible user benefit. It risks destabilizing code, introducing errors, and making the code less recognizable to the other developers who created and maintained it.
vstinner commented on Sep 7, 2020
Some related changes in modules: posix, _hashopenssl, _ast (Python-ast.c), zlib, _struct, tkinter, _curses_panel.
commit b396663
Author: Eddie Elizondo <eelizondo@fb.com>
Date: Tue Nov 5 07:16:14 2019 -0800
commit df69e75
Author: Christian Heimes <christian@python.org>
Date: Wed Sep 25 23:03:30 2019 +0200
commit ac46eb4
Author: Dino Viehland <dinoviehland@fb.com>
Date: Wed Sep 11 10:16:34 2019 -0700
commit a1ffad0
Author: Dino Viehland <dinoviehland@gmail.com>
Date: Tue Sep 10 11:27:03 2019 +0100
commit 4f384af
Author: Dino Viehland <dinoviehland@gmail.com>
Date: Tue Sep 10 11:18:37 2019 +0100
commit d2217a8
Author: Andrew Svetlov <andrew.svetlov@gmail.com>
Date: Tue Oct 30 22:49:16 2012 +0200
commit bc07cb8
Author: Martin v. Löwis <martin@v.loewis.de>
Date: Thu Jun 14 16:01:23 2012 +0200
179 remaining items
pythongh-85283: Add PySys_AuditTuple() function (python#108965)
pythongh-85283: If Py_LIMITED_API is defined, undefine Py_BUILD_CORE (p…
pythongh-85283: _stat extension now uses the limited C API (python#11…
pythongh-85283: Build _testimportmultiple with limited C API (python#…
pythongh-85283: Add PyMem_RawMalloc() to the limited C API (python#10…
pythongh-85283: Build errno and _ctypes_test with limited C API (pyth…
pythongh-85283: Build md5 extension with limited C API (python#110967)
pythongh-85283: Fix Argument Clinic for md5 extension (python#110976)
pythongh-85283: Build winsound extension with limited C API (python#1…
pythongh-85283: Add PySys_Audit() to the limited C API (python#108571)
pythongh-85283: Fix _ctypes_test build on Windows in release mode (py…
pythongh-85283: Build resource extension with limited C API (python#1…
pythongh-85283: Build _scproxy extension with limited C API (python#1…
pythongh-85283: Build _uuid extension with limited C API (python#111010)
pythongh-85283: Build posixshmem extension with Limited C API (python…