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 2, 2023

Does any of the other removed API have a similar issue?

I am not aware of any PEP asking to keep private names prefixed by an underscore.

@gvanrossum
Copy link
Member

We can talk till we’re blue in the face but please no more action (i.e., no more moving/removing APIs) until the full WG has had a chance to discuss this and make a decision.

(Restoring removed APIs at users’ requests is fine.)

@encukou
Copy link
Member

encukou commented Nov 2, 2023

FWIW, some of the the user requests are now tracked here: #111481 (comment)

@victor
Copy link

victor commented Nov 2, 2023

@victor, you mentioned in python/steering-council#212 that you didn't know _PyObject_Vectorcall was specified in a PEP. (You must have known they were documented, though, since you removed the documentation.)

@encukou I didn’t do any such mention. Please be more careful with @-mentions

@vstinner

This comment was marked as resolved.

@encukou

This comment was marked as resolved.

@gpshead
Copy link
Member

gpshead commented Nov 6, 2023

Does any of the other removed API have a similar issue?

I am not aware of any PEP asking to keep private names prefixed by an underscore.

That wasn't what was being asked. The question was more: Which of the so far removed APIs were documented.

"private" in the context of breaking changes has nothing to do with the _ prefix (a mere convention) but to do with them being publicly accessible at all outside of our internals via a Python.h include. That means code may have used them regardless of our intent. Documentation is one indicator of intent.

@gvanrossum
Copy link
Member

Note there is a moratorium on removing APIs until the term “private” has been clarified by the C API WG.

@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 <stefan_ml@behnel.de>
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 <stefan_ml@behnel.de>
@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.
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