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