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