blob: 59c19bb1e70df2d2195b9871fcab94c5638d5b7e [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 */
26 PyObject *func_closure; /* NULL or a tuple of cell objects */
27 PyObject *func_doc; /* The __doc__ attribute, can be anything */
28 PyObject *func_name; /* The __name__ attribute, a string object */
29 PyObject *func_dict; /* The __dict__ attribute, a dict or NULL */
30 PyObject *func_weakreflist; /* List of weak references */
31 PyObject *func_module; /* The __module__ attribute, can be anything */
32
33 /* Invariant:
34 * func_closure contains the bindings for func_code->co_freevars, so
35 * PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)
36 * (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).
37 */
Guido van Rossumcaa63801995-01-12 11:45:45 +000038} PyFunctionObject;
Guido van Rossum25831651993-05-19 14:50:45 +000039
Mark Hammond91a681d2002-08-12 07:21:58 +000040PyAPI_DATA(PyTypeObject) PyFunction_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossumcaa63801995-01-12 11:45:45 +000042#define PyFunction_Check(op) ((op)->ob_type == &PyFunction_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043
Mark Hammond91a681d2002-08-12 07:21:58 +000044PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
45PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
46PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000047PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000048PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
49PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
50PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
51PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
Guido van Rossuma3309961993-07-28 09:05:47 +000052
Guido van Rossumba0d0611998-07-10 15:47:08 +000053/* Macros for direct access to these values. Type checks are *not*
54 done, so use with care. */
55#define PyFunction_GET_CODE(func) \
56 (((PyFunctionObject *)func) -> func_code)
57#define PyFunction_GET_GLOBALS(func) \
58 (((PyFunctionObject *)func) -> func_globals)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000059#define PyFunction_GET_MODULE(func) \
60 (((PyFunctionObject *)func) -> func_module)
Guido van Rossumba0d0611998-07-10 15:47:08 +000061#define PyFunction_GET_DEFAULTS(func) \
62 (((PyFunctionObject *)func) -> func_defaults)
Jeremy Hylton64949cb2001-01-25 20:06:59 +000063#define PyFunction_GET_CLOSURE(func) \
64 (((PyFunctionObject *)func) -> func_closure)
Guido van Rossumba0d0611998-07-10 15:47:08 +000065
Tim Peters6d6c1a32001-08-02 04:15:00 +000066/* The classmethod and staticmethod types lives here, too */
Mark Hammond91a681d2002-08-12 07:21:58 +000067PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
68PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
Tim Peters6d6c1a32001-08-02 04:15:00 +000069
Mark Hammond91a681d2002-08-12 07:21:58 +000070PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
71PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +000072
Guido van Rossuma3309961993-07-28 09:05:47 +000073#ifdef __cplusplus
74}
75#endif
76#endif /* !Py_FUNCOBJECT_H */