CPython extensions providing optimized execution of Python bytecode (e.g. the Cinder JIT) may need to hook into the lifecycle of function objects to determine what to optimize, invalidate previously-optimized functions, or free resources allocated for functions that no longer exist. For example, when inlining a function, the Cinder JIT will use the bytecode of the inlined function that was known at compile-time. If the bytecode for the inlined function changes at runtime (i.e. if __code__ was reassigned) the JIT needs to invalidate any code into which the function was inlined. We propose adding an API to allow extensions to set callbacks that will be invoked whenever functions are created, modified, or destroyed.
Proposed API:
```
typedef enum {
PYFUNC_LCEVT_CREATED,
PYFUNC_LCEVT_MODIFIED,
PYFUNC_LCEVT_DESTROYED
} PyFunction_LifecycleEvent;
typedef enum {
PYFUNC_ATTR_CODE,
PYFUNC_ATTR_GLOBALS,
PYFUNC_ATTR_DEFAULTS,
PYFUNC_ATTR_KWDEFAULTS,
PYFUNC_ATTR_CLOSURE,
PYFUNC_ATTR_NOT_APPLICABLE,
} PyFunction_AttrId;
// A callback to be called in response to events in a function's lifecycle.
//
// The callback is invoked after a function is created and before the function
// is modified or destroyed.
//
// On modification the third argument indicates which attribute was modified
// and the fourth argument is the new value.
// Otherwise the third argument is PYFUNC_ATTR_NOT_APPLICABLE and the fourth
// argument is NULL.
typedef void(*PyFunction_LifecycleCallback)(
PyFunction_LifecycleEvent event,
PyFunctionObject* func,
PyFunction_AttrId attr,
PyObject* new_value);
void PyFunction_SetLifecycleCallback(PyFunction_LifecycleCallback callback);
PyFunction_LifecycleCallback PyFunction_GetLifecycleCallback();
```
|