| 1 | /* Function object interface */ |
| 2 | |
| 3 | #ifndef Py_LIMITED_API |
| 4 | #ifndef Py_FUNCOBJECT_H |
| 5 | #define Py_FUNCOBJECT_H |
| 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
| 10 | |
| 11 | #define COMMON_FIELDS(PREFIX) \ |
| 12 | PyObject *PREFIX ## globals; \ |
| 13 | PyObject *PREFIX ## builtins; \ |
| 14 | PyObject *PREFIX ## name; \ |
| 15 | PyObject *PREFIX ## qualname; \ |
| 16 | PyObject *PREFIX ## code; /* A code object, the __code__ attribute */ \ |
| 17 | PyObject *PREFIX ## defaults; /* NULL or a tuple */ \ |
| 18 | PyObject *PREFIX ## kwdefaults; /* NULL or a dict */ \ |
| 19 | PyObject *PREFIX ## closure; /* NULL or a tuple of cell objects */ |
| 20 | |
| 21 | typedef struct { |
| 22 | COMMON_FIELDS(fc_) |
| 23 | } PyFrameConstructor; |
| 24 | |
| 25 | /* Function objects and code objects should not be confused with each other: |
| 26 | * |
| 27 | * Function objects are created by the execution of the 'def' statement. |
| 28 | * They reference a code object in their __code__ attribute, which is a |
| 29 | * purely syntactic object, i.e. nothing more than a compiled version of some |
| 30 | * source code lines. There is one code object per source code "fragment", |
| 31 | * but each code object can be referenced by zero or many function objects |
| 32 | * depending only on how many times the 'def' statement in the source was |
| 33 | * executed so far. |
| 34 | */ |
| 35 | |
| 36 | typedef struct { |
| 37 | PyObject_HEAD |
| 38 | COMMON_FIELDS(func_) |
| 39 | PyObject *func_doc; /* The __doc__ attribute, can be anything */ |
| 40 | PyObject *func_dict; /* The __dict__ attribute, a dict or NULL */ |
| 41 | PyObject *func_weakreflist; /* List of weak references */ |
| 42 | PyObject *func_module; /* The __module__ attribute, can be anything */ |
| 43 | PyObject *func_annotations; /* Annotations, a dict or NULL */ |
| 44 | PyObject *func_typeparams; /* Tuple of active type variables or NULL */ |
| 45 | vectorcallfunc vectorcall; |
| 46 | /* Version number for use by specializer. |
| 47 | * Can set to non-zero when we want to specialize. |
| 48 | * Will be set to zero if any of these change: |
| 49 | * defaults |
| 50 | * kwdefaults (only if the object changes, not the contents of the dict) |
| 51 | * code |
| 52 | * annotations |
| 53 | * vectorcall function pointer */ |
| 54 | uint32_t func_version; |
| 55 | |
| 56 | /* Invariant: |
| 57 | * func_closure contains the bindings for func_code->co_freevars, so |
| 58 | * PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code) |
| 59 | * (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0). |
| 60 | */ |
| 61 | } PyFunctionObject; |
| 62 | |
| 63 | PyAPI_DATA(PyTypeObject) PyFunction_Type; |
| 64 | |
| 65 | #define PyFunction_Check(op) Py_IS_TYPE((op), &PyFunction_Type) |
| 66 | |
| 67 | PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *); |
| 68 | PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *); |
| 69 | PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *); |
| 70 | PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *); |
| 71 | PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *); |
| 72 | PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *); |
| 73 | PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *); |
| 74 | PyAPI_FUNC(void) PyFunction_SetVectorcall(PyFunctionObject *, vectorcallfunc); |
| 75 | PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *); |
| 76 | PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *); |
| 77 | PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *); |
| 78 | PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *); |
| 79 | PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *); |
| 80 | PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *); |
| 81 | |
| 82 | PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall( |
| 83 | PyObject *func, |
| 84 | PyObject *const *stack, |
| 85 | size_t nargsf, |
| 86 | PyObject *kwnames); |
| 87 | |
| 88 | #define _PyFunction_CAST(func) \ |
| 89 | (assert(PyFunction_Check(func)), _Py_CAST(PyFunctionObject*, func)) |
| 90 | |
| 91 | /* Static inline functions for direct access to these values. |
| 92 | Type checks are *not* done, so use with care. */ |
| 93 | static inline PyObject* PyFunction_GET_CODE(PyObject *func) { |
| 94 | return _PyFunction_CAST(func)->func_code; |
| 95 | } |
| 96 | #define PyFunction_GET_CODE(func) PyFunction_GET_CODE(_PyObject_CAST(func)) |
| 97 | |
| 98 | static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) { |
| 99 | return _PyFunction_CAST(func)->func_globals; |
| 100 | } |
| 101 | #define PyFunction_GET_GLOBALS(func) PyFunction_GET_GLOBALS(_PyObject_CAST(func)) |
| 102 | |
| 103 | static inline PyObject* PyFunction_GET_MODULE(PyObject *func) { |
| 104 | return _PyFunction_CAST(func)->func_module; |
| 105 | } |
| 106 | #define PyFunction_GET_MODULE(func) PyFunction_GET_MODULE(_PyObject_CAST(func)) |
| 107 | |
| 108 | static inline PyObject* PyFunction_GET_DEFAULTS(PyObject *func) { |
| 109 | return _PyFunction_CAST(func)->func_defaults; |
| 110 | } |
| 111 | #define PyFunction_GET_DEFAULTS(func) PyFunction_GET_DEFAULTS(_PyObject_CAST(func)) |
| 112 | |
| 113 | static inline PyObject* PyFunction_GET_KW_DEFAULTS(PyObject *func) { |
| 114 | return _PyFunction_CAST(func)->func_kwdefaults; |
| 115 | } |
| 116 | #define PyFunction_GET_KW_DEFAULTS(func) PyFunction_GET_KW_DEFAULTS(_PyObject_CAST(func)) |
| 117 | |
| 118 | static inline PyObject* PyFunction_GET_CLOSURE(PyObject *func) { |
| 119 | return _PyFunction_CAST(func)->func_closure; |
| 120 | } |
| 121 | #define PyFunction_GET_CLOSURE(func) PyFunction_GET_CLOSURE(_PyObject_CAST(func)) |
| 122 | |
| 123 | static inline PyObject* PyFunction_GET_ANNOTATIONS(PyObject *func) { |
| 124 | return _PyFunction_CAST(func)->func_annotations; |
| 125 | } |
| 126 | #define PyFunction_GET_ANNOTATIONS(func) PyFunction_GET_ANNOTATIONS(_PyObject_CAST(func)) |
| 127 | |
| 128 | /* The classmethod and staticmethod types lives here, too */ |
| 129 | PyAPI_DATA(PyTypeObject) PyClassMethod_Type; |
| 130 | PyAPI_DATA(PyTypeObject) PyStaticMethod_Type; |
| 131 | |
| 132 | PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *); |
| 133 | PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *); |
| 134 | |
| 135 | #define PY_FOREACH_FUNC_EVENT(V) \ |
| 136 | V(CREATE) \ |
| 137 | V(DESTROY) \ |
| 138 | V(MODIFY_CODE) \ |
| 139 | V(MODIFY_DEFAULTS) \ |
| 140 | V(MODIFY_KWDEFAULTS) |
| 141 | |
| 142 | typedef enum { |
| 143 | #define PY_DEF_EVENT(EVENT) PyFunction_EVENT_##EVENT, |
| 144 | PY_FOREACH_FUNC_EVENT(PY_DEF_EVENT) |
| 145 | #undef PY_DEF_EVENT |
| 146 | } PyFunction_WatchEvent; |
| 147 | |
| 148 | /* |
| 149 | * A callback that is invoked for different events in a function's lifecycle. |
| 150 | * |
| 151 | * The callback is invoked with a borrowed reference to func, after it is |
| 152 | * created and before it is modified or destroyed. The callback should not |
| 153 | * modify func. |
| 154 | * |
| 155 | * When a function's code object, defaults, or kwdefaults are modified the |
| 156 | * callback will be invoked with the respective event and new_value will |
| 157 | * contain a borrowed reference to the new value that is about to be stored in |
| 158 | * the function. Otherwise the third argument is NULL. |
| 159 | * |
| 160 | * If the callback returns with an exception set, it must return -1. Otherwise |
| 161 | * it should return 0. |
| 162 | */ |
| 163 | typedef int (*PyFunction_WatchCallback)( |
| 164 | PyFunction_WatchEvent event, |
| 165 | PyFunctionObject *func, |
| 166 | PyObject *new_value); |
| 167 | |
| 168 | /* |
| 169 | * Register a per-interpreter callback that will be invoked for function lifecycle |
| 170 | * events. |
| 171 | * |
| 172 | * Returns a handle that may be passed to PyFunction_ClearWatcher on success, |
| 173 | * or -1 and sets an error if no more handles are available. |
| 174 | */ |
| 175 | PyAPI_FUNC(int) PyFunction_AddWatcher(PyFunction_WatchCallback callback); |
| 176 | |
| 177 | /* |
| 178 | * Clear the watcher associated with the watcher_id handle. |
| 179 | * |
| 180 | * Returns 0 on success or -1 if no watcher exists for the supplied id. |
| 181 | */ |
| 182 | PyAPI_FUNC(int) PyFunction_ClearWatcher(int watcher_id); |
| 183 | |
| 184 | #ifdef __cplusplus |
| 185 | } |
| 186 | #endif |
| 187 | #endif /* !Py_FUNCOBJECT_H */ |
| 188 | #endif /* Py_LIMITED_API */ |
| 189 | |