Skip to content

Add API to allow extensions to set callback function on creation and destruction of PyCodeObject #91054

@mpage

Description

@mpage
mannequin
Mannequin
BPO 46898
Nosy @carljm, @DinoV, @itamaro, @mpage

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 = None
created_at = <Date 2022-03-01.22:20:58.520>
labels = ['expert-C-API', 'type-feature', '3.11']
title = 'Add API to allow extensions to set callback function on creation and destruction of PyCodeObject'
updated_at = <Date 2022-03-01.22:20:58.520>
user = 'https://github.com/mpage'

bugs.python.org fields:

activity = <Date 2022-03-01.22:20:58.520>
actor = 'mpage'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['C API']
creation = <Date 2022-03-01.22:20:58.520>
creator = 'mpage'
dependencies = []
files = []
hgrepos = []
issue_num = 46898
keywords = []
message_count = 1.0
messages = ['414309']
nosy_count = 4.0
nosy_names = ['carljm', 'dino.viehland', 'itamaro', 'mpage']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue46898'
versions = ['Python 3.11']

Linked PRs

Activity

mpage

mpage commented on Mar 1, 2022

@mpage
MannequinAuthor

CPython extensions providing optimized execution of Python bytecode (e.g. the Cinder JIT) may need to hook into the lifecycle of code objects to determine what to optimize or to free resources allocated for code objects that no longer exist. We propose adding an API to allow extensions to set callbacks that will be invoked whenever code objects are created or destroyed.

Proposed API:

typedef enum {
  PYCODE_LCEVT_CREATED,
  PYCODE_LCEVT_DESTROYED
} PyCode_LifecycleEvent;

// A callback to be called when a code object is created or about to be destroyed.
typedef void(*PyCode_LifecycleCallback)(
  PyCode_LifecycleEvent event,
  PyCodeObject* code);

void PyCode_SetLifecycleCallback(PyCode_LifecycleCallback callback);
PyCode_LifecycleCallback PyCode_GetLifecycleCallback();

transferred this issue fromon Apr 10, 2022
gvanrossum

gvanrossum commented on Sep 15, 2022

@gvanrossum
Member

In which header file would you put that? Maybe this is simple enough that you should just submit a PR?

itamaro

itamaro commented on Sep 15, 2022

@itamaro
Contributor

In which header file would you put that?

would Include/cpython/code.h make sense for this API?

Maybe this is simple enough that you should just submit a PR?

hopefully yes, we intend to do that for 3.12.
does the proposed API look reasonable?

added
3.12only security fixes
and removed
3.11only security fixes
on Sep 24, 2022
gvanrossum

gvanrossum commented on Sep 24, 2022

@gvanrossum
Member

The API looks reasonable -- I'd spell LifeCycle with two capital letters (though Wikipedia seems divided on this), and I'd start the enum values with PY_CODE, similar to _PyCodeLocationInfoKind in the same file. The implementation would seem straightforward, and code objects aren't created or destroyed at a monstrous rate (it might be worth seeing if this slows down unmarshalling noticeably, but I doubt it). The callback should probably be stored in the per-interpreter state.

added a commit that references this issue on Nov 23, 2022

21 remaining items

Loading
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

        Participants

        @itamaro@gvanrossum@pablogsal

        Issue actions

          Add API to allow extensions to set callback function on creation and destruction of PyCodeObject · Issue #91054 · python/cpython