Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Function object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 5 | #include "compile.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 6 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 8 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 9 | PyFunction_New(PyObject *code, PyObject *globals) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 11 | PyFunctionObject *op = PyObject_NEW(PyFunctionObject, |
| 12 | &PyFunction_Type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13 | if (op != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 14 | PyObject *doc; |
| 15 | PyObject *consts; |
| 16 | Py_INCREF(code); |
Guido van Rossum | 846e431 | 1990-11-18 17:44:06 +0000 | [diff] [blame] | 17 | op->func_code = code; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 18 | Py_INCREF(globals); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 19 | op->func_globals = globals; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 20 | op->func_name = ((PyCodeObject *)code)->co_name; |
| 21 | Py_INCREF(op->func_name); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 22 | op->func_defaults = NULL; /* No default arguments */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 23 | op->func_closure = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 24 | consts = ((PyCodeObject *)code)->co_consts; |
| 25 | if (PyTuple_Size(consts) >= 1) { |
| 26 | doc = PyTuple_GetItem(consts, 0); |
Guido van Rossum | ec5b776 | 2000-04-27 20:14:13 +0000 | [diff] [blame] | 27 | if (!PyString_Check(doc) && !PyUnicode_Check(doc)) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 28 | doc = Py_None; |
Guido van Rossum | 5bd3805 | 1995-01-07 12:01:30 +0000 | [diff] [blame] | 29 | } |
| 30 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 31 | doc = Py_None; |
| 32 | Py_INCREF(doc); |
Guido van Rossum | 5bd3805 | 1995-01-07 12:01:30 +0000 | [diff] [blame] | 33 | op->func_doc = doc; |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 34 | op->func_dict = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 36 | else |
| 37 | return NULL; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 38 | PyObject_GC_Init(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 39 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 42 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 43 | PyFunction_GetCode(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | if (!PyFunction_Check(op)) { |
| 46 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | return NULL; |
| 48 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 49 | return ((PyFunctionObject *) op) -> func_code; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 53 | PyFunction_GetGlobals(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 55 | if (!PyFunction_Check(op)) { |
| 56 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | return NULL; |
| 58 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 59 | return ((PyFunctionObject *) op) -> func_globals; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 63 | PyFunction_GetDefaults(PyObject *op) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 64 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 65 | if (!PyFunction_Check(op)) { |
| 66 | PyErr_BadInternalCall(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 69 | return ((PyFunctionObject *) op) -> func_defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 73 | PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 74 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 75 | if (!PyFunction_Check(op)) { |
| 76 | PyErr_BadInternalCall(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 77 | return -1; |
| 78 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 79 | if (defaults == Py_None) |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 80 | defaults = NULL; |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 81 | else if (PyTuple_Check(defaults)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | Py_XINCREF(defaults); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 83 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 84 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 85 | PyErr_SetString(PyExc_SystemError, "non-tuple default args"); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 86 | return -1; |
| 87 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | Py_XDECREF(((PyFunctionObject *) op) -> func_defaults); |
| 89 | ((PyFunctionObject *) op) -> func_defaults = defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 93 | PyObject * |
| 94 | PyFunction_GetClosure(PyObject *op) |
| 95 | { |
| 96 | if (!PyFunction_Check(op)) { |
| 97 | PyErr_BadInternalCall(); |
| 98 | return NULL; |
| 99 | } |
| 100 | return ((PyFunctionObject *) op) -> func_closure; |
| 101 | } |
| 102 | |
| 103 | int |
| 104 | PyFunction_SetClosure(PyObject *op, PyObject *closure) |
| 105 | { |
| 106 | if (!PyFunction_Check(op)) { |
| 107 | PyErr_BadInternalCall(); |
| 108 | return -1; |
| 109 | } |
| 110 | if (closure == Py_None) |
| 111 | closure = NULL; |
| 112 | else if (PyTuple_Check(closure)) { |
| 113 | Py_XINCREF(closure); |
| 114 | } |
| 115 | else { |
| 116 | PyErr_SetString(PyExc_SystemError, "non-tuple closure"); |
| 117 | return -1; |
| 118 | } |
| 119 | Py_XDECREF(((PyFunctionObject *) op) -> func_closure); |
| 120 | ((PyFunctionObject *) op) -> func_closure = closure; |
| 121 | return 0; |
| 122 | } |
| 123 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | /* Methods */ |
| 125 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 126 | #define OFF(x) offsetof(PyFunctionObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 127 | |
| 128 | static struct memberlist func_memberlist[] = { |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 129 | {"func_code", T_OBJECT, OFF(func_code)}, |
| 130 | {"func_globals", T_OBJECT, OFF(func_globals), READONLY}, |
| 131 | {"func_name", T_OBJECT, OFF(func_name), READONLY}, |
| 132 | {"__name__", T_OBJECT, OFF(func_name), READONLY}, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 133 | {"func_closure", T_OBJECT, OFF(func_closure)}, |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 134 | {"func_defaults", T_OBJECT, OFF(func_defaults)}, |
| 135 | {"func_doc", T_OBJECT, OFF(func_doc)}, |
| 136 | {"__doc__", T_OBJECT, OFF(func_doc)}, |
| 137 | {"func_dict", T_OBJECT, OFF(func_dict)}, |
| 138 | {"__dict__", T_OBJECT, OFF(func_dict)}, |
| 139 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 142 | static PyObject * |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 143 | func_getattro(PyFunctionObject *op, PyObject *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 144 | { |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 145 | PyObject *rtn; |
| 146 | char *sname = PyString_AsString(name); |
| 147 | |
| 148 | if (sname[0] != '_' && PyEval_GetRestricted()) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 149 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 150 | "function attributes not accessible in restricted mode"); |
| 151 | return NULL; |
| 152 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 153 | |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 154 | /* no API for PyMember_HasAttr() */ |
| 155 | rtn = PyMember_Get((char *)op, func_memberlist, sname); |
| 156 | |
| 157 | if (rtn == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 158 | PyErr_Clear(); |
| 159 | if (op->func_dict != NULL) { |
| 160 | rtn = PyDict_GetItem(op->func_dict, name); |
| 161 | Py_XINCREF(rtn); |
| 162 | } |
| 163 | if (rtn == NULL) |
| 164 | PyErr_SetObject(PyExc_AttributeError, name); |
| 165 | } |
| 166 | return rtn; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 169 | static int |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 170 | func_setattro(PyFunctionObject *op, PyObject *name, PyObject *value) |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 171 | { |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 172 | int rtn; |
| 173 | char *sname = PyString_AsString(name); |
| 174 | |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 175 | if (PyEval_GetRestricted()) { |
| 176 | PyErr_SetString(PyExc_RuntimeError, |
| 177 | "function attributes not settable in restricted mode"); |
| 178 | return -1; |
| 179 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 180 | if (strcmp(sname, "func_code") == 0) { |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 181 | /* not legal to del f.func_code or to set it to anything |
| 182 | * other than a code object. |
| 183 | */ |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 184 | if (value == NULL || !PyCode_Check(value)) { |
| 185 | PyErr_SetString( |
| 186 | PyExc_TypeError, |
| 187 | "func_code must be set to a code object"); |
| 188 | return -1; |
| 189 | } |
| 190 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 191 | else if (strcmp(sname, "func_defaults") == 0) { |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 192 | /* legal to del f.func_defaults. Can only set |
| 193 | * func_defaults to NULL or a tuple. |
| 194 | */ |
| 195 | if (value == Py_None) |
| 196 | value = NULL; |
| 197 | if (value != NULL && !PyTuple_Check(value)) { |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 198 | PyErr_SetString( |
| 199 | PyExc_TypeError, |
| 200 | "func_defaults must be set to a tuple object"); |
| 201 | return -1; |
| 202 | } |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 203 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 204 | else if (!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) { |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 205 | /* legal to del f.func_dict. Can only set func_dict to |
| 206 | * NULL or a dictionary. |
| 207 | */ |
| 208 | if (value == Py_None) |
| 209 | value = NULL; |
| 210 | if (value != NULL && !PyDict_Check(value)) { |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 211 | PyErr_SetString( |
| 212 | PyExc_TypeError, |
| 213 | "func_dict must be set to a dict object"); |
| 214 | return -1; |
| 215 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | rtn = PyMember_Set((char *)op, func_memberlist, sname, value); |
| 219 | if (rtn < 0 && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 220 | PyErr_Clear(); |
| 221 | if (op->func_dict == NULL) { |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 222 | /* don't create the dict if we're deleting an |
| 223 | * attribute. In that case, we know we'll get an |
| 224 | * AttributeError. |
| 225 | */ |
| 226 | if (value == NULL) { |
| 227 | PyErr_SetString(PyExc_AttributeError, sname); |
| 228 | return -1; |
| 229 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 230 | op->func_dict = PyDict_New(); |
| 231 | if (op->func_dict == NULL) |
| 232 | return -1; |
| 233 | } |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 234 | if (value == NULL) |
| 235 | rtn = PyDict_DelItem(op->func_dict, name); |
| 236 | else |
| 237 | rtn = PyDict_SetItem(op->func_dict, name, value); |
| 238 | /* transform KeyError into AttributeError */ |
| 239 | if (rtn < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
| 240 | PyErr_SetString(PyExc_AttributeError, sname); |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 241 | } |
| 242 | return rtn; |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 246 | func_dealloc(PyFunctionObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 247 | { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 248 | PyObject_GC_Fini(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 249 | Py_DECREF(op->func_code); |
| 250 | Py_DECREF(op->func_globals); |
| 251 | Py_DECREF(op->func_name); |
| 252 | Py_XDECREF(op->func_defaults); |
| 253 | Py_XDECREF(op->func_doc); |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 254 | Py_XDECREF(op->func_dict); |
Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 255 | op = (PyFunctionObject *) PyObject_AS_GC(op); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 256 | PyObject_DEL(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 259 | static PyObject* |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 260 | func_repr(PyFunctionObject *op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 261 | { |
| 262 | char buf[140]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 263 | if (op->func_name == Py_None) |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 264 | sprintf(buf, "<anonymous function at %p>", op); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 265 | else |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 266 | sprintf(buf, "<function %.100s at %p>", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 267 | PyString_AsString(op->func_name), |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 268 | op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 269 | return PyString_FromString(buf); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 272 | static int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 273 | func_compare(PyFunctionObject *f, PyFunctionObject *g) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 274 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 275 | int c; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 276 | if (f->func_globals != g->func_globals) |
| 277 | return (f->func_globals < g->func_globals) ? -1 : 1; |
Guido van Rossum | 83f9ad8 | 1997-08-05 16:51:05 +0000 | [diff] [blame] | 278 | if (f->func_defaults != g->func_defaults) { |
| 279 | if (f->func_defaults == NULL) |
| 280 | return -1; |
| 281 | if (g->func_defaults == NULL) |
| 282 | return 1; |
| 283 | c = PyObject_Compare(f->func_defaults, g->func_defaults); |
| 284 | if (c != 0) |
| 285 | return c; |
| 286 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 287 | return PyObject_Compare(f->func_code, g->func_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static long |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 291 | func_hash(PyFunctionObject *f) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 292 | { |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 293 | long h,x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 294 | h = PyObject_Hash(f->func_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 295 | if (h == -1) return h; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 296 | x = _Py_HashPointer(f->func_globals); |
| 297 | if (x == -1) return x; |
| 298 | h ^= x; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 299 | if (h == -1) h = -2; |
| 300 | return h; |
| 301 | } |
| 302 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 303 | static int |
| 304 | func_traverse(PyFunctionObject *f, visitproc visit, void *arg) |
| 305 | { |
| 306 | int err; |
| 307 | if (f->func_code) { |
| 308 | err = visit(f->func_code, arg); |
| 309 | if (err) |
| 310 | return err; |
| 311 | } |
| 312 | if (f->func_globals) { |
| 313 | err = visit(f->func_globals, arg); |
| 314 | if (err) |
| 315 | return err; |
| 316 | } |
| 317 | if (f->func_defaults) { |
| 318 | err = visit(f->func_defaults, arg); |
| 319 | if (err) |
| 320 | return err; |
| 321 | } |
| 322 | if (f->func_doc) { |
| 323 | err = visit(f->func_doc, arg); |
| 324 | if (err) |
| 325 | return err; |
| 326 | } |
| 327 | if (f->func_name) { |
| 328 | err = visit(f->func_name, arg); |
| 329 | if (err) |
| 330 | return err; |
| 331 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 332 | if (f->func_dict) { |
| 333 | err = visit(f->func_dict, arg); |
| 334 | if (err) |
| 335 | return err; |
| 336 | } |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 340 | PyTypeObject PyFunction_Type = { |
| 341 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | 0, |
| 343 | "function", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 344 | sizeof(PyFunctionObject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | 0, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 346 | (destructor)func_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 846e431 | 1990-11-18 17:44:06 +0000 | [diff] [blame] | 347 | 0, /*tp_print*/ |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 348 | 0, /*tp_getattr*/ |
| 349 | 0, /*tp_setattr*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 350 | (cmpfunc)func_compare, /*tp_compare*/ |
| 351 | (reprfunc)func_repr, /*tp_repr*/ |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 352 | 0, /*tp_as_number*/ |
| 353 | 0, /*tp_as_sequence*/ |
| 354 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 355 | (hashfunc)func_hash, /*tp_hash*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 356 | 0, /*tp_call*/ |
| 357 | 0, /*tp_str*/ |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 358 | (getattrofunc)func_getattro, /*tp_getattro*/ |
| 359 | (setattrofunc)func_setattro, /*tp_setattro*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 360 | 0, /* tp_as_buffer */ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 361 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 362 | 0, /* tp_doc */ |
| 363 | (traverseproc)func_traverse, /* tp_traverse */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | }; |