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: |
Martin Panter | 6d57fe1 | 2016-09-17 03:26:16 +0000 | [diff] [blame] | 700 | @classmethod |
| 701 | def f(cls, arg1, arg2, ...): |
| 702 | ... |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 704 | It can be called either on the class (e.g. C.f()) or on an instance |
| 705 | (e.g. C().f()); the instance is ignored except for its class. |
| 706 | If a class method is called for a derived class, the derived class |
| 707 | object is passed as the implied first argument. |
| 708 | |
| 709 | Class methods are different than C++ or Java static methods. |
| 710 | If you want those, see static methods below. |
| 711 | */ |
| 712 | |
| 713 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | PyObject_HEAD |
| 715 | PyObject *cm_callable; |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 716 | PyObject *cm_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 717 | } classmethod; |
| 718 | |
| 719 | static void |
| 720 | cm_dealloc(classmethod *cm) |
| 721 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | _PyObject_GC_UNTRACK((PyObject *)cm); |
| 723 | Py_XDECREF(cm->cm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 724 | Py_XDECREF(cm->cm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | Py_TYPE(cm)->tp_free((PyObject *)cm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 728 | static int |
| 729 | cm_traverse(classmethod *cm, visitproc visit, void *arg) |
| 730 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | Py_VISIT(cm->cm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 732 | Py_VISIT(cm->cm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static int |
| 737 | cm_clear(classmethod *cm) |
| 738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | Py_CLEAR(cm->cm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 740 | Py_CLEAR(cm->cm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 745 | static PyObject * |
| 746 | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 747 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | classmethod *cm = (classmethod *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 750 | if (cm->cm_callable == NULL) { |
| 751 | PyErr_SetString(PyExc_RuntimeError, |
| 752 | "uninitialized classmethod object"); |
| 753 | return NULL; |
| 754 | } |
| 755 | if (type == NULL) |
| 756 | type = (PyObject *)(Py_TYPE(obj)); |
| 757 | return PyMethod_New(cm->cm_callable, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | static int |
| 761 | cm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | classmethod *cm = (classmethod *)self; |
| 764 | PyObject *callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 766 | if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable)) |
| 767 | return -1; |
| 768 | if (!_PyArg_NoKeywords("classmethod", kwds)) |
| 769 | return -1; |
| 770 | Py_INCREF(callable); |
| 771 | cm->cm_callable = callable; |
| 772 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 775 | static PyMemberDef cm_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 | {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY}, |
| 777 | {NULL} /* Sentinel */ |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 778 | }; |
| 779 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 780 | static PyObject * |
| 781 | cm_get___isabstractmethod__(classmethod *cm, void *closure) |
| 782 | { |
| 783 | int res = _PyObject_IsAbstract(cm->cm_callable); |
| 784 | if (res == -1) { |
| 785 | return NULL; |
| 786 | } |
| 787 | else if (res) { |
| 788 | Py_RETURN_TRUE; |
| 789 | } |
| 790 | Py_RETURN_FALSE; |
| 791 | } |
| 792 | |
| 793 | static PyGetSetDef cm_getsetlist[] = { |
| 794 | {"__isabstractmethod__", |
| 795 | (getter)cm_get___isabstractmethod__, NULL, |
| 796 | NULL, |
| 797 | NULL}, |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 798 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL}, |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 799 | {NULL} /* Sentinel */ |
| 800 | }; |
| 801 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 802 | PyDoc_STRVAR(classmethod_doc, |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 803 | "classmethod(function) -> method\n\ |
| 804 | \n\ |
| 805 | Convert a function to be a class method.\n\ |
| 806 | \n\ |
| 807 | A class method receives the class as implicit first argument,\n\ |
| 808 | just like an instance method receives the instance.\n\ |
| 809 | To declare a class method, use this idiom:\n\ |
| 810 | \n\ |
| 811 | class C:\n\ |
Martin Panter | 6d57fe1 | 2016-09-17 03:26:16 +0000 | [diff] [blame] | 812 | @classmethod\n\ |
| 813 | def f(cls, arg1, arg2, ...):\n\ |
| 814 | ...\n\ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 815 | \n\ |
| 816 | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
| 817 | (e.g. C().f()). The instance is ignored except for its class.\n\ |
| 818 | If a class method is called for a derived class, the derived class\n\ |
| 819 | object is passed as the implied first argument.\n\ |
Sjoerd Mullender | 564980b | 2001-12-17 11:39:56 +0000 | [diff] [blame] | 820 | \n\ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 821 | 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] | 822 | If you want those, see the staticmethod builtin."); |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 823 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 824 | PyTypeObject PyClassMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 826 | "classmethod", |
| 827 | sizeof(classmethod), |
| 828 | 0, |
| 829 | (destructor)cm_dealloc, /* tp_dealloc */ |
| 830 | 0, /* tp_print */ |
| 831 | 0, /* tp_getattr */ |
| 832 | 0, /* tp_setattr */ |
| 833 | 0, /* tp_reserved */ |
| 834 | 0, /* tp_repr */ |
| 835 | 0, /* tp_as_number */ |
| 836 | 0, /* tp_as_sequence */ |
| 837 | 0, /* tp_as_mapping */ |
| 838 | 0, /* tp_hash */ |
| 839 | 0, /* tp_call */ |
| 840 | 0, /* tp_str */ |
Benjamin Peterson | 2cf936f | 2012-02-19 01:16:13 -0500 | [diff] [blame] | 841 | 0, /* tp_getattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | 0, /* tp_setattro */ |
| 843 | 0, /* tp_as_buffer */ |
| 844 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 845 | classmethod_doc, /* tp_doc */ |
| 846 | (traverseproc)cm_traverse, /* tp_traverse */ |
| 847 | (inquiry)cm_clear, /* tp_clear */ |
| 848 | 0, /* tp_richcompare */ |
| 849 | 0, /* tp_weaklistoffset */ |
| 850 | 0, /* tp_iter */ |
| 851 | 0, /* tp_iternext */ |
| 852 | 0, /* tp_methods */ |
| 853 | cm_memberlist, /* tp_members */ |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 854 | cm_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | 0, /* tp_base */ |
| 856 | 0, /* tp_dict */ |
| 857 | cm_descr_get, /* tp_descr_get */ |
| 858 | 0, /* tp_descr_set */ |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 859 | offsetof(classmethod, cm_dict), /* tp_dictoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | cm_init, /* tp_init */ |
| 861 | PyType_GenericAlloc, /* tp_alloc */ |
| 862 | PyType_GenericNew, /* tp_new */ |
| 863 | PyObject_GC_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 864 | }; |
| 865 | |
| 866 | PyObject * |
| 867 | PyClassMethod_New(PyObject *callable) |
| 868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | classmethod *cm = (classmethod *) |
| 870 | PyType_GenericAlloc(&PyClassMethod_Type, 0); |
| 871 | if (cm != NULL) { |
| 872 | Py_INCREF(callable); |
| 873 | cm->cm_callable = callable; |
| 874 | } |
| 875 | return (PyObject *)cm; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | |
| 879 | /* Static method object */ |
| 880 | |
| 881 | /* A static method does not receive an implicit first argument. |
| 882 | To declare a static method, use this idiom: |
| 883 | |
| 884 | class C: |
Martin Panter | 6d57fe1 | 2016-09-17 03:26:16 +0000 | [diff] [blame] | 885 | @staticmethod |
| 886 | def f(arg1, arg2, ...): |
| 887 | ... |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 888 | |
| 889 | It can be called either on the class (e.g. C.f()) or on an instance |
| 890 | (e.g. C().f()); the instance is ignored except for its class. |
| 891 | |
| 892 | Static methods in Python are similar to those found in Java or C++. |
| 893 | For a more advanced concept, see class methods above. |
| 894 | */ |
| 895 | |
| 896 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | PyObject_HEAD |
| 898 | PyObject *sm_callable; |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 899 | PyObject *sm_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 900 | } staticmethod; |
| 901 | |
| 902 | static void |
| 903 | sm_dealloc(staticmethod *sm) |
| 904 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | _PyObject_GC_UNTRACK((PyObject *)sm); |
| 906 | Py_XDECREF(sm->sm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 907 | Py_XDECREF(sm->sm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | Py_TYPE(sm)->tp_free((PyObject *)sm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 911 | static int |
| 912 | sm_traverse(staticmethod *sm, visitproc visit, void *arg) |
| 913 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | Py_VISIT(sm->sm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 915 | Py_VISIT(sm->sm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | static int |
| 920 | sm_clear(staticmethod *sm) |
| 921 | { |
Benjamin Peterson | 496c53d | 2012-02-19 01:11:56 -0500 | [diff] [blame] | 922 | Py_CLEAR(sm->sm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 923 | Py_CLEAR(sm->sm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 927 | static PyObject * |
| 928 | sm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 929 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | staticmethod *sm = (staticmethod *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | if (sm->sm_callable == NULL) { |
| 933 | PyErr_SetString(PyExc_RuntimeError, |
| 934 | "uninitialized staticmethod object"); |
| 935 | return NULL; |
| 936 | } |
| 937 | Py_INCREF(sm->sm_callable); |
| 938 | return sm->sm_callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | static int |
| 942 | sm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | staticmethod *sm = (staticmethod *)self; |
| 945 | PyObject *callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable)) |
| 948 | return -1; |
| 949 | if (!_PyArg_NoKeywords("staticmethod", kwds)) |
| 950 | return -1; |
| 951 | Py_INCREF(callable); |
| 952 | sm->sm_callable = callable; |
| 953 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 956 | static PyMemberDef sm_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY}, |
| 958 | {NULL} /* Sentinel */ |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 959 | }; |
| 960 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 961 | static PyObject * |
| 962 | sm_get___isabstractmethod__(staticmethod *sm, void *closure) |
| 963 | { |
| 964 | int res = _PyObject_IsAbstract(sm->sm_callable); |
| 965 | if (res == -1) { |
| 966 | return NULL; |
| 967 | } |
| 968 | else if (res) { |
| 969 | Py_RETURN_TRUE; |
| 970 | } |
| 971 | Py_RETURN_FALSE; |
| 972 | } |
| 973 | |
| 974 | static PyGetSetDef sm_getsetlist[] = { |
| 975 | {"__isabstractmethod__", |
| 976 | (getter)sm_get___isabstractmethod__, NULL, |
| 977 | NULL, |
| 978 | NULL}, |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 979 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL}, |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 980 | {NULL} /* Sentinel */ |
| 981 | }; |
| 982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 983 | PyDoc_STRVAR(staticmethod_doc, |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 984 | "staticmethod(function) -> method\n\ |
| 985 | \n\ |
| 986 | Convert a function to be a static method.\n\ |
| 987 | \n\ |
| 988 | A static method does not receive an implicit first argument.\n\ |
| 989 | To declare a static method, use this idiom:\n\ |
| 990 | \n\ |
| 991 | class C:\n\ |
Martin Panter | 6d57fe1 | 2016-09-17 03:26:16 +0000 | [diff] [blame] | 992 | @staticmethod\n\ |
| 993 | def f(arg1, arg2, ...):\n\ |
| 994 | ...\n\ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 995 | \n\ |
| 996 | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
| 997 | (e.g. C().f()). The instance is ignored except for its class.\n\ |
| 998 | \n\ |
| 999 | 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] | 1000 | For a more advanced concept, see the classmethod builtin."); |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 1001 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1002 | PyTypeObject PyStaticMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1004 | "staticmethod", |
| 1005 | sizeof(staticmethod), |
| 1006 | 0, |
| 1007 | (destructor)sm_dealloc, /* tp_dealloc */ |
| 1008 | 0, /* tp_print */ |
| 1009 | 0, /* tp_getattr */ |
| 1010 | 0, /* tp_setattr */ |
| 1011 | 0, /* tp_reserved */ |
| 1012 | 0, /* tp_repr */ |
| 1013 | 0, /* tp_as_number */ |
| 1014 | 0, /* tp_as_sequence */ |
| 1015 | 0, /* tp_as_mapping */ |
| 1016 | 0, /* tp_hash */ |
| 1017 | 0, /* tp_call */ |
| 1018 | 0, /* tp_str */ |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 1019 | 0, /* tp_getattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | 0, /* tp_setattro */ |
| 1021 | 0, /* tp_as_buffer */ |
| 1022 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 1023 | staticmethod_doc, /* tp_doc */ |
| 1024 | (traverseproc)sm_traverse, /* tp_traverse */ |
| 1025 | (inquiry)sm_clear, /* tp_clear */ |
| 1026 | 0, /* tp_richcompare */ |
| 1027 | 0, /* tp_weaklistoffset */ |
| 1028 | 0, /* tp_iter */ |
| 1029 | 0, /* tp_iternext */ |
| 1030 | 0, /* tp_methods */ |
| 1031 | sm_memberlist, /* tp_members */ |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 1032 | sm_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | 0, /* tp_base */ |
| 1034 | 0, /* tp_dict */ |
| 1035 | sm_descr_get, /* tp_descr_get */ |
| 1036 | 0, /* tp_descr_set */ |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 1037 | offsetof(staticmethod, sm_dict), /* tp_dictoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | sm_init, /* tp_init */ |
| 1039 | PyType_GenericAlloc, /* tp_alloc */ |
| 1040 | PyType_GenericNew, /* tp_new */ |
| 1041 | PyObject_GC_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1042 | }; |
| 1043 | |
| 1044 | PyObject * |
| 1045 | PyStaticMethod_New(PyObject *callable) |
| 1046 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | staticmethod *sm = (staticmethod *) |
| 1048 | PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
| 1049 | if (sm != NULL) { |
| 1050 | Py_INCREF(callable); |
| 1051 | sm->sm_callable = callable; |
| 1052 | } |
| 1053 | return (PyObject *)sm; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1054 | } |