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