Skip to content

[C-API] Add a function to access PyCodeObject.co_code in C #92154

Description

@Fidget-Spinner

Feature or enhancement

Previously, you could access the co_code field in PyCodeObject directly to access the bytecode. This is no longer the case since 2bde682. Currently in 3.11a7+, C code cannot easily obtain the equivalent of co_code in 3.10. co_code_adaptive gets quickened bytecode which is not equivalent to the 3.10 behavior.

I would imagine many tools relying on the old behavior will break.

Pitch

I propose we expose some form of the currently internal _PyCode_GetCode.

Previous discussion

Related to #91397.
See also coverage.py issue coveragepy/coveragepy#1367

CC @vstinner @nedbat @brandtbucher @markshannon

Activity

  1. vstinner commented on May 2, 2022

    @vstinner
    Member

    See also #91397 "[C API] Move the PyCodeObject structure to the internal C API (make the structure opaque)".

  2. vstinner commented on May 2, 2022

    @vstinner
    Member

    Even if no function is added, please document at least the member removal in https://docs.python.org/dev/whatsnew/3.11.html (C API > Porting to Python 3.11) since the PyCodeObject structure is part of the public C API.

  3. markshannon commented on May 2, 2022

    @markshannon
    Member

    Exposing PyCode_GetCode makes sense to me, as it equivalent to code.co_code.

    @brandtbucher

  4. brandtbucher commented on May 2, 2022

    @brandtbucher
    Member

    Sounds good to me as well.

  5. added a commit that references this issue on May 3, 2022
  6. vstinner commented on May 3, 2022

    @vstinner
    Member

    And I added the function to pythoncapi-compat: python/pythoncapi-compat@85d1bd8

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions