| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
The Python C API compatibility project is made of two parts:
pythoncapi_compat.h supports Python 3.5 to Python 3.10, Python 2.7, PyPy 3.6 and PyPy 2.7. A C99 subset is required, like static inline functions: see PEP 7. ISO C90 is partially supported for Python 2.7: avoid mixed declarations and code (GCC -Werror=declaration-after-statement flag) and support Visual Studio 2008.
upgrade_pythoncapi.py requires Python 3.6 or newer.
Homepage: https://github.com/pythoncapi/pythoncapi_compat
Latest header file: https://raw.githubusercontent.com/pythoncapi/pythoncapi_compat/master/pythoncapi_compat.h
This project is distributed under the MIT license.
This project is covered by the PSF Code of Conduct.
Upgrade mod.c file:
python3 upgrade_pythoncapi.py mod.c
Upgrade all .c and .h files of a project:
python3 upgrade_pythoncapi.py directory/
WARNING: files are modified in-place! If a file is modified, the original file is saved as <filename>.old.
To see command line options and list available operations, run it with no arguments:
python3 upgrade_pythoncapi.py
For example, to only replace op->ob_type with Py_TYPE(op), use:
python3 upgrade_pythoncapi.py -o Py_TYPE mod.c
Or the opposite, to apply all operations but leave op->ob_type unchanged, use:
python3 upgrade_pythoncapi.py -o all,-Py_TYPE mod.c
Most upgrade_pythoncapi.py operations add #include "pythoncapi_compat.h". You may have to copy the pythoncapi_compat.h header file to your project. It can be copied from:
https://raw.githubusercontent.com/pythoncapi/pythoncapi_compat/master/pythoncapi_compat.h
upgrade_pythoncapi.py implements the following operations:
Some functions related to frame objects are not available on PyPy.
PyObject* Py_NewRef(PyObject *obj); PyObject* Py_XNewRef(PyObject *obj); int Py_Is(PyObject *x, PyObject *y); int Py_IsNone(PyObject *x); int Py_IsTrue(PyObject *x); int Py_IsFalse(PyObject *x); int PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value);
void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt); void Py_SET_TYPE(PyObject *ob, PyTypeObject *type); void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size); int Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type); PyObject* PyObject_CallNoArgs(PyObject *func); PyObject* PyObject_CallOneArg(PyObject *func, PyObject *arg);
PyCodeObject* PyFrame_GetCode(PyFrameObject *frame); // Not available on PyPy PyFrameObject* PyFrame_GetBack(PyFrameObject *frame);
// Not available on PyPy PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate); PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate); // Availability: Python 3.7. Not available on PyPy. uint64_t PyThreadState_GetID(PyThreadState *tstate);
PyInterpreterState* PyInterpreterState_Get(void);
// Not available on PyPy. int PyObject_GC_IsTracked(PyObject* obj); // Availability: Python 3.4. Not available on PyPy. int PyObject_GC_IsFinalized(PyObject *obj);
int PyModule_AddType(PyObject *module, PyTypeObject *type);
Py_SETREF(op, op2) Py_XSETREF(op, op2)
Py_UNUSED(name)
To ease migration of C extensions to the new C API, a variant is provided to return borrowed references rather than strong references:
// Similar to "Py_INCREF(ob); return ob;" PyObject* _Py_StealRef(PyObject *ob); // Similar to "Py_XINCREF(ob); return ob;" PyObject* _Py_XStealRef(PyObject *ob); // PyThreadState_GetFrame(). Not available on PyPy. PyFrameObject* _PyThreadState_GetFrameBorrow(PyThreadState *tstate) // PyFrame_GetCode() PyCodeObject* _PyFrame_GetCodeBorrow(PyFrameObject *frame) // PyFrame_GetBack(). Not available on PyPy. PyFrameObject* _PyFrame_GetBackBorrow(PyFrameObject *frame)
For example, tstate->frame can be replaced with _PyThreadState_GetFrameBorrow(tstate) to avoid accessing directly PyThreadState.frame member.
These functions are only available in pythoncapi_compat.h and are not part of the Python C API.
Run tests:
python3 runtests.py
Only test the current Python version, don't test multiple Python versions (-c, --current):
python3 runtests.py --current
Verbose mode (-v, --verbose):
python3 runtests.py --verbose
See tests in the tests/ subdirectory.
| Back | FazBrowse Home | New Git URL |