Skip to content

[C API] Add public getter functions for the internal PyFrameObject structure #84601

Closed

Description

@vstinner
BPO 40421
Nosy @scoder, @vstinner, @nedbat, @encukou, @markshannon, @miss-islington, @brandtbucher
PRs
  • bpo-40421: Add pyframe.h header file #19755
  • bpo-40421: Add Include/cpython/code.h header file #19756
  • bpo-40421: Add PyFrame_GetCode() function #19757
  • bpo-40421: Add PyFrame_GetLastInstr() function #19764
  • bpo-40421: Add PyFrame_GetBack() function #19765
  • Add symbols of the stable ABI to python3dll.c #23598
  • [3.9] bpo-42415: Add symbols of the stable ABI to python3dll.c (GH-23598) #23801
  • bpo-40421: Fix PyFrame_GetCode() documentation #31535
  • bpo-40421: What's New in Python 3.11: PyFrameObject.f_lasti #31536
  • [3.9] bpo-40421: Fix PyFrame_GetCode() documentation (GH-31535) #31537
  • [3.10] bpo-40421: Fix PyFrame_GetCode() documentation (GH-31535) #31538
  • bpo-40421: Add missing getters for frame object attributes to C-API. #32114
  • bpo-40421: test_capi uses assertEqual(), not assertEquals() #32361
  • bpo-40421: Add new PyFrame_GetLasti C-API function #32413
  • bpo-40421: Cleanup PyFrame C API #32417
  • 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:

    assignee = None
    closed_at = <Date 2022-04-06.13:18:28.654>
    created_at = <Date 2020-04-28.13:17:50.323>
    labels = ['expert-C-API', '3.11']
    title = '[C API] Add public getter functions for the internal PyFrameObject structure'
    updated_at = <Date 2022-04-08.14:45:29.774>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2022-04-08.14:45:29.774>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-04-06.13:18:28.654>
    closer = 'vstinner'
    components = ['C API']
    creation = <Date 2020-04-28.13:17:50.323>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40421
    keywords = ['patch']
    message_count = 34.0
    messages = ['367527', '367532', '367535', '367545', '367550', '367604', '368342', '368350', '368424', '383167', '383209', '403814', '403815', '406144', '408953', '413719', '413720', '413797', '413835', '413842', '413843', '413844', '413845', '413848', '413849', '413850', '414314', '414344', '415777', '415791', '416447', '416866', '416867', '416870']
    nosy_count = 7.0
    nosy_names = ['scoder', 'vstinner', 'nedbat', 'petr.viktorin', 'Mark.Shannon', 'miss-islington', 'brandtbucher']
    pr_nums = ['19755', '19756', '19757', '19764', '19765', '23598', '23801', '31535', '31536', '31537', '31538', '32114', '32361', '32413', '32417']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue40421'
    versions = ['Python 3.11']

    Activity

    1. vstinner commented on Apr 28, 2020

      @vstinner
      MemberAuthor

      Similarly to bpo-39573 (make PyObject opaque) and bpo-39947 (make PyThreadState opaque), I propose to add getter functions to access PyFrameObject members without exposing the PyFrameObject structure in the C API.

      The first step is to identify common usage of the PyFrameObject structure inside CPython code base to add getter functions, and maybe a few setter functions as well.

      frameobject.h is not part of Python.h, but it's part of the public C API. The long term plan is to move PyFrameObject structure to the internal C API to hide implementation details from the public C API.

    2. vstinner commented on Apr 28, 2020

      @vstinner
      MemberAuthor

      New changeset 7c59d7c by Victor Stinner in branch 'master':
      bpo-40421: Add pyframe.h header file (GH-19755)
      7c59d7c

    3. vstinner commented on Apr 28, 2020

      @vstinner
      MemberAuthor

      New changeset b8f704d by Victor Stinner in branch 'master':
      bpo-40421: Add Include/cpython/code.h header file (GH-19756)
      b8f704d

    4. vstinner commented on Apr 28, 2020

      @vstinner
      MemberAuthor

      New changeset a42ca74 by Victor Stinner in branch 'master':
      bpo-40421: Add PyFrame_GetCode() function (GH-19757)
      a42ca74

    5. vstinner commented on Apr 28, 2020

      @vstinner
      MemberAuthor

      I looked how Cython uses PyFrameObject:

      • read f_lasti
      • read/write f_back
      • write f_lineno
      • read f_localsplus
      • read/write f_trace

      Details:

      • Cython/Debugger/libpython.py: code using the Python API of gdb to read PyFrameObject.f_lasti. It it used to compute the line number of a frame. The python_step() function puts a watch point on "f->f_lasti".
      • Cython/Utility/Coroutine.c: set PyFrameObject.f_back using "f->f_back = tstate->frame;" and "Py_CLEAR(f->f_back);".
      • Cython/Utility/ModuleSetupCode.c, __Pyx_PyFrame_SetLineNumber(): set PyFrameObject.f_lineno member. The limited C API flavor of this function does nothing, since this member cannot be set in the limited C API.
      • Cython/Utility/ObjectHandling.c, __Pyx_PyFrame_GetLocalsplus(): complex implementation to access PyFrameObject.f_localsplus:
        // Initialised by module init code.
        static size_t __pyx_pyframe_localsplus_offset = 0;
      
        #include "frameobject.h"
        // This is the long runtime version of
        //     #define __Pyx_PyFrame_GetLocalsplus(frame)  ((frame)->f_localsplus)
        // offsetof(PyFrameObject, f_localsplus) differs between regular C-Python and Stackless Python.
        // Therefore the offset is computed at run time from PyFrame_type.tp_basicsize. That is feasible,
        // because f_localsplus is the last field of PyFrameObject (checked by Py_BUILD_ASSERT_EXPR below).
        #define __Pxy_PyFrame_Initialize_Offsets()  \
          ((void)__Pyx_BUILD_ASSERT_EXPR(sizeof(PyFrameObject) == offsetof(PyFrameObject, f_localsplus) + Py_MEMBER_SIZE(PyFrameObject, f_localsplus)), \
           (void)(__pyx_pyframe_localsplus_offset = ((size_t)PyFrame_Type.tp_basicsize) - Py_MEMBER_SIZE(PyFrameObject, f_localsplus)))
        #define __Pyx_PyFrame_GetLocalsplus(frame)  \
          (assert(__pyx_pyframe_localsplus_offset), (PyObject **)(((char *)(frame)) + __pyx_pyframe_localsplus_offset))
      • Cython/Utility/Profile.c, __Pyx_TraceLine(): read PyFrameObject.f_trace.

      • Cython/Utility/Profile.c, __Pyx_TraceSetupAndCall(): set PyFrameObject.f_trace.

    6. vstinner commented on Apr 29, 2020

      @vstinner
      MemberAuthor

      New changeset 7036477 by Victor Stinner in branch 'master':
      bpo-40421: Add PyFrame_GetBack() function (GH-19765)
      7036477

    7. markshannon commented on May 7, 2020

      @markshannon
      Member

      "maybe a few setter functions as well"
      Please don't add any setter functions, that would a major change to the VM and would need a PEP.

      Also, could you remove PyFrame_GetLastInstr(PyFrameObject *frame)?
      The only purpose of f_lasti is to get the line number and that can be done directly via PyFrame_GetLineNumber(PyFrameObject *frame)

      Perhaps Stefan can tell us why Cython needs to access the internals of the frame object.

    8. vstinner commented on May 7, 2020

      @vstinner
      MemberAuthor

      Please don't add any setter functions, that would a major change to the VM and would need a PEP.

      There is no such "major change". PyFrameObject structure was fully exposed in the public C API since the earliest Python version.

      I don't see how adding setter is a major change, since it's already possible to directly modify *any* field of PyFrameObject without any kind of protection.

      My plan is to have two milestones:

      A) Make the structure opaque, so it's not longer possible to directly access it.

      B) Deprecate or remove a few getter or setter functions, or move them to the internal C API.

      I don't think that moving directly to step (B) is a realistic migration plan. So far, nobody analyzed all C extensions on PyPI to see how PyFrameObject is accessed.

      I prefer to move slowly, step by step.

      Breaking C extensions which currently *modify* directly PyFrameObject on purpose doesn't seem like a reasonable option to me.

      --

      I'm trying to distinguish functions which should be "safe"/"portable" from the ones which may be too "CPython specific":

      Note: Compared to accessing directly PyFrameObject.f_code, PyFrame_GetCode() also avoids the issue of borrowed references since it returns a strong reference.

      PyFrame_GetBack() looks specific to the current implementation of CPython. Another implementation might decide to not chain frames. Or maybe don't provide an easy way to traverse the chain of frames. Or at least, it might be a different API than PyFrame_GetBack().

      --

      Also, could you remove PyFrame_GetLastInstr(PyFrameObject *frame)?

      I didn't add it :-) It's the pending PR 19764.

      I didn't merge it since it's unclear to me if it's a good idea to directly expose it or not. Cython uses it, but Cython also abuses CPython internals in general, usually for best performances :-)

      *Maybe* one compromise would be to add a private _PyFrame_GetLastInstr() to ease migration to step (A) (replace direct access to the structure with function calls).

    9. scoder commented on May 8, 2020

      @scoder
      Contributor

      Adding to the list above:

      "f_lasti" is used in "libpython.py", which is an almost exact copy of the one in CPython's "Tools/gdb/" for debugging Python code in gdb. If that implementation changes, we can probably adapt, especially since it uses runtime generated code. I don't find a C function for this necessary at this point, given that CPython will also need to access its own internals here in some way.

      "f_localsplus" is used in the FASTCALL implementation and seems specific to Py3.6/7(?) which lacks the vectorcall protocol. So I guess it won't impact 3.8+ anymore. (The reason why its access is so complex is that StacklessPython has a slightly different frame struct, but we need to be binary(!) compatible with both.)

      "f_back" is used in the coroutine implementation for correct exception traceback building, and the usage is pretty much copied from CPython. I doubt that there is much use for setting it outside of "code that tries to reimplement CPython behaviour", but Cython has to do exactly that here.

      "f_lineno" is also used for traceback generation, because Cython creates frames when an exception occurs and needs to tell it what the current code line is. It's only used on newly created frames, although I can't guarantee that that will never change. Being able to read and set it seems reasonable.

      "f_trace" is obviously used for tracing/profiling, and there isn't currently a good C-API for that, besides talking to frame struct fields (which is quite efficient when it comes to performance).

    10. vstinner commented on Dec 16, 2020

      @vstinner
      MemberAuthor

      New changeset fcc6935 by Victor Stinner in branch 'master':
      Add symbols of the stable ABI to python3dll.c (GH-23598)
      fcc6935

    11. vstinner commented on Dec 16, 2020

      @vstinner
      MemberAuthor

      New changeset 1662868 by Victor Stinner in branch '3.9':
      Add symbols of the stable ABI to python3dll.c (GH-23598) (GH-23801)
      1662868

    12. 17 remaining items

    13. vstinner commented on Mar 22, 2022

      @vstinner
      MemberAuthor

      I created bpo-47092: [C API] Add PyFrame_GetVar(frame, name) function.

    14. vstinner commented on Mar 22, 2022

      @vstinner
      MemberAuthor

      Issue title: "[C API] Add getter functions for PyFrameObject and maybe move PyFrameObject to the internal C API"

      bpo-46836 moved PyFrameObject to the internal C API. I update the issue title.

    15. added
      3.11
      and removed
      3.9 (EOL)
      on Mar 22, 2022
    16. changed the title [-][C API] Add getter functions for PyFrameObject and maybe move PyFrameObject to the internal C API[/-] [+][C API] Add public getter functions for the internal PyFrameObject structure[/+] on Mar 22, 2022
    17. added
      3.11
      and removed
      3.9 (EOL)
      on Mar 22, 2022
    18. changed the title [-][C API] Add getter functions for PyFrameObject and maybe move PyFrameObject to the internal C API[/-] [+][C API] Add public getter functions for the internal PyFrameObject structure[/+] on Mar 22, 2022
    19. markshannon commented on Mar 31, 2022

      @markshannon
      Member

      New changeset 74b95d8 by Mark Shannon in branch 'main':
      bpo-40421: Add missing getters for frame object attributes to C-API. (GH-32114)
      74b95d8

    20. vstinner commented on Apr 6, 2022

      @vstinner
      MemberAuthor

      New changeset 14a9b48 by Victor Stinner in branch 'main':
      bpo-40421: test_capi uses assertEqual(), not assertEquals() (GH-32361)
      14a9b48

    21. vstinner commented on Apr 6, 2022

      @vstinner
      MemberAuthor

      IMO the initial goal is now reached. I close the issue. Thanks to everyone who helped implementing these changes!

      The PyFrameObject structure is now opaque in Python 3.11. New getter functions of the Python 3.11 C API:

      • PyFrame_GetBuiltins()
      • PyFrame_GetGenerator()
      • PyFrame_GetGlobals()
      • PyFrame_GetLocals()

      Finally, the PyFrameObject structure now has its own page in the C API documentation:
      https://docs.python.org/dev/c-api/frame.html

      As explained in previous comments, the work is not done: Cython, greenlet, gevent and coverage still need more getter and/or setter functions. Adding more functions is being discussed in this external issue:
      faster-cpython/ideas#309

      I propose to open new specific issues to add new functions. For example, open an issue to add a getter for the f_lasti member.

    22. vstinner commented on Apr 6, 2022

      @vstinner
      MemberAuthor

      The PyFrameObject structure was made opaque by bpo-46836.

    23. transferred this issue fromon Apr 10, 2022
    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

      3.11topic-C-API

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions