Skip to content

take_ownership may erroneously clear MemoryError exceptions #131173

Closed

Description

@mpage

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

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Mar 13, 2025
  2. self-assigned this
    on Mar 13, 2025
  3. added a commit that references this issue on Apr 16, 2025
  4. added a commit that references this issue on Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions