| 1 |
++++++++++++++++++++++++++++ |
| 2 |
upgrade_pythoncapi.py script |
| 3 |
++++++++++++++++++++++++++++ |
| 4 |
|
| 5 |
``upgrade_pythoncapi.py`` requires Python 3.6 or newer. |
| 6 |
|
| 7 |
Usage |
| 8 |
===== |
| 9 |
|
| 10 |
Run the with no arguments to display command line options and list available |
| 11 |
operations:: |
| 12 |
|
| 13 |
python3 upgrade_pythoncapi.py |
| 14 |
|
| 15 |
Select files and directories |
| 16 |
---------------------------- |
| 17 |
|
| 18 |
To upgrade ``module.c`` file, type:: |
| 19 |
|
| 20 |
python3 upgrade_pythoncapi.py module.c |
| 21 |
|
| 22 |
To upgrade all C and C++ files (``.c``, ``.h``, ``.cc``, ``.cpp``, ``.cxx`` and |
| 23 |
``.hpp`` files) in the ``directory/`` directory, type:: |
| 24 |
|
| 25 |
python3 upgrade_pythoncapi.py directory/ |
| 26 |
|
| 27 |
Multiple filenames an directories can be specified on the command line. |
| 28 |
|
| 29 |
Files are modified in-place! If a file is modified, a copy of the original file |
| 30 |
is created with the ``.old`` suffix. |
| 31 |
|
| 32 |
Select operations |
| 33 |
----------------- |
| 34 |
|
| 35 |
To only replace ``op->ob_type`` with ``Py_TYPE(op)``, select the ``Py_TYPE`` |
| 36 |
operation with:: |
| 37 |
|
| 38 |
python3 upgrade_pythoncapi.py -o Py_TYPE module.c |
| 39 |
|
| 40 |
Or the opposite, to apply all operations but leave ``op->ob_type`` unchanged, |
| 41 |
deselect the ``Py_TYPE`` operation with:: |
| 42 |
|
| 43 |
python3 upgrade_pythoncapi.py -o all,-Py_TYPE module.c |
| 44 |
|
| 45 |
Download pythoncapi_compat.h |
| 46 |
---------------------------- |
| 47 |
|
| 48 |
Most ``upgrade_pythoncapi.py`` operations add ``#include |
| 49 |
"pythoncapi_compat.h"``. You may have to download the ``pythoncapi_compat.h`` |
| 50 |
header file to your project. It can be downloaded with:: |
| 51 |
|
| 52 |
python3 upgrade_pythoncapi.py --download PATH |
| 53 |
|
| 54 |
|
| 55 |
Upgrade Operations |
| 56 |
================== |
| 57 |
|
| 58 |
``upgrade_pythoncapi.py`` implements the following operations: |
| 59 |
|
| 60 |
Py_TYPE |
| 61 |
------- |
| 62 |
|
| 63 |
* Replace ``op->ob_type`` with ``Py_TYPE(op)``. |
| 64 |
|
| 65 |
Py_SIZE |
| 66 |
------- |
| 67 |
|
| 68 |
* Replace ``op->ob_size`` with ``Py_SIZE(op)``. |
| 69 |
|
| 70 |
Py_REFCNT |
| 71 |
--------- |
| 72 |
|
| 73 |
* Replace ``op->ob_refcnt`` with ``Py_REFCNT(op)``. |
| 74 |
|
| 75 |
Py_SET_TYPE |
| 76 |
----------- |
| 77 |
|
| 78 |
* Replace ``obj->ob_type = type;`` with ``Py_SET_TYPE(obj, type);``. |
| 79 |
* Replace ``Py_TYPE(obj) = type;`` with ``Py_SET_TYPE(obj, type);``. |
| 80 |
|
| 81 |
Py_SET_SIZE |
| 82 |
----------- |
| 83 |
|
| 84 |
* Replace ``obj->ob_size = size;`` with ``Py_SET_SIZE(obj, size);``. |
| 85 |
* Replace ``Py_SIZE(obj) = size;`` with ``Py_SET_SIZE(obj, size);``. |
| 86 |
|
| 87 |
Py_SET_REFCNT |
| 88 |
------------- |
| 89 |
|
| 90 |
* Replace ``obj->ob_refcnt = refcnt;`` with ``Py_SET_REFCNT(obj, refcnt);``. |
| 91 |
* Replace ``Py_REFCNT(obj) = refcnt;`` with ``Py_SET_REFCNT(obj, refcnt);``. |
| 92 |
|
| 93 |
Py_Is |
| 94 |
----- |
| 95 |
|
| 96 |
* Replace ``x == Py_None`` with ``Py_IsNone(x)``. |
| 97 |
* Replace ``x == Py_True`` with ``Py_IsTrue(x)``. |
| 98 |
* Replace ``x == Py_False`` with ``Py_IsFalse(x)``. |
| 99 |
* Replace ``x != Py_None`` with ``!Py_IsNone(x)``. |
| 100 |
* Replace ``x != Py_True`` with ``!Py_IsTrue(x)``. |
| 101 |
* Replace ``x != Py_False`` with ``!Py_IsFalse(x)``. |
| 102 |
|
| 103 |
PyObject_NEW |
| 104 |
------------ |
| 105 |
|
| 106 |
* Replace ``PyObject_NEW(...)`` with ``PyObject_New(...)``. |
| 107 |
* Replace ``PyObject_NEW_VAR(...)`` with ``PyObject_NewVar(...)``. |
| 108 |
|
| 109 |
PyMem_MALLOC |
| 110 |
------------ |
| 111 |
|
| 112 |
* Replace ``PyMem_MALLOC(n)`` with ``PyMem_Malloc(n)``. |
| 113 |
* Replace ``PyMem_REALLOC(ptr, n)`` with ``PyMem_Realloc(ptr, n)``. |
| 114 |
* Replace ``PyMem_FREE(ptr)``, ``PyMem_DEL(ptr)`` and ``PyMem_Del(ptr)`` . |
| 115 |
with ``PyMem_Free(n)``. |
| 116 |
|
| 117 |
PyObject_MALLOC |
| 118 |
--------------- |
| 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 |
|
| 128 |
* Replace ``frame->f_back`` with ``_PyFrame_GetBackBorrow(frame)``. |
| 129 |
|
| 130 |
PyFrame_GetCode |
| 131 |
--------------- |
| 132 |
|
| 133 |
* Replace ``frame->f_code`` with ``_PyFrame_GetCodeBorrow(frame)``. |
| 134 |
|
| 135 |
PyThreadState_GetInterpreter |
| 136 |
---------------------------- |
| 137 |
|
| 138 |
* Replace ``tstate->interp`` with ``PyThreadState_GetInterpreter(tstate)``. |
| 139 |
|
| 140 |
PyThreadState_GetFrame |
| 141 |
---------------------- |
| 142 |
|
| 143 |
* Replace ``tstate->frame`` with ``_PyThreadState_GetFrameBorrow(tstate)``. |
| 144 |
|
| 145 |
Experimental operations |
| 146 |
----------------------- |
| 147 |
|
| 148 |
The following operations are experimental (ex: can introduce compiler warnings) |
| 149 |
and so not included in the ``all`` group, they have to be selected explicitly. |
| 150 |
Example: ``-o all,Py_SETREF``. |
| 151 |
|
| 152 |
Experimental operations: |
| 153 |
|
| 154 |
* ``Py_NewRef``: |
| 155 |
|
| 156 |
* Replace ``Py_INCREF(res); return res;`` with ``return Py_NewRef(res);`` |
| 157 |
* Replace ``x = y; Py_INCREF(x);`` with ``x = Py_NewRef(y);`` |
| 158 |
* Replace ``x = y; Py_INCREF(y);`` with ``x = Py_NewRef(y);`` |
| 159 |
* Replace ``Py_INCREF(y); x = y;`` with ``x = Py_NewRef(y);`` |
| 160 |
|
| 161 |
* ``Py_CLEAR``: |
| 162 |
|
| 163 |
* Replace ``Py_XDECREF(var); var = NULL;`` with ``Py_CLEAR(var);`` |
| 164 |
|
| 165 |
* ``Py_SETREF``: |
| 166 |
|
| 167 |
* Replace ``Py_DECREF(x); x = y;`` with ``Py_SETREF(x, y);`` |
| 168 |
* Replace ``Py_XDECREF(x); x = y;`` with ``Py_XSETREF(x, y);`` |