8000 hide impl detail · adamtheturtle/pythoncapi@67b84f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 67b84f6

Browse files
committed
hide impl detail
1 parent 27eb15a commit 67b84f6

File tree

3 files changed

+91
-17
lines changed

3 files changed

+91
-17
lines changed

doc/new_api.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ Design goals
1111
to be able to use the new C API on the maximum number of existing C
1212
extensions which use directly the C API.
1313
* Hide most CPython implementation details. The exact list has to be written.
14+
One required change is to replace macros with functions calls. The option
15+
question remains if it will be possible to replace ``Py_INCREF()`` and
16+
``Py_DECREF()`` with function calls without killing performances.
1417
* Reduce the size of the C API to reduce the maintenance burden of :ref:`Python
1518
implementations other than CPython <other-python-impl>`. See :ref:`Remove
1619
functions <remove-funcs>`.
20+
* Reduce the size of the ABI, especially export less symbols.
21+
22+
The :ref:`backward compatibility <back-compat>` issue is partially solved by
23+
keeping the existing :ref:`old C API <c-api>` available as an opt-in option:
24+
see the :ref:`Regular runtime <regular-runtime>`.
1725

1826
Non-goal
1927
========
@@ -22,3 +30,61 @@ Non-goal
2230
intent to replace Cython. Moreover, there is no intent to make Cython
2331
development harder. Cython will still be able to access directly the full C
2432
API which includes implementation details and low-level "private" APIs.
33+
34+
Hide implementation details
35+
===========================
36+
37+
What are implementation details?
38+
--------------------------------
39+
40+
"Implementation details" is not well specified at this point, but maybe hiding
41+
implementation can be done incrementally.
42+
43+
The PEP 384 "Defining a Stable ABI" is a very good stable to find the borders
44+
between the public C API and implementation details: see :ref:`Stable ABI
45+
<stable-abi>`.
46+
47+
Replace macros with function calls
48+
----------------------------------
49+
50+
Replacing macros with functions calls is one part of the practical solution.
51+
For example::
52+
53+
#define PyList_GET_ITEM(op, i) ((PyListObject *)op)->ob_item[i]
54+
55+
would become::
56+
57+
#define PyList_GET_ITEM(op, i) PyList_GetItem(op, i)
58+
59+
or maybe even::
60+
61+
PyObject* PyList_GET_ITEM(PyObjcet *op, PyObject *i) { return PyList_GetItem(op, i); }
62+
63+
Adding a **new** ``PyList_GET_ITEM()`` **function** would make the ABI larger,
64+
whereas the ABI should become smaller.
65+
66+
This change remains backward compatible in term of **C API**. Moreover, using
67+
function calls helps to make C extension backward compatible at the **ABI
68+
level** as well.
69+
70+
.. _incref:
71+
72+
Py_INCREF()
73+
-----------
74+
75+
The open question remains if it will be possible to replace ``Py_INCREF()`` and
76+
``Py_DECREF()`` with function calls without killing performances.
77+
78+
Hide C structures
79+
-----------------
80+
81+
The most backward incompatible change is to hide fields of C structures, up to
82+
PyObject. To final goal will be able to hide ``PyObject.ob_refcnt`` from the
83+
public C API.
84+
85+
C extensions must be modified to use functions to access fields.
86+
87+
In the worst case, there will be no way to access to hidden field from the
88+
public C API. For these users, the only option will be to stick at the
89+
:ref:`old C API <c-api>` which remains backward compatible and still expose
90+
implementation details like C structure fields.

doc/roadmap.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ Roadmap
77

