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

C API: Remove private C API functions (move them to the internal C API) #106320

Closed
vstinner opened this issue Jul 1, 2023 · 53 comments
Closed

C API: Remove private C API functions (move them to the internal C API) #106320

vstinner opened this issue Jul 1, 2023 · 53 comments

Comments

@vstinner
Copy link
Member

vstinner commented Jul 1, 2023

Over the years, we accumulated many private functions as part of the public C API header files. I propose to remove them: move them to the internal C API.

If many C extensions are affected by these removals, it's a sign that we should consider promoted used private functions as public functions: provide a better API, add error handling, write documentation, write tests.


Summary: My plan to clarify private vs public functions in Python 3.13


Linked PRs

vstinner added a commit to vstinner/cpython that referenced this issue Jul 1, 2023
Replace calls to the (removed) slow _PyInterpreterState_Get() with
fast inlined _PyInterpreterState_GET() function.
vstinner added a commit to vstinner/cpython that referenced this issue Jul 1, 2023
Replace calls to the (removed) slow _PyInterpreterState_Get() with
fast inlined _PyInterpreterState_GET() function.
vstinner added a commit to vstinner/cpython that referenced this issue Jul 1, 2023
Replace calls to the (removed) slow _PyInterpreterState_Get() with
fast inlined _PyInterpreterState_GET() function.
@vstinner
Copy link
Member Author

vstinner commented Jul 1, 2023

See also issue #106316: remove pytime.h header file (_PyTime functions).

@vstinner
Copy link
Member Author

vstinner commented Jul 1, 2023

See also issue #106084: C API: Remove private functions from abstract.h.

@vstinner
Copy link
Member Author

vstinner commented Jul 1, 2023

See also commit 0b51463:

Remove the following private functions of the C API:

* _PyCodecInfo_GetIncrementalDecoder()
* _PyCodecInfo_GetIncrementalEncoder()
* _PyCodec_DecodeText()
* _PyCodec_EncodeText()
* _PyCodec_Forget()
* _PyCodec_Lookup()
* _PyCodec_LookupTextEncoding()

vstinner added a commit to vstinner/cpython that referenced this issue Jul 1, 2023
Replace calls to the (removed) slow _PyInterpreterState_Get() with
fast inlined _PyInterpreterState_GET() function.
vstinner added a commit that referenced this issue Jul 1, 2023
Replace calls to the (removed) slow _PyInterpreterState_Get() with
fast inlined _PyInterpreterState_GET() function.
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
* Remove private _PyTracemalloc C API functions: move them to the
  internal C API.
* Rename Include/tracemalloc.h to Include/cpython/tracemalloc.h
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
* Remove private _PyTracemalloc C API functions: move them to the
  internal C API.
* Don't export private _PyTracemalloc functions anymore.
* Rename Include/tracemalloc.h to Include/cpython/tracemalloc.h
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
* Remove private _PyTraceMalloc C API functions: move them to the
  internal C API.
* Don't export most of these functions anymore, but still export
  _PyTraceMalloc_GetTraceback() used by tests.
* Rename Include/tracemalloc.h to Include/cpython/tracemalloc.h
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
vstinner added a commit that referenced this issue Jul 2, 2023
* Remove private _PyTraceMalloc C API functions: move them to the
  internal C API.
* Don't export most of these functions anymore, but still export
  _PyTraceMalloc_GetTraceback() used by tests.
* Rename Include/tracemalloc.h to Include/cpython/tracemalloc.h
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
vstinner added a commit that referenced this issue Jul 2, 2023
Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
Replace PyInterpreterState_Get() with inlined
_PyInterpreterState_GET().
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
Replace PyInterpreterState_Get() with inlined
_PyInterpreterState_GET().
vstinner added a commit that referenced this issue Jul 2, 2023
Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
Replace PyInterpreterState_Get() with inlined
_PyInterpreterState_GET().
vstinner added a commit that referenced this issue Jul 2, 2023
Replace PyInterpreterState_Get() with inlined
_PyInterpreterState_GET().
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
Add internal pycore_complexobject.h header file.

Move _Py_c_xxx() functions and _PyComplex_FormatAdvancedWriter()
function to this new header file.
vstinner added a commit to vstinner/cpython that referenced this issue Jul 2, 2023
…ng()

Replace private _PyUnicode_EqualToASCIIString() with public
PyUnicode_CompareWithASCIIString().
@vstinner
Copy link
Member Author

vstinner commented Nov 6, 2023

Which of the so far removed APIs were documented.

The only removed functions which were documented in Doc/c-api/ were removed by PR #106085 and PR #106273: the "vectorcall" aliases (doc removed by commit 80b3d8f).

No other removed API were documented in Doc/c-api/. There are a few private documented functions, such as _PyTuple_Resize() and _PyBytes_Resize():

