-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
gh-141518: Add PyUnstable_InterpreterState_SetEvalFrameFunc() #141665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Add PyUnstable API for PEP 523: * PyUnstable_FrameEvalFunction type * PyUnstable_InterpreterState_GetEvalFrameFunc() * PyUnstable_InterpreterState_SetEvalFrameFunc() Keep the old names as deprecated aliases to new names: * _PyFrameEvalFunction * _PyInterpreterState_GetEvalFrameFunc * _PyInterpreterState_SetEvalFrameFunc
|
Do we need tests on |
There are tests in test_optimizer: see |
Thanks! |
I added tests. |
Co-authored-by: Mikhail Efimov <efimov.mikhail@gmail.com>
|
cc @emmatyping |
emmatyping
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question but otherwise looks good!
|
I created a C API Working Group decision issue. |
|
|
||
|
|
||
| static PyObject * | ||
| noop_eval(PyThreadState *tstate, struct _PyInterpreterFrame *f, int exc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't _PyInterpreterFrame, need a public name too? Otherwise you can't quite use the functions without private API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To implement an eval function, you should likely use the internal C API (pycore_interpframe.h) to access frame members, you're right. I don't want to add _PyInterpreterFrame structure members to the PyUnstable API, it's too unstable :-D I prefer to leave it in the internal C API.
I don't think that it's worth it to expose struct _PyInterpreterFrame* type in the PyUnstable API. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions are rather useless without making _PyInterpreterFrame public. At least should be an opaque type, with a function to upgrade it to the full PyFrameObject.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API changed in Python 3.11 to take a _PyInterpreterFrame* instead of a PyFrameObject*. It's basically only used by PyTorch Dynamo which already uses the internal C API:
I wouldn't say that this API is useless without a public API for _PyInterpreterFrame*. It's just "unusual" :-)
At least should be an opaque type, with a function to upgrade it to the full PyFrameObject.
It would make the code (way) slower, _PyInterpreterFrame* idea is to avoid creating a concrete Python object for a frame unless it's strictly needed. I suggest using the internal C API for _PyInterpreterFrame* instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't say that this API is useless without a public API for
_PyInterpreterFrame*.
What is the use, then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@emmatyping @efimov-mikhail: Do you have an opinion on these questions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, the main idea of this change is not really to make those functions public. We mark them as PyUnstable_ for sending clear message to its users: "we remember about those function, we don't want to change or remove them in the near future". One can use them and rely on their presence in C API, but can't rely on stability of _PyInterpreterFrame structure. So, there's a need to use some private headers in this case.
I agree that it's not the perfect position for us, but current situation isn't any better.
We have functions that both private and public.
They're private, because they have _Py* names.
But they're "kinda public", since PEP 523 is guaranteed their presence.
PyUnstable_ suits better for those semi-public functions.
But making _PyInterpreterFrame and all its members PyUnstable_ too seems redundant to me.
Add a PyUnstable API for PEP 523:
Keep the old names as deprecated aliases to new names:
PyUnstable_InterpreterState_SetEvalFrameFunc()#141518