Qucs-S S-parameter Viewer & RF Synthesis Tools
Loading...
Searching...
No Matches
greenlet_cpython_compat.hpp
1/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2#ifndef GREENLET_CPYTHON_COMPAT_H
3#define GREENLET_CPYTHON_COMPAT_H
4
9#define PY_SSIZE_T_CLEAN
10#include "Python.h"
11
12
13#if PY_VERSION_HEX >= 0x30A00B1
14# define GREENLET_PY310 1
15#else
16# define GREENLET_PY310 0
17#endif
18
19/*
20Python 3.10 beta 1 changed tstate->use_tracing to a nested cframe member.
21See https://github.com/python/cpython/pull/25276
22We have to save and restore this as well.
23
24Python 3.13 removed PyThreadState.cframe (GH-108035).
25*/
26#if GREENLET_PY310 && PY_VERSION_HEX < 0x30D0000
27# define GREENLET_USE_CFRAME 1
28#else
29# define GREENLET_USE_CFRAME 0
30#endif
31
32
33#if PY_VERSION_HEX >= 0x30B00A4
34/*
35Greenlet won't compile on anything older than Python 3.11 alpha 4 (see
36https://bugs.python.org/issue46090). Summary of breaking internal changes:
37- Python 3.11 alpha 1 changed how frame objects are represented internally.
38 - https://github.com/python/cpython/pull/30122
39- Python 3.11 alpha 3 changed how recursion limits are stored.
40 - https://github.com/python/cpython/pull/29524
41- Python 3.11 alpha 4 changed how exception state is stored. It also includes a
42 change to help greenlet save and restore the interpreter frame "data stack".
43 - https://github.com/python/cpython/pull/30122
44 - https://github.com/python/cpython/pull/30234
45*/
46# define GREENLET_PY311 1
47#else
48# define GREENLET_PY311 0
49#endif
50
51
52#if PY_VERSION_HEX >= 0x30C0000
53# define GREENLET_PY312 1
54#else
55# define GREENLET_PY312 0
56#endif
57
58#if PY_VERSION_HEX >= 0x30D0000
59# define GREENLET_PY313 1
60#else
61# define GREENLET_PY313 0
62#endif
63
64#if PY_VERSION_HEX >= 0x30E0000
65# define GREENLET_PY314 1
66#else
67# define GREENLET_PY314 0
68#endif
69
70#ifndef Py_SET_REFCNT
71/* Py_REFCNT and Py_SIZE macros are converted to functions
72https://bugs.python.org/issue39573 */
73# define Py_SET_REFCNT(obj, refcnt) Py_REFCNT(obj) = (refcnt)
74#endif
75
76#ifdef _Py_DEC_REFTOTAL
77# define GREENLET_Py_DEC_REFTOTAL _Py_DEC_REFTOTAL
78#else
79/* _Py_DEC_REFTOTAL macro has been removed from Python 3.9 by:
80 https://github.com/python/cpython/commit/49932fec62c616ec88da52642339d83ae719e924
81
82 The symbol we use to replace it was removed by at least 3.12.
83*/
84# ifdef Py_REF_DEBUG
85# if GREENLET_PY312
86# define GREENLET_Py_DEC_REFTOTAL
87# else
88# define GREENLET_Py_DEC_REFTOTAL _Py_RefTotal--
89# endif
90# else
91# define GREENLET_Py_DEC_REFTOTAL
92# endif
93#endif
94// Define these flags like Cython does if we're on an old version.
95#ifndef Py_TPFLAGS_CHECKTYPES
96 #define Py_TPFLAGS_CHECKTYPES 0
97#endif
98#ifndef Py_TPFLAGS_HAVE_INDEX
99 #define Py_TPFLAGS_HAVE_INDEX 0
100#endif
101#ifndef Py_TPFLAGS_HAVE_NEWBUFFER
102 #define Py_TPFLAGS_HAVE_NEWBUFFER 0
103#endif
104
105#ifndef Py_TPFLAGS_HAVE_VERSION_TAG
106 #define Py_TPFLAGS_HAVE_VERSION_TAG 0
107#endif
108
109#define G_TPFLAGS_DEFAULT Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_VERSION_TAG | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_HAVE_GC
110
111
112#if PY_VERSION_HEX < 0x03090000
113// The official version only became available in 3.9
114# define PyObject_GC_IsTracked(o) _PyObject_GC_IS_TRACKED(o)
115#endif
116
117
118// bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
119#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
120static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
121{
122 tstate->tracing++;
123#if PY_VERSION_HEX >= 0x030A00A1
124 tstate->cframe->use_tracing = 0;
125#else
126 tstate->use_tracing = 0;
127#endif
128}
129#endif
130
131// bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
132#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
133static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
134{
135 tstate->tracing--;
136 int use_tracing = (tstate->c_tracefunc != NULL
137 || tstate->c_profilefunc != NULL);
138#if PY_VERSION_HEX >= 0x030A00A1
139 tstate->cframe->use_tracing = use_tracing;
140#else
141 tstate->use_tracing = use_tracing;
142#endif
143}
144#endif
145
146#if !defined(Py_C_RECURSION_LIMIT) && defined(C_RECURSION_LIMIT)
147# define Py_C_RECURSION_LIMIT C_RECURSION_LIMIT
148#endif
149
150#endif /* GREENLET_CPYTHON_COMPAT_H */