allocation.rst:.. c:function:: PyObject* _PyObject_New(PyTypeObject *type)
allocation.rst:.. c:function:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
bytes.rst:.. c:function:: int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize)
complex.rst:.. c:function:: Py_complex _Py_c_sum(Py_complex left, Py_complex right)
complex.rst:.. c:function:: Py_complex _Py_c_diff(Py_complex left, Py_complex right)
complex.rst:.. c:function:: Py_complex _Py_c_neg(Py_complex num)
complex.rst:.. c:function:: Py_complex _Py_c_prod(Py_complex left, Py_complex right)
complex.rst:.. c:function:: Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)
complex.rst:.. c:function:: Py_complex _Py_c_pow(Py_complex num, Py_complex exp)
frame.rst:.. c:function:: PyObject* PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame);
frame.rst:.. c:function:: int PyUnstable_InterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame);
frame.rst:.. c:function:: int PyUnstable_InterpreterFrame_GetLine(struct _PyInterpreterFrame *frame);
init.rst:.. c:function:: _PyFrameEvalFunction _PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
init.rst:.. c:function:: void _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp, _PyFrameEvalFunction eval_frame)
init_config.rst:.. c:function:: PyStatus _Py_InitializeMain(void)
object.rst:.. c:function:: PyObject** _PyObject_GetDictPtr(PyObject *obj)
tuple.rst:.. c:function:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)

Stats on private functions documented with the .. c:function:: markup (it wasn't the case for the removed aliases, that I mentioned):

$ cd ~/python/main/Doc/c-api/; git grep 'function::.*_Py'|wc -l
17

$ cd ~/python/3.12/Doc/c-api/; git grep 'function::.*_Py'|wc -l
17

@vstinner
Copy link
Member Author

vstinner commented Nov 8, 2023

@gvanrossum
Copy link
Member

Yeah, just do it already. :-)

vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2023
Change the API of the internal _PyDict_Pop_KnownHash() function:
return an int.

Co-Authored-By: Stefan Behnel <[email protected]>
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2023
Change the API of the internal _PyDict_Pop_KnownHash() function
to return an int.

Co-Authored-By: Stefan Behnel <[email protected]>
@encukou
Copy link
Member

encukou commented Nov 13, 2023

The only removed functions which were documented in Doc/c-api/ were removed by PR #106085 and PR #106273: the "vectorcall" aliases (doc removed by commit 80b3d8f).

No other removed API were documented in Doc/c-api/.

Note that the documented functions _Py_c_sum, _Py_c_diff & others were removed (in #106339), despite what you said here.
I wonder if there are any more.

@vstinner
Copy link
Member Author

Oh I forgot the details about _Py_c_sum() functions, they are now discussion in gh-112019. I suggest to move the discussion there.

adorilson pushed a commit to adorilson/cpython that referenced this issue Dec 27, 2023
…#106335)

Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
adorilson pushed a commit to adorilson/cpython that referenced this issue Dec 29, 2023
…#106335)

Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
adorilson pushed a commit to adorilson/cpython that referenced this issue Jan 20, 2024
…#106335)

Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
…#111162)

* pythongh-106320: Re-add _PyLong_FromByteArray(), _PyLong_AsByteArray() and _PyLong_GCD() to the public header files since they are used by third-party packages and there is no efficient replacement.

See python#111140
See python#111139

* pythongh-111262: Re-add _PyDict_Pop() to have a C-API until a new public one is designed.
lachbr pushed a commit to toontownretro/panda that referenced this issue Feb 17, 2024
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
…#106335)

Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 27, 2024
…#106335)

Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
adorilson pushed a commit to adorilson/cpython that referenced this issue Jun 26, 2024
…#106335)

Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
arthurzam added a commit to arthurzam/py-tree-sitter that referenced this issue Aug 28, 2024
The `_PyErr_FormatFromCause` function is a private function, which was
never documented. While it existed for a long time, it was removed in
Python 3.13 [1].

Use instead the public function `PyErr_Format`, which is also part of
the stable API.

[1] python/cpython#106320

Signed-off-by: Arthur Zamarin <[email protected]>
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
…#111162)

* pythongh-106320: Re-add _PyLong_FromByteArray(), _PyLong_AsByteArray() and _PyLong_GCD() to the public header files since they are used by third-party packages and there is no efficient replacement.

See python#111140
See python#111139

* pythongh-111262: Re-add _PyDict_Pop() to have a C-API until a new public one is designed.
ludwigschwardt added a commit to ludwigschwardt/python-gnureadline that referenced this issue Oct 17, 2024
Python 3.13 moved some private C API functions to internal header files.
See python/cpython#106320 for a general discussion on this (glad to see
that gnureadline was at least listed as an affected PyPI package :-) ).

These private functions affect us:
  - _Py_SetLocaleFromEnv -> pycore_pylifecycle.h
    (see python/cpython#106400)
  - _PyArg_CheckPositional, _PyArg_BadArgument -> pycore_modsupport.h
    (see python/cpython#110964)

Since we can't include these anymore, patch the relevant declarations into
the module code. The alternative (add the internal headers to this package)
is much messier, as we would have to pull in most of those headers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants