1#ifndef PYTHON_COMPAT
2#define PYTHON_COMPAT
3
4#include <torch/csrc/utils/pythoncapi_compat.h>
5
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10// PyTorch-only compat functions
11
12#define IS_PYTHON_3_11_PLUS PY_VERSION_HEX >= 0x030B00C1
13
14PYCAPI_COMPAT_STATIC_INLINE(int)
15PyCode_GetNCellvars(PyCodeObject* code) {
16// gh-26364 added co_ncellvars to Python 3.11.0rc1
17#if IS_PYTHON_3_11_PLUS
18 return code->co_ncellvars;
19#else
20 return PyTuple_GET_SIZE(code->co_cellvars);
21#endif
22}
23
24PYCAPI_COMPAT_STATIC_INLINE(int)
25PyCode_GetNFreevars(PyCodeObject* code) {
26// gh-26364 added co_nfreevars to Python 3.11.0rc1
27#if IS_PYTHON_3_11_PLUS
28 return code->co_nfreevars;
29#else
30 return PyTuple_GET_SIZE(code->co_freevars);
31#endif
32}
33
34#ifdef __cplusplus
35}
36#endif
37#endif // PYTHON_COMPAT
38