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

Add support for the limited C API to Argument Clinic #108494

Closed
vstinner opened this issue Aug 25, 2023 · 8 comments
Closed

Add support for the limited C API to Argument Clinic #108494

vstinner opened this issue Aug 25, 2023 · 8 comments
Labels
topic-argument-clinic type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

vstinner commented Aug 25, 2023

Feature or enhancement

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

Proposal:

I propose adding support for the limited C API to Argument Clinic. Since Argument Clinic supports a wide diversity of parameter types and object types, I propose to add the support incrementally (step by step).

The final goal is to restrict some stdlib C extensions to the limited C API.

Linked PRs

@vstinner vstinner added the type-feature A feature request or enhancement label Aug 25, 2023
vstinner added a commit to vstinner/cpython that referenced this issue Aug 25, 2023
Argument Clinic now has a partial support of the
Limited API:

* Add --limited option to clinic.c.
* Add '_testclinic_limited' extension which is built with
  the limited C API version 3.13.
* For now, hardcode in clinic.py that "_testclinic_limited.c" targets
  the limited C API.
vstinner added a commit that referenced this issue Aug 25, 2023
Argument Clinic now has a partial support of the
Limited API:

* Add --limited option to clinic.c.
* Add '_testclinic_limited' extension which is built with
  the limited C API version 3.13.
* For now, hardcode in clinic.py that "_testclinic_limited.c" targets
  the limited C API.
@vstinner
Copy link
Member Author

My first change adds this code to build _testclinic_limited.c with the limited C API:

    # XXX Temporary solution
    if os.path.basename(filename) == '_testclinic_limited.c':
        print(f"{filename} uses limited C API")
        limited_capi = True

Maybe we could add a marker using Argument Clinic syntax to announce "hey, I want the limited C API"!

Or AC could parse the C code and look for #define Py_LIMITED_API?

vstinner added a commit to vstinner/cpython that referenced this issue Aug 25, 2023
AC now checks for "#define Py_LIMITED_API" pattern to use the limited
C API.
vstinner added a commit to vstinner/cpython that referenced this issue Aug 25, 2023
AC now checks for "#define Py_LIMITED_API" pattern to use the limited
C API.
@vstinner
Copy link
Member Author

My change added support for limited C API version 3.13. If we consider supporting older Python versions, this issue should be carefully reviewed: #71686 "PY_SSIZE_T_CLEAN conflicts with Py_LIMITED_API". Maybe it has been fixed in the meanwhile, I'm not sure. The goal here would be to be able build a C extension with Python 3.13 which works on Python 3.2 to 3.12.

@vstinner
Copy link
Member Author

Something else to consider, @shihai1991 wrote:

METH_FASTCALL is the part of the stable ABI in #23009.

I don't in which Python version it was usable as part of the stable ABI. My current implementation is limited to METH_VARARGS and METH_VARARGS | METH_KEYWORDS to focus on backward compatibility first.

@vstinner
Copy link
Member Author

Well, see issue #85283 for the interesting discussion in 2020-2021.

vstinner added a commit that referenced this issue Aug 25, 2023
AC now checks for "#define Py_LIMITED_API" pattern to use the limited
C API.
@vstinner
Copy link
Member Author

Or AC could parse the C code and look for #define Py_LIMITED_API?

I implemented that in my second PR, see LIMITED_CAPI_REGEX.

vstinner added a commit to vstinner/cpython that referenced this issue Aug 26, 2023
Revert my change adding 'ns' parameter, add back 'verify' parameter,
and add also 'limited_capi' parameter.
vstinner added a commit to vstinner/cpython that referenced this issue Aug 26, 2023
Revert my change adding 'ns' parameter, add back 'verify' parameter,
and add also 'limited_capi' parameter.
vstinner added a commit to vstinner/cpython that referenced this issue Aug 26, 2023
Fix the limited C API code path in Argument Clinic to parse
"positional or keywords" arguments.

Add an unit test in _testclinic_limited.
vstinner added a commit to vstinner/cpython that referenced this issue Aug 26, 2023
Fix the limited C API code path in Argument Clinic to parse
"positional or keywords" arguments.

Add an unit test in _testclinic_limited.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 27, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 27, 2023
vstinner added a commit to vstinner/cpython that referenced this issue Aug 28, 2023
Revert my change adding 'ns' parameter, add back 'verify' parameter,
and add also 'limited_capi' parameter.
vstinner added a commit to vstinner/cpython that referenced this issue Aug 28, 2023
Use PyTuple_Size() instead of PyTuple_GET_SIZE().
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Aug 28, 2023
@vstinner
Copy link
Member Author

