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