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