• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Function object interface */
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 
46     /* Invariant:
47      *     func_closure contains the bindings for func_code->co_freevars, so
48      *     PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)
49      *     (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).
50      */
51 } PyFunctionObject;
52 
53 PyAPI_DATA(PyTypeObject) PyFunction_Type;
54 
55 #define PyFunction_Check(op) Py_IS_TYPE(op, &PyFunction_Type)
56 
57 PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
58 PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
59 PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
60 PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
61 PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
62 PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
63 PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
64 PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
65 PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
66 PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
67 PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
68 PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
69 PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
70 
71 #ifndef Py_LIMITED_API
72 PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall(
73     PyObject *func,
74     PyObject *const *stack,
75     size_t nargsf,
76     PyObject *kwnames);
77 #endif
78 
79 /* Macros for direct access to these values. Type checks are *not*
80    done, so use with care. */
81 #define PyFunction_GET_CODE(func) \
82         (((PyFunctionObject *)func) -> func_code)
83 #define PyFunction_GET_GLOBALS(func) \
84         (((PyFunctionObject *)func) -> func_globals)
85 #define PyFunction_GET_MODULE(func) \
86         (((PyFunctionObject *)func) -> func_module)
87 #define PyFunction_GET_DEFAULTS(func) \
88         (((PyFunctionObject *)func) -> func_defaults)
89 #define PyFunction_GET_KW_DEFAULTS(func) \
90         (((PyFunctionObject *)func) -> func_kwdefaults)
91 #define PyFunction_GET_CLOSURE(func) \
92         (((PyFunctionObject *)func) -> func_closure)
93 #define PyFunction_GET_ANNOTATIONS(func) \
94         (((PyFunctionObject *)func) -> func_annotations)
95 
96 #define PyFunction_AS_FRAME_CONSTRUCTOR(func) \
97         ((PyFrameConstructor *)&((PyFunctionObject *)(func))->func_globals)
98 
99 /* The classmethod and staticmethod types lives here, too */
100 PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
101 PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
102 
103 PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
104 PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 #endif /* !Py_FUNCOBJECT_H */
110 #endif /* Py_LIMITED_API */
111