blob: d7acd18c6519e486c416771dcbdbfc09bcf4b9c4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Function object interface */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003#ifndef Py_LIMITED_API
Fred Drakeea9cb5a2000-07-09 00:20:36 +00004#ifndef Py_FUNCOBJECT_H
5#define Py_FUNCOBJECT_H
6#ifdef __cplusplus
7extern "C" {
8#endif
9
Mark Shannond6c33fb2021-01-29 13:24:55 +000010
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
21typedef struct {
22 COMMON_FIELDS(fc_)
23} PyFrameConstructor;
24
Armin Rigo89a39462004-10-28 16:32:00 +000025/* 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.
Neal Norwitz8ced9612007-08-25 17:01:41 +000028 * They reference a code object in their __code__ attribute, which is a
Armin Rigo89a39462004-10-28 16:32:00 +000029 * 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
Guido van Rossum25831651993-05-19 14:50:45 +000036typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000037 PyObject_HEAD
Mark Shannond6c33fb2021-01-29 13:24:55 +000038 COMMON_FIELDS(func_)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020039 PyObject *func_doc; /* The __doc__ attribute, can be anything */
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020040 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 */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020044 vectorcallfunc vectorcall;
Armin Rigo89a39462004-10-28 16:32:00 +000045
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 */
Guido van Rossumcaa63801995-01-12 11:45:45 +000051} PyFunctionObject;
Guido van Rossum25831651993-05-19 14:50:45 +000052
Mark Hammond91a681d2002-08-12 07:21:58 +000053PyAPI_DATA(PyTypeObject) PyFunction_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054
Dong-hee Nad905df72020-02-14 02:37:17 +090055#define PyFunction_Check(op) Py_IS_TYPE(op, &PyFunction_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056
Mark Hammond91a681d2002-08-12 07:21:58 +000057PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
Antoine Pitrou86a36b52011-11-25 18:56:07 +010058PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000059PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
60PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000061PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000062PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
63PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
Guido van Rossum4f72a782006-10-27 23:31:49 +000064PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
65PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000066PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
67PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
Neal Norwitzc1505362006-12-28 06:47:50 +000068PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
69PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
Guido van Rossuma3309961993-07-28 09:05:47 +000070
Victor Stinner9be7e7b2016-08-19 16:11:43 +020071#ifndef Py_LIMITED_API
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020072PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall(
Victor Stinnerd8735722016-09-09 12:36:44 -070073 PyObject *func,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020074 PyObject *const *stack,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020075 size_t nargsf,
Victor Stinnerd8735722016-09-09 12:36:44 -070076 PyObject *kwnames);
Victor Stinner9be7e7b2016-08-19 16:11:43 +020077#endif
78
Guido van Rossumba0d0611998-07-10 15:47:08 +000079/* 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) \
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020084 (((PyFunctionObject *)func) -> func_globals)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000085#define PyFunction_GET_MODULE(func) \
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020086 (((PyFunctionObject *)func) -> func_module)
Guido van Rossumba0d0611998-07-10 15:47:08 +000087#define PyFunction_GET_DEFAULTS(func) \
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020088 (((PyFunctionObject *)func) -> func_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +000089#define PyFunction_GET_KW_DEFAULTS(func) \
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020090 (((PyFunctionObject *)func) -> func_kwdefaults)
Jeremy Hylton64949cb2001-01-25 20:06:59 +000091#define PyFunction_GET_CLOSURE(func) \
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020092 (((PyFunctionObject *)func) -> func_closure)
Neal Norwitzc1505362006-12-28 06:47:50 +000093#define PyFunction_GET_ANNOTATIONS(func) \
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020094 (((PyFunctionObject *)func) -> func_annotations)
Guido van Rossumba0d0611998-07-10 15:47:08 +000095
Mark Shannond6c33fb2021-01-29 13:24:55 +000096#define PyFunction_AS_FRAME_CONSTRUCTOR(func) \
97 ((PyFrameConstructor *)&((PyFunctionObject *)(func))->func_globals)
98
Tim Peters6d6c1a32001-08-02 04:15:00 +000099/* The classmethod and staticmethod types lives here, too */
Mark Hammond91a681d2002-08-12 07:21:58 +0000100PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
101PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000102
Mark Hammond91a681d2002-08-12 07:21:58 +0000103PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
104PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000105
Guido van Rossuma3309961993-07-28 09:05:47 +0000106#ifdef __cplusplus
107}
108#endif
109#endif /* !Py_FUNCOBJECT_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000110#endif /* Py_LIMITED_API */