blob: 7acbe6e7c1d3ea7a78f523e08b1e7710fcd27e80 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Function object interface */
3
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.
13 * They reference a code object in their func_code attribute, which is a
14 * 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
Armin Rigo89a39462004-10-28 16:32:00 +000023 PyObject *func_code; /* A code object */
24 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 */
33
34 /* Invariant:
35 * func_closure contains the bindings for func_code->co_freevars, so
36 * PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)
37 * (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).
38 */
Guido van Rossumcaa63801995-01-12 11:45:45 +000039} PyFunctionObject;
Guido van Rossum25831651993-05-19 14:50:45 +000040
Mark Hammond91a681d2002-08-12 07:21:58 +000041PyAPI_DATA(PyTypeObject) PyFunction_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042
Guido van Rossumcaa63801995-01-12 11:45:45 +000043#define PyFunction_Check(op) ((op)->ob_type == &PyFunction_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Mark Hammond91a681d2002-08-12 07:21:58 +000045PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
46PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
47PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000048PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000049PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
50PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
Guido van Rossum4f72a782006-10-27 23:31:49 +000051PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
52PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000053PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
54PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
Guido van Rossuma3309961993-07-28 09:05:47 +000055
Guido van Rossumba0d0611998-07-10 15:47:08 +000056/* Macros for direct access to these values. Type checks are *not*
57 done, so use with care. */
58#define PyFunction_GET_CODE(func) \
59 (((PyFunctionObject *)func) -> func_code)
60#define PyFunction_GET_GLOBALS(func) \
61 (((PyFunctionObject *)func) -> func_globals)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000062#define PyFunction_GET_MODULE(func) \
63 (((PyFunctionObject *)func) -> func_module)
Guido van Rossumba0d0611998-07-10 15:47:08 +000064#define PyFunction_GET_DEFAULTS(func) \
65 (((PyFunctionObject *)func) -> func_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +000066#define PyFunction_GET_KW_DEFAULTS(func) \
67 (((PyFunctionObject *)func) -> func_kwdefaults)
Jeremy Hylton64949cb2001-01-25 20:06:59 +000068#define PyFunction_GET_CLOSURE(func) \
69 (((PyFunctionObject *)func) -> func_closure)
Guido van Rossumba0d0611998-07-10 15:47:08 +000070
Tim Peters6d6c1a32001-08-02 04:15:00 +000071/* The classmethod and staticmethod types lives here, too */
Mark Hammond91a681d2002-08-12 07:21:58 +000072PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
73PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
Tim Peters6d6c1a32001-08-02 04:15:00 +000074
Mark Hammond91a681d2002-08-12 07:21:58 +000075PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
76PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +000077
Guido van Rossuma3309961993-07-28 09:05:47 +000078#ifdef __cplusplus
79}
80#endif
81#endif /* !Py_FUNCOBJECT_H */