Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
greenlet_cpython_compat.hpp118 linesDownload Raw Back to greenlet
1/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2#ifndef GREENLET_CPYTHON_COMPAT_H
3#define GREENLET_CPYTHON_COMPAT_H
4
5/**
6 * Helpers for compatibility with multiple versions of CPython.
7 */
8
9#define PY_SSIZE_T_CLEAN
10#include "Python.h"
11
12/*
13Python 3.10 beta 1 changed tstate->use_tracing to a nested cframe member.
14See https://github.com/python/cpython/pull/25276
15We have to save and restore this as well.
16
17Python 3.13 removed PyThreadState.cframe (GH-108035).
18*/
19#if PY_VERSION_HEX < 0x30D0000
20#    define GREENLET_USE_CFRAME 1
21#else
22#    define GREENLET_USE_CFRAME 0
23#endif
24
25
26#if PY_VERSION_HEX >= 0x30B00A4
27/*
28Greenlet won't compile on anything older than Python 3.11 alpha 4 (see
29https://bugs.python.org/issue46090). Summary of breaking internal changes:
30- Python 3.11 alpha 1 changed how frame objects are represented internally.
31  - https://github.com/python/cpython/pull/30122
32- Python 3.11 alpha 3 changed how recursion limits are stored.
33  - https://github.com/python/cpython/pull/29524
34- Python 3.11 alpha 4 changed how exception state is stored. It also includes a
35  change to help greenlet save and restore the interpreter frame "data stack".
36  - https://github.com/python/cpython/pull/30122
37  - https://github.com/python/cpython/pull/30234
38*/
39#    define GREENLET_PY311 1
40#else
41#    define GREENLET_PY311 0
42#endif
43
44
45#if PY_VERSION_HEX >= 0x30C0000
46#    define GREENLET_PY312 1
47#else
48#    define GREENLET_PY312 0
49#endif
50
51#if PY_VERSION_HEX >= 0x30D0000
52#    define GREENLET_PY313 1
53#else
54#    define GREENLET_PY313 0
55#endif
56
57#if PY_VERSION_HEX >= 0x30E0000
58#    define GREENLET_PY314 1
59#else
60#    define GREENLET_PY314 0
61#endif
62
63#if PY_VERSION_HEX >= 0x30F0000
64#    define GREENLET_PY315 1
65#else
66#    define GREENLET_PY315 0
67#endif
68
69
70/* _Py_DEC_REFTOTAL macro has been removed from Python 3.9 by:
71  https://github.com/python/cpython/commit/49932fec62c616ec88da52642339d83ae719e924
72
73  The symbol we use to replace it was removed by at least 3.12.
74*/
75#if defined(Py_REF_DEBUG) && !GREENLET_PY312
76#    define GREENLET_Py_DEC_REFTOTAL _Py_RefTotal--
77#else
78#    define GREENLET_Py_DEC_REFTOTAL
79#endif
80
81
82#define G_TPFLAGS_DEFAULT Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_VERSION_TAG | Py_TPFLAGS_HAVE_GC
83
84
85// bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
86#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
87static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
88{
89    tstate->tracing++;
90    tstate->cframe->use_tracing = 0;
91}
92#endif
93
94// bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
95#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
96static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
97{
98    tstate->tracing--;
99    int use_tracing = (tstate->c_tracefunc != NULL
100                       || tstate->c_profilefunc != NULL);
101    tstate->cframe->use_tracing = use_tracing;
102}
103#endif
104
105#if !defined(Py_C_RECURSION_LIMIT) && defined(C_RECURSION_LIMIT)
106#  define Py_C_RECURSION_LIMIT C_RECURSION_LIMIT
107#endif
108
109// Py_IsFinalizing() became a public API in Python 3.13. Map it to the
110// private _Py_IsFinalizing() on older versions so all call sites can
111// use the standard name. Remove this once greenlet drops support for
112// Python < 3.13.
113#if !GREENLET_PY313
114#  define Py_IsFinalizing() _Py_IsFinalizing()
115#endif
116
117#endif /* GREENLET_CPYTHON_COMPAT_H */
118 
codekingpro/portable-devtools · Team Ai