Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:40

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 PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall(
0085     PyObject *func,
0086     PyObject *const *stack,
0087     size_t nargsf,
0088     PyObject *kwnames);
0089 
0090 #define _PyFunction_CAST(func) \
0091     (assert(PyFunction_Check(func)), _Py_CAST(PyFunctionObject*, func))
0092 
0093 /* Static inline functions for direct access to these values.
0094    Type checks are *not* done, so use with care. */
0095 static inline PyObject* PyFunction_GET_CODE(PyObject *func) {
0096     return _PyFunction_CAST(func)->func_code;
0097 }
0098 #define PyFunction_GET_CODE(func) PyFunction_GET_CODE(_PyObject_CAST(func))
0099 
0100 static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) {
0101     return _PyFunction_CAST(func)->func_globals;
0102 }
0103 #define PyFunction_GET_GLOBALS(func) PyFunction_GET_GLOBALS(_PyObject_CAST(func))
0104 
0105 static inline PyObject* PyFunction_GET_MODULE(PyObject *func) {
0106     return _PyFunction_CAST(func)->func_module;
0107 }
0108 #define PyFunction_GET_MODULE(func) PyFunction_GET_MODULE(_PyObject_CAST(func))
0109 
0110 static inline PyObject* PyFunction_GET_DEFAULTS(PyObject *func) {
0111     return _PyFunction_CAST(func)->func_defaults;
0112 }
0113 #define PyFunction_GET_DEFAULTS(func) PyFunction_GET_DEFAULTS(_PyObject_CAST(func))
0114 
0115 static inline PyObject* PyFunction_GET_KW_DEFAULTS(PyObject *func) {
0116     return _PyFunction_CAST(func)->func_kwdefaults;
0117 }
0118 #define PyFunction_GET_KW_DEFAULTS(func) PyFunction_GET_KW_DEFAULTS(_PyObject_CAST(func))
0119 
0120 static inline PyObject* PyFunction_GET_CLOSURE(PyObject *func) {
0121     return _PyFunction_CAST(func)->func_closure;
0122 }
0123 #define PyFunction_GET_CLOSURE(func) PyFunction_GET_CLOSURE(_PyObject_CAST(func))
0124 
0125 static inline PyObject* PyFunction_GET_ANNOTATIONS(PyObject *func) {
0126     return _PyFunction_CAST(func)->func_annotations;
0127 }
0128 #define PyFunction_GET_ANNOTATIONS(func) PyFunction_GET_ANNOTATIONS(_PyObject_CAST(func))
0129 
0130 /* The classmethod and staticmethod types lives here, too */
0131 PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
0132 PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
0133 
0134 PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
0135 PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
0136 
0137 #define PY_FOREACH_FUNC_EVENT(V) \
0138     V(CREATE)                    \
0139     V(DESTROY)                   \
0140     V(MODIFY_CODE)               \
0141     V(MODIFY_DEFAULTS)           \
0142     V(MODIFY_KWDEFAULTS)
0143 
0144 typedef enum {
0145     #define PY_DEF_EVENT(EVENT) PyFunction_EVENT_##EVENT,
0146     PY_FOREACH_FUNC_EVENT(PY_DEF_EVENT)
0147     #undef PY_DEF_EVENT
0148 } PyFunction_WatchEvent;
0149 
0150 /*
0151  * A callback that is invoked for different events in a function's lifecycle.
0152  *
0153  * The callback is invoked with a borrowed reference to func, after it is
0154  * created and before it is modified or destroyed. The callback should not
0155  * modify func.
0156  *
0157  * When a function's code object, defaults, or kwdefaults are modified the
0158  * callback will be invoked with the respective event and new_value will
0159  * contain a borrowed reference to the new value that is about to be stored in
0160  * the function. Otherwise the third argument is NULL.
0161  *
0162  * If the callback returns with an exception set, it must return -1. Otherwise
0163  * it should return 0.
0164  */
0165 typedef int (*PyFunction_WatchCallback)(
0166   PyFunction_WatchEvent event,
0167   PyFunctionObject *func,
0168   PyObject *new_value);
0169 
0170 /*
0171  * Register a per-interpreter callback that will be invoked for function lifecycle
0172  * events.
0173  *
0174  * Returns a handle that may be passed to PyFunction_ClearWatcher on success,
0175  * or -1 and sets an error if no more handles are available.
0176  */
0177 PyAPI_FUNC(int) PyFunction_AddWatcher(PyFunction_WatchCallback callback);
0178 
0179 /*
0180  * Clear the watcher associated with the watcher_id handle.
0181  *
0182  * Returns 0 on success or -1 if no watcher exists for the supplied id.
0183  */
0184 PyAPI_FUNC(int) PyFunction_ClearWatcher(int watcher_id);
0185 
0186 #ifdef __cplusplus
0187 }
0188 #endif
0189 #endif /* !Py_FUNCOBJECT_H */
0190 #endif /* Py_LIMITED_API */