GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Inconsistencies in error propagation after calling `_PyFrame_GetFrameObject` · Issue #144693 · python/cpython

Inconsistencies in error propagation after calling _PyFrame_GetFrameObject #144693

New issue
Copy link
New issue
Copy link
Closed
Closed
Inconsistencies in error propagation after calling _PyFrame_GetFrameObject#144693
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@taegyunkim
taegyunkim
opened on Feb 10, 2026
Issue body actions

Bug report

Bug description:

There are 3 callers of _PyFrame_GetFrameObject which explicitly clear error when it returns NULL.

  1. take_ownership

    cpython/Python/frame.c

    Lines 74 to 79 in b67a64d

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

    cpython/Python/ceval.c

    Lines 2562 to 2564 in b67a64d

    PyFrameObject *f = _PyFrame_GetFrameObject(frame);
    if (f == NULL) {
    PyErr_Clear();
  3. PyThreadState_GetFrame

    cpython/Python/pystate.c

    Lines 2101 to 2103 in b67a64d

    PyFrameObject *frame = _PyFrame_GetFrameObject(f);
    if (frame == NULL) {
    PyErr_Clear();

PyEval_GetFrame and PyThreadState_GetFrame docs say that NULL is returned when "no frame is currently executing", and doesn't say anything about any error (probably as they explicitly clear it?).

I wonder whether PyFrame_GetBack also would want to clear error

cpython/Objects/frameobject.c

Lines 2407 to 2409 in b67a64d

if (prev) {
back = _PyFrame_GetFrameObject(prev);
}

PyFrame_GetBack's documentation doesn't say anything about MemoryError which can be set by its internal API calls https://docs.python.org/3.15/c-api/frame.html#c.PyFrame_GetBack

The rest of the _PyFrame_GetFrameObject callers that don't clear error including PyFrame_GetBack are

  • _PyEval_ExceptionGroupMatch
  • _PyEval_EvalFrameDefault (error label)
  • _gen_getframe
  • sys__getframe_impl
  • _PyThread_CurrentFrames
  • _Py_call_instrumentation_line
  • _PyFrame_GetLocals
  • PyEval_GetLocals — Public C API (deprecated), no NULL check
  • PyFrame_GetBack — Public C API, no NULL check

And PyEval_GetLocals and PyFrame_GetBack are public C APIs that don't check for NULL and don't clear the error. I guess the internals are ok to propagate the errors?

Can someone explain whether PyFrame_GetBack not clearing error on null is ok/intended or it's a discrepancy/bug?

I would prefer PyFrame_GetBack to not propagate an error given that its documentation doesn't say anything about MemoryError.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

  • gh-144693: Clarify that PyFrame_GetBack does not raises exception #144824
  • [3.14] gh-144693: Clarify that PyFrame_GetBack does not raise exceptions (GH-144824) #145318
  • [3.13] gh-144693: Clarify that PyFrame_GetBack does not raise exceptions (GH-144824) #145319

Activity

  1. taegyunkim
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Feb 10, 2026
  2. sergey-miryanov
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Feb 11, 2026
  3. sergey-miryanov commented on Feb 11, 2026

    @sergey-miryanov
    sergey-miryanov
    on Feb 11, 2026
    Contributor
    More actions

    _PyFrame_GetFrameObject is not setting an exception (but preserves it if it exists). PyFrame_GetBack can return NULL and this is handled by frame_back_get_impl, for example. In this case frame.f_back will have None value.

    From Python point of view, frame.f_back and PyFrame_GetBack can't be called with exception set.
    From C-API point of view, it is responsibility of the user to handle exceptions that can exist before calling a PyFrame_GetBack.

    Maybe it is worth to update a documentation that API preserves existing exceptions.

    Reacted by Petr Viktorin
  4. sergey-miryanov commented on Feb 11, 2026

    @sergey-miryanov
    sergey-miryanov
    on Feb 11, 2026
    Contributor
    More actions

    It seems that _PyFrame_GetLocals potentially can be called with frame == NULL, maybe it is worth to add checks there.

    Would you like open PRs? If so please open two separate PRs, one for documentation update, one for _PyFrame_GetLocals.

    Reacted by Taegyun Kim
  5. sergey-miryanov commented on Feb 11, 2026

    @sergey-miryanov
    sergey-miryanov
    on Feb 11, 2026
    Contributor
    More actions

    I believe _gen_getframe, PyEval_GetLocals should have NULL checks or at least asserts.

    sys__getframe_impl, _PyThread_CurrentFrames, _Py_call_instrumentation_line should set an exception if _PyFrame_GetFrameObject returns NULL.

    _PyEval_ExceptionGroupMatch seems OK.

  6. sergey-miryanov commented on Feb 11, 2026

    @sergey-miryanov
    sergey-miryanov
    on Feb 11, 2026
    Contributor
    More actions

    cc @markshannon as an owner
    cc @colesbury as an expert

  7. taegyunkim commented on Feb 11, 2026

    @taegyunkim
    taegyunkim
    on Feb 11, 2026
    ContributorAuthor
    More actions

    Would you like open PRs? If so please open two separate PRs, one for documentation update, one for _PyFrame_GetLocals.

    Sure will do shortly

  8. taegyunkim
    added a commit that references this issue on Feb 14, 2026

    pythongh-144693: Clarify that PyFrame_GetBack preserves existing exce…

    56234be
  9. encukou commented on Feb 16, 2026

    @encukou
    encukou
    on Feb 16, 2026
    Member
    More actions

    From C-API point of view, it is responsibility of the user to handle exceptions that can exist before calling a PyFrame_GetBack.

    Yes, that's the general expectation for public C API functions. The behaviour is undefined if called with an active exception. If preserving the exception isn't documented, it's an implementation detail that can change in future versions.

    I'm not sure if the frame experts want to turn it into a documented guarantee.

  10. ZeroIntensity
    added a commit that references this issue on Feb 27, 2026

    gh-144693: Clarify that `PyFrame_GetBack` does not raise exceptions (G…

    8775f90
  11. miss-islington
    added 2 commits that reference this issue on Feb 27, 2026

    pythongh-144693: Clarify that `PyFrame_GetBack` does not raise except…

    9aab246

    pythongh-144693: Clarify that `PyFrame_GetBack` does not raise except…

    4de49e5
  12. ZeroIntensity
    added 2 commits that reference this issue on Feb 27, 2026

    [3.14] gh-144693: Clarify that `PyFrame_GetBack` does not raise excep…

    1e4b4a6

    [3.13] gh-144693: Clarify that `PyFrame_GetBack` does not raise excep…

    67f6368
  13. brijkapadia
    added a commit that references this issue on Feb 28, 2026

    pythongh-144693: Clarify that `PyFrame_GetBack` does not raise except…

    b1ae874
  14. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-144693: Clarify that `PyFrame_GetBack` does not raise except…

    be632e9
  15. StanFromIreland
    closed this as completedon May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.