python-greenlet / greenlet

Lightweight in-process concurrent programming
Other
1.64k stars 247 forks source link

support for python3.12 #323

Closed tacaswell closed 1 year ago

tacaswell commented 2 years ago

https://github.com/python/cpython/pull/96510 de-coupled the Python and C level recursion limits which renamed some internal names. The following patch fixes the compilation errors with greenlet on the CPython main branch:

diff --git a/src/greenlet/greenlet_greenlet.hpp b/src/greenlet/greenlet_greenlet.hpp
index 1b8a83f..a036a08 100644
--- a/src/greenlet/greenlet_greenlet.hpp
+++ b/src/greenlet/greenlet_greenlet.hpp
@@ -826,7 +826,7 @@ void PythonState::operator<<(const PyThreadState *const tstate) G_NOEXCEPT
     this->use_tracing = tstate->cframe->use_tracing;
 #endif
 #if GREENLET_PY311
-    this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
+    this->recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
     this->current_frame = tstate->cframe->current_frame;
     this->datastack_chunk = tstate->datastack_chunk;
     this->datastack_top = tstate->datastack_top;
@@ -862,7 +862,7 @@ void PythonState::operator>>(PyThreadState *const tstate) G_NOEXCEPT
     tstate->cframe->use_tracing = this->use_tracing;
 #endif
 #if GREENLET_PY311
-    tstate->recursion_remaining = tstate->recursion_limit - this->recursion_depth;
+    tstate->py_recursion_remaining = tstate->py_recursion_limit - this->recursion_depth;
     tstate->cframe->current_frame = this->current_frame;
     tstate->datastack_chunk = this->datastack_chunk;
     tstate->datastack_top = this->datastack_top;
@@ -891,7 +891,7 @@ void PythonState::set_initial_state(const PyThreadState* const tstate) G_NOEXCEP
 {
     this->_top_frame = nullptr;
 #if GREENLET_PY311
-    this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
+    this->recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
 #else
     this->recursion_depth = tstate->recursion_depth;
 #endif

however does not do any of the version gating needed to work on a released version of CPython.

mdboom commented 1 year ago

@tacaswell: I assume you don't mind if I take your patch and add the version guards to make it into a full pull request? (This is also breaking pyperformance atm).

tacaswell commented 1 year ago

@mdboom nope, not at all!

amotl commented 1 year ago

Also seeing this at https://github.com/isarengineering/SecPi/actions/runs/3466304638/jobs/5789990791#step:9:442. Thank you already for submitting #327, @mdboom!