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" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5 | #include "code.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 * |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 9 | PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | { |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 11 | PyFunctionObject *op; |
| 12 | PyObject *doc, *consts, *module; |
| 13 | static PyObject *__name__ = NULL; |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 14 | |
Victor Stinner | 34f96b8 | 2013-07-22 23:04:55 +0200 | [diff] [blame] | 15 | if (__name__ == NULL) { |
| 16 | __name__ = PyUnicode_InternFromString("__name__"); |
| 17 | if (__name__ == NULL) |
| 18 | return NULL; |
| 19 | } |
| 20 | |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 21 | op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type); |
| 22 | if (op == NULL) |
| 23 | return NULL; |
| 24 | |
| 25 | op->func_weakreflist = NULL; |
| 26 | Py_INCREF(code); |
| 27 | op->func_code = code; |
| 28 | Py_INCREF(globals); |
| 29 | op->func_globals = globals; |
| 30 | op->func_name = ((PyCodeObject *)code)->co_name; |
| 31 | Py_INCREF(op->func_name); |
| 32 | op->func_defaults = NULL; /* No default arguments */ |
| 33 | op->func_kwdefaults = NULL; /* No keyword only defaults */ |
| 34 | op->func_closure = NULL; |
Victor Stinner | 34f96b8 | 2013-07-22 23:04:55 +0200 | [diff] [blame] | 35 | |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 36 | consts = ((PyCodeObject *)code)->co_consts; |
| 37 | if (PyTuple_Size(consts) >= 1) { |
| 38 | doc = PyTuple_GetItem(consts, 0); |
| 39 | if (!PyUnicode_Check(doc)) |
| 40 | doc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | } |
| 42 | else |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 43 | doc = Py_None; |
| 44 | Py_INCREF(doc); |
| 45 | op->func_doc = doc; |
Victor Stinner | 34f96b8 | 2013-07-22 23:04:55 +0200 | [diff] [blame] | 46 | |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 47 | op->func_dict = NULL; |
| 48 | op->func_module = NULL; |
| 49 | op->func_annotations = NULL; |
| 50 | |
| 51 | /* __module__: If module name is in globals, use it. |
Victor Stinner | 34f96b8 | 2013-07-22 23:04:55 +0200 | [diff] [blame] | 52 | Otherwise, use None. */ |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 53 | module = PyDict_GetItem(globals, __name__); |
| 54 | if (module) { |
| 55 | Py_INCREF(module); |
| 56 | op->func_module = module; |
| 57 | } |
| 58 | if (qualname) |
| 59 | op->func_qualname = qualname; |
| 60 | else |
| 61 | op->func_qualname = op->func_name; |
| 62 | Py_INCREF(op->func_qualname); |
| 63 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | _PyObject_GC_TRACK(op); |
| 65 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 68 | PyObject * |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 69 | PyFunction_New(PyObject *code, PyObject *globals) |
| 70 | { |
| 71 | return PyFunction_NewWithQualName(code, globals, NULL); |
| 72 | } |
| 73 | |
| 74 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 75 | PyFunction_GetCode(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | if (!PyFunction_Check(op)) { |
| 78 | PyErr_BadInternalCall(); |
| 79 | return NULL; |
| 80 | } |
| 81 | return ((PyFunctionObject *) op) -> func_code; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 84 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 85 | PyFunction_GetGlobals(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | if (!PyFunction_Check(op)) { |
| 88 | PyErr_BadInternalCall(); |
| 89 | return NULL; |
| 90 | } |
| 91 | return ((PyFunctionObject *) op) -> func_globals; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 94 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 95 | PyFunction_GetModule(PyObject *op) |
| 96 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | if (!PyFunction_Check(op)) { |
| 98 | PyErr_BadInternalCall(); |
| 99 | return NULL; |
| 100 | } |
| 101 | return ((PyFunctionObject *) op) -> func_module; |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 105 | PyFunction_GetDefaults(PyObject *op) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 106 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | if (!PyFunction_Check(op)) { |
| 108 | PyErr_BadInternalCall(); |
| 109 | return NULL; |
| 110 | } |
| 111 | return ((PyFunctionObject *) op) -> func_defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 115 | PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | if (!PyFunction_Check(op)) { |
| 118 | PyErr_BadInternalCall(); |
| 119 | return -1; |
| 120 | } |
| 121 | if (defaults == Py_None) |
| 122 | defaults = NULL; |
| 123 | else if (defaults && PyTuple_Check(defaults)) { |
| 124 | Py_INCREF(defaults); |
| 125 | } |
| 126 | else { |
| 127 | PyErr_SetString(PyExc_SystemError, "non-tuple default args"); |
| 128 | return -1; |
| 129 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 130 | Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | return 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 134 | PyObject * |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 135 | PyFunction_GetKwDefaults(PyObject *op) |
| 136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | if (!PyFunction_Check(op)) { |
| 138 | PyErr_BadInternalCall(); |
| 139 | return NULL; |
| 140 | } |
| 141 | return ((PyFunctionObject *) op) -> func_kwdefaults; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int |
| 145 | PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults) |
| 146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | if (!PyFunction_Check(op)) { |
| 148 | PyErr_BadInternalCall(); |
| 149 | return -1; |
| 150 | } |
| 151 | if (defaults == Py_None) |
| 152 | defaults = NULL; |
| 153 | else if (defaults && PyDict_Check(defaults)) { |
| 154 | Py_INCREF(defaults); |
| 155 | } |
| 156 | else { |
| 157 | PyErr_SetString(PyExc_SystemError, |
| 158 | "non-dict keyword only default args"); |
| 159 | return -1; |
| 160 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 161 | Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | PyObject * |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 166 | PyFunction_GetClosure(PyObject *op) |
| 167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | if (!PyFunction_Check(op)) { |
| 169 | PyErr_BadInternalCall(); |
| 170 | return NULL; |
| 171 | } |
| 172 | return ((PyFunctionObject *) op) -> func_closure; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | int |
| 176 | PyFunction_SetClosure(PyObject *op, PyObject *closure) |
| 177 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | if (!PyFunction_Check(op)) { |
| 179 | PyErr_BadInternalCall(); |
| 180 | return -1; |
| 181 | } |
| 182 | if (closure == Py_None) |
| 183 | closure = NULL; |
| 184 | else if (PyTuple_Check(closure)) { |
| 185 | Py_INCREF(closure); |
| 186 | } |
| 187 | else { |
| 188 | PyErr_Format(PyExc_SystemError, |
| 189 | "expected tuple for closure, got '%.100s'", |
| 190 | closure->ob_type->tp_name); |
| 191 | return -1; |
| 192 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 193 | Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 197 | PyObject * |
| 198 | PyFunction_GetAnnotations(PyObject *op) |
| 199 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | if (!PyFunction_Check(op)) { |
| 201 | PyErr_BadInternalCall(); |
| 202 | return NULL; |
| 203 | } |
| 204 | return ((PyFunctionObject *) op) -> func_annotations; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | int |
| 208 | PyFunction_SetAnnotations(PyObject *op, PyObject *annotations) |
| 209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | if (!PyFunction_Check(op)) { |
| 211 | PyErr_BadInternalCall(); |
| 212 | return -1; |
| 213 | } |
| 214 | if (annotations == Py_None) |
| 215 | annotations = NULL; |
| 216 | else if (annotations && PyDict_Check(annotations)) { |
| 217 | Py_INCREF(annotations); |
| 218 | } |
| 219 | else { |
| 220 | PyErr_SetString(PyExc_SystemError, |
| 221 | "non-dict annotations"); |
| 222 | return -1; |
| 223 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 224 | Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | /* Methods */ |
| 229 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 230 | #define OFF(x) offsetof(PyFunctionObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 232 | static PyMemberDef func_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | {"__closure__", T_OBJECT, OFF(func_closure), |
| 234 | RESTRICTED|READONLY}, |
| 235 | {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED}, |
| 236 | {"__globals__", T_OBJECT, OFF(func_globals), |
| 237 | RESTRICTED|READONLY}, |
| 238 | {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED}, |
| 239 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 242 | static PyObject * |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 243 | func_get_code(PyFunctionObject *op) |
| 244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | Py_INCREF(op->func_code); |
| 246 | return op->func_code; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static int |
| 250 | func_set_code(PyFunctionObject *op, PyObject *value) |
| 251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | PyObject *tmp; |
| 253 | Py_ssize_t nfree, nclosure; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | /* Not legal to del f.func_code or to set it to anything |
| 256 | * other than a code object. */ |
| 257 | if (value == NULL || !PyCode_Check(value)) { |
| 258 | PyErr_SetString(PyExc_TypeError, |
| 259 | "__code__ must be set to a code object"); |
| 260 | return -1; |
| 261 | } |
| 262 | nfree = PyCode_GetNumFree((PyCodeObject *)value); |
| 263 | nclosure = (op->func_closure == NULL ? 0 : |
| 264 | PyTuple_GET_SIZE(op->func_closure)); |
| 265 | if (nclosure != nfree) { |
| 266 | PyErr_Format(PyExc_ValueError, |
| 267 | "%U() requires a code object with %zd free vars," |
| 268 | " not %zd", |
| 269 | op->func_name, |
| 270 | nclosure, nfree); |
| 271 | return -1; |
| 272 | } |
| 273 | tmp = op->func_code; |
| 274 | Py_INCREF(value); |
| 275 | op->func_code = value; |
| 276 | Py_DECREF(tmp); |
| 277 | return 0; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static PyObject * |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 281 | func_get_name(PyFunctionObject *op) |
| 282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | Py_INCREF(op->func_name); |
| 284 | return op->func_name; |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static int |
| 288 | func_set_name(PyFunctionObject *op, PyObject *value) |
| 289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | PyObject *tmp; |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | /* Not legal to del f.func_name or to set it to anything |
| 293 | * other than a string object. */ |
| 294 | if (value == NULL || !PyUnicode_Check(value)) { |
| 295 | PyErr_SetString(PyExc_TypeError, |
| 296 | "__name__ must be set to a string object"); |
| 297 | return -1; |
| 298 | } |
| 299 | tmp = op->func_name; |
| 300 | Py_INCREF(value); |
| 301 | op->func_name = value; |
| 302 | Py_DECREF(tmp); |
| 303 | return 0; |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static PyObject * |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 307 | func_get_qualname(PyFunctionObject *op) |
| 308 | { |
| 309 | Py_INCREF(op->func_qualname); |
| 310 | return op->func_qualname; |
| 311 | } |
| 312 | |
| 313 | static int |
| 314 | func_set_qualname(PyFunctionObject *op, PyObject *value) |
| 315 | { |
| 316 | PyObject *tmp; |
| 317 | |
| 318 | /* Not legal to del f.__qualname__ or to set it to anything |
| 319 | * other than a string object. */ |
| 320 | if (value == NULL || !PyUnicode_Check(value)) { |
| 321 | PyErr_SetString(PyExc_TypeError, |
| 322 | "__qualname__ must be set to a string object"); |
| 323 | return -1; |
| 324 | } |
| 325 | tmp = op->func_qualname; |
| 326 | Py_INCREF(value); |
| 327 | op->func_qualname = value; |
| 328 | Py_DECREF(tmp); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static PyObject * |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 333 | func_get_defaults(PyFunctionObject *op) |
| 334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | if (op->func_defaults == NULL) { |
| 336 | Py_INCREF(Py_None); |
| 337 | return Py_None; |
| 338 | } |
| 339 | Py_INCREF(op->func_defaults); |
| 340 | return op->func_defaults; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static int |
| 344 | func_set_defaults(PyFunctionObject *op, PyObject *value) |
| 345 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | PyObject *tmp; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | /* Legal to del f.func_defaults. |
| 349 | * Can only set func_defaults to NULL or a tuple. */ |
| 350 | if (value == Py_None) |
| 351 | value = NULL; |
| 352 | if (value != NULL && !PyTuple_Check(value)) { |
| 353 | PyErr_SetString(PyExc_TypeError, |
| 354 | "__defaults__ must be set to a tuple object"); |
| 355 | return -1; |
| 356 | } |
| 357 | tmp = op->func_defaults; |
| 358 | Py_XINCREF(value); |
| 359 | op->func_defaults = value; |
| 360 | Py_XDECREF(tmp); |
| 361 | return 0; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 364 | static PyObject * |
| 365 | func_get_kwdefaults(PyFunctionObject *op) |
| 366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | if (op->func_kwdefaults == NULL) { |
| 368 | Py_INCREF(Py_None); |
| 369 | return Py_None; |
| 370 | } |
| 371 | Py_INCREF(op->func_kwdefaults); |
| 372 | return op->func_kwdefaults; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static int |
| 376 | func_set_kwdefaults(PyFunctionObject *op, PyObject *value) |
| 377 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | PyObject *tmp; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | if (value == Py_None) |
| 381 | value = NULL; |
| 382 | /* Legal to del f.func_kwdefaults. |
| 383 | * Can only set func_kwdefaults to NULL or a dict. */ |
| 384 | if (value != NULL && !PyDict_Check(value)) { |
| 385 | PyErr_SetString(PyExc_TypeError, |
| 386 | "__kwdefaults__ must be set to a dict object"); |
| 387 | return -1; |
| 388 | } |
| 389 | tmp = op->func_kwdefaults; |
| 390 | Py_XINCREF(value); |
| 391 | op->func_kwdefaults = value; |
| 392 | Py_XDECREF(tmp); |
| 393 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 396 | static PyObject * |
| 397 | func_get_annotations(PyFunctionObject *op) |
| 398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | if (op->func_annotations == NULL) { |
| 400 | op->func_annotations = PyDict_New(); |
| 401 | if (op->func_annotations == NULL) |
| 402 | return NULL; |
| 403 | } |
| 404 | Py_INCREF(op->func_annotations); |
| 405 | return op->func_annotations; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static int |
| 409 | func_set_annotations(PyFunctionObject *op, PyObject *value) |
| 410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | PyObject *tmp; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | if (value == Py_None) |
| 414 | value = NULL; |
| 415 | /* Legal to del f.func_annotations. |
| 416 | * Can only set func_annotations to NULL (through C api) |
| 417 | * or a dict. */ |
| 418 | if (value != NULL && !PyDict_Check(value)) { |
| 419 | PyErr_SetString(PyExc_TypeError, |
| 420 | "__annotations__ must be set to a dict object"); |
| 421 | return -1; |
| 422 | } |
| 423 | tmp = op->func_annotations; |
| 424 | Py_XINCREF(value); |
| 425 | op->func_annotations = value; |
| 426 | Py_XDECREF(tmp); |
| 427 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 430 | static PyGetSetDef func_getsetlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | {"__code__", (getter)func_get_code, (setter)func_set_code}, |
| 432 | {"__defaults__", (getter)func_get_defaults, |
| 433 | (setter)func_set_defaults}, |
| 434 | {"__kwdefaults__", (getter)func_get_kwdefaults, |
| 435 | (setter)func_set_kwdefaults}, |
| 436 | {"__annotations__", (getter)func_get_annotations, |
| 437 | (setter)func_set_annotations}, |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 438 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | {"__name__", (getter)func_get_name, (setter)func_set_name}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 440 | {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | {NULL} /* Sentinel */ |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 442 | }; |
| 443 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 444 | PyDoc_STRVAR(func_doc, |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 445 | "function(code, globals[, name[, argdefs[, closure]]])\n\ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 446 | \n\ |
| 447 | Create a function object from a code object and a dictionary.\n\ |
| 448 | The optional name string overrides the name from the code object.\n\ |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 449 | The optional argdefs tuple specifies the default argument values.\n\ |
| 450 | The optional closure tuple supplies the bindings for free variables."); |
| 451 | |
| 452 | /* func_new() maintains the following invariants for closures. The |
| 453 | closure must correspond to the free variables of the code object. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | |
| 455 | if len(code.co_freevars) == 0: |
| 456 | closure = NULL |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 457 | else: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | len(closure) == len(code.co_freevars) |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 459 | for every elt in closure, type(elt) == cell |
| 460 | */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 461 | |
| 462 | static PyObject * |
| 463 | func_new(PyTypeObject* type, PyObject* args, PyObject* kw) |
| 464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | PyCodeObject *code; |
| 466 | PyObject *globals; |
| 467 | PyObject *name = Py_None; |
| 468 | PyObject *defaults = Py_None; |
| 469 | PyObject *closure = Py_None; |
| 470 | PyFunctionObject *newfunc; |
| 471 | Py_ssize_t nfree, nclosure; |
| 472 | static char *kwlist[] = {"code", "globals", "name", |
| 473 | "argdefs", "closure", 0}; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function", |
| 476 | kwlist, |
| 477 | &PyCode_Type, &code, |
| 478 | &PyDict_Type, &globals, |
| 479 | &name, &defaults, &closure)) |
| 480 | return NULL; |
| 481 | if (name != Py_None && !PyUnicode_Check(name)) { |
| 482 | PyErr_SetString(PyExc_TypeError, |
| 483 | "arg 3 (name) must be None or string"); |
| 484 | return NULL; |
| 485 | } |
| 486 | if (defaults != Py_None && !PyTuple_Check(defaults)) { |
| 487 | PyErr_SetString(PyExc_TypeError, |
| 488 | "arg 4 (defaults) must be None or tuple"); |
| 489 | return NULL; |
| 490 | } |
| 491 | nfree = PyTuple_GET_SIZE(code->co_freevars); |
| 492 | if (!PyTuple_Check(closure)) { |
| 493 | if (nfree && closure == Py_None) { |
| 494 | PyErr_SetString(PyExc_TypeError, |
| 495 | "arg 5 (closure) must be tuple"); |
| 496 | return NULL; |
| 497 | } |
| 498 | else if (closure != Py_None) { |
| 499 | PyErr_SetString(PyExc_TypeError, |
| 500 | "arg 5 (closure) must be None or tuple"); |
| 501 | return NULL; |
| 502 | } |
| 503 | } |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | /* check that the closure is well-formed */ |
| 506 | nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); |
| 507 | if (nfree != nclosure) |
| 508 | return PyErr_Format(PyExc_ValueError, |
| 509 | "%U requires closure of length %zd, not %zd", |
| 510 | code->co_name, nfree, nclosure); |
| 511 | if (nclosure) { |
| 512 | Py_ssize_t i; |
| 513 | for (i = 0; i < nclosure; i++) { |
| 514 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 515 | if (!PyCell_Check(o)) { |
| 516 | return PyErr_Format(PyExc_TypeError, |
| 517 | "arg 5 (closure) expected cell, found %s", |
| 518 | o->ob_type->tp_name); |
| 519 | } |
| 520 | } |
| 521 | } |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code, |
| 524 | globals); |
| 525 | if (newfunc == NULL) |
| 526 | return NULL; |
| 527 | |
| 528 | if (name != Py_None) { |
| 529 | Py_INCREF(name); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame^] | 530 | Py_SETREF(newfunc->func_name, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | } |
| 532 | if (defaults != Py_None) { |
| 533 | Py_INCREF(defaults); |
| 534 | newfunc->func_defaults = defaults; |
| 535 | } |
| 536 | if (closure != Py_None) { |
| 537 | Py_INCREF(closure); |
| 538 | newfunc->func_closure = closure; |
| 539 | } |
| 540 | |
| 541 | return (PyObject *)newfunc; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 544 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 545 | func_dealloc(PyFunctionObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | _PyObject_GC_UNTRACK(op); |
| 548 | if (op->func_weakreflist != NULL) |
| 549 | PyObject_ClearWeakRefs((PyObject *) op); |
| 550 | Py_DECREF(op->func_code); |
| 551 | Py_DECREF(op->func_globals); |
| 552 | Py_XDECREF(op->func_module); |
| 553 | Py_DECREF(op->func_name); |
| 554 | Py_XDECREF(op->func_defaults); |
| 555 | Py_XDECREF(op->func_kwdefaults); |
| 556 | Py_XDECREF(op->func_doc); |
| 557 | Py_XDECREF(op->func_dict); |
| 558 | Py_XDECREF(op->func_closure); |
| 559 | Py_XDECREF(op->func_annotations); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 560 | Py_XDECREF(op->func_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | PyObject_GC_Del(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 564 | static PyObject* |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 565 | func_repr(PyFunctionObject *op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | return PyUnicode_FromFormat("<function %U at %p>", |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 568 | op->func_qualname, op); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 571 | static int |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 572 | func_traverse(PyFunctionObject *f, visitproc visit, void *arg) |
| 573 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | Py_VISIT(f->func_code); |
| 575 | Py_VISIT(f->func_globals); |
| 576 | Py_VISIT(f->func_module); |
| 577 | Py_VISIT(f->func_defaults); |
| 578 | Py_VISIT(f->func_kwdefaults); |
| 579 | Py_VISIT(f->func_doc); |
| 580 | Py_VISIT(f->func_name); |
| 581 | Py_VISIT(f->func_dict); |
| 582 | Py_VISIT(f->func_closure); |
| 583 | Py_VISIT(f->func_annotations); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 584 | Py_VISIT(f->func_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 588 | static PyObject * |
| 589 | function_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | PyObject *result; |
| 592 | PyObject *argdefs; |
| 593 | PyObject *kwtuple = NULL; |
| 594 | PyObject **d, **k; |
| 595 | Py_ssize_t nk, nd; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | argdefs = PyFunction_GET_DEFAULTS(func); |
| 598 | if (argdefs != NULL && PyTuple_Check(argdefs)) { |
| 599 | d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0); |
| 600 | nd = PyTuple_GET_SIZE(argdefs); |
| 601 | } |
| 602 | else { |
| 603 | d = NULL; |
| 604 | nd = 0; |
| 605 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | if (kw != NULL && PyDict_Check(kw)) { |
| 608 | Py_ssize_t pos, i; |
| 609 | nk = PyDict_Size(kw); |
| 610 | kwtuple = PyTuple_New(2*nk); |
| 611 | if (kwtuple == NULL) |
| 612 | return NULL; |
| 613 | k = &PyTuple_GET_ITEM(kwtuple, 0); |
| 614 | pos = i = 0; |
| 615 | while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) { |
| 616 | Py_INCREF(k[i]); |
| 617 | Py_INCREF(k[i+1]); |
| 618 | i += 2; |
| 619 | } |
| 620 | nk = i/2; |
| 621 | } |
| 622 | else { |
| 623 | k = NULL; |
| 624 | nk = 0; |
| 625 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | result = PyEval_EvalCodeEx( |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 628 | PyFunction_GET_CODE(func), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | PyFunction_GET_GLOBALS(func), (PyObject *)NULL, |
| 630 | &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg), |
| 631 | k, nk, d, nd, |
| 632 | PyFunction_GET_KW_DEFAULTS(func), |
| 633 | PyFunction_GET_CLOSURE(func)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | Py_XDECREF(kwtuple); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /* Bind a function to an object */ |
| 641 | static PyObject * |
| 642 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | if (obj == Py_None || obj == NULL) { |
| 645 | Py_INCREF(func); |
| 646 | return func; |
| 647 | } |
| 648 | return PyMethod_New(func, obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 651 | PyTypeObject PyFunction_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 653 | "function", |
| 654 | sizeof(PyFunctionObject), |
| 655 | 0, |
| 656 | (destructor)func_dealloc, /* tp_dealloc */ |
| 657 | 0, /* tp_print */ |
| 658 | 0, /* tp_getattr */ |
| 659 | 0, /* tp_setattr */ |
| 660 | 0, /* tp_reserved */ |
| 661 | (reprfunc)func_repr, /* tp_repr */ |
| 662 | 0, /* tp_as_number */ |
| 663 | 0, /* tp_as_sequence */ |
| 664 | 0, /* tp_as_mapping */ |
| 665 | 0, /* tp_hash */ |
| 666 | function_call, /* tp_call */ |
| 667 | 0, /* tp_str */ |
Benjamin Peterson | 2cf936f | 2012-02-19 01:16:13 -0500 | [diff] [blame] | 668 | 0, /* tp_getattro */ |
| 669 | 0, /* tp_setattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | 0, /* tp_as_buffer */ |
| 671 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 672 | func_doc, /* tp_doc */ |
| 673 | (traverseproc)func_traverse, /* tp_traverse */ |
| 674 | 0, /* tp_clear */ |
| 675 | 0, /* tp_richcompare */ |
| 676 | offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */ |
| 677 | 0, /* tp_iter */ |
| 678 | 0, /* tp_iternext */ |
| 679 | 0, /* tp_methods */ |
| 680 | func_memberlist, /* tp_members */ |
| 681 | func_getsetlist, /* tp_getset */ |
| 682 | 0, /* tp_base */ |
| 683 | 0, /* tp_dict */ |
| 684 | func_descr_get, /* tp_descr_get */ |
| 685 | 0, /* tp_descr_set */ |
| 686 | offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */ |
| 687 | 0, /* tp_init */ |
| 688 | 0, /* tp_alloc */ |
| 689 | func_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 690 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 691 | |
| 692 | |
| 693 | /* Class method object */ |
| 694 | |
| 695 | /* A class method receives the class as implicit first argument, |
| 696 | just like an instance method receives the instance. |
| 697 | To declare a class method, use this idiom: |
| 698 | |
| 699 | class C: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | def f(cls, arg1, arg2, ...): ... |
| 701 | f = classmethod(f) |
| 702 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 703 | It can be called either on the class (e.g. C.f()) or on an instance |
| 704 | (e.g. C().f()); the instance is ignored except for its class. |
| 705 | If a class method is called for a derived class, the derived class |
| 706 | object is passed as the implied first argument. |
| 707 | |
| 708 | Class methods are different than C++ or Java static methods. |
| 709 | If you want those, see static methods below. |
| 710 | */ |
| 711 | |
| 712 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | PyObject_HEAD |
| 714 | PyObject *cm_callable; |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 715 | PyObject *cm_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 716 | } classmethod; |
| 717 | |
| 718 | static void |
| 719 | cm_dealloc(classmethod *cm) |
| 720 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | _PyObject_GC_UNTRACK((PyObject *)cm); |
| 722 | Py_XDECREF(cm->cm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 723 | Py_XDECREF(cm->cm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 724 | Py_TYPE(cm)->tp_free((PyObject *)cm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 727 | static int |
| 728 | cm_traverse(classmethod *cm, visitproc visit, void *arg) |
| 729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | Py_VISIT(cm->cm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 731 | Py_VISIT(cm->cm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static int |
| 736 | cm_clear(classmethod *cm) |
| 737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | Py_CLEAR(cm->cm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 739 | Py_CLEAR(cm->cm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 744 | static PyObject * |
| 745 | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 746 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | classmethod *cm = (classmethod *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | if (cm->cm_callable == NULL) { |
| 750 | PyErr_SetString(PyExc_RuntimeError, |
| 751 | "uninitialized classmethod object"); |
| 752 | return NULL; |
| 753 | } |
| 754 | if (type == NULL) |
| 755 | type = (PyObject *)(Py_TYPE(obj)); |
| 756 | return PyMethod_New(cm->cm_callable, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static int |
| 760 | cm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 761 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | classmethod *cm = (classmethod *)self; |
| 763 | PyObject *callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable)) |
| 766 | return -1; |
| 767 | if (!_PyArg_NoKeywords("classmethod", kwds)) |
| 768 | return -1; |
| 769 | Py_INCREF(callable); |
| 770 | cm->cm_callable = callable; |
| 771 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 774 | static PyMemberDef cm_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY}, |
| 776 | {NULL} /* Sentinel */ |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 777 | }; |
| 778 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 779 | static PyObject * |
| 780 | cm_get___isabstractmethod__(classmethod *cm, void *closure) |
| 781 | { |
| 782 | int res = _PyObject_IsAbstract(cm->cm_callable); |
| 783 | if (res == -1) { |
| 784 | return NULL; |
| 785 | } |
| 786 | else if (res) { |
| 787 | Py_RETURN_TRUE; |
| 788 | } |
| 789 | Py_RETURN_FALSE; |
| 790 | } |
| 791 | |
| 792 | static PyGetSetDef cm_getsetlist[] = { |
| 793 | {"__isabstractmethod__", |
| 794 | (getter)cm_get___isabstractmethod__, NULL, |
| 795 | NULL, |
| 796 | NULL}, |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 797 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL}, |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 798 | {NULL} /* Sentinel */ |
| 799 | }; |
| 800 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 801 | PyDoc_STRVAR(classmethod_doc, |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 802 | "classmethod(function) -> method\n\ |
| 803 | \n\ |
| 804 | Convert a function to be a class method.\n\ |
| 805 | \n\ |
| 806 | A class method receives the class as implicit first argument,\n\ |
| 807 | just like an instance method receives the instance.\n\ |
| 808 | To declare a class method, use this idiom:\n\ |
| 809 | \n\ |
| 810 | class C:\n\ |
| 811 | def f(cls, arg1, arg2, ...): ...\n\ |
| 812 | f = classmethod(f)\n\ |
| 813 | \n\ |
| 814 | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
| 815 | (e.g. C().f()). The instance is ignored except for its class.\n\ |
| 816 | If a class method is called for a derived class, the derived class\n\ |
| 817 | object is passed as the implied first argument.\n\ |
Sjoerd Mullender | 564980b | 2001-12-17 11:39:56 +0000 | [diff] [blame] | 818 | \n\ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 819 | Class methods are different than C++ or Java static methods.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 820 | If you want those, see the staticmethod builtin."); |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 821 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 822 | PyTypeObject PyClassMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 824 | "classmethod", |
| 825 | sizeof(classmethod), |
| 826 | 0, |
| 827 | (destructor)cm_dealloc, /* tp_dealloc */ |
| 828 | 0, /* tp_print */ |
| 829 | 0, /* tp_getattr */ |
| 830 | 0, /* tp_setattr */ |
| 831 | 0, /* tp_reserved */ |
| 832 | 0, /* tp_repr */ |
| 833 | 0, /* tp_as_number */ |
| 834 | 0, /* tp_as_sequence */ |
| 835 | 0, /* tp_as_mapping */ |
| 836 | 0, /* tp_hash */ |
| 837 | 0, /* tp_call */ |
| 838 | 0, /* tp_str */ |
Benjamin Peterson | 2cf936f | 2012-02-19 01:16:13 -0500 | [diff] [blame] | 839 | 0, /* tp_getattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | 0, /* tp_setattro */ |
| 841 | 0, /* tp_as_buffer */ |
| 842 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 843 | classmethod_doc, /* tp_doc */ |
| 844 | (traverseproc)cm_traverse, /* tp_traverse */ |
| 845 | (inquiry)cm_clear, /* tp_clear */ |
| 846 | 0, /* tp_richcompare */ |
| 847 | 0, /* tp_weaklistoffset */ |
| 848 | 0, /* tp_iter */ |
| 849 | 0, /* tp_iternext */ |
| 850 | 0, /* tp_methods */ |
| 851 | cm_memberlist, /* tp_members */ |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 852 | cm_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | 0, /* tp_base */ |
| 854 | 0, /* tp_dict */ |
| 855 | cm_descr_get, /* tp_descr_get */ |
| 856 | 0, /* tp_descr_set */ |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 857 | offsetof(classmethod, cm_dict), /* tp_dictoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | cm_init, /* tp_init */ |
| 859 | PyType_GenericAlloc, /* tp_alloc */ |
| 860 | PyType_GenericNew, /* tp_new */ |
| 861 | PyObject_GC_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 862 | }; |
| 863 | |
| 864 | PyObject * |
| 865 | PyClassMethod_New(PyObject *callable) |
| 866 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | classmethod *cm = (classmethod *) |
| 868 | PyType_GenericAlloc(&PyClassMethod_Type, 0); |
| 869 | if (cm != NULL) { |
| 870 | Py_INCREF(callable); |
| 871 | cm->cm_callable = callable; |
| 872 | } |
| 873 | return (PyObject *)cm; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | |
| 877 | /* Static method object */ |
| 878 | |
| 879 | /* A static method does not receive an implicit first argument. |
| 880 | To declare a static method, use this idiom: |
| 881 | |
| 882 | class C: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 883 | def f(arg1, arg2, ...): ... |
| 884 | f = staticmethod(f) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 885 | |
| 886 | It can be called either on the class (e.g. C.f()) or on an instance |
| 887 | (e.g. C().f()); the instance is ignored except for its class. |
| 888 | |
| 889 | Static methods in Python are similar to those found in Java or C++. |
| 890 | For a more advanced concept, see class methods above. |
| 891 | */ |
| 892 | |
| 893 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | PyObject_HEAD |
| 895 | PyObject *sm_callable; |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 896 | PyObject *sm_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 897 | } staticmethod; |
| 898 | |
| 899 | static void |
| 900 | sm_dealloc(staticmethod *sm) |
| 901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | _PyObject_GC_UNTRACK((PyObject *)sm); |
| 903 | Py_XDECREF(sm->sm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 904 | Py_XDECREF(sm->sm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | Py_TYPE(sm)->tp_free((PyObject *)sm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 908 | static int |
| 909 | sm_traverse(staticmethod *sm, visitproc visit, void *arg) |
| 910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | Py_VISIT(sm->sm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 912 | Py_VISIT(sm->sm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static int |
| 917 | sm_clear(staticmethod *sm) |
| 918 | { |
Benjamin Peterson | 496c53d | 2012-02-19 01:11:56 -0500 | [diff] [blame] | 919 | Py_CLEAR(sm->sm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 920 | Py_CLEAR(sm->sm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 924 | static PyObject * |
| 925 | sm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 926 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | staticmethod *sm = (staticmethod *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | if (sm->sm_callable == NULL) { |
| 930 | PyErr_SetString(PyExc_RuntimeError, |
| 931 | "uninitialized staticmethod object"); |
| 932 | return NULL; |
| 933 | } |
| 934 | Py_INCREF(sm->sm_callable); |
| 935 | return sm->sm_callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static int |
| 939 | sm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 940 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | staticmethod *sm = (staticmethod *)self; |
| 942 | PyObject *callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable)) |
| 945 | return -1; |
| 946 | if (!_PyArg_NoKeywords("staticmethod", kwds)) |
| 947 | return -1; |
| 948 | Py_INCREF(callable); |
| 949 | sm->sm_callable = callable; |
| 950 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 953 | static PyMemberDef sm_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY}, |
| 955 | {NULL} /* Sentinel */ |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 956 | }; |
| 957 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 958 | static PyObject * |
| 959 | sm_get___isabstractmethod__(staticmethod *sm, void *closure) |
| 960 | { |
| 961 | int res = _PyObject_IsAbstract(sm->sm_callable); |
| 962 | if (res == -1) { |
| 963 | return NULL; |
| 964 | } |
| 965 | else if (res) { |
| 966 | Py_RETURN_TRUE; |
| 967 | } |
| 968 | Py_RETURN_FALSE; |
| 969 | } |
| 970 | |
| 971 | static PyGetSetDef sm_getsetlist[] = { |
| 972 | {"__isabstractmethod__", |
| 973 | (getter)sm_get___isabstractmethod__, NULL, |
| 974 | NULL, |
| 975 | NULL}, |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 976 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL}, |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 977 | {NULL} /* Sentinel */ |
| 978 | }; |
| 979 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 980 | PyDoc_STRVAR(staticmethod_doc, |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 981 | "staticmethod(function) -> method\n\ |
| 982 | \n\ |
| 983 | Convert a function to be a static method.\n\ |
| 984 | \n\ |
| 985 | A static method does not receive an implicit first argument.\n\ |
| 986 | To declare a static method, use this idiom:\n\ |
| 987 | \n\ |
| 988 | class C:\n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 | def f(arg1, arg2, ...): ...\n\ |
| 990 | f = staticmethod(f)\n\ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 991 | \n\ |
| 992 | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
| 993 | (e.g. C().f()). The instance is ignored except for its class.\n\ |
| 994 | \n\ |
| 995 | Static methods in Python are similar to those found in Java or C++.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 996 | For a more advanced concept, see the classmethod builtin."); |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 997 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 998 | PyTypeObject PyStaticMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1000 | "staticmethod", |
| 1001 | sizeof(staticmethod), |
| 1002 | 0, |
| 1003 | (destructor)sm_dealloc, /* tp_dealloc */ |
| 1004 | 0, /* tp_print */ |
| 1005 | 0, /* tp_getattr */ |
| 1006 | 0, /* tp_setattr */ |
| 1007 | 0, /* tp_reserved */ |
| 1008 | 0, /* tp_repr */ |
| 1009 | 0, /* tp_as_number */ |
| 1010 | 0, /* tp_as_sequence */ |
| 1011 | 0, /* tp_as_mapping */ |
| 1012 | 0, /* tp_hash */ |
| 1013 | 0, /* tp_call */ |
| 1014 | 0, /* tp_str */ |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 1015 | 0, /* tp_getattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | 0, /* tp_setattro */ |
| 1017 | 0, /* tp_as_buffer */ |
| 1018 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 1019 | staticmethod_doc, /* tp_doc */ |
| 1020 | (traverseproc)sm_traverse, /* tp_traverse */ |
| 1021 | (inquiry)sm_clear, /* tp_clear */ |
| 1022 | 0, /* tp_richcompare */ |
| 1023 | 0, /* tp_weaklistoffset */ |
| 1024 | 0, /* tp_iter */ |
| 1025 | 0, /* tp_iternext */ |
| 1026 | 0, /* tp_methods */ |
| 1027 | sm_memberlist, /* tp_members */ |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 1028 | sm_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | 0, /* tp_base */ |
| 1030 | 0, /* tp_dict */ |
| 1031 | sm_descr_get, /* tp_descr_get */ |
| 1032 | 0, /* tp_descr_set */ |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 1033 | offsetof(staticmethod, sm_dict), /* tp_dictoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | sm_init, /* tp_init */ |
| 1035 | PyType_GenericAlloc, /* tp_alloc */ |
| 1036 | PyType_GenericNew, /* tp_new */ |
| 1037 | PyObject_GC_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1038 | }; |
| 1039 | |
| 1040 | PyObject * |
| 1041 | PyStaticMethod_New(PyObject *callable) |
| 1042 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | staticmethod *sm = (staticmethod *) |
| 1044 | PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
| 1045 | if (sm != NULL) { |
| 1046 | Py_INCREF(callable); |
| 1047 | sm->sm_callable = callable; |
| 1048 | } |
| 1049 | return (PyObject *)sm; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1050 | } |