Skip to content

Commit 9497948

Browse files
committed
Fix #392: Port to Python 3.13
* Replace C_RECURSION_LIMIT with Py_C_RECURSION_LIMIT. * Add Py_C_RECURSION_LIMIT for Python 3.12 and older. * Disable GREENLET_USE_CFRAME on Python 3.13. * Define Py_BUILD_CORE to include pycore_frame.h.
1 parent 937f150 commit 9497948

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/greenlet/TPythonState.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ void PythonState::operator<<(const PyThreadState *const tstate) noexcept
130130
#if GREENLET_PY311
131131
#if GREENLET_PY312
132132
this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
133-
this->c_recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
133+
this->c_recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
134134
#else // not 312
135135
this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
136136
#endif // GREENLET_PY312
137+
#if GREENLET_USE_CFRAME
137138
this->current_frame = tstate->cframe->current_frame;
139+
#endif
138140
this->datastack_chunk = tstate->datastack_chunk;
139141
this->datastack_top = tstate->datastack_top;
140142
this->datastack_limit = tstate->datastack_limit;
@@ -199,12 +201,14 @@ void PythonState::operator>>(PyThreadState *const tstate) noexcept
199201
#if GREENLET_PY311
200202
#if GREENLET_PY312
201203
tstate->py_recursion_remaining = tstate->py_recursion_limit - this->py_recursion_depth;
202-
tstate->c_recursion_remaining = C_RECURSION_LIMIT - this->c_recursion_depth;
204+
tstate->c_recursion_remaining = Py_C_RECURSION_LIMIT - this->c_recursion_depth;
203205
this->unexpose_frames();
204206
#else // \/ 3.11
205207
tstate->recursion_remaining = tstate->recursion_limit - this->recursion_depth;
206208
#endif // GREENLET_PY312
209+
#if GREENLET_USE_CFRAME
207210
tstate->cframe->current_frame = this->current_frame;
211+
#endif
208212
tstate->datastack_chunk = this->datastack_chunk;
209213
tstate->datastack_top = this->datastack_top;
210214
tstate->datastack_limit = this->datastack_limit;
@@ -238,7 +242,7 @@ void PythonState::set_initial_state(const PyThreadState* const tstate) noexcept
238242
#if GREENLET_PY312
239243
this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
240244
// XXX: TODO: Comment from a reviewer:
241-
// Should this be ``C_RECURSION_LIMIT - tstate->c_recursion_remaining``?
245+
// Should this be ``Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining``?
242246
// But to me it looks more like that might not be the right
243247
// initialization either?
244248
this->c_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;

src/greenlet/greenlet_cpython_compat.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,24 @@
1212

1313
#if PY_VERSION_HEX >= 0x30A00B1
1414
# define GREENLET_PY310 1
15+
#else
16+
# define GREENLET_PY310 0
17+
#endif
18+
1519
/*
1620
Python 3.10 beta 1 changed tstate->use_tracing to a nested cframe member.
1721
See https://github.com/python/cpython/pull/25276
1822
We have to save and restore this as well.
23+
24+
Python 3.13 removed PyThreadState.cframe (GH-108035).
1925
*/
26+
#if GREENLET_PY310 && PY_VERSION_HEX < 0x30D0000
2027
# define GREENLET_USE_CFRAME 1
2128
#else
2229
# define GREENLET_USE_CFRAME 0
23-
# define GREENLET_PY310 0
2430
#endif
2531

2632

27-
2833
#if PY_VERSION_HEX >= 0x30B00A4
2934
/*
3035
Greenlet won't compile on anything older than Python 3.11 alpha 4 (see
@@ -124,4 +129,8 @@ static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
124129
}
125130
#endif
126131

132+
#if !defined(Py_C_RECURSION_LIMIT) && defined(C_RECURSION_LIMIT)
133+
# define Py_C_RECURSION_LIMIT C_RECURSION_LIMIT
134+
#endif
135+
127136
#endif /* GREENLET_CPYTHON_COMPAT_H */

src/greenlet/greenlet_greenlet.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using greenlet::refs::BorrowedGreenlet;
2323
#endif
2424

2525
#if GREENLET_PY312
26+
# define Py_BUILD_CORE
2627
# include "internal/pycore_frame.h"
2728
#endif
2829

0 commit comments

Comments
 (0)