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