blob: 77bb8c39aeb7500528d464d5c31ad0a0d0bed03c [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
Armin Rigo89a39462004-10-28 16:32:00 +000010/* Function objects and code objects should not be confused with each other:
11 *
12 * Function objects are created by the execution of the 'def' statement.
Neal Norwitz8ced9612007-08-25 17:01:41 +000013 * They reference a code object in their __code__ attribute, which is a
Armin Rigo89a39462004-10-28 16:32:00 +000014 * purely syntactic object, i.e. nothing more than a compiled version of some
15 * source code lines. There is one code object per source code "fragment",
16 * but each code object can be referenced by zero or many function objects
17 * depending only on how many times the 'def' statement in the source was
18 * executed so far.
19 */
20
Guido van Rossum25831651993-05-19 14:50:45 +000021typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000022 PyObject_HEAD
Neal Norwitz8ced9612007-08-25 17:01:41 +000023 PyObject *func_code; /* A code object, the __code__ attribute */
Armin Rigo89a39462004-10-28 16:32:00 +000024 PyObject *func_globals; /* A dictionary (other mappings won't do) */
25 PyObject *func_defaults; /* NULL or a tuple */
Guido van Rossum4f72a782006-10-27 23:31:49 +000026 PyObject *func_kwdefaults; /* NULL or a dict */
Armin Rigo89a39462004-10-28 16:32:00 +000027 PyObject *func_closure; /* NULL or a tuple of cell objects */
28 PyObject *func_doc; /* The __doc__ attribute, can be anything */
29 PyObject *func_name; /* The __name__ attribute, a string object */
30 PyObject *func_dict; /* The __dict__ attribute, a dict or NULL */
31 PyObject *func_weakreflist; /* List of weak references */
32 PyObject *func_module; /* The __module__ attribute, can be anything */
Neal Norwitzc1505362006-12-28 06:47:50 +000033 PyObject *func_annotations; /* Annotations, a dict or NULL */
Antoine Pitrou86a36b52011-11-25 18:56:07 +010034 PyObject *func_qualname; /* The qualified name */
Armin Rigo89a39462004-10-28 16:32:00 +000035
36 /* Invariant:
37 * func_closure contains the bindings for func_code->co_freevars, so
38 * PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)
39 * (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).
40 */
Guido van Rossumcaa63801995-01-12 11:45:45 +000041} PyFunctionObject;
Guido van Rossum25831651993-05-19 14:50:45 +000042
Mark Hammond91a681d2002-08-12 07:21:58 +000043PyAPI_DATA(PyTypeObject) PyFunction_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Christian Heimes90aa7642007-12-19 02:45:37 +000045#define PyFunction_Check(op) (Py_TYPE(op) == &PyFunction_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Mark Hammond91a681d2002-08-12 07:21:58 +000047PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
Antoine Pitrou86a36b52011-11-25 18:56:07 +010048PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000049PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
50PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000051PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000052PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
53PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
Guido van Rossum4f72a782006-10-27 23:31:49 +000054PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
55PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000056PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
57PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
Neal Norwitzc1505362006-12-28 06:47:50 +000058PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
59PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
Guido van Rossuma3309961993-07-28 09:05:47 +000060
Victor Stinner9be7e7b2016-08-19 16:11:43 +020061#ifndef Py_LIMITED_API
Victor Stinnerb9009392016-08-22 23:15:44 +020062PyAPI_FUNC(PyObject *) _PyFunction_FastCallDict(
Victor Stinner9be7e7b2016-08-19 16:11:43 +020063 PyObject *func,
Victor Stinner74319ae2016-08-25 00:04:09 +020064 PyObject **args,
65 Py_ssize_t nargs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +020066 PyObject *kwargs);
Victor Stinnerd8735722016-09-09 12:36:44 -070067
68PyAPI_FUNC(PyObject *) _PyFunction_FastCallKeywords(
69 PyObject *func,
70 PyObject **stack,
71 Py_ssize_t nargs,
72 PyObject *kwnames);
Victor Stinner9be7e7b2016-08-19 16:11:43 +020073#endif
74
Guido van Rossumba0d0611998-07-10 15:47:08 +000075/* Macros for direct access to these values. Type checks are *not*
76 done, so use with care. */
77#define PyFunction_GET_CODE(func) \
78 (((PyFunctionObject *)func) -> func_code)
79#define PyFunction_GET_GLOBALS(func) \
80 (((PyFunctionObject *)func) -> func_globals)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000081#define PyFunction_GET_MODULE(func) \
82 (((PyFunctionObject *)func) -> func_module)
Guido van Rossumba0d0611998-07-10 15:47:08 +000083#define PyFunction_GET_DEFAULTS(func) \
84 (((PyFunctionObject *)func) -> func_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +000085#define PyFunction_GET_KW_DEFAULTS(func) \
86 (((PyFunctionObject *)func) -> func_kwdefaults)
Jeremy Hylton64949cb2001-01-25 20:06:59 +000087#define PyFunction_GET_CLOSURE(func) \
88 (((PyFunctionObject *)func) -> func_closure)
Neal Norwitzc1505362006-12-28 06:47:50 +000089#define PyFunction_GET_ANNOTATIONS(func) \
90 (((PyFunctionObject *)func) -> func_annotations)
Guido van Rossumba0d0611998-07-10 15:47:08 +000091
Tim Peters6d6c1a32001-08-02 04:15:00 +000092/* The classmethod and staticmethod types lives here, too */
Mark Hammond91a681d2002-08-12 07:21:58 +000093PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
94PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
Tim Peters6d6c1a32001-08-02 04:15:00 +000095
Mark Hammond91a681d2002-08-12 07:21:58 +000096PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
97PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +000098
Guido van Rossuma3309961993-07-28 09:05:47 +000099#ifdef __cplusplus
100}
101#endif
102#endif /* !Py_FUNCOBJECT_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000103#endif /* Py_LIMITED_API */