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