Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:41

0001 /* Function object interface */
0002 
0003 #ifndef Py_LIMITED_API
0004 #ifndef Py_FUNCOBJECT_H
0005 #define Py_FUNCOBJECT_H
0006 #ifdef __cplusplus
0007 extern "C" {
0008 #endif
0009 
0010 
0011 #define _Py_COMMON_FIELDS(PREFIX) \
0012     PyObject *PREFIX ## globals; \
0013     PyObject *PREFIX ## builtins; \
0014     PyObject *PREFIX ## name; \
0015     PyObject *PREFIX ## qualname; \
0016     PyObject *PREFIX ## code;        /* A code object, the __code__ attribute */ \
0017     PyObject *PREFIX ## defaults;    /* NULL or a tuple */ \
0018     PyObject *PREFIX ## kwdefaults;  /* NULL or a dict */ \
0019     PyObject *PREFIX ## closure;     /* NULL or a tuple of cell objects */
0020 
0021 typedef struct {
0022     _Py_COMMON_FIELDS(fc_)
0023 } PyFrameConstructor;
0024 
0025 /* Function objects and code objects should not be confused with each other:
0026  *
0027  * Function objects are created by the execution of the 'def' statement.
0028  * They reference a code object in their __code__ attribute, which is a
0029  * purely syntactic object, i.e. nothing more than a compiled version of some
0030  * source code lines.  There is one code object per source code "fragment",
0031  * but each code object can be referenced by zero or many function objects
0032  * depending only on how many times the 'def' statement in the source was
0033  * executed so far.
0034  */
0035 
0036 typedef struct {
0037     PyObject_HEAD
0038     _Py_COMMON_FIELDS(func_)
0039     PyObject *func_doc;         /* The __doc__ attribute, can be anything */
0040     PyObject *func_dict;        /* The __dict__ attribute, a dict or NULL */
0041     PyObject *func_weakreflist; /* List of weak references */
0042     PyObject *func_module;      /* The __module__ attribute, can be anything */
0043     PyObject *func_annotations; /* Annotations, a dict or NULL */
0044     PyObject *func_typeparams;  /* Tuple of active type variables or NULL */
0045     vectorcallfunc vectorcall;
0046     /* Version number for use by specializer.
0047      * Can set to non-zero when we want to specialize.
0048      * Will be set to zero if any of these change:
0049      *     defaults
0050      *     kwdefaults (only if the object changes, not the contents of the dict)
0051      *     code
0052      *     annotations
0053      *     vectorcall function pointer */
0054     uint32_t func_version;
0055 
0056     /* Invariant:
0057      *     func_closure contains the bindings for func_code->co_freevars, so
0058      *     PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)
0059      *     (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).
0060      */
0061 } PyFunctionObject;
0062 
0063 #undef _Py_COMMON_FIELDS
0064 
0065 PyAPI_DATA(PyTypeObject) PyFunction_Type;
0066 
0067 #define PyFunction_Check(op) Py_IS_TYPE((op), &PyFunction_Type)
0068 
0069 PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
0070 PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
0071 PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
0072 PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
0073 PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
0074 PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
0075 PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
0076 PyAPI_FUNC(void) PyFunction_SetVectorcall(PyFunctionObject *, vectorcallfunc);
0077 PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
0078 PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
0079 PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
0080 PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
0081 PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
0082 PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
0083 
0084 #define _PyFunction_CAST(func) \
0085     (assert(PyFunction_Check(func)), _Py_CAST(PyFunctionObject*, func))
0086 
0087 /* Static inline functions for direct access to these values.
0088    Type checks are *not* done, so use with care. */
0089 static inline PyObject* PyFunction_GET_CODE(PyObject *func) {
0090     return _PyFunction_CAST(func)->func_code;
0091 }
0092 #define PyFunction_GET_CODE(func) PyFunction_GET_CODE(_PyObject_CAST(func))
0093 
0094 static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) {
0095     return _PyFunction_CAST(func)->func_globals;
0096 }
0097 #define PyFunction_GET_GLOBALS(func) PyFunction_GET_GLOBALS(_PyObject_CAST(func))
0098 
0099 static inline PyObject* PyFunction_GET_MODULE(PyObject *func) {
0100     return _PyFunction_CAST(func)->func_module;
0101 }
0102 #define PyFunction_GET_MODULE(func) PyFunction_GET_MODULE(_PyObject_CAST(func))
0103 
0104 static inline PyObject* PyFunction_GET_DEFAULTS(PyObject *func) {
0105     return _PyFunction_CAST(func)->func_defaults;
0106 }
0107 #define PyFunction_GET_DEFAULTS(func) PyFunction_GET_DEFAULTS(_PyObject_CAST(func))
0108 
0109 static inline PyObject* PyFunction_GET_KW_DEFAULTS(PyObject *func) {
0110     return _PyFunction_CAST(func)->func_kwdefaults;
0111 }
0112 #define PyFunction_GET_KW_DEFAULTS(func) PyFunction_GET_KW_DEFAULTS(_PyObject_CAST(func))
0113 
0114 static inline PyObject* PyFunction_GET_CLOSURE(PyObject *func) {
0115     return _PyFunction_CAST(func)->func_closure;
0116 }
0117 #define PyFunction_GET_CLOSURE(func) PyFunction_GET_CLOSURE(_PyObject_CAST(func))
0118 
0119 static inline PyObject* PyFunction_GET_ANNOTATIONS(PyObject *func) {
0120     return _PyFunction_CAST(func)->func_annotations;
0121 }
0122 #define PyFunction_GET_ANNOTATIONS(func) PyFunction_GET_ANNOTATIONS(_PyObject_CAST(func))
0123 
0124 /* The classmethod and staticmethod types lives here, too */
0125 PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
0126 PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
0127 
0128 PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
0129 PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
0130 
0131 #define PY_FOREACH_FUNC_EVENT(V) \
0132     V(CREATE)                    \
0133     V(DESTROY)                   \
0134     V(MODIFY_CODE)               \
0135     V(MODIFY_DEFAULTS)           \
0136     V(MODIFY_KWDEFAULTS)
0137 
0138 typedef enum {
0139     #define PY_DEF_EVENT(EVENT) PyFunction_EVENT_##EVENT,
0140     PY_FOREACH_FUNC_EVENT(PY_DEF_EVENT)
0141     #undef PY_DEF_EVENT
0142 } PyFunction_WatchEvent;
0143 
0144 /*
0145  * A callback that is invoked for different events in a function's lifecycle.
0146  *
0147  * The callback is invoked with a borrowed reference to func, after it is
0148  * created and before it is modified or destroyed. The callback should not
0149  * modify func.
0150  *
0151  * When a function's code object, defaults, or kwdefaults are modified the
0152  * callback will be invoked with the respective event and new_value will
0153  * contain a borrowed reference to the new value that is about to be stored in
0154  * the function. Otherwise the third argument is NULL.
0155  *
0156  * If the callback returns with an exception set, it must return -1. Otherwise
0157  * it should return 0.
0158  */
0159 typedef int (*PyFunction_WatchCallback)(
0160   PyFunction_WatchEvent event,
0161   PyFunctionObject *func,
0162   PyObject *new_value);
0163 
0164 /*
0165  * Register a per-interpreter callback that will be invoked for function lifecycle
0166  * events.
0167  *
0168  * Returns a handle that may be passed to PyFunction_ClearWatcher on success,
0169  * or -1 and sets an error if no more handles are available.
0170  */
0171 PyAPI_FUNC(int) PyFunction_AddWatcher(PyFunction_WatchCallback callback);
0172 
0173 /*
0174  * Clear the watcher associated with the watcher_id handle.
0175  *
0176  * Returns 0 on success or -1 if no watcher exists for the supplied id.
0177  */
0178 PyAPI_FUNC(int) PyFunction_ClearWatcher(int watcher_id);
0179 
0180 #ifdef __cplusplus
0181 }
0182 #endif
0183 #endif /* !Py_FUNCOBJECT_H */
0184 #endif /* Py_LIMITED_API */