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