Open markshannon opened 2 years ago
We need to provide an API to create, swap and free frame stacks for greenlets.
Since this is primarily for greenlets (and any other stackful coroutines libraries that want to use it) it will be "unstable".
In this case, by "unstable" I mean:
The API will be:
typedef struct _frame_stack {
_PyStackChunk *current_chunk;
PyObject **top;
PyObject **limit;
int chunk_size;
} _PyFrameStack;
PyAPI_FUNC(void) _PyFrameStack_Init(_PyFrameStack *fs, int chunk_size);
PyAPI_FUNC(void) _PyFrameStack_Swap(_PyFrameStack *fs);
PyAPI_FUNC(void) _PyFrameStack_Free(_PyFrameStack *fs);
greenlet code to save the Python thread state (C++): https://github.com/python-greenlet/greenlet/blob/937f150e07823ee03344aeeb5111c0bb371a831d/src/greenlet/TPythonState.cpp#L111
greenlet is used by eventlet and gevent projects.
PyOdide has similar code (in C): https://github.com/pyodide/pyodide/blob/d317e66d174efa203c59f094eb7714c0b90f7613/src/core/stack_switching/pystate.c#L161-L187
cc @hoodmane
Experimental greenlet change extracting the "save/restore" logic into a C API: https://github.com/python-greenlet/greenlet/pull/407
Members which should be saved/restored per Python version.
Python 3.10 (4):
tstate->cframe
tstate->cframe->use_tracing
tstate->recursion_depth
tstate->trash_delete_nesting
Python 3.11 (8):
tstate->cframe
tstate->cframe->current_frame
tstate->cframe->use_tracing
tstate->datastack_chunk
tstate->datastack_limit
tstate->datastack_top
tstate->recursion_remaining
tstate->trash_delete_nesting
Python 3.12 (8):
tstate->c_recursion_remaining
tstate->cframe->current_frame
tstate->cframe
tstate->datastack_chunk
tstate->datastack_limit
tstate->datastack_top
tstate->py_recursion_remaining
tstate->trash.delete_nesting
For Python 3.13, I'm not sure. I found 8 members:
tstate->c_recursion_remaining
tstate->context
tstate->current_frame
(UPDATE)tstate->datastack_chunk
tstate->datastack_limit
tstate->datastack_top
tstate->delete_later
tstate->py_recursion_remaining
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = 'https://github.com/markshannon' closed_at = None created_at =
labels = ['expert-C-API', 'type-feature', '3.11']
title = 'Add "unstable" frame stack api'
updated_at =
user = 'https://github.com/markshannon'
```
bugs.python.org fields:
```python
activity =
actor = 'Mark.Shannon'
assignee = 'Mark.Shannon'
closed = False
closed_date = None
closer = None
components = ['C API']
creation =
creator = 'Mark.Shannon'
dependencies = []
files = []
hgrepos = []
issue_num = 47215
keywords = ['patch']
message_count = 1.0
messages = ['416665']
nosy_count = 2.0
nosy_names = ['Mark.Shannon', 'brandtbucher']
pr_nums = ['32303']
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue47215'
versions = ['Python 3.11']
```
Linked PRs