blob: 2839a247efed7165c5cf7e109b6044e763d59af5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Function object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +01009PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
12 &PyFunction_Type);
13 static PyObject *__name__ = 0;
14 if (op != NULL) {
15 PyObject *doc;
16 PyObject *consts;
17 PyObject *module;
18 op->func_weakreflist = NULL;
19 Py_INCREF(code);
20 op->func_code = code;
21 Py_INCREF(globals);
22 op->func_globals = globals;
23 op->func_name = ((PyCodeObject *)code)->co_name;
24 Py_INCREF(op->func_name);
25 op->func_defaults = NULL; /* No default arguments */
26 op->func_kwdefaults = NULL; /* No keyword only defaults */
27 op->func_closure = NULL;
28 consts = ((PyCodeObject *)code)->co_consts;
29 if (PyTuple_Size(consts) >= 1) {
30 doc = PyTuple_GetItem(consts, 0);
31 if (!PyUnicode_Check(doc))
32 doc = Py_None;
33 }
34 else
35 doc = Py_None;
36 Py_INCREF(doc);
37 op->func_doc = doc;
38 op->func_dict = NULL;
39 op->func_module = NULL;
40 op->func_annotations = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* __module__: If module name is in globals, use it.
43 Otherwise, use None.
44 */
45 if (!__name__) {
46 __name__ = PyUnicode_InternFromString("__name__");
47 if (!__name__) {
48 Py_DECREF(op);
49 return NULL;
50 }
51 }
52 module = PyDict_GetItem(globals, __name__);
53 if (module) {
54 Py_INCREF(module);
55 op->func_module = module;
56 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +010057 if (qualname)
58 op->func_qualname = qualname;
59 else
60 op->func_qualname = op->func_name;
61 Py_INCREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 }
63 else
64 return NULL;
65 _PyObject_GC_TRACK(op);
66 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010070PyFunction_New(PyObject *code, PyObject *globals)
71{
72 return PyFunction_NewWithQualName(code, globals, NULL);
73}
74
75PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000076PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (!PyFunction_Check(op)) {
79 PyErr_BadInternalCall();
80 return NULL;
81 }
82 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000086PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (!PyFunction_Check(op)) {
89 PyErr_BadInternalCall();
90 return NULL;
91 }
92 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093}
94
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000096PyFunction_GetModule(PyObject *op)
97{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 if (!PyFunction_Check(op)) {
99 PyErr_BadInternalCall();
100 return NULL;
101 }
102 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000103}
104
105PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000106PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (!PyFunction_Check(op)) {
109 PyErr_BadInternalCall();
110 return NULL;
111 }
112 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000113}
114
115int
Fred Drakeee238b92000-07-09 06:03:25 +0000116PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 if (!PyFunction_Check(op)) {
119 PyErr_BadInternalCall();
120 return -1;
121 }
122 if (defaults == Py_None)
123 defaults = NULL;
124 else if (defaults && PyTuple_Check(defaults)) {
125 Py_INCREF(defaults);
126 }
127 else {
128 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
129 return -1;
130 }
131 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
132 ((PyFunctionObject *) op) -> func_defaults = defaults;
133 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000134}
135
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000136PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000137PyFunction_GetKwDefaults(PyObject *op)
138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 if (!PyFunction_Check(op)) {
140 PyErr_BadInternalCall();
141 return NULL;
142 }
143 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000144}
145
146int
147PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 if (!PyFunction_Check(op)) {
150 PyErr_BadInternalCall();
151 return -1;
152 }
153 if (defaults == Py_None)
154 defaults = NULL;
155 else if (defaults && PyDict_Check(defaults)) {
156 Py_INCREF(defaults);
157 }
158 else {
159 PyErr_SetString(PyExc_SystemError,
160 "non-dict keyword only default args");
161 return -1;
162 }
163 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
164 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
165 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000166}
167
168PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000169PyFunction_GetClosure(PyObject *op)
170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (!PyFunction_Check(op)) {
172 PyErr_BadInternalCall();
173 return NULL;
174 }
175 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000176}
177
178int
179PyFunction_SetClosure(PyObject *op, PyObject *closure)
180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (!PyFunction_Check(op)) {
182 PyErr_BadInternalCall();
183 return -1;
184 }
185 if (closure == Py_None)
186 closure = NULL;
187 else if (PyTuple_Check(closure)) {
188 Py_INCREF(closure);
189 }
190 else {
191 PyErr_Format(PyExc_SystemError,
192 "expected tuple for closure, got '%.100s'",
193 closure->ob_type->tp_name);
194 return -1;
195 }
196 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
197 ((PyFunctionObject *) op) -> func_closure = closure;
198 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000199}
200
Neal Norwitzc1505362006-12-28 06:47:50 +0000201PyObject *
202PyFunction_GetAnnotations(PyObject *op)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (!PyFunction_Check(op)) {
205 PyErr_BadInternalCall();
206 return NULL;
207 }
208 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000209}
210
211int
212PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (!PyFunction_Check(op)) {
215 PyErr_BadInternalCall();
216 return -1;
217 }
218 if (annotations == Py_None)
219 annotations = NULL;
220 else if (annotations && PyDict_Check(annotations)) {
221 Py_INCREF(annotations);
222 }
223 else {
224 PyErr_SetString(PyExc_SystemError,
225 "non-dict annotations");
226 return -1;
227 }
228 Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
229 ((PyFunctionObject *) op) -> func_annotations = annotations;
230 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000231}
232
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233/* Methods */
234
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000236
Guido van Rossum6f799372001-09-20 20:46:19 +0000237static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 {"__closure__", T_OBJECT, OFF(func_closure),
239 RESTRICTED|READONLY},
240 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
241 {"__globals__", T_OBJECT, OFF(func_globals),
242 RESTRICTED|READONLY},
243 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
244 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000245};
246
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000247static PyObject *
248func_get_dict(PyFunctionObject *op)
249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if (op->func_dict == NULL) {
251 op->func_dict = PyDict_New();
252 if (op->func_dict == NULL)
253 return NULL;
254 }
255 Py_INCREF(op->func_dict);
256 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000257}
258
Guido van Rossum0dabace1998-05-22 00:55:34 +0000259static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000260func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* It is illegal to del f.func_dict */
265 if (value == NULL) {
266 PyErr_SetString(PyExc_TypeError,
267 "function's dictionary may not be deleted");
268 return -1;
269 }
270 /* Can only set func_dict to a dictionary */
271 if (!PyDict_Check(value)) {
272 PyErr_SetString(PyExc_TypeError,
273 "setting function's dictionary to a non-dict");
274 return -1;
275 }
276 tmp = op->func_dict;
277 Py_INCREF(value);
278 op->func_dict = value;
279 Py_XDECREF(tmp);
280 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000281}
282
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000283static PyObject *
284func_get_code(PyFunctionObject *op)
285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_INCREF(op->func_code);
287 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000288}
289
290static int
291func_set_code(PyFunctionObject *op, PyObject *value)
292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 PyObject *tmp;
294 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 /* Not legal to del f.func_code or to set it to anything
297 * other than a code object. */
298 if (value == NULL || !PyCode_Check(value)) {
299 PyErr_SetString(PyExc_TypeError,
300 "__code__ must be set to a code object");
301 return -1;
302 }
303 nfree = PyCode_GetNumFree((PyCodeObject *)value);
304 nclosure = (op->func_closure == NULL ? 0 :
305 PyTuple_GET_SIZE(op->func_closure));
306 if (nclosure != nfree) {
307 PyErr_Format(PyExc_ValueError,
308 "%U() requires a code object with %zd free vars,"
309 " not %zd",
310 op->func_name,
311 nclosure, nfree);
312 return -1;
313 }
314 tmp = op->func_code;
315 Py_INCREF(value);
316 op->func_code = value;
317 Py_DECREF(tmp);
318 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000319}
320
321static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000322func_get_name(PyFunctionObject *op)
323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 Py_INCREF(op->func_name);
325 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000326}
327
328static int
329func_set_name(PyFunctionObject *op, PyObject *value)
330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 PyObject *tmp;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* Not legal to del f.func_name or to set it to anything
334 * other than a string object. */
335 if (value == NULL || !PyUnicode_Check(value)) {
336 PyErr_SetString(PyExc_TypeError,
337 "__name__ must be set to a string object");
338 return -1;
339 }
340 tmp = op->func_name;
341 Py_INCREF(value);
342 op->func_name = value;
343 Py_DECREF(tmp);
344 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000345}
346
347static PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100348func_get_qualname(PyFunctionObject *op)
349{
350 Py_INCREF(op->func_qualname);
351 return op->func_qualname;
352}
353
354static int
355func_set_qualname(PyFunctionObject *op, PyObject *value)
356{
357 PyObject *tmp;
358
359 /* Not legal to del f.__qualname__ or to set it to anything
360 * other than a string object. */
361 if (value == NULL || !PyUnicode_Check(value)) {
362 PyErr_SetString(PyExc_TypeError,
363 "__qualname__ must be set to a string object");
364 return -1;
365 }
366 tmp = op->func_qualname;
367 Py_INCREF(value);
368 op->func_qualname = value;
369 Py_DECREF(tmp);
370 return 0;
371}
372
373static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000374func_get_defaults(PyFunctionObject *op)
375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (op->func_defaults == NULL) {
377 Py_INCREF(Py_None);
378 return Py_None;
379 }
380 Py_INCREF(op->func_defaults);
381 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000382}
383
384static int
385func_set_defaults(PyFunctionObject *op, PyObject *value)
386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 PyObject *tmp;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* Legal to del f.func_defaults.
390 * Can only set func_defaults to NULL or a tuple. */
391 if (value == Py_None)
392 value = NULL;
393 if (value != NULL && !PyTuple_Check(value)) {
394 PyErr_SetString(PyExc_TypeError,
395 "__defaults__ must be set to a tuple object");
396 return -1;
397 }
398 tmp = op->func_defaults;
399 Py_XINCREF(value);
400 op->func_defaults = value;
401 Py_XDECREF(tmp);
402 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000403}
404
Guido van Rossum4f72a782006-10-27 23:31:49 +0000405static PyObject *
406func_get_kwdefaults(PyFunctionObject *op)
407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (op->func_kwdefaults == NULL) {
409 Py_INCREF(Py_None);
410 return Py_None;
411 }
412 Py_INCREF(op->func_kwdefaults);
413 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000414}
415
416static int
417func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 PyObject *tmp;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (value == Py_None)
422 value = NULL;
423 /* Legal to del f.func_kwdefaults.
424 * Can only set func_kwdefaults to NULL or a dict. */
425 if (value != NULL && !PyDict_Check(value)) {
426 PyErr_SetString(PyExc_TypeError,
427 "__kwdefaults__ must be set to a dict object");
428 return -1;
429 }
430 tmp = op->func_kwdefaults;
431 Py_XINCREF(value);
432 op->func_kwdefaults = value;
433 Py_XDECREF(tmp);
434 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000435}
436
Neal Norwitzc1505362006-12-28 06:47:50 +0000437static PyObject *
438func_get_annotations(PyFunctionObject *op)
439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (op->func_annotations == NULL) {
441 op->func_annotations = PyDict_New();
442 if (op->func_annotations == NULL)
443 return NULL;
444 }
445 Py_INCREF(op->func_annotations);
446 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000447}
448
449static int
450func_set_annotations(PyFunctionObject *op, PyObject *value)
451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 PyObject *tmp;
Neal Norwitzc1505362006-12-28 06:47:50 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 if (value == Py_None)
455 value = NULL;
456 /* Legal to del f.func_annotations.
457 * Can only set func_annotations to NULL (through C api)
458 * or a dict. */
459 if (value != NULL && !PyDict_Check(value)) {
460 PyErr_SetString(PyExc_TypeError,
461 "__annotations__ must be set to a dict object");
462 return -1;
463 }
464 tmp = op->func_annotations;
465 Py_XINCREF(value);
466 op->func_annotations = value;
467 Py_XDECREF(tmp);
468 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000469}
470
Guido van Rossum32d34c82001-09-20 21:45:26 +0000471static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 {"__code__", (getter)func_get_code, (setter)func_set_code},
473 {"__defaults__", (getter)func_get_defaults,
474 (setter)func_set_defaults},
475 {"__kwdefaults__", (getter)func_get_kwdefaults,
476 (setter)func_set_kwdefaults},
477 {"__annotations__", (getter)func_get_annotations,
478 (setter)func_set_annotations},
479 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
480 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100481 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000483};
484
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000485PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000486"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000487\n\
488Create a function object from a code object and a dictionary.\n\
489The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000490The optional argdefs tuple specifies the default argument values.\n\
491The optional closure tuple supplies the bindings for free variables.");
492
493/* func_new() maintains the following invariants for closures. The
494 closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495
496 if len(code.co_freevars) == 0:
497 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000498 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000500 for every elt in closure, type(elt) == cell
501*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000502
503static PyObject *
504func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PyCodeObject *code;
507 PyObject *globals;
508 PyObject *name = Py_None;
509 PyObject *defaults = Py_None;
510 PyObject *closure = Py_None;
511 PyFunctionObject *newfunc;
512 Py_ssize_t nfree, nclosure;
513 static char *kwlist[] = {"code", "globals", "name",
514 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
517 kwlist,
518 &PyCode_Type, &code,
519 &PyDict_Type, &globals,
520 &name, &defaults, &closure))
521 return NULL;
522 if (name != Py_None && !PyUnicode_Check(name)) {
523 PyErr_SetString(PyExc_TypeError,
524 "arg 3 (name) must be None or string");
525 return NULL;
526 }
527 if (defaults != Py_None && !PyTuple_Check(defaults)) {
528 PyErr_SetString(PyExc_TypeError,
529 "arg 4 (defaults) must be None or tuple");
530 return NULL;
531 }
532 nfree = PyTuple_GET_SIZE(code->co_freevars);
533 if (!PyTuple_Check(closure)) {
534 if (nfree && closure == Py_None) {
535 PyErr_SetString(PyExc_TypeError,
536 "arg 5 (closure) must be tuple");
537 return NULL;
538 }
539 else if (closure != Py_None) {
540 PyErr_SetString(PyExc_TypeError,
541 "arg 5 (closure) must be None or tuple");
542 return NULL;
543 }
544 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 /* check that the closure is well-formed */
547 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
548 if (nfree != nclosure)
549 return PyErr_Format(PyExc_ValueError,
550 "%U requires closure of length %zd, not %zd",
551 code->co_name, nfree, nclosure);
552 if (nclosure) {
553 Py_ssize_t i;
554 for (i = 0; i < nclosure; i++) {
555 PyObject *o = PyTuple_GET_ITEM(closure, i);
556 if (!PyCell_Check(o)) {
557 return PyErr_Format(PyExc_TypeError,
558 "arg 5 (closure) expected cell, found %s",
559 o->ob_type->tp_name);
560 }
561 }
562 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
565 globals);
566 if (newfunc == NULL)
567 return NULL;
568
569 if (name != Py_None) {
570 Py_INCREF(name);
571 Py_DECREF(newfunc->func_name);
572 newfunc->func_name = name;
573 }
574 if (defaults != Py_None) {
575 Py_INCREF(defaults);
576 newfunc->func_defaults = defaults;
577 }
578 if (closure != Py_None) {
579 Py_INCREF(closure);
580 newfunc->func_closure = closure;
581 }
582
583 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000584}
585
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586static void
Fred Drakeee238b92000-07-09 06:03:25 +0000587func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 _PyObject_GC_UNTRACK(op);
590 if (op->func_weakreflist != NULL)
591 PyObject_ClearWeakRefs((PyObject *) op);
592 Py_DECREF(op->func_code);
593 Py_DECREF(op->func_globals);
594 Py_XDECREF(op->func_module);
595 Py_DECREF(op->func_name);
596 Py_XDECREF(op->func_defaults);
597 Py_XDECREF(op->func_kwdefaults);
598 Py_XDECREF(op->func_doc);
599 Py_XDECREF(op->func_dict);
600 Py_XDECREF(op->func_closure);
601 Py_XDECREF(op->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100602 Py_XDECREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604}
605
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000607func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100610 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000611}
612
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000613static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000614func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 Py_VISIT(f->func_code);
617 Py_VISIT(f->func_globals);
618 Py_VISIT(f->func_module);
619 Py_VISIT(f->func_defaults);
620 Py_VISIT(f->func_kwdefaults);
621 Py_VISIT(f->func_doc);
622 Py_VISIT(f->func_name);
623 Py_VISIT(f->func_dict);
624 Py_VISIT(f->func_closure);
625 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100626 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000628}
629
Tim Peters6d6c1a32001-08-02 04:15:00 +0000630static PyObject *
631function_call(PyObject *func, PyObject *arg, PyObject *kw)
632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 PyObject *result;
634 PyObject *argdefs;
635 PyObject *kwtuple = NULL;
636 PyObject **d, **k;
637 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 argdefs = PyFunction_GET_DEFAULTS(func);
640 if (argdefs != NULL && PyTuple_Check(argdefs)) {
641 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
642 nd = PyTuple_GET_SIZE(argdefs);
643 }
644 else {
645 d = NULL;
646 nd = 0;
647 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (kw != NULL && PyDict_Check(kw)) {
650 Py_ssize_t pos, i;
651 nk = PyDict_Size(kw);
652 kwtuple = PyTuple_New(2*nk);
653 if (kwtuple == NULL)
654 return NULL;
655 k = &PyTuple_GET_ITEM(kwtuple, 0);
656 pos = i = 0;
657 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
658 Py_INCREF(k[i]);
659 Py_INCREF(k[i+1]);
660 i += 2;
661 }
662 nk = i/2;
663 }
664 else {
665 k = NULL;
666 nk = 0;
667 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 result = PyEval_EvalCodeEx(
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000670 PyFunction_GET_CODE(func),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
672 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
673 k, nk, d, nd,
674 PyFunction_GET_KW_DEFAULTS(func),
675 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000680}
681
682/* Bind a function to an object */
683static PyObject *
684func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (obj == Py_None || obj == NULL) {
687 Py_INCREF(func);
688 return func;
689 }
690 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000691}
692
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 PyVarObject_HEAD_INIT(&PyType_Type, 0)
695 "function",
696 sizeof(PyFunctionObject),
697 0,
698 (destructor)func_dealloc, /* tp_dealloc */
699 0, /* tp_print */
700 0, /* tp_getattr */
701 0, /* tp_setattr */
702 0, /* tp_reserved */
703 (reprfunc)func_repr, /* tp_repr */
704 0, /* tp_as_number */
705 0, /* tp_as_sequence */
706 0, /* tp_as_mapping */
707 0, /* tp_hash */
708 function_call, /* tp_call */
709 0, /* tp_str */
710 PyObject_GenericGetAttr, /* tp_getattro */
711 PyObject_GenericSetAttr, /* tp_setattro */
712 0, /* tp_as_buffer */
713 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
714 func_doc, /* tp_doc */
715 (traverseproc)func_traverse, /* tp_traverse */
716 0, /* tp_clear */
717 0, /* tp_richcompare */
718 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
719 0, /* tp_iter */
720 0, /* tp_iternext */
721 0, /* tp_methods */
722 func_memberlist, /* tp_members */
723 func_getsetlist, /* tp_getset */
724 0, /* tp_base */
725 0, /* tp_dict */
726 func_descr_get, /* tp_descr_get */
727 0, /* tp_descr_set */
728 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
729 0, /* tp_init */
730 0, /* tp_alloc */
731 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000732};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733
734
735/* Class method object */
736
737/* A class method receives the class as implicit first argument,
738 just like an instance method receives the instance.
739 To declare a class method, use this idiom:
740
741 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 def f(cls, arg1, arg2, ...): ...
743 f = classmethod(f)
744
Tim Peters6d6c1a32001-08-02 04:15:00 +0000745 It can be called either on the class (e.g. C.f()) or on an instance
746 (e.g. C().f()); the instance is ignored except for its class.
747 If a class method is called for a derived class, the derived class
748 object is passed as the implied first argument.
749
750 Class methods are different than C++ or Java static methods.
751 If you want those, see static methods below.
752*/
753
754typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 PyObject_HEAD
756 PyObject *cm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000757} classmethod;
758
759static void
760cm_dealloc(classmethod *cm)
761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 _PyObject_GC_UNTRACK((PyObject *)cm);
763 Py_XDECREF(cm->cm_callable);
764 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000765}
766
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000767static int
768cm_traverse(classmethod *cm, visitproc visit, void *arg)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 Py_VISIT(cm->cm_callable);
771 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000772}
773
774static int
775cm_clear(classmethod *cm)
776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 Py_CLEAR(cm->cm_callable);
778 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000779}
780
781
Tim Peters6d6c1a32001-08-02 04:15:00 +0000782static PyObject *
783cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 if (cm->cm_callable == NULL) {
788 PyErr_SetString(PyExc_RuntimeError,
789 "uninitialized classmethod object");
790 return NULL;
791 }
792 if (type == NULL)
793 type = (PyObject *)(Py_TYPE(obj));
794 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000795}
796
797static int
798cm_init(PyObject *self, PyObject *args, PyObject *kwds)
799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 classmethod *cm = (classmethod *)self;
801 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
804 return -1;
805 if (!_PyArg_NoKeywords("classmethod", kwds))
806 return -1;
807 Py_INCREF(callable);
808 cm->cm_callable = callable;
809 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810}
811
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000812static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
814 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000815};
816
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000817PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000818"classmethod(function) -> method\n\
819\n\
820Convert a function to be a class method.\n\
821\n\
822A class method receives the class as implicit first argument,\n\
823just like an instance method receives the instance.\n\
824To declare a class method, use this idiom:\n\
825\n\
826 class C:\n\
827 def f(cls, arg1, arg2, ...): ...\n\
828 f = classmethod(f)\n\
829\n\
830It can be called either on the class (e.g. C.f()) or on an instance\n\
831(e.g. C().f()). The instance is ignored except for its class.\n\
832If a class method is called for a derived class, the derived class\n\
833object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000834\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000835Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000836If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000837
Tim Peters6d6c1a32001-08-02 04:15:00 +0000838PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 PyVarObject_HEAD_INIT(&PyType_Type, 0)
840 "classmethod",
841 sizeof(classmethod),
842 0,
843 (destructor)cm_dealloc, /* tp_dealloc */
844 0, /* tp_print */
845 0, /* tp_getattr */
846 0, /* tp_setattr */
847 0, /* tp_reserved */
848 0, /* tp_repr */
849 0, /* tp_as_number */
850 0, /* tp_as_sequence */
851 0, /* tp_as_mapping */
852 0, /* tp_hash */
853 0, /* tp_call */
854 0, /* tp_str */
855 PyObject_GenericGetAttr, /* tp_getattro */
856 0, /* tp_setattro */
857 0, /* tp_as_buffer */
858 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
859 classmethod_doc, /* tp_doc */
860 (traverseproc)cm_traverse, /* tp_traverse */
861 (inquiry)cm_clear, /* tp_clear */
862 0, /* tp_richcompare */
863 0, /* tp_weaklistoffset */
864 0, /* tp_iter */
865 0, /* tp_iternext */
866 0, /* tp_methods */
867 cm_memberlist, /* tp_members */
868 0, /* tp_getset */
869 0, /* tp_base */
870 0, /* tp_dict */
871 cm_descr_get, /* tp_descr_get */
872 0, /* tp_descr_set */
873 0, /* tp_dictoffset */
874 cm_init, /* tp_init */
875 PyType_GenericAlloc, /* tp_alloc */
876 PyType_GenericNew, /* tp_new */
877 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000878};
879
880PyObject *
881PyClassMethod_New(PyObject *callable)
882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 classmethod *cm = (classmethod *)
884 PyType_GenericAlloc(&PyClassMethod_Type, 0);
885 if (cm != NULL) {
886 Py_INCREF(callable);
887 cm->cm_callable = callable;
888 }
889 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890}
891
892
893/* Static method object */
894
895/* A static method does not receive an implicit first argument.
896 To declare a static method, use this idiom:
897
898 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 def f(arg1, arg2, ...): ...
900 f = staticmethod(f)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000901
902 It can be called either on the class (e.g. C.f()) or on an instance
903 (e.g. C().f()); the instance is ignored except for its class.
904
905 Static methods in Python are similar to those found in Java or C++.
906 For a more advanced concept, see class methods above.
907*/
908
909typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 PyObject_HEAD
911 PyObject *sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000912} staticmethod;
913
914static void
915sm_dealloc(staticmethod *sm)
916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 _PyObject_GC_UNTRACK((PyObject *)sm);
918 Py_XDECREF(sm->sm_callable);
919 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000920}
921
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000922static int
923sm_traverse(staticmethod *sm, visitproc visit, void *arg)
924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 Py_VISIT(sm->sm_callable);
926 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000927}
928
929static int
930sm_clear(staticmethod *sm)
931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 Py_XDECREF(sm->sm_callable);
933 sm->sm_callable = NULL;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000936}
937
Tim Peters6d6c1a32001-08-02 04:15:00 +0000938static PyObject *
939sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 if (sm->sm_callable == NULL) {
944 PyErr_SetString(PyExc_RuntimeError,
945 "uninitialized staticmethod object");
946 return NULL;
947 }
948 Py_INCREF(sm->sm_callable);
949 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950}
951
952static int
953sm_init(PyObject *self, PyObject *args, PyObject *kwds)
954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 staticmethod *sm = (staticmethod *)self;
956 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
959 return -1;
960 if (!_PyArg_NoKeywords("staticmethod", kwds))
961 return -1;
962 Py_INCREF(callable);
963 sm->sm_callable = callable;
964 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000965}
966
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000967static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
969 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000970};
971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000972PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000973"staticmethod(function) -> method\n\
974\n\
975Convert a function to be a static method.\n\
976\n\
977A static method does not receive an implicit first argument.\n\
978To declare a static method, use this idiom:\n\
979\n\
980 class C:\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 def f(arg1, arg2, ...): ...\n\
982 f = staticmethod(f)\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000983\n\
984It can be called either on the class (e.g. C.f()) or on an instance\n\
985(e.g. C().f()). The instance is ignored except for its class.\n\
986\n\
987Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000988For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000989
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 PyVarObject_HEAD_INIT(&PyType_Type, 0)
992 "staticmethod",
993 sizeof(staticmethod),
994 0,
995 (destructor)sm_dealloc, /* tp_dealloc */
996 0, /* tp_print */
997 0, /* tp_getattr */
998 0, /* tp_setattr */
999 0, /* tp_reserved */
1000 0, /* tp_repr */
1001 0, /* tp_as_number */
1002 0, /* tp_as_sequence */
1003 0, /* tp_as_mapping */
1004 0, /* tp_hash */
1005 0, /* tp_call */
1006 0, /* tp_str */
1007 PyObject_GenericGetAttr, /* tp_getattro */
1008 0, /* tp_setattro */
1009 0, /* tp_as_buffer */
1010 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1011 staticmethod_doc, /* tp_doc */
1012 (traverseproc)sm_traverse, /* tp_traverse */
1013 (inquiry)sm_clear, /* tp_clear */
1014 0, /* tp_richcompare */
1015 0, /* tp_weaklistoffset */
1016 0, /* tp_iter */
1017 0, /* tp_iternext */
1018 0, /* tp_methods */
1019 sm_memberlist, /* tp_members */
1020 0, /* tp_getset */
1021 0, /* tp_base */
1022 0, /* tp_dict */
1023 sm_descr_get, /* tp_descr_get */
1024 0, /* tp_descr_set */
1025 0, /* tp_dictoffset */
1026 sm_init, /* tp_init */
1027 PyType_GenericAlloc, /* tp_alloc */
1028 PyType_GenericNew, /* tp_new */
1029 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001030};
1031
1032PyObject *
1033PyStaticMethod_New(PyObject *callable)
1034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 staticmethod *sm = (staticmethod *)
1036 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1037 if (sm != NULL) {
1038 Py_INCREF(callable);
1039 sm->sm_callable = callable;
1040 }
1041 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001042}