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