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     vectorcallfunc vectorcall;
45     /* Version number for use by specializer.
46      * Can set to non-zero when we want to specialize.
47      * Will be set to zero if any of these change:
48      *     defaults
49      *     kwdefaults (only if the object changes, not the contents of the dict)
50      *     code
51      *     annotations */
52     uint32_t func_version;
53 
54     /* Invariant:
55      *     func_closure contains the bindings for func_code->co_freevars, so
56      *     PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)
57      *     (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).
58      */
59 } PyFunctionObject;
60 
61 PyAPI_DATA(PyTypeObject) PyFunction_Type;
62 
63 #define PyFunction_Check(op) Py_IS_TYPE(op, &PyFunction_Type)
64 
65 PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
66 PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
67 PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
68 PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
69 PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
70 PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
71 PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
72 PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
73 PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
74 PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
75 PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
76 PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
77 PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
78 
79 PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall(
80     PyObject *func,
81     PyObject *const *stack,
82     size_t nargsf,
83     PyObject *kwnames);
84 
85 /* Macros for direct access to these values. Type checks are *not*
86    done, so use with care. */
87 #define PyFunction_GET_CODE(func) \
88         (((PyFunctionObject *)func) -> func_code)
89 #define PyFunction_GET_GLOBALS(func) \
90         (((PyFunctionObject *)func) -> func_globals)
91 #define PyFunction_GET_MODULE(func) \
92         (((PyFunctionObject *)func) -> func_module)
93 #define PyFunction_GET_DEFAULTS(func) \
94         (((PyFunctionObject *)func) -> func_defaults)
95 #define PyFunction_GET_KW_DEFAULTS(func) \
96         (((PyFunctionObject *)func) -> func_kwdefaults)
97 #define PyFunction_GET_CLOSURE(func) \
98         (((PyFunctionObject *)func) -> func_closure)
99 #define PyFunction_GET_ANNOTATIONS(func) \
100         (((PyFunctionObject *)func) -> func_annotations)
101 
102 /* The classmethod and staticmethod types lives here, too */
103 PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
104 PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
105 
106 PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
107 PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 #endif /* !Py_FUNCOBJECT_H */
113 #endif /* Py_LIMITED_API */
114