Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Function object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 5 | #include "pycore_object.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 6 | #include "pycore_pymem.h" |
| 7 | #include "pycore_pystate.h" |
Victor Stinner | ec13b93 | 2018-11-25 23:56:17 +0100 | [diff] [blame] | 8 | #include "pycore_tupleobject.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 9 | #include "code.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 10 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 12 | PyObject * |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 13 | PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | { |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 15 | PyFunctionObject *op; |
| 16 | PyObject *doc, *consts, *module; |
| 17 | static PyObject *__name__ = NULL; |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 18 | |
Victor Stinner | 34f96b8 | 2013-07-22 23:04:55 +0200 | [diff] [blame] | 19 | if (__name__ == NULL) { |
| 20 | __name__ = PyUnicode_InternFromString("__name__"); |
| 21 | if (__name__ == NULL) |
| 22 | return NULL; |
| 23 | } |
| 24 | |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 25 | op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type); |
| 26 | if (op == NULL) |
| 27 | return NULL; |
| 28 | |
| 29 | op->func_weakreflist = NULL; |
| 30 | Py_INCREF(code); |
| 31 | op->func_code = code; |
| 32 | Py_INCREF(globals); |
| 33 | op->func_globals = globals; |
| 34 | op->func_name = ((PyCodeObject *)code)->co_name; |
| 35 | Py_INCREF(op->func_name); |
| 36 | op->func_defaults = NULL; /* No default arguments */ |
| 37 | op->func_kwdefaults = NULL; /* No keyword only defaults */ |
| 38 | op->func_closure = NULL; |
Jeroen Demeyer | 37788bc | 2019-05-30 15:11:22 +0200 | [diff] [blame] | 39 | op->vectorcall = _PyFunction_Vectorcall; |
Victor Stinner | 34f96b8 | 2013-07-22 23:04:55 +0200 | [diff] [blame] | 40 | |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 41 | consts = ((PyCodeObject *)code)->co_consts; |
| 42 | if (PyTuple_Size(consts) >= 1) { |
| 43 | doc = PyTuple_GetItem(consts, 0); |
| 44 | if (!PyUnicode_Check(doc)) |
| 45 | doc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | } |
| 47 | else |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 48 | doc = Py_None; |
| 49 | Py_INCREF(doc); |
| 50 | op->func_doc = doc; |
Victor Stinner | 34f96b8 | 2013-07-22 23:04:55 +0200 | [diff] [blame] | 51 | |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 52 | op->func_dict = NULL; |
| 53 | op->func_module = NULL; |
| 54 | op->func_annotations = NULL; |
| 55 | |
| 56 | /* __module__: If module name is in globals, use it. |
Victor Stinner | 34f96b8 | 2013-07-22 23:04:55 +0200 | [diff] [blame] | 57 | Otherwise, use None. */ |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 58 | module = PyDict_GetItemWithError(globals, __name__); |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 59 | if (module) { |
| 60 | Py_INCREF(module); |
| 61 | op->func_module = module; |
| 62 | } |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 63 | else if (PyErr_Occurred()) { |
| 64 | Py_DECREF(op); |
| 65 | return NULL; |
| 66 | } |
Victor Stinner | 4d1f5d6 | 2013-07-22 23:02:05 +0200 | [diff] [blame] | 67 | if (qualname) |
| 68 | op->func_qualname = qualname; |
| 69 | else |
| 70 | op->func_qualname = op->func_name; |
| 71 | Py_INCREF(op->func_qualname); |
| 72 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | _PyObject_GC_TRACK(op); |
| 74 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 77 | PyObject * |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 78 | PyFunction_New(PyObject *code, PyObject *globals) |
| 79 | { |
| 80 | return PyFunction_NewWithQualName(code, globals, NULL); |
| 81 | } |
| 82 | |
| 83 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 84 | PyFunction_GetCode(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | if (!PyFunction_Check(op)) { |
| 87 | PyErr_BadInternalCall(); |
| 88 | return NULL; |
| 89 | } |
| 90 | return ((PyFunctionObject *) op) -> func_code; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 94 | PyFunction_GetGlobals(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | if (!PyFunction_Check(op)) { |
| 97 | PyErr_BadInternalCall(); |
| 98 | return NULL; |
| 99 | } |
| 100 | return ((PyFunctionObject *) op) -> func_globals; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 103 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 104 | PyFunction_GetModule(PyObject *op) |
| 105 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | if (!PyFunction_Check(op)) { |
| 107 | PyErr_BadInternalCall(); |
| 108 | return NULL; |
| 109 | } |
| 110 | return ((PyFunctionObject *) op) -> func_module; |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 114 | PyFunction_GetDefaults(PyObject *op) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 115 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | if (!PyFunction_Check(op)) { |
| 117 | PyErr_BadInternalCall(); |
| 118 | return NULL; |
| 119 | } |
| 120 | return ((PyFunctionObject *) op) -> func_defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 124 | PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | if (!PyFunction_Check(op)) { |
| 127 | PyErr_BadInternalCall(); |
| 128 | return -1; |
| 129 | } |
| 130 | if (defaults == Py_None) |
| 131 | defaults = NULL; |
| 132 | else if (defaults && PyTuple_Check(defaults)) { |
| 133 | Py_INCREF(defaults); |
| 134 | } |
| 135 | else { |
| 136 | PyErr_SetString(PyExc_SystemError, "non-tuple default args"); |
| 137 | return -1; |
| 138 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 139 | Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | return 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 143 | PyObject * |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 144 | PyFunction_GetKwDefaults(PyObject *op) |
| 145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | if (!PyFunction_Check(op)) { |
| 147 | PyErr_BadInternalCall(); |
| 148 | return NULL; |
| 149 | } |
| 150 | return ((PyFunctionObject *) op) -> func_kwdefaults; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | int |
| 154 | PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults) |
| 155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | if (!PyFunction_Check(op)) { |
| 157 | PyErr_BadInternalCall(); |
| 158 | return -1; |
| 159 | } |
| 160 | if (defaults == Py_None) |
| 161 | defaults = NULL; |
| 162 | else if (defaults && PyDict_Check(defaults)) { |
| 163 | Py_INCREF(defaults); |
| 164 | } |
| 165 | else { |
| 166 | PyErr_SetString(PyExc_SystemError, |
| 167 | "non-dict keyword only default args"); |
| 168 | return -1; |
| 169 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 170 | Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | PyObject * |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 175 | PyFunction_GetClosure(PyObject *op) |
| 176 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | if (!PyFunction_Check(op)) { |
| 178 | PyErr_BadInternalCall(); |
| 179 | return NULL; |
| 180 | } |
| 181 | return ((PyFunctionObject *) op) -> func_closure; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | int |
| 185 | PyFunction_SetClosure(PyObject *op, PyObject *closure) |
| 186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | if (!PyFunction_Check(op)) { |
| 188 | PyErr_BadInternalCall(); |
| 189 | return -1; |
| 190 | } |
| 191 | if (closure == Py_None) |
| 192 | closure = NULL; |
| 193 | else if (PyTuple_Check(closure)) { |
| 194 | Py_INCREF(closure); |
| 195 | } |
| 196 | else { |
| 197 | PyErr_Format(PyExc_SystemError, |
| 198 | "expected tuple for closure, got '%.100s'", |
| 199 | closure->ob_type->tp_name); |
| 200 | return -1; |
| 201 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 202 | Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 206 | PyObject * |
| 207 | PyFunction_GetAnnotations(PyObject *op) |
| 208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | if (!PyFunction_Check(op)) { |
| 210 | PyErr_BadInternalCall(); |
| 211 | return NULL; |
| 212 | } |
| 213 | return ((PyFunctionObject *) op) -> func_annotations; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | int |
| 217 | PyFunction_SetAnnotations(PyObject *op, PyObject *annotations) |
| 218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | if (!PyFunction_Check(op)) { |
| 220 | PyErr_BadInternalCall(); |
| 221 | return -1; |
| 222 | } |
| 223 | if (annotations == Py_None) |
| 224 | annotations = NULL; |
| 225 | else if (annotations && PyDict_Check(annotations)) { |
| 226 | Py_INCREF(annotations); |
| 227 | } |
| 228 | else { |
| 229 | PyErr_SetString(PyExc_SystemError, |
| 230 | "non-dict annotations"); |
| 231 | return -1; |
| 232 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 233 | Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | /* Methods */ |
| 238 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 239 | #define OFF(x) offsetof(PyFunctionObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 241 | static PyMemberDef func_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | {"__closure__", T_OBJECT, OFF(func_closure), |
| 243 | RESTRICTED|READONLY}, |
| 244 | {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED}, |
| 245 | {"__globals__", T_OBJECT, OFF(func_globals), |
| 246 | RESTRICTED|READONLY}, |
| 247 | {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED}, |
| 248 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 251 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 252 | func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored)) |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 253 | { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 254 | if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) { |
| 255 | return NULL; |
| 256 | } |
| 257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | Py_INCREF(op->func_code); |
| 259 | return op->func_code; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 263 | func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | Py_ssize_t nfree, nclosure; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | /* Not legal to del f.func_code or to set it to anything |
| 268 | * other than a code object. */ |
| 269 | if (value == NULL || !PyCode_Check(value)) { |
| 270 | PyErr_SetString(PyExc_TypeError, |
| 271 | "__code__ must be set to a code object"); |
| 272 | return -1; |
| 273 | } |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 274 | |
| 275 | if (PySys_Audit("object.__setattr__", "OsO", |
| 276 | op, "__code__", value) < 0) { |
| 277 | return -1; |
| 278 | } |
| 279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | nfree = PyCode_GetNumFree((PyCodeObject *)value); |
| 281 | nclosure = (op->func_closure == NULL ? 0 : |
| 282 | PyTuple_GET_SIZE(op->func_closure)); |
| 283 | if (nclosure != nfree) { |
| 284 | PyErr_Format(PyExc_ValueError, |
| 285 | "%U() requires a code object with %zd free vars," |
| 286 | " not %zd", |
| 287 | op->func_name, |
| 288 | nclosure, nfree); |
| 289 | return -1; |
| 290 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 292 | Py_XSETREF(op->func_code, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | return 0; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 297 | func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored)) |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | Py_INCREF(op->func_name); |
| 300 | return op->func_name; |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 304 | func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | /* Not legal to del f.func_name or to set it to anything |
| 307 | * other than a string object. */ |
| 308 | if (value == NULL || !PyUnicode_Check(value)) { |
| 309 | PyErr_SetString(PyExc_TypeError, |
| 310 | "__name__ must be set to a string object"); |
| 311 | return -1; |
| 312 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 314 | Py_XSETREF(op->func_name, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | return 0; |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 319 | func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored)) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 320 | { |
| 321 | Py_INCREF(op->func_qualname); |
| 322 | return op->func_qualname; |
| 323 | } |
| 324 | |
| 325 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 326 | func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 327 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 328 | /* Not legal to del f.__qualname__ or to set it to anything |
| 329 | * other than a string object. */ |
| 330 | if (value == NULL || !PyUnicode_Check(value)) { |
| 331 | PyErr_SetString(PyExc_TypeError, |
| 332 | "__qualname__ must be set to a string object"); |
| 333 | return -1; |
| 334 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 335 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 336 | Py_XSETREF(op->func_qualname, value); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 341 | func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored)) |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 342 | { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 343 | if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) { |
| 344 | return NULL; |
| 345 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | if (op->func_defaults == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 347 | Py_RETURN_NONE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | } |
| 349 | Py_INCREF(op->func_defaults); |
| 350 | return op->func_defaults; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 354 | func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | /* Legal to del f.func_defaults. |
| 357 | * Can only set func_defaults to NULL or a tuple. */ |
| 358 | if (value == Py_None) |
| 359 | value = NULL; |
| 360 | if (value != NULL && !PyTuple_Check(value)) { |
| 361 | PyErr_SetString(PyExc_TypeError, |
| 362 | "__defaults__ must be set to a tuple object"); |
| 363 | return -1; |
| 364 | } |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 365 | if (value) { |
| 366 | if (PySys_Audit("object.__setattr__", "OsO", |
| 367 | op, "__defaults__", value) < 0) { |
| 368 | return -1; |
| 369 | } |
| 370 | } else if (PySys_Audit("object.__delattr__", "Os", |
| 371 | op, "__defaults__") < 0) { |
| 372 | return -1; |
| 373 | } |
| 374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | Py_XINCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 376 | Py_XSETREF(op->func_defaults, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | return 0; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 380 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 381 | func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored)) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 382 | { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 383 | if (PySys_Audit("object.__getattr__", "Os", |
| 384 | op, "__kwdefaults__") < 0) { |
| 385 | return NULL; |
| 386 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | if (op->func_kwdefaults == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 388 | Py_RETURN_NONE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | } |
| 390 | Py_INCREF(op->func_kwdefaults); |
| 391 | return op->func_kwdefaults; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 395 | func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 396 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | if (value == Py_None) |
| 398 | value = NULL; |
| 399 | /* Legal to del f.func_kwdefaults. |
| 400 | * Can only set func_kwdefaults to NULL or a dict. */ |
| 401 | if (value != NULL && !PyDict_Check(value)) { |
| 402 | PyErr_SetString(PyExc_TypeError, |
| 403 | "__kwdefaults__ must be set to a dict object"); |
| 404 | return -1; |
| 405 | } |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 406 | if (value) { |
| 407 | if (PySys_Audit("object.__setattr__", "OsO", |
| 408 | op, "__kwdefaults__", value) < 0) { |
| 409 | return -1; |
| 410 | } |
| 411 | } else if (PySys_Audit("object.__delattr__", "Os", |
| 412 | op, "__kwdefaults__") < 0) { |
| 413 | return -1; |
| 414 | } |
| 415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | Py_XINCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 417 | Py_XSETREF(op->func_kwdefaults, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 421 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 422 | func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored)) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | if (op->func_annotations == NULL) { |
| 425 | op->func_annotations = PyDict_New(); |
| 426 | if (op->func_annotations == NULL) |
| 427 | return NULL; |
| 428 | } |
| 429 | Py_INCREF(op->func_annotations); |
| 430 | return op->func_annotations; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 434 | func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | if (value == Py_None) |
| 437 | value = NULL; |
| 438 | /* Legal to del f.func_annotations. |
| 439 | * Can only set func_annotations to NULL (through C api) |
| 440 | * or a dict. */ |
| 441 | if (value != NULL && !PyDict_Check(value)) { |
| 442 | PyErr_SetString(PyExc_TypeError, |
| 443 | "__annotations__ must be set to a dict object"); |
| 444 | return -1; |
| 445 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | Py_XINCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 447 | Py_XSETREF(op->func_annotations, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 451 | static PyGetSetDef func_getsetlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | {"__code__", (getter)func_get_code, (setter)func_set_code}, |
| 453 | {"__defaults__", (getter)func_get_defaults, |
| 454 | (setter)func_set_defaults}, |
| 455 | {"__kwdefaults__", (getter)func_get_kwdefaults, |
| 456 | (setter)func_set_kwdefaults}, |
| 457 | {"__annotations__", (getter)func_get_annotations, |
| 458 | (setter)func_set_annotations}, |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 459 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | {"__name__", (getter)func_get_name, (setter)func_set_name}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 461 | {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | {NULL} /* Sentinel */ |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 465 | /*[clinic input] |
| 466 | class function "PyFunctionObject *" "&PyFunction_Type" |
| 467 | [clinic start generated code]*/ |
| 468 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/ |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 469 | |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 470 | #include "clinic/funcobject.c.h" |
| 471 | |
| 472 | /* function.__new__() maintains the following invariants for closures. |
| 473 | The closure must correspond to the free variables of the code object. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | |
| 475 | if len(code.co_freevars) == 0: |
| 476 | closure = NULL |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 477 | else: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | len(closure) == len(code.co_freevars) |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 479 | for every elt in closure, type(elt) == cell |
| 480 | */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 481 | |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 482 | /*[clinic input] |
| 483 | @classmethod |
| 484 | function.__new__ as func_new |
| 485 | code: object(type="PyCodeObject *", subclass_of="&PyCode_Type") |
| 486 | a code object |
| 487 | globals: object(subclass_of="&PyDict_Type") |
| 488 | the globals dictionary |
| 489 | name: object = None |
| 490 | a string that overrides the name from the code object |
| 491 | argdefs as defaults: object = None |
| 492 | a tuple that specifies the default argument values |
| 493 | closure: object = None |
| 494 | a tuple that supplies the bindings for free variables |
| 495 | |
| 496 | Create a function object. |
| 497 | [clinic start generated code]*/ |
| 498 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 499 | static PyObject * |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 500 | func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals, |
| 501 | PyObject *name, PyObject *defaults, PyObject *closure) |
| 502 | /*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 503 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | PyFunctionObject *newfunc; |
| 505 | Py_ssize_t nfree, nclosure; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | if (name != Py_None && !PyUnicode_Check(name)) { |
| 508 | PyErr_SetString(PyExc_TypeError, |
| 509 | "arg 3 (name) must be None or string"); |
| 510 | return NULL; |
| 511 | } |
| 512 | if (defaults != Py_None && !PyTuple_Check(defaults)) { |
| 513 | PyErr_SetString(PyExc_TypeError, |
| 514 | "arg 4 (defaults) must be None or tuple"); |
| 515 | return NULL; |
| 516 | } |
| 517 | nfree = PyTuple_GET_SIZE(code->co_freevars); |
| 518 | if (!PyTuple_Check(closure)) { |
| 519 | if (nfree && closure == Py_None) { |
| 520 | PyErr_SetString(PyExc_TypeError, |
| 521 | "arg 5 (closure) must be tuple"); |
| 522 | return NULL; |
| 523 | } |
| 524 | else if (closure != Py_None) { |
| 525 | PyErr_SetString(PyExc_TypeError, |
| 526 | "arg 5 (closure) must be None or tuple"); |
| 527 | return NULL; |
| 528 | } |
| 529 | } |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | /* check that the closure is well-formed */ |
| 532 | nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); |
| 533 | if (nfree != nclosure) |
| 534 | return PyErr_Format(PyExc_ValueError, |
| 535 | "%U requires closure of length %zd, not %zd", |
| 536 | code->co_name, nfree, nclosure); |
| 537 | if (nclosure) { |
| 538 | Py_ssize_t i; |
| 539 | for (i = 0; i < nclosure; i++) { |
| 540 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 541 | if (!PyCell_Check(o)) { |
| 542 | return PyErr_Format(PyExc_TypeError, |
| 543 | "arg 5 (closure) expected cell, found %s", |
| 544 | o->ob_type->tp_name); |
| 545 | } |
| 546 | } |
| 547 | } |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 548 | if (PySys_Audit("function.__new__", "O", code) < 0) { |
| 549 | return NULL; |
| 550 | } |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code, |
| 553 | globals); |
| 554 | if (newfunc == NULL) |
| 555 | return NULL; |
| 556 | |
| 557 | if (name != Py_None) { |
| 558 | Py_INCREF(name); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 559 | Py_SETREF(newfunc->func_name, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | } |
| 561 | if (defaults != Py_None) { |
| 562 | Py_INCREF(defaults); |
| 563 | newfunc->func_defaults = defaults; |
| 564 | } |
| 565 | if (closure != Py_None) { |
| 566 | Py_INCREF(closure); |
| 567 | newfunc->func_closure = closure; |
| 568 | } |
| 569 | |
| 570 | return (PyObject *)newfunc; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 571 | } |
| 572 | |
INADA Naoki | 3c45240 | 2018-07-04 11:15:50 +0900 | [diff] [blame] | 573 | static int |
| 574 | func_clear(PyFunctionObject *op) |
| 575 | { |
| 576 | Py_CLEAR(op->func_code); |
| 577 | Py_CLEAR(op->func_globals); |
| 578 | Py_CLEAR(op->func_module); |
| 579 | Py_CLEAR(op->func_name); |
| 580 | Py_CLEAR(op->func_defaults); |
| 581 | Py_CLEAR(op->func_kwdefaults); |
| 582 | Py_CLEAR(op->func_doc); |
| 583 | Py_CLEAR(op->func_dict); |
| 584 | Py_CLEAR(op->func_closure); |
| 585 | Py_CLEAR(op->func_annotations); |
| 586 | Py_CLEAR(op->func_qualname); |
| 587 | return 0; |
| 588 | } |
| 589 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 590 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 591 | func_dealloc(PyFunctionObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | _PyObject_GC_UNTRACK(op); |
INADA Naoki | 3c45240 | 2018-07-04 11:15:50 +0900 | [diff] [blame] | 594 | if (op->func_weakreflist != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | PyObject_ClearWeakRefs((PyObject *) op); |
INADA Naoki | 3c45240 | 2018-07-04 11:15:50 +0900 | [diff] [blame] | 596 | } |
| 597 | (void)func_clear(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | PyObject_GC_Del(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 601 | static PyObject* |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 602 | func_repr(PyFunctionObject *op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | return PyUnicode_FromFormat("<function %U at %p>", |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 605 | op->func_qualname, op); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 608 | static int |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 609 | func_traverse(PyFunctionObject *f, visitproc visit, void *arg) |
| 610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | Py_VISIT(f->func_code); |
| 612 | Py_VISIT(f->func_globals); |
| 613 | Py_VISIT(f->func_module); |
| 614 | Py_VISIT(f->func_defaults); |
| 615 | Py_VISIT(f->func_kwdefaults); |
| 616 | Py_VISIT(f->func_doc); |
| 617 | Py_VISIT(f->func_name); |
| 618 | Py_VISIT(f->func_dict); |
| 619 | Py_VISIT(f->func_closure); |
| 620 | Py_VISIT(f->func_annotations); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 621 | Py_VISIT(f->func_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 625 | /* Bind a function to an object */ |
| 626 | static PyObject * |
| 627 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | if (obj == Py_None || obj == NULL) { |
| 630 | Py_INCREF(func); |
| 631 | return func; |
| 632 | } |
| 633 | return PyMethod_New(func, obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 636 | PyTypeObject PyFunction_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 638 | "function", |
| 639 | sizeof(PyFunctionObject), |
| 640 | 0, |
| 641 | (destructor)func_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 642 | offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | 0, /* tp_getattr */ |
| 644 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 645 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | (reprfunc)func_repr, /* tp_repr */ |
| 647 | 0, /* tp_as_number */ |
| 648 | 0, /* tp_as_sequence */ |
| 649 | 0, /* tp_as_mapping */ |
| 650 | 0, /* tp_hash */ |
Jeroen Demeyer | 5954334 | 2019-06-18 13:05:41 +0200 | [diff] [blame] | 651 | PyVectorcall_Call, /* tp_call */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | 0, /* tp_str */ |
Benjamin Peterson | 2cf936f | 2012-02-19 01:16:13 -0500 | [diff] [blame] | 653 | 0, /* tp_getattro */ |
| 654 | 0, /* tp_setattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | 0, /* tp_as_buffer */ |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 656 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 657 | _Py_TPFLAGS_HAVE_VECTORCALL | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 658 | Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */ |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 659 | func_new__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | (traverseproc)func_traverse, /* tp_traverse */ |
INADA Naoki | 3c45240 | 2018-07-04 11:15:50 +0900 | [diff] [blame] | 661 | (inquiry)func_clear, /* tp_clear */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | 0, /* tp_richcompare */ |
| 663 | offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */ |
| 664 | 0, /* tp_iter */ |
| 665 | 0, /* tp_iternext */ |
| 666 | 0, /* tp_methods */ |
| 667 | func_memberlist, /* tp_members */ |
| 668 | func_getsetlist, /* tp_getset */ |
| 669 | 0, /* tp_base */ |
| 670 | 0, /* tp_dict */ |
| 671 | func_descr_get, /* tp_descr_get */ |
| 672 | 0, /* tp_descr_set */ |
| 673 | offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */ |
| 674 | 0, /* tp_init */ |
| 675 | 0, /* tp_alloc */ |
| 676 | func_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 677 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 678 | |
| 679 | |
| 680 | /* Class method object */ |
| 681 | |
| 682 | /* A class method receives the class as implicit first argument, |
| 683 | just like an instance method receives the instance. |
| 684 | To declare a class method, use this idiom: |
| 685 | |
| 686 | class C: |
Martin Panter | 6d57fe1 | 2016-09-17 03:26:16 +0000 | [diff] [blame] | 687 | @classmethod |
| 688 | def f(cls, arg1, arg2, ...): |
| 689 | ... |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 691 | It can be called either on the class (e.g. C.f()) or on an instance |
| 692 | (e.g. C().f()); the instance is ignored except for its class. |
| 693 | If a class method is called for a derived class, the derived class |
| 694 | object is passed as the implied first argument. |
| 695 | |
| 696 | Class methods are different than C++ or Java static methods. |
| 697 | If you want those, see static methods below. |
| 698 | */ |
| 699 | |
| 700 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | PyObject_HEAD |
| 702 | PyObject *cm_callable; |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 703 | PyObject *cm_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 704 | } classmethod; |
| 705 | |
| 706 | static void |
| 707 | cm_dealloc(classmethod *cm) |
| 708 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | _PyObject_GC_UNTRACK((PyObject *)cm); |
| 710 | Py_XDECREF(cm->cm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 711 | Py_XDECREF(cm->cm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | Py_TYPE(cm)->tp_free((PyObject *)cm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 715 | static int |
| 716 | cm_traverse(classmethod *cm, visitproc visit, void *arg) |
| 717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | Py_VISIT(cm->cm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 719 | Py_VISIT(cm->cm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | static int |
| 724 | cm_clear(classmethod *cm) |
| 725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | Py_CLEAR(cm->cm_callable); |
Benjamin Peterson | 01d7eba | 2012-02-19 01:10:25 -0500 | [diff] [blame] | 727 | Py_CLEAR(cm->cm_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 732 | static PyObject * |
| 733 | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | classmethod *cm = (classmethod *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | if (cm->cm_callable == NULL) { |
| 738 | PyErr_SetString(PyExc_RuntimeError, |
| 739 | "uninitialized classmethod object"); |
| 740 | return NULL; |
| 741 | } |
| 742 | if (type == NULL) |
| 743 | type = (PyObject *)(Py_TYPE(obj)); |
| 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 | } |