Skip to content
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

take_ownership may erroneously clear MemoryError exceptions #131173

Open
mpage opened this issue Mar 13, 2025 · 0 comments
Open

take_ownership may erroneously clear MemoryError exceptions #131173

mpage opened this issue Mar 13, 2025 · 0 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@mpage
Copy link
Contributor

mpage commented Mar 13, 2025

Bug report

Bug description:

take_ownership clears any MemoryError exceptions when getting the previous frame fails (even if they were not raised by the call to _PyFrame_GetFrameObject):

cpython/Python/frame.c

Lines 74 to 79 in 1e4a434

PyFrameObject *back = _PyFrame_GetFrameObject(prev);
if (back == NULL) {
/* Memory error here. */
assert(PyErr_ExceptionMatches(PyExc_MemoryError));
/* Nothing we can do about it */
PyErr_Clear();

I think we should save and restore the exception around the call to _PyFrame_GetFrameObject(prev). Something like:

PyObject *exc = PyErr_GetRaisedException();
PyFrameObject *back = _PyFrame_GetFrameObject(prev);
if (back == NULL) {
    /* Memory error here. */
    assert(PyErr_ExceptionMatches(PyExc_MemoryError));
    /* Nothing we can do about it */
    PyErr_Clear();
}
else {
    f->f_back = (PyFrameObject *)Py_NewRef(back);
}
PyErr_SetRaisedException(exc);

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

No response

@mpage mpage added the type-bug An unexpected behavior, bug, or error label Mar 13, 2025
@mpage mpage self-assigned this Mar 13, 2025
@mpage mpage added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant