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