codekingpro/portable-devtools
115k
1/* Function object interface */2 3#ifndef Py_LIMITED_API4#ifndef Py_FUNCOBJECT_H5#define Py_FUNCOBJECT_H6#ifdef __cplusplus7extern "C" {8#endif9 10 11#define _Py_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 21typedef struct {22 _Py_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 a29 * purely syntactic object, i.e. nothing more than a compiled version of some30 * 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 objects32 * depending only on how many times the 'def' statement in the source was33 * executed so far.34 */35 36typedef struct {37 PyObject_HEAD38 _Py_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_annotate; /* Callable to fill the annotations dictionary */45 PyObject *func_typeparams; /* Tuple of active type variables or NULL */46 vectorcallfunc vectorcall;47 /* Version number for use by specializer.48 * Can set to non-zero when we want to specialize.49 * Will be set to zero if any of these change:50 * defaults51 * kwdefaults (only if the object changes, not the contents of the dict)52 * code53 * annotations54 * vectorcall function pointer */55 uint32_t func_version;56 57 /* Invariant:58 * func_closure contains the bindings for func_code->co_freevars, so59 * PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)60 * (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).61 */62} PyFunctionObject;63 64#undef _Py_COMMON_FIELDS65 66PyAPI_DATA(PyTypeObject) PyFunction_Type;67 68#define PyFunction_Check(op) Py_IS_TYPE((op), &PyFunction_Type)69 70PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);71PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);72PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);73PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);74PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);75PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);76PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);77PyAPI_FUNC(void) PyFunction_SetVectorcall(PyFunctionObject *, vectorcallfunc);78PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);79PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);80PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);81PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);82PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);83PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);84 85#define _PyFunction_CAST(func) \86 (assert(PyFunction_Check(func)), _Py_CAST(PyFunctionObject*, func))87 88/* Static inline functions for direct access to these values.89 Type checks are *not* done, so use with care. */90static inline PyObject* PyFunction_GET_CODE(PyObject *func) {91 return _PyFunction_CAST(func)->func_code;92}93#define PyFunction_GET_CODE(func) PyFunction_GET_CODE(_PyObject_CAST(func))94 95static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) {96 return _PyFunction_CAST(func)->func_globals;97}98#define PyFunction_GET_GLOBALS(func) PyFunction_GET_GLOBALS(_PyObject_CAST(func))99 100static inline PyObject* PyFunction_GET_MODULE(PyObject *func) {101 return _PyFunction_CAST(func)->func_module;102}103#define PyFunction_GET_MODULE(func) PyFunction_GET_MODULE(_PyObject_CAST(func))104 105static inline PyObject* PyFunction_GET_DEFAULTS(PyObject *func) {106 return _PyFunction_CAST(func)->func_defaults;107}108#define PyFunction_GET_DEFAULTS(func) PyFunction_GET_DEFAULTS(_PyObject_CAST(func))109 110static inline PyObject* PyFunction_GET_KW_DEFAULTS(PyObject *func) {111 return _PyFunction_CAST(func)->func_kwdefaults;112}113#define PyFunction_GET_KW_DEFAULTS(func) PyFunction_GET_KW_DEFAULTS(_PyObject_CAST(func))114 115static inline PyObject* PyFunction_GET_CLOSURE(PyObject *func) {116 return _PyFunction_CAST(func)->func_closure;117}118#define PyFunction_GET_CLOSURE(func) PyFunction_GET_CLOSURE(_PyObject_CAST(func))119 120static inline PyObject* PyFunction_GET_ANNOTATIONS(PyObject *func) {121 return _PyFunction_CAST(func)->func_annotations;122}123#define PyFunction_GET_ANNOTATIONS(func) PyFunction_GET_ANNOTATIONS(_PyObject_CAST(func))124 125/* The classmethod and staticmethod types lives here, too */126PyAPI_DATA(PyTypeObject) PyClassMethod_Type;127PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;128 129PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);130PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);131 132#define PY_FOREACH_FUNC_EVENT(V) \133 V(CREATE) \134 V(DESTROY) \135 V(MODIFY_CODE) \136 V(MODIFY_DEFAULTS) \137 V(MODIFY_KWDEFAULTS)138 139typedef enum {140 #define PY_DEF_EVENT(EVENT) PyFunction_EVENT_##EVENT,141 PY_FOREACH_FUNC_EVENT(PY_DEF_EVENT)142 #undef PY_DEF_EVENT143} PyFunction_WatchEvent;144 145/*146 * A callback that is invoked for different events in a function's lifecycle.147 *148 * The callback is invoked with a borrowed reference to func, after it is149 * created and before it is modified or destroyed. The callback should not150 * modify func.151 *152 * When a function's code object, defaults, or kwdefaults are modified the153 * callback will be invoked with the respective event and new_value will154 * contain a borrowed reference to the new value that is about to be stored in155 * the function. Otherwise the third argument is NULL.156 *157 * If the callback returns with an exception set, it must return -1. Otherwise158 * it should return 0.159 */160typedef int (*PyFunction_WatchCallback)(161 PyFunction_WatchEvent event,162 PyFunctionObject *func,163 PyObject *new_value);164 165/*166 * Register a per-interpreter callback that will be invoked for function lifecycle167 * events.168 *169 * Returns a handle that may be passed to PyFunction_ClearWatcher on success,170 * or -1 and sets an error if no more handles are available.171 */172PyAPI_FUNC(int) PyFunction_AddWatcher(PyFunction_WatchCallback callback);173 174/*175 * Clear the watcher associated with the watcher_id handle.176 *177 * Returns 0 on success or -1 if no watcher exists for the supplied id.178 */179PyAPI_FUNC(int) PyFunction_ClearWatcher(int watcher_id);180 181#ifdef __cplusplus182}183#endif184#endif /* !Py_FUNCOBJECT_H */185#endif /* Py_LIMITED_API */186 