blob: 521d87bf9f16ffad9556ef79eac50c9f7be4dd98 [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 */
Armin Rigo89a39462004-10-28 16:32:00 +000034
35 /* Invariant:
36 * func_closure contains the bindings for func_code->co_freevars, so
37 * PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)
38 * (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).
39 */
Guido van Rossumcaa63801995-01-12 11:45:45 +000040} PyFunctionObject;
Guido van Rossum25831651993-05-19 14:50:45 +000041
Mark Hammond91a681d2002-08-12 07:21:58 +000042PyAPI_DATA(PyTypeObject) PyFunction_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043
Christian Heimes90aa7642007-12-19 02:45:37 +000044#define PyFunction_Check(op) (Py_TYPE(op) == &PyFunction_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Mark Hammond91a681d2002-08-12 07:21:58 +000046PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
47PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
48PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000049PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000050PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
51PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
Guido van Rossum4f72a782006-10-27 23:31:49 +000052PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
53PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000054PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
55PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
Neal Norwitzc1505362006-12-28 06:47:50 +000056PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
57PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
Guido van Rossuma3309961993-07-28 09:05:47 +000058
Guido van Rossumba0d0611998-07-10 15:47:08 +000059/* Macros for direct access to these values. Type checks are *not*
60 done, so use with care. */
61#define PyFunction_GET_CODE(func) \
62 (((PyFunctionObject *)func) -> func_code)
63#define PyFunction_GET_GLOBALS(func) \
64 (((PyFunctionObject *)func) -> func_globals)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000065#define PyFunction_GET_MODULE(func) \
66 (((PyFunctionObject *)func) -> func_module)
Guido van Rossumba0d0611998-07-10 15:47:08 +000067#define PyFunction_GET_DEFAULTS(func) \
68 (((PyFunctionObject *)func) -> func_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +000069#define PyFunction_GET_KW_DEFAULTS(func) \
70 (((PyFunctionObject *)func) -> func_kwdefaults)
Jeremy Hylton64949cb2001-01-25 20:06:59 +000071#define PyFunction_GET_CLOSURE(func) \
72 (((PyFunctionObject *)func) -> func_closure)
Neal Norwitzc1505362006-12-28 06:47:50 +000073#define PyFunction_GET_ANNOTATIONS(func) \
74 (((PyFunctionObject *)func) -> func_annotations)
Guido van Rossumba0d0611998-07-10 15:47:08 +000075
Tim Peters6d6c1a32001-08-02 04:15:00 +000076/* The classmethod and staticmethod types lives here, too */
Mark Hammond91a681d2002-08-12 07:21:58 +000077PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
78PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
Tim Peters6d6c1a32001-08-02 04:15:00 +000079
Mark Hammond91a681d2002-08-12 07:21:58 +000080PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
81PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +000082
Guido van Rossuma3309961993-07-28 09:05:47 +000083#ifdef __cplusplus
84}
85#endif
86#endif /* !Py_FUNCOBJECT_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000087#endif /* Py_LIMITED_API */