88
* Step 1: Identify :ref:`Bad C API <bad-api>` and list functions that should
99
be modified or even removed
10-
* Step 2: Add an **opt-in** new C API with these cleanups. Test :ref:`popular
10+
* Step 2: Add an **opt-in** :ref:`new C API <new-c-api>` with these cleanups. Test :ref:`popular
1111
C extensions <consumers>` to measure how much code is broken. Start to fix
12-
these C extensions. Slowly involve more and more players into the game.
13-
* Step 3: Remove more functions. Maybe replace Py_INCREF() macro with a
14-
function call. Finish to all PyObject structure. Measure the performance.
12+
these C extensions by making them **forward** compatible. Slowly involve more
13+
and more players into the game.
14+
* Step 3: :ref:`Remove more functions <remove-funcs>`. Maybe replace
15+
:ref:`Py_INCREF() macro <incref>` with a function call. Finish to hide all C
16+
structures especially ``PyObject.ob_refcnt``. Measure the performance.
1517
Decide what to do.
1618
* Step 4: if step 3 gone fine and most people are still ok to continue, make
17-
the new C API as the default in CPython and add an option for **opt-out**.
19+
the :ref:`new C API <new-c-api>` as the default in CPython and add an option
20+
for **opt-out** to stick with the :ref:`old C API <c-api>`.
1821

1922
Status
2023
======

doc/runtimes.rst

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ experimental new changes but requires using the newer C API.
1212
**Welcome into the bright world of multiple new cool and compatible Python
1313
runtimes!**
1414

15+
.. _regular-runtime:
16+
1517
Regular Python: /usr/bin/python3
1618
================================
1719

1820
* Python compiled in release mode
1921
* This runtime still provides ``Py_INCREF()`` macro:
2022
modify ``PyObject.ob_refcnt`` at the ABI level.
21-
* Compatible with Python 3.7 C **API**.
22-
* Should be compatible with Python 3.7 **ABI**.
23+
* Should be fully compatible with :ref:`Python 3.7 C API <c-api>`
24+
* Should be fully compatible with Python 3.7 stable **ABI** (it may become
25+
incompatible with the Python 3.7 full ABI).
2326

2427
Compared to Python 3.7 regular runtime, this runtime no longer check its
25-
arguments. The debug runtime should now be preferred to develop C extensions
26-
and to run tests.
28+
arguments for performance reasons. The debug runtime should now be preferred to
29+
develop C extensions and to run tests.
2730

2831
Example of Python 3.7 code::
2932

@@ -61,6 +64,8 @@ Debug runtime: /usr/bin/python3-dbg
6164
function arguments are stored in registers, and so gdb fails with the
6265
"<optimized out>" message.
6366

67+
For example, the debug runtime can check that the GIL is held by the caller.
68+
6469
.. _exp-runtime:
6570

6671
New experimental runtime: python3-exp
@@ -79,7 +84,7 @@ New experimental runtime: python3-exp
7984
* ``Py_GC`` header and ``PyObject`` structure can be very different from the
8085
one used by the regular and debug runtimes.
8186

82-
Technically, this experimental runtime is likely to be a opt-in compilation
87+
Technically, this experimental runtime can be a opt-in compilation
8388
mode of the upstream CPython code base.
8489

8590
See :ref:`Optimization ideas <optim-ideas>`.
@@ -90,19 +95,19 @@ See :ref:`Optimization ideas <optim-ideas>`.
9095
PyPy, RustPython and others
9196
===========================
9297

93-
Since the `C API will be smaller <new-c-api>` and the `stable ABI will become
94-
more usable <stable-abi>`, you can imagine that Python implementations other
95-
than CPython will be able to more easily have a **full and up-to-date support**
96-
of the latest full C API.
98+
Since the :ref:`C API will be smaller <new-c-api>` and the :ref:`stable ABI
99+
will become more usable <stable-abi>`, you can imagine that Python
100+
implementations other than CPython will be able to more easily have a **full
101+
and up-to-date support** of the latest full C API.
97102

98103

99104
Put your CPython fork here!
100105
===========================
101106

102107
Since a :ref:`stable ABI <stable-abi>` have been designed, if all your C
103-
extensions have opt-in for the new C API: you are now allowed to fork CPython
104-
and experiment your own flavor CPython. Do whatever you want: C extensions only
105-
access your runtime through runtime.
108+
extensions have opt-in for the :ref:`new C API <new-c-api>`: you are now
109+
allowed to fork CPython and experiment your own flavor CPython. Do whatever you
110+
want: C extensions only calls your runtime through function calls.
106111

107112
See :ref:`Optimization ideas <optim-ideas>`.
108113

0 commit comments

Comments
 (0)
0