Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Function object implementation */ |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 35 | #include "compile.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 36 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 38 | PyObject * |
| 39 | PyFunction_New(code, globals) |
| 40 | PyObject *code; |
| 41 | PyObject *globals; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 43 | PyFunctionObject *op = PyObject_NEW(PyFunctionObject, |
| 44 | &PyFunction_Type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | if (op != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 46 | PyObject *doc; |
| 47 | PyObject *consts; |
| 48 | Py_INCREF(code); |
Guido van Rossum | 846e431 | 1990-11-18 17:44:06 +0000 | [diff] [blame] | 49 | op->func_code = code; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 50 | Py_INCREF(globals); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | op->func_globals = globals; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | op->func_name = ((PyCodeObject *)code)->co_name; |
| 53 | Py_INCREF(op->func_name); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 54 | op->func_defaults = NULL; /* No default arguments */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 55 | consts = ((PyCodeObject *)code)->co_consts; |
| 56 | if (PyTuple_Size(consts) >= 1) { |
| 57 | doc = PyTuple_GetItem(consts, 0); |
| 58 | if (!PyString_Check(doc)) |
| 59 | doc = Py_None; |
Guido van Rossum | 5bd3805 | 1995-01-07 12:01:30 +0000 | [diff] [blame] | 60 | } |
| 61 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | doc = Py_None; |
| 63 | Py_INCREF(doc); |
Guido van Rossum | 5bd3805 | 1995-01-07 12:01:30 +0000 | [diff] [blame] | 64 | op->func_doc = doc; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 66 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 69 | PyObject * |
| 70 | PyFunction_GetCode(op) |
| 71 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 73 | if (!PyFunction_Check(op)) { |
| 74 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 77 | return ((PyFunctionObject *) op) -> func_code; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 80 | PyObject * |
| 81 | PyFunction_GetGlobals(op) |
| 82 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 84 | if (!PyFunction_Check(op)) { |
| 85 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | return NULL; |
| 87 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | return ((PyFunctionObject *) op) -> func_globals; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 91 | PyObject * |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 92 | PyFunction_GetDefaults(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | PyObject *op; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 94 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | if (!PyFunction_Check(op)) { |
| 96 | PyErr_BadInternalCall(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | return ((PyFunctionObject *) op) -> func_defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 103 | PyFunction_SetDefaults(op, defaults) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 104 | PyObject *op; |
| 105 | PyObject *defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 106 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 107 | if (!PyFunction_Check(op)) { |
| 108 | PyErr_BadInternalCall(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 109 | return -1; |
| 110 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 111 | if (defaults == Py_None) |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 112 | defaults = NULL; |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 113 | else if (PyTuple_Check(defaults)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 114 | Py_XINCREF(defaults); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 115 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 116 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 117 | PyErr_SetString(PyExc_SystemError, "non-tuple default args"); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 118 | return -1; |
| 119 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 120 | Py_XDECREF(((PyFunctionObject *) op) -> func_defaults); |
| 121 | ((PyFunctionObject *) op) -> func_defaults = defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 125 | /* Methods */ |
| 126 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 127 | #define OFF(x) offsetof(PyFunctionObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 128 | |
| 129 | static struct memberlist func_memberlist[] = { |
Guido van Rossum | a38c0ff | 1992-01-14 18:32:20 +0000 | [diff] [blame] | 130 | {"func_code", T_OBJECT, OFF(func_code), READONLY}, |
| 131 | {"func_globals",T_OBJECT, OFF(func_globals), READONLY}, |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 132 | {"func_name", T_OBJECT, OFF(func_name), READONLY}, |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 133 | {"__name__", T_OBJECT, OFF(func_name), READONLY}, |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 134 | {"func_defaults",T_OBJECT, OFF(func_defaults), READONLY}, |
Guido van Rossum | 5bd3805 | 1995-01-07 12:01:30 +0000 | [diff] [blame] | 135 | {"func_doc", T_OBJECT, OFF(func_doc)}, |
| 136 | {"__doc__", T_OBJECT, OFF(func_doc)}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 137 | {NULL} /* Sentinel */ |
| 138 | }; |
| 139 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 140 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 141 | func_getattr(op, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 142 | PyFunctionObject *op; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 143 | char *name; |
| 144 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 145 | if (name[0] != '_' && PyEval_GetRestricted()) { |
| 146 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 147 | "function attributes not accessible in restricted mode"); |
| 148 | return NULL; |
| 149 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 150 | return PyMember_Get((char *)op, func_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 154 | func_dealloc(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 155 | PyFunctionObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 156 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | Py_DECREF(op->func_code); |
| 158 | Py_DECREF(op->func_globals); |
| 159 | Py_DECREF(op->func_name); |
| 160 | Py_XDECREF(op->func_defaults); |
| 161 | Py_XDECREF(op->func_doc); |
| 162 | PyMem_DEL(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 165 | static PyObject* |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 166 | func_repr(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 167 | PyFunctionObject *op; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 168 | { |
| 169 | char buf[140]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 170 | if (op->func_name == Py_None) |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 171 | sprintf(buf, "<anonymous function at %lx>", (long)op); |
| 172 | else |
| 173 | sprintf(buf, "<function %.100s at %lx>", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 174 | PyString_AsString(op->func_name), |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 175 | (long)op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 176 | return PyString_FromString(buf); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 179 | static int |
| 180 | func_compare(f, g) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 181 | PyFunctionObject *f, *g; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 182 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 183 | int c; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 184 | if (f->func_globals != g->func_globals) |
| 185 | return (f->func_globals < g->func_globals) ? -1 : 1; |
Guido van Rossum | 83f9ad8 | 1997-08-05 16:51:05 +0000 | [diff] [blame] | 186 | if (f->func_defaults != g->func_defaults) { |
| 187 | if (f->func_defaults == NULL) |
| 188 | return -1; |
| 189 | if (g->func_defaults == NULL) |
| 190 | return 1; |
| 191 | c = PyObject_Compare(f->func_defaults, g->func_defaults); |
| 192 | if (c != 0) |
| 193 | return c; |
| 194 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 195 | return PyObject_Compare(f->func_code, g->func_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static long |
| 199 | func_hash(f) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 200 | PyFunctionObject *f; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 201 | { |
| 202 | long h; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 203 | h = PyObject_Hash(f->func_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 204 | if (h == -1) return h; |
| 205 | h = h ^ (long)f->func_globals; |
| 206 | if (h == -1) h = -2; |
| 207 | return h; |
| 208 | } |
| 209 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 210 | PyTypeObject PyFunction_Type = { |
| 211 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | 0, |
| 213 | "function", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 214 | sizeof(PyFunctionObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 215 | 0, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 216 | (destructor)func_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 846e431 | 1990-11-18 17:44:06 +0000 | [diff] [blame] | 217 | 0, /*tp_print*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 218 | (getattrfunc)func_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | 0, /*tp_setattr*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 220 | (cmpfunc)func_compare, /*tp_compare*/ |
| 221 | (reprfunc)func_repr, /*tp_repr*/ |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 222 | 0, /*tp_as_number*/ |
| 223 | 0, /*tp_as_sequence*/ |
| 224 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 225 | (hashfunc)func_hash, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | }; |