When I try to convert the _bisect module to the limited C API, Argument Clinic generates code using _Py_convert_optional_to_ssize_t() which is not part of the limited C API for Py_ssize_t parameters (ex: _bisect.bisect_right()) :-(

vstinner added a commit to vstinner/cpython that referenced this issue Aug 29, 2023
vstinner added a commit that referenced this issue Aug 30, 2023
…he limited C API (#108584)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
carljm added a commit to carljm/cpython that referenced this issue Aug 30, 2023
* main:
  pythongh-108520: Fix bad fork detection in nested multiprocessing use case (python#108568)
  pythongh-108590: Revert pythongh-108657 (commit 400a1ce) (python#108686)
  pythongh-108494: Argument Clinic: Document how to generate code that uses the limited C API (python#108584)
  Document Python build requirements (python#108646)
  pythongh-101100: Fix Sphinx warnings in the Logging Cookbook (python#108678)
  Fix typo in multiprocessing docs (python#108666)
  pythongh-108669: unittest: Fix documentation for TestResult.collectedDurations (python#108670)
  pythongh-108590: Fix sqlite3.iterdump for invalid Unicode in TEXT columns (python#108657)
  Revert "pythongh-103224: Use the realpath of the Python executable in `test_venv` (pythonGH-103243)" (pythonGH-108667)
  pythongh-106320: Remove private _Py_ForgetReference() (python#108664)
  Mention Ellipsis pickling in the docs (python#103660)
  Revert "Use non alternate name for Kyiv (pythonGH-108533)" (pythonGH-108649)
  pythongh-108278: Deprecate passing the first param of sqlite3.Connection callback APIs by keyword (python#108632)
  pythongh-108455: peg_generator: install two stubs packages before running mypy (python#108637)
  pythongh-107801: Improve the accuracy of io.IOBase.seek docs (python#108268)
serhiy-storchaka added a commit that referenced this issue Sep 3, 2023
AA-Turner pushed a commit to AA-Turner/devguide that referenced this issue Sep 26, 2023
… that uses the limited C API (python/cpython#108584)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit to python/devguide that referenced this issue Oct 1, 2023
… that uses the limited C API (python/cpython#108584)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
vstinner added a commit that referenced this issue Oct 2, 2023
Add _testclinic_limited to Tools/msi/test/test_files.wxs.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 2, 2023
…ythonGH-110077)

Add _testclinic_limited to Tools/msi/test/test_files.wxs.
(cherry picked from commit 6387b53)

Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit to vstinner/cpython that referenced this issue Oct 2, 2023
vstinner added a commit to miss-islington/cpython that referenced this issue Oct 2, 2023
…ythonGH-110077)

(cherry picked from commit 6387b53)

Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this issue Oct 2, 2023
…#110077) (#110232)

gh-108494: Document how to add a project in PCbuild/readme.txt (#110077)

(cherry picked from commit 6387b53)
vstinner added a commit that referenced this issue Oct 2, 2023
…GH-110077) (#110230)

gh-108494: Document how to add a project in PCbuild/readme.txt (GH-110077)

(cherry picked from commit 6387b53)

Co-authored-by: Victor Stinner <vstinner@python.org>
@vstinner
Copy link
Member Author

Thanks to @serhiy-storchaka, Argument Clinic now supports all formats for the limited C API. I'm sure that we will find code which doesn't work and room for improvement. It can be done later. I close the issue. Thanks to everybody who helped me to make it possible :-)

@vstinner
Copy link
Member Author

vstinner commented Oct 17, 2023

Follow-up: PR #110964 "[C API] Move undocumented private _PyArg C API to pycore_modsupport.h internal C API".

vstinner added a commit to vstinner/cpython that referenced this issue Mar 11, 2024
Accept spaces in "#  define Py_LIMITED_API 0x030d0000".
vstinner added a commit to vstinner/cpython that referenced this issue Mar 11, 2024
Accept spaces in "#  define Py_LIMITED_API 0x030d0000".
vstinner added a commit to vstinner/cpython that referenced this issue Mar 11, 2024
Accept spaces in "#  define Py_LIMITED_API 0x030d0000".
vstinner added a commit that referenced this issue Mar 11, 2024
Accept spaces in "#  define Py_LIMITED_API 0x030d0000".
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
Accept spaces in "#  define Py_LIMITED_API 0x030d0000".
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
Accept spaces in "#  define Py_LIMITED_API 0x030d0000".
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-argument-clinic type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants