1 #ifndef PYTHON_COMPAT 2 #define PYTHON_COMPAT 3 4 #include <torch/csrc/utils/pythoncapi_compat.h> 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 // PyTorch-only compat functions 11 12 #define IS_PYTHON_3_11_PLUS PY_VERSION_HEX >= 0x030B00C1 13 #define IS_PYTHON_3_12_PLUS PY_VERSION_HEX >= 0x030C0000 14 #define IS_PYTHON_3_13_PLUS PY_VERSION_HEX >= 0x030D0000 15 #define IS_PYTHON_3_14_PLUS PY_VERSION_HEX >= 0x030E0000 16 17 PYCAPI_COMPAT_STATIC_INLINE(int) PyCode_GetNCellvars(PyCodeObject * code)18PyCode_GetNCellvars(PyCodeObject* code) { 19 // gh-26364 added co_ncellvars to Python 3.11.0rc1 20 #if IS_PYTHON_3_11_PLUS 21 return code->co_ncellvars; 22 #else 23 return PyTuple_GET_SIZE(code->co_cellvars); 24 #endif 25 } 26 27 PYCAPI_COMPAT_STATIC_INLINE(int) PyCode_GetNFreevars(PyCodeObject * code)28PyCode_GetNFreevars(PyCodeObject* code) { 29 // gh-26364 added co_nfreevars to Python 3.11.0rc1 30 #if IS_PYTHON_3_11_PLUS 31 return code->co_nfreevars; 32 #else 33 return PyTuple_GET_SIZE(code->co_freevars); 34 #endif 35 } 36 37 // Provided by CPython but getting the header for them is very hard 38 extern void _PyWeakref_ClearRef(PyWeakReference* self); 39 40 #ifdef __cplusplus 41 } 42 #endif 43 #endif // PYTHON_COMPAT 44