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