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