blob: cc1426cdc2eb79672224123e847cbdd5a6f67df9 [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
Guido van Rossumba0d0611998-07-10 15:47:08 +000061/* Macros for direct access to these values. Type checks are *not*
62 done, so use with care. */
63#define PyFunction_GET_CODE(func) \
64 (((PyFunctionObject *)func) -> func_code)
65#define PyFunction_GET_GLOBALS(func) \
66 (((PyFunctionObject *)func) -> func_globals)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000067#define PyFunction_GET_MODULE(func) \
68 (((PyFunctionObject *)func) -> func_module)
Guido van Rossumba0d0611998-07-10 15:47:08 +000069#define PyFunction_GET_DEFAULTS(func) \
70 (((PyFunctionObject *)func) -> func_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +000071#define PyFunction_GET_KW_DEFAULTS(func) \
72 (((PyFunctionObject *)func) -> func_kwdefaults)
Jeremy Hylton64949cb2001-01-25 20:06:59 +000073#define PyFunction_GET_CLOSURE(func) \
74 (((PyFunctionObject *)func) -> func_closure)
Neal Norwitzc1505362006-12-28 06:47:50 +000075#define PyFunction_GET_ANNOTATIONS(func) \
76 (((PyFunctionObject *)func) -> func_annotations)
Guido van Rossumba0d0611998-07-10 15:47:08 +000077
Tim Peters6d6c1a32001-08-02 04:15:00 +000078/* The classmethod and staticmethod types lives here, too */
Mark Hammond91a681d2002-08-12 07:21:58 +000079PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
80PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
Tim Peters6d6c1a32001-08-02 04:15:00 +000081
Mark Hammond91a681d2002-08-12 07:21:58 +000082PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
83PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +000084
Guido van Rossuma3309961993-07-28 09:05:47 +000085#ifdef __cplusplus
86}
87#endif
88#endif /* !Py_FUNCOBJECT_H */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000089#endif /* Py_LIMITED_API */