1515#include < limits>
1616#include < frameobject.h>
1717
18- #if !IS_PRE_PYTHON_3_11
18+ #if !IS_LT_PYTHON_3_11
19+ #if IS_GE_PYTHON_3_13
20+ # define Py_BUILD_CORE 1
21+ #endif
1922#include < internal/pycore_code.h>
2023#include < internal/pycore_frame.h>
2124#endif
@@ -48,7 +51,7 @@ PyNode::~PyNode()
4851void PyNode::init ( PyObjectPtr inputs, PyObjectPtr outputs )
4952{
5053 PyGenObject * pygen = ( PyGenObject * ) m_gen.ptr ();
51-
54+
5255 // call gen first yield to setup locals
5356 call_gen ();
5457
@@ -61,7 +64,7 @@ void PyNode::init( PyObjectPtr inputs, PyObjectPtr outputs )
6164 m_localVars = ( PyObject *** ) calloc ( numInputs (), sizeof ( PyObject ** ) );
6265
6366 // printf( "Starting %s slots: %ld rank: %d\n", name(), slots, rank() );
64- #if IS_PRE_PYTHON_3_11
67+ #if IS_LT_PYTHON_3_11
6568 PyCodeObject * code = ( PyCodeObject * ) pygen -> gi_code;
6669 Py_ssize_t numCells = PyTuple_GET_SIZE ( code -> co_cellvars );
6770 size_t cell2argIdx = 0 ;
@@ -85,7 +88,13 @@ void PyNode::init( PyObjectPtr inputs, PyObjectPtr outputs )
8588// PY311+ changes
8689#else
8790 _PyInterpreterFrame * frame = ( _PyInterpreterFrame * ) pygen -> gi_iframe;
88- PyCodeObject * code = frame -> f_code;
91+ PyCodeObject * code;
92+
93+ #if IS_GE_PYTHON_3_13
94+ code = (PyCodeObject *)PyUnstable_InterpreterFrame_GetCode (frame);
95+ #else
96+ code = frame -> f_code;
97+ #endif
8998 int localPlusIndex = 0 ;
9099 for ( int stackloc = code -> co_argcount; stackloc < code -> co_nlocalsplus; ++stackloc, ++localPlusIndex )
91100 {
@@ -102,7 +111,7 @@ void PyNode::init( PyObjectPtr inputs, PyObjectPtr outputs )
102111 var = &( ( ( PyCellObject * ) *var ) -> ob_ref );
103112 else if ( kind == CO_FAST_CELL )
104113 continue ;
105- #endif
114+ #endif
106115 // null var indicates a stack slot for a local state variable ( state hasnt initialized yet )
107116 // we can skip those
108117 if ( !*var )
@@ -119,7 +128,7 @@ void PyNode::init( PyObjectPtr inputs, PyObjectPtr outputs )
119128 CSP_ASSERT ( !isInputBasket ( index ) );
120129
121130 m_localVars[ index ] = var;
122- // These vars will be "deleted" from the python stack after start
131+ // These vars will be "deleted" from the python stack after start
123132 continue ;
124133 }
125134
@@ -215,7 +224,7 @@ void PyNode::stop()
215224 if ( this -> rootEngine () -> interrupted () && PyErr_CheckSignals () == -1 )
216225 {
217226 // When an interrupt occurs a KeyboardInterrupt exception is raised in Python, which we need to clear
218- // before calling "close" on the generator. Else, the close method will fail due to the unhandled
227+ // before calling "close" on the generator. Else, the close method will fail due to the unhandled
219228 // exception, and we lose the state of the generator before the "finally" block that calls stop() is executed.
220229 PyErr_Clear ();
221230 }
@@ -230,15 +239,15 @@ PyNode * PyNode::create( PyEngine * pyengine, PyObject * inputs, PyObject * outp
230239 // parse inputs/outputs, create outputs time series, etc
231240 Py_ssize_t numInputs = PyTuple_GET_SIZE ( inputs );
232241 Py_ssize_t numOutputs = PyTuple_GET_SIZE ( outputs );
233-
242+
234243 if ( size_t ( numInputs ) >= InputId::maxId () )
235244 CSP_THROW ( ValueError, " number of inputs exceeds limit of " << InputId::maxBasketElements () );
236245
237246 if ( size_t ( numOutputs ) > OutputId::maxId () )
238247 CSP_THROW ( ValueError, " number of outputs exceeds limit of " << OutputId::maxBasketElements () );
239248
240- return pyengine -> engine () -> createOwnedObject<PyNode>( PyObjectPtr::incref ( gen ),
241- PyObjectPtr::incref ( inputs ),
249+ return pyengine -> engine () -> createOwnedObject<PyNode>( PyObjectPtr::incref ( gen ),
250+ PyObjectPtr::incref ( inputs ),
242251 PyObjectPtr::incref ( outputs ),
243252 NodeDef ( numInputs, numOutputs ) );
244253}
@@ -317,10 +326,10 @@ static PyObject * PyNode_create( PyObject * module, PyObject * args )
317326 PyObject * outputs;
318327 PyObject * gen;
319328
320- if ( !PyArg_ParseTuple ( args, " O!O!O!O!" ,
321- &PyEngine::PyType, &engine,
322- &PyTuple_Type, &inputs,
323- &PyTuple_Type, &outputs,
329+ if ( !PyArg_ParseTuple ( args, " O!O!O!O!" ,
330+ &PyEngine::PyType, &engine,
331+ &PyTuple_Type, &inputs,
332+ &PyTuple_Type, &outputs,
324333 &PyGen_Type, &gen ) )
325334 CSP_THROW ( PythonPassthrough, " " );
326335
0 commit comments