@@ -6,11 +6,17 @@ Python C API compatibility
66 :alt: Build status of pyperf on Travis CI
77 :target: https://travis-ci.com/github/pythoncapi/pythoncapi_compat
88
9- Header file providing new functions of the Python C API to Python 3.6.
9+ The Python C API compatibility project is made of two parts:
1010
11- Python 3.6 to Python 3.10 are supported. It requires a subset of C99 like
12- ``static inline `` functions:
13- see `PEP 7 <https://www.python.org/dev/peps/pep-0007/ >`_.
11+ * ``pythoncapi_compat.h ``: Header file providing new functions of the Python C
12+ API to Python 3.6.
13+ * ``upgrade_pythoncapi.py ``: Script upgrading C extension modules to newer
14+ Python API without losing support for Python 3.6. It relies on
15+ ``pythoncapi_compat.h ``.
16+
17+ Python 3.6 to Python 3.10 are supported. A subset of C99 is required, like
18+ ``static inline `` functions: see `PEP 7
19+ <https://www.python.org/dev/peps/pep-0007/> `_.
1420
1521Homepage:
1622https://github.com/pythoncapi/pythoncapi_compat
@@ -27,13 +33,114 @@ This project is covered by the `PSF Code of Conduct
2733Usage
2834=====
2935
30- Copy the header file in your project and include it using::
36+ Run upgrade_pythoncapi.py
37+ -------------------------
3138
32- #include "pythoncapi_compat.h"
39+ Upgrade `` mod.c `` file::
3340
41+ python3 upgrade_pythoncapi.py mod.c
3442
35- Functions
36- =========
43+ Upgrade all ``.c `` and ``.h `` files of a project::
44+
45+ python3 upgrade_pythoncapi.py directory/
46+
47+ WARNING: files are modified in-place! If a file is modified, the original file
48+ is saved as ``<filename>.old ``.
49+
50+ To see command line options and list available operations, run it with no
51+ arguments::
52+
53+ python3 upgrade_pythoncapi.py
54+
55+ For example, to only replace ``op->ob_type `` with ``Py_TYPE(op) ``, use::
56+
57+ python3 upgrade_pythoncapi.py -o Py_TYPE mod.c
58+
59+ Or the opposite, to apply all operations but leave ``op->ob_type `` unchanged,
60+ use::
61+
62+ python3 upgrade_pythoncapi.py -o all,-Py_TYPE mod.c
63+
64+ Copy pythoncapi_compat.h
65+ ------------------------
66+
67+ Most upgrade_pythoncapi.py operations add ``#include "pythoncapi_compat.h" ``.
68+ You may have to copy the ``pythoncapi_compat.h `` header file to your project.
69+ It can be copied from::
70+
71+ https://raw.githubusercontent.com/pythoncapi/pythoncapi_compat/master/pythoncapi_compat.h
72+
73+
74+ Upgrade Operations
75+ ==================
76+
77+ ``upgrade_pythoncapi.py `` implements the following operations:
78+
79+ * ``Py_TYPE ``:
80+
81+ * Replace ``op->ob_type `` with ``Py_TYPE(op) ``.
82+
83+ * ``Py_SIZE ``:
84+
85+ * Replace ``op->ob_size `` with ``Py_SIZE(op) ``.
86+
87+ * ``Py_REFCNT ``:
88+
89+ * Replace ``op->ob_refcnt `` with ``Py_REFCNT(op) ``.
90+
91+ * ``Py_SET_TYPE ``:
92+
93+ * Replace ``obj->ob_type = type; `` with ``Py_SET_TYPE(obj, type); ``.
94+ * Replace ``Py_TYPE(obj) = type; `` with ``Py_SET_TYPE(obj, type); ``.
95+
96+ * ``Py_SET_SIZE ``:
97+
98+ * Replace ``obj->ob_size = size; `` with ``Py_SET_SIZE(obj, size); ``.
99+ * Replace ``Py_SIZE(obj) = size; `` with ``Py_SET_SIZE(obj, size); ``.
100+
101+ * ``Py_SET_REFCNT ``:
102+
103+ * Replace ``obj->ob_refcnt = refcnt; `` with ``Py_SET_REFCNT(obj, refcnt); ``.
104+ * Replace ``Py_REFCNT(obj) = refcnt; `` with ``Py_SET_REFCNT(obj, refcnt); ``.
105+
106+ * ``PyObject_NEW ``:
107+
108+ * Replace ``PyObject_NEW(...) `` with ``PyObject_New(...) ``.
109+ * Replace ``PyObject_NEW_VAR(...) `` with ``PyObject_NewVar(...) ``.
110+
111+ * ``PyMem_MALLOC ``:
112+
113+ * Replace ``PyMem_MALLOC(n) `` with ``PyMem_Malloc(n) ``.
114+ * Replace ``PyMem_REALLOC(ptr, n) `` with ``PyMem_Realloc(ptr, n) ``.
115+ * Replace ``PyMem_FREE(ptr) ``, ``PyMem_DEL(ptr) `` and ``PyMem_Del(ptr) `` .
116+ with ``PyMem_Free(n) ``.
117+
118+ * ``PyObject_MALLOC ``:
119+
120+ * Replace ``PyObject_MALLOC(n) `` with ``PyObject_Malloc(n) ``.
121+ * Replace ``PyObject_REALLOC(ptr, n) `` with ``PyObject_Realloc(ptr, n) ``.
122+ * Replace ``PyObject_FREE(ptr) ``, ``PyObject_DEL(ptr) ``
123+ and ``PyObject_Del(ptr) `` . with ``PyObject_Free(n) ``.
124+
125+ * ``PyFrame_GetBack ``:
126+
127+ * Replace ``frame->f_back `` with ``_PyFrame_GetBackBorrow(frame) ``.
128+
129+ * ``PyFrame_GetCode ``:
130+
131+ * Replace ``frame->f_code `` with ``_PyFrame_GetCodeBorrow(frame) ``.
132+
133+ * ``PyThreadState_GetInterpreter ``:
134+
135+ * Replace ``tstate->interp `` with ``PyThreadState_GetInterpreter(tstate) ``.
136+
137+ * ``PyThreadState_GetFrame ``:
138+
139+ * Replace ``tstate->frame `` with ``_PyThreadState_GetFrameBorrow(tstate) ``.
140+
141+
142+ pythoncapi_compat.h functions
143+ =============================
37144
38145Borrow variant
39146--------------
@@ -105,16 +212,38 @@ Run tests
105212
106213Run the command::
107214
108- python3 tests/test_matrix.py
109- # add -v option for verbose mode
215+ python3 runtests.py
216+
217+ Verbose mode::
110218
111- To test one specific Python executable::
219+ python3 runtests.py -v
112220
113- python3.6 tests/run_tests.py
114- # add -v option for verbose mode
221+ See tests in the ``tests/ `` subdirectory.
222+
223+
224+ Links
225+ =====
115226
227+ * `pythoncapi_compat.h header
228+ <https://github.com/pythoncapi/pythoncapi_compat> `_:
229+ Header file providing new functions of the Python C API for old Python
230+ versions.
231+ * `Py_SET_TYPE() function documentation
232+ <https://docs.python.org/dev/c-api/structures.html#c.Py_SET_TYPE> `_
233+ (Python 3.9)
234+ * `Py_SET_SIZE() function documentation
235+ <https://docs.python.org/dev/c-api/structures.html#c.Py_SET_SIZE> `_
236+ (Python 3.9)
237+ * `Py_SET_REFCNT() function documentation
238+ <https://docs.python.org/dev/c-api/structures.html#c.Py_SET_REFCNT> `_
239+ (Python 3.9)
240+ * `bpo-39573: [C API] Make PyObject an opaque structure in the limited C API
241+ <https://bugs.python.org/issue39573> `_
242+ * `PEP 620 -- Hide implementation details from the C API
243+ <https://www.python.org/dev/peps/pep-0620/> `_
116244
117245Changelog
118246=========
119247
120- * 2020-06-04: Creation of the project.
248+ * 2020-11-30: Creation of the upgrade_pythoncapi.py script.
249+ * 2020-06-04: Creation of the pythoncapi_compat.h header file.
0 commit comments