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