blob: 63bdc21d8060536e7876b622039180cd82f93471 [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"
Tim Peters6d6c1a32001-08-02 04:15:00 +00006#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000010PyFunction_New(PyObject *code, PyObject *globals)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011{
Neil Schemenauere83c00e2001-08-29 23:54:21 +000012 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
Guido van Rossumc0b618a1997-05-02 03:12:38 +000013 &PyFunction_Type);
Martin v. Löwis321c9ab2004-03-23 18:40:15 +000014 static PyObject *__name__ = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015 if (op != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000016 PyObject *doc;
17 PyObject *consts;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000018 PyObject *module;
Fred Drakedb81e8d2001-03-23 04:19:27 +000019 op->func_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000020 Py_INCREF(code);
Guido van Rossum846e4311990-11-18 17:44:06 +000021 op->func_code = code;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000022 Py_INCREF(globals);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000023 op->func_globals = globals;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000024 op->func_name = ((PyCodeObject *)code)->co_name;
25 Py_INCREF(op->func_name);
Guido van Rossum2271bf71995-07-18 14:30:34 +000026 op->func_defaults = NULL; /* No default arguments */
Guido van Rossum4f72a782006-10-27 23:31:49 +000027 op->func_kwdefaults = NULL; /* No keyword only defaults */
Jeremy Hylton64949cb2001-01-25 20:06:59 +000028 op->func_closure = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029 consts = ((PyCodeObject *)code)->co_consts;
30 if (PyTuple_Size(consts) >= 1) {
31 doc = PyTuple_GetItem(consts, 0);
Guido van Rossumec5b7762000-04-27 20:14:13 +000032 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033 doc = Py_None;
Guido van Rossum5bd38051995-01-07 12:01:30 +000034 }
35 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036 doc = Py_None;
37 Py_INCREF(doc);
Guido van Rossum5bd38051995-01-07 12:01:30 +000038 op->func_doc = doc;
Barry Warsawd6a9e842001-01-15 20:40:19 +000039 op->func_dict = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000040 op->func_module = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +000041 op->func_annotations = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000042
43 /* __module__: If module name is in globals, use it.
44 Otherwise, use None.
45 */
Martin v. Löwis321c9ab2004-03-23 18:40:15 +000046 if (!__name__) {
47 __name__ = PyString_InternFromString("__name__");
48 if (!__name__) {
49 Py_DECREF(op);
50 return NULL;
51 }
52 }
53 module = PyDict_GetItem(globals, __name__);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000054 if (module) {
55 Py_INCREF(module);
56 op->func_module = module;
57 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 }
Barry Warsawd6a9e842001-01-15 20:40:19 +000059 else
60 return NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000061 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063}
64
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000066PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 if (!PyFunction_Check(op)) {
69 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 return NULL;
71 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000072 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000076PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 if (!PyFunction_Check(op)) {
79 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080 return NULL;
81 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000086PyFunction_GetModule(PyObject *op)
87{
88 if (!PyFunction_Check(op)) {
89 PyErr_BadInternalCall();
90 return NULL;
91 }
92 return ((PyFunctionObject *) op) -> func_module;
93}
94
95PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000096PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000097{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 if (!PyFunction_Check(op)) {
99 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000100 return NULL;
101 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000103}
104
105int
Fred Drakeee238b92000-07-09 06:03:25 +0000106PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000107{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000108 if (!PyFunction_Check(op)) {
109 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000110 return -1;
111 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 if (defaults == Py_None)
Guido van Rossum2271bf71995-07-18 14:30:34 +0000113 defaults = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000114 else if (defaults && PyTuple_Check(defaults)) {
115 Py_INCREF(defaults);
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000116 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000117 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000119 return -1;
120 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
122 ((PyFunctionObject *) op) -> func_defaults = defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000123 return 0;
124}
125
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000126PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000127PyFunction_GetKwDefaults(PyObject *op)
128{
129 if (!PyFunction_Check(op)) {
130 PyErr_BadInternalCall();
131 return NULL;
132 }
133 return ((PyFunctionObject *) op) -> func_kwdefaults;
134}
135
136int
137PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
138{
139 if (!PyFunction_Check(op)) {
140 PyErr_BadInternalCall();
141 return -1;
142 }
143 if (defaults == Py_None)
144 defaults = NULL;
145 else if (defaults && PyDict_Check(defaults)) {
146 Py_INCREF(defaults);
147 }
148 else {
149 PyErr_SetString(PyExc_SystemError,
150 "non-dict keyword only default args");
151 return -1;
152 }
153 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
154 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
155 return 0;
156}
157
158PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000159PyFunction_GetClosure(PyObject *op)
160{
161 if (!PyFunction_Check(op)) {
162 PyErr_BadInternalCall();
163 return NULL;
164 }
165 return ((PyFunctionObject *) op) -> func_closure;
166}
167
168int
169PyFunction_SetClosure(PyObject *op, PyObject *closure)
170{
171 if (!PyFunction_Check(op)) {
172 PyErr_BadInternalCall();
173 return -1;
174 }
175 if (closure == Py_None)
176 closure = NULL;
177 else if (PyTuple_Check(closure)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178 Py_INCREF(closure);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000179 }
180 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181 PyErr_Format(PyExc_SystemError,
182 "expected tuple for closure, got '%.100s'",
183 closure->ob_type->tp_name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000184 return -1;
185 }
186 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
187 ((PyFunctionObject *) op) -> func_closure = closure;
188 return 0;
189}
190
Neal Norwitzc1505362006-12-28 06:47:50 +0000191PyObject *
192PyFunction_GetAnnotations(PyObject *op)
193{
194 if (!PyFunction_Check(op)) {
195 PyErr_BadInternalCall();
196 return NULL;
197 }
198 return ((PyFunctionObject *) op) -> func_annotations;
199}
200
201int
202PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
203{
204 if (!PyFunction_Check(op)) {
205 PyErr_BadInternalCall();
206 return -1;
207 }
208 if (annotations == Py_None)
209 annotations = NULL;
210 else if (annotations && PyDict_Check(annotations)) {
211 Py_INCREF(annotations);
212 }
213 else {
214 PyErr_SetString(PyExc_SystemError,
215 "non-dict annotations");
216 return -1;
217 }
218 Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
219 ((PyFunctionObject *) op) -> func_annotations = annotations;
220 return 0;
221}
222
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223/* Methods */
224
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000226
Guido van Rossum6f799372001-09-20 20:46:19 +0000227static PyMemberDef func_memberlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000228 {"func_closure", T_OBJECT, OFF(func_closure),
229 RESTRICTED|READONLY},
230 {"func_doc", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
231 {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
232 {"func_globals", T_OBJECT, OFF(func_globals),
233 RESTRICTED|READONLY},
Guido van Rossum6b29c012003-02-18 17:18:35 +0000234 {"__module__", T_OBJECT, OFF(func_module), WRITE_RESTRICTED},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000235 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000236};
237
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000238static int
239restricted(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000240{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000241 if (!PyEval_GetRestricted())
242 return 0;
243 PyErr_SetString(PyExc_RuntimeError,
244 "function attributes not accessible in restricted mode");
245 return 1;
246}
247
248static PyObject *
249func_get_dict(PyFunctionObject *op)
250{
251 if (restricted())
Guido van Rossum10393b11995-01-10 10:39:49 +0000252 return NULL;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000253 if (op->func_dict == NULL) {
254 op->func_dict = PyDict_New();
255 if (op->func_dict == NULL)
Barry Warsaw142865c2001-08-14 18:23:58 +0000256 return NULL;
257 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000258 Py_INCREF(op->func_dict);
259 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000260}
261
Guido van Rossum0dabace1998-05-22 00:55:34 +0000262static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000263func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000264{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000265 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000266
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000267 if (restricted())
268 return -1;
269 /* It is illegal to del f.func_dict */
270 if (value == NULL) {
271 PyErr_SetString(PyExc_TypeError,
272 "function's dictionary may not be deleted");
Guido van Rossum0dabace1998-05-22 00:55:34 +0000273 return -1;
274 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000275 /* Can only set func_dict to a dictionary */
276 if (!PyDict_Check(value)) {
277 PyErr_SetString(PyExc_TypeError,
Barry Warsaw142865c2001-08-14 18:23:58 +0000278 "setting function's dictionary to a non-dict");
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000279 return -1;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000280 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000281 tmp = op->func_dict;
282 Py_INCREF(value);
283 op->func_dict = value;
284 Py_XDECREF(tmp);
285 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000286}
287
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000288static PyObject *
289func_get_code(PyFunctionObject *op)
290{
291 if (restricted())
292 return NULL;
293 Py_INCREF(op->func_code);
294 return op->func_code;
295}
296
297static int
298func_set_code(PyFunctionObject *op, PyObject *value)
299{
300 PyObject *tmp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000301 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000302
303 if (restricted())
304 return -1;
305 /* Not legal to del f.func_code or to set it to anything
306 * other than a code object. */
307 if (value == NULL || !PyCode_Check(value)) {
308 PyErr_SetString(PyExc_TypeError,
309 "func_code must be set to a code object");
310 return -1;
311 }
Armin Rigo89a39462004-10-28 16:32:00 +0000312 nfree = PyCode_GetNumFree((PyCodeObject *)value);
313 nclosure = (op->func_closure == NULL ? 0 :
314 PyTuple_GET_SIZE(op->func_closure));
315 if (nclosure != nfree) {
316 PyErr_Format(PyExc_ValueError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +0000317 "%s() requires a code object with %zd free vars,"
318 " not %zd",
Armin Rigo89a39462004-10-28 16:32:00 +0000319 PyString_AsString(op->func_name),
Martin v. Löwise0e89f72006-02-16 06:59:22 +0000320 nclosure, nfree);
Armin Rigo89a39462004-10-28 16:32:00 +0000321 return -1;
322 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000323 tmp = op->func_code;
324 Py_INCREF(value);
325 op->func_code = value;
326 Py_DECREF(tmp);
327 return 0;
328}
329
330static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000331func_get_name(PyFunctionObject *op)
332{
Michael W. Hudson5e897952004-08-12 18:12:44 +0000333 Py_INCREF(op->func_name);
334 return op->func_name;
335}
336
337static int
338func_set_name(PyFunctionObject *op, PyObject *value)
339{
340 PyObject *tmp;
341
342 if (restricted())
343 return -1;
344 /* Not legal to del f.func_name or to set it to anything
345 * other than a string object. */
346 if (value == NULL || !PyString_Check(value)) {
347 PyErr_SetString(PyExc_TypeError,
348 "func_name must be set to a string object");
349 return -1;
350 }
351 tmp = op->func_name;
352 Py_INCREF(value);
353 op->func_name = value;
354 Py_DECREF(tmp);
355 return 0;
356}
357
358static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000359func_get_defaults(PyFunctionObject *op)
360{
361 if (restricted())
362 return NULL;
363 if (op->func_defaults == NULL) {
364 Py_INCREF(Py_None);
365 return Py_None;
366 }
367 Py_INCREF(op->func_defaults);
368 return op->func_defaults;
369}
370
371static int
372func_set_defaults(PyFunctionObject *op, PyObject *value)
373{
374 PyObject *tmp;
375
376 if (restricted())
377 return -1;
378 /* Legal to del f.func_defaults.
379 * Can only set func_defaults to NULL or a tuple. */
380 if (value == Py_None)
381 value = NULL;
382 if (value != NULL && !PyTuple_Check(value)) {
383 PyErr_SetString(PyExc_TypeError,
384 "func_defaults must be set to a tuple object");
385 return -1;
386 }
387 tmp = op->func_defaults;
388 Py_XINCREF(value);
389 op->func_defaults = value;
390 Py_XDECREF(tmp);
391 return 0;
392}
393
Guido van Rossum4f72a782006-10-27 23:31:49 +0000394static PyObject *
395func_get_kwdefaults(PyFunctionObject *op)
396{
397 if (restricted())
398 return NULL;
399 if (op->func_kwdefaults == NULL) {
400 Py_INCREF(Py_None);
401 return Py_None;
402 }
403 Py_INCREF(op->func_kwdefaults);
404 return op->func_kwdefaults;
405}
406
407static int
408func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
409{
410 PyObject *tmp;
411
412 if (restricted())
413 return -1;
414
415 if (value == Py_None)
416 value = NULL;
417 /* Legal to del f.func_defaults.
418 * Can only set func_kwdefaults to NULL or a dict. */
419 if (value != NULL && !PyDict_Check(value)) {
420 PyErr_SetString(PyExc_TypeError,
421 "func_kwdefaults must be set to a dict object");
422 return -1;
423 }
424 tmp = op->func_kwdefaults;
425 Py_XINCREF(value);
426 op->func_kwdefaults = value;
427 Py_XDECREF(tmp);
428 return 0;
429}
430
Neal Norwitzc1505362006-12-28 06:47:50 +0000431static PyObject *
432func_get_annotations(PyFunctionObject *op)
433{
434 if (op->func_annotations == NULL) {
435 op->func_annotations = PyDict_New();
436 if (op->func_annotations == NULL)
437 return NULL;
438 }
439 Py_INCREF(op->func_annotations);
440 return op->func_annotations;
441}
442
443static int
444func_set_annotations(PyFunctionObject *op, PyObject *value)
445{
446 PyObject *tmp;
447
448 if (value == Py_None)
449 value = NULL;
450 /* Legal to del f.func_annotations.
451 * Can only set func_annotations to NULL (through C api)
452 * or a dict. */
453 if (value != NULL && !PyDict_Check(value)) {
454 PyErr_SetString(PyExc_TypeError,
455 "func_annotations must be set to a dict object");
456 return -1;
457 }
458 tmp = op->func_annotations;
459 Py_XINCREF(value);
460 op->func_annotations = value;
461 Py_XDECREF(tmp);
462 return 0;
463}
464
Guido van Rossum32d34c82001-09-20 21:45:26 +0000465static PyGetSetDef func_getsetlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000466 {"func_code", (getter)func_get_code, (setter)func_set_code},
467 {"func_defaults", (getter)func_get_defaults,
468 (setter)func_set_defaults},
Guido van Rossum4f72a782006-10-27 23:31:49 +0000469 {"func_kwdefaults", (getter)func_get_kwdefaults,
470 (setter)func_set_kwdefaults},
Neal Norwitzc1505362006-12-28 06:47:50 +0000471 {"func_annotations", (getter)func_get_annotations,
472 (setter)func_set_annotations},
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000473 {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
474 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
Michael W. Hudson5e897952004-08-12 18:12:44 +0000475 {"func_name", (getter)func_get_name, (setter)func_set_name},
476 {"__name__", (getter)func_get_name, (setter)func_set_name},
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000477 {NULL} /* Sentinel */
478};
479
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000480PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000481"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000482\n\
483Create a function object from a code object and a dictionary.\n\
484The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000485The optional argdefs tuple specifies the default argument values.\n\
486The optional closure tuple supplies the bindings for free variables.");
487
488/* func_new() maintains the following invariants for closures. The
489 closure must correspond to the free variables of the code object.
490
491 if len(code.co_freevars) == 0:
492 closure = NULL
493 else:
494 len(closure) == len(code.co_freevars)
495 for every elt in closure, type(elt) == cell
496*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000497
498static PyObject *
499func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
500{
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000501 PyCodeObject *code;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000502 PyObject *globals;
503 PyObject *name = Py_None;
504 PyObject *defaults = Py_None;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000505 PyObject *closure = Py_None;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000506 PyFunctionObject *newfunc;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000507 Py_ssize_t nfree, nclosure;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000508 static char *kwlist[] = {"code", "globals", "name",
Raymond Hettinger86578452003-05-06 09:01:41 +0000509 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000510
Raymond Hettinger86578452003-05-06 09:01:41 +0000511 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
512 kwlist,
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000513 &PyCode_Type, &code,
514 &PyDict_Type, &globals,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000515 &name, &defaults, &closure))
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000516 return NULL;
517 if (name != Py_None && !PyString_Check(name)) {
518 PyErr_SetString(PyExc_TypeError,
519 "arg 3 (name) must be None or string");
520 return NULL;
521 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000522 if (defaults != Py_None && !PyTuple_Check(defaults)) {
523 PyErr_SetString(PyExc_TypeError,
524 "arg 4 (defaults) must be None or tuple");
525 return NULL;
526 }
527 nfree = PyTuple_GET_SIZE(code->co_freevars);
528 if (!PyTuple_Check(closure)) {
529 if (nfree && closure == Py_None) {
530 PyErr_SetString(PyExc_TypeError,
531 "arg 5 (closure) must be tuple");
532 return NULL;
533 }
534 else if (closure != Py_None) {
535 PyErr_SetString(PyExc_TypeError,
536 "arg 5 (closure) must be None or tuple");
537 return NULL;
538 }
539 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000540
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000541 /* check that the closure is well-formed */
542 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
543 if (nfree != nclosure)
544 return PyErr_Format(PyExc_ValueError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +0000545 "%s requires closure of length %zd, not %zd",
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000546 PyString_AS_STRING(code->co_name),
Martin v. Löwise0e89f72006-02-16 06:59:22 +0000547 nfree, nclosure);
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000548 if (nclosure) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000549 Py_ssize_t i;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000550 for (i = 0; i < nclosure; i++) {
551 PyObject *o = PyTuple_GET_ITEM(closure, i);
552 if (!PyCell_Check(o)) {
553 return PyErr_Format(PyExc_TypeError,
554 "arg 5 (closure) expected cell, found %s",
555 o->ob_type->tp_name);
556 }
557 }
558 }
559
560 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
561 globals);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000562 if (newfunc == NULL)
563 return NULL;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000564
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000565 if (name != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000566 Py_INCREF(name);
567 Py_DECREF(newfunc->func_name);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000568 newfunc->func_name = name;
569 }
570 if (defaults != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000571 Py_INCREF(defaults);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000572 newfunc->func_defaults = defaults;
573 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000574 if (closure != Py_None) {
575 Py_INCREF(closure);
576 newfunc->func_closure = closure;
577 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000578
579 return (PyObject *)newfunc;
580}
581
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000582static void
Fred Drakeee238b92000-07-09 06:03:25 +0000583func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000585 _PyObject_GC_UNTRACK(op);
Fred Drakec916f5a2001-10-26 17:56:51 +0000586 if (op->func_weakreflist != NULL)
587 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588 Py_DECREF(op->func_code);
589 Py_DECREF(op->func_globals);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000590 Py_XDECREF(op->func_module);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 Py_DECREF(op->func_name);
592 Py_XDECREF(op->func_defaults);
593 Py_XDECREF(op->func_doc);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000594 Py_XDECREF(op->func_dict);
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000595 Py_XDECREF(op->func_closure);
Neal Norwitzc1505362006-12-28 06:47:50 +0000596 Py_XDECREF(op->func_annotations);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000597 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598}
599
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000600static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000601func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000602{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000603 return PyString_FromFormat("<function %s at %p>",
604 PyString_AsString(op->func_name),
605 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000606}
607
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000608static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000609func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
610{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611 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_doc);
616 Py_VISIT(f->func_name);
617 Py_VISIT(f->func_dict);
618 Py_VISIT(f->func_closure);
Neal Norwitzc1505362006-12-28 06:47:50 +0000619 Py_VISIT(f->func_annotations);
Jeremy Hylton8caad492000-06-23 14:18:11 +0000620 return 0;
621}
622
Tim Peters6d6c1a32001-08-02 04:15:00 +0000623static PyObject *
624function_call(PyObject *func, PyObject *arg, PyObject *kw)
625{
626 PyObject *result;
627 PyObject *argdefs;
628 PyObject **d, **k;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000629 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000630
631 argdefs = PyFunction_GET_DEFAULTS(func);
632 if (argdefs != NULL && PyTuple_Check(argdefs)) {
633 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
634 nd = PyTuple_Size(argdefs);
635 }
636 else {
637 d = NULL;
638 nd = 0;
639 }
640
641 if (kw != NULL && PyDict_Check(kw)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000642 Py_ssize_t pos, i;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000643 nk = PyDict_Size(kw);
644 k = PyMem_NEW(PyObject *, 2*nk);
645 if (k == NULL) {
646 PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647 return NULL;
648 }
649 pos = i = 0;
650 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
651 i += 2;
652 nk = i/2;
653 /* XXX This is broken if the caller deletes dict items! */
654 }
655 else {
656 k = NULL;
657 nk = 0;
658 }
659
660 result = PyEval_EvalCodeEx(
661 (PyCodeObject *)PyFunction_GET_CODE(func),
662 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
663 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
664 k, nk, d, nd,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000665 PyFunction_GET_KW_DEFAULTS(func),
Tim Peters6d6c1a32001-08-02 04:15:00 +0000666 PyFunction_GET_CLOSURE(func));
667
668 if (k != NULL)
669 PyMem_DEL(k);
670
671 return result;
672}
673
674/* Bind a function to an object */
675static PyObject *
676func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
677{
678 if (obj == Py_None)
679 obj = NULL;
680 return PyMethod_New(func, obj, type);
681}
682
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683PyTypeObject PyFunction_Type = {
684 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000685 0,
686 "function",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000687 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000688 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689 (destructor)func_dealloc, /* tp_dealloc */
690 0, /* tp_print */
691 0, /* tp_getattr */
692 0, /* tp_setattr */
693 0, /* tp_compare */
694 (reprfunc)func_repr, /* tp_repr */
695 0, /* tp_as_number */
696 0, /* tp_as_sequence */
697 0, /* tp_as_mapping */
698 0, /* tp_hash */
699 function_call, /* tp_call */
700 0, /* tp_str */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000701 PyObject_GenericGetAttr, /* tp_getattro */
702 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000703 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000704 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000705 func_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000706 (traverseproc)func_traverse, /* tp_traverse */
707 0, /* tp_clear */
708 0, /* tp_richcompare */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000709 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000710 0, /* tp_iter */
711 0, /* tp_iternext */
712 0, /* tp_methods */
713 func_memberlist, /* tp_members */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000714 func_getsetlist, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000715 0, /* tp_base */
716 0, /* tp_dict */
717 func_descr_get, /* tp_descr_get */
718 0, /* tp_descr_set */
719 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000720 0, /* tp_init */
721 0, /* tp_alloc */
722 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000723};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000724
725
726/* Class method object */
727
728/* A class method receives the class as implicit first argument,
729 just like an instance method receives the instance.
730 To declare a class method, use this idiom:
731
732 class C:
733 def f(cls, arg1, arg2, ...): ...
734 f = classmethod(f)
735
736 It can be called either on the class (e.g. C.f()) or on an instance
737 (e.g. C().f()); the instance is ignored except for its class.
738 If a class method is called for a derived class, the derived class
739 object is passed as the implied first argument.
740
741 Class methods are different than C++ or Java static methods.
742 If you want those, see static methods below.
743*/
744
745typedef struct {
746 PyObject_HEAD
747 PyObject *cm_callable;
748} classmethod;
749
750static void
751cm_dealloc(classmethod *cm)
752{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000753 _PyObject_GC_UNTRACK((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000754 Py_XDECREF(cm->cm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000755 cm->ob_type->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756}
757
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000758static int
759cm_traverse(classmethod *cm, visitproc visit, void *arg)
760{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761 Py_VISIT(cm->cm_callable);
762 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000763}
764
765static int
766cm_clear(classmethod *cm)
767{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768 Py_CLEAR(cm->cm_callable);
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000769 return 0;
770}
771
772
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773static PyObject *
774cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
775{
776 classmethod *cm = (classmethod *)self;
777
778 if (cm->cm_callable == NULL) {
779 PyErr_SetString(PyExc_RuntimeError,
780 "uninitialized classmethod object");
781 return NULL;
782 }
Guido van Rossum7e305482002-03-18 03:09:06 +0000783 if (type == NULL)
784 type = (PyObject *)(obj->ob_type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000785 return PyMethod_New(cm->cm_callable,
786 type, (PyObject *)(type->ob_type));
787}
788
789static int
790cm_init(PyObject *self, PyObject *args, PyObject *kwds)
791{
792 classmethod *cm = (classmethod *)self;
793 PyObject *callable;
794
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000795 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000796 return -1;
Georg Brandld02db402006-02-21 22:13:44 +0000797 if (!_PyArg_NoKeywords("classmethod", kwds))
798 return -1;
Raymond Hettingerbe971532003-06-18 01:13:41 +0000799 if (!PyCallable_Check(callable)) {
800 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
801 callable->ob_type->tp_name);
802 return -1;
803 }
804
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805 Py_INCREF(callable);
806 cm->cm_callable = callable;
807 return 0;
808}
809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000810PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000811"classmethod(function) -> method\n\
812\n\
813Convert a function to be a class method.\n\
814\n\
815A class method receives the class as implicit first argument,\n\
816just like an instance method receives the instance.\n\
817To declare a class method, use this idiom:\n\
818\n\
819 class C:\n\
820 def f(cls, arg1, arg2, ...): ...\n\
821 f = classmethod(f)\n\
822\n\
823It can be called either on the class (e.g. C.f()) or on an instance\n\
824(e.g. C().f()). The instance is ignored except for its class.\n\
825If a class method is called for a derived class, the derived class\n\
826object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000827\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000828Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000829If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000830
Tim Peters6d6c1a32001-08-02 04:15:00 +0000831PyTypeObject PyClassMethod_Type = {
832 PyObject_HEAD_INIT(&PyType_Type)
833 0,
834 "classmethod",
835 sizeof(classmethod),
836 0,
837 (destructor)cm_dealloc, /* tp_dealloc */
838 0, /* tp_print */
839 0, /* tp_getattr */
840 0, /* tp_setattr */
841 0, /* tp_compare */
842 0, /* tp_repr */
843 0, /* tp_as_number */
844 0, /* tp_as_sequence */
845 0, /* tp_as_mapping */
846 0, /* tp_hash */
847 0, /* tp_call */
848 0, /* tp_str */
849 PyObject_GenericGetAttr, /* tp_getattro */
850 0, /* tp_setattro */
851 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000852 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000853 classmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000854 (traverseproc)cm_traverse, /* tp_traverse */
855 (inquiry)cm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000856 0, /* tp_richcompare */
857 0, /* tp_weaklistoffset */
858 0, /* tp_iter */
859 0, /* tp_iternext */
860 0, /* tp_methods */
861 0, /* tp_members */
862 0, /* tp_getset */
863 0, /* tp_base */
864 0, /* tp_dict */
865 cm_descr_get, /* tp_descr_get */
866 0, /* tp_descr_set */
867 0, /* tp_dictoffset */
868 cm_init, /* tp_init */
869 PyType_GenericAlloc, /* tp_alloc */
870 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000871 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000872};
873
874PyObject *
875PyClassMethod_New(PyObject *callable)
876{
877 classmethod *cm = (classmethod *)
878 PyType_GenericAlloc(&PyClassMethod_Type, 0);
879 if (cm != NULL) {
880 Py_INCREF(callable);
881 cm->cm_callable = callable;
882 }
883 return (PyObject *)cm;
884}
885
886
887/* Static method object */
888
889/* A static method does not receive an implicit first argument.
890 To declare a static method, use this idiom:
891
892 class C:
893 def f(arg1, arg2, ...): ...
894 f = staticmethod(f)
895
896 It can be called either on the class (e.g. C.f()) or on an instance
897 (e.g. C().f()); the instance is ignored except for its class.
898
899 Static methods in Python are similar to those found in Java or C++.
900 For a more advanced concept, see class methods above.
901*/
902
903typedef struct {
904 PyObject_HEAD
905 PyObject *sm_callable;
906} staticmethod;
907
908static void
909sm_dealloc(staticmethod *sm)
910{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000911 _PyObject_GC_UNTRACK((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000912 Py_XDECREF(sm->sm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000913 sm->ob_type->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000914}
915
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000916static int
917sm_traverse(staticmethod *sm, visitproc visit, void *arg)
918{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000919 Py_VISIT(sm->sm_callable);
920 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000921}
922
923static int
924sm_clear(staticmethod *sm)
925{
926 Py_XDECREF(sm->sm_callable);
927 sm->sm_callable = NULL;
928
929 return 0;
930}
931
Tim Peters6d6c1a32001-08-02 04:15:00 +0000932static PyObject *
933sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
934{
935 staticmethod *sm = (staticmethod *)self;
936
937 if (sm->sm_callable == NULL) {
938 PyErr_SetString(PyExc_RuntimeError,
939 "uninitialized staticmethod object");
940 return NULL;
941 }
942 Py_INCREF(sm->sm_callable);
943 return sm->sm_callable;
944}
945
946static int
947sm_init(PyObject *self, PyObject *args, PyObject *kwds)
948{
949 staticmethod *sm = (staticmethod *)self;
950 PyObject *callable;
951
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000952 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000953 return -1;
Georg Brandld02db402006-02-21 22:13:44 +0000954 if (!_PyArg_NoKeywords("staticmethod", kwds))
955 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000956 Py_INCREF(callable);
957 sm->sm_callable = callable;
958 return 0;
959}
960
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000961PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000962"staticmethod(function) -> method\n\
963\n\
964Convert a function to be a static method.\n\
965\n\
966A static method does not receive an implicit first argument.\n\
967To declare a static method, use this idiom:\n\
968\n\
969 class C:\n\
970 def f(arg1, arg2, ...): ...\n\
971 f = staticmethod(f)\n\
972\n\
973It can be called either on the class (e.g. C.f()) or on an instance\n\
974(e.g. C().f()). The instance is ignored except for its class.\n\
975\n\
976Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000977For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000978
Tim Peters6d6c1a32001-08-02 04:15:00 +0000979PyTypeObject PyStaticMethod_Type = {
980 PyObject_HEAD_INIT(&PyType_Type)
981 0,
982 "staticmethod",
983 sizeof(staticmethod),
984 0,
985 (destructor)sm_dealloc, /* tp_dealloc */
986 0, /* tp_print */
987 0, /* tp_getattr */
988 0, /* tp_setattr */
989 0, /* tp_compare */
990 0, /* tp_repr */
991 0, /* tp_as_number */
992 0, /* tp_as_sequence */
993 0, /* tp_as_mapping */
994 0, /* tp_hash */
995 0, /* tp_call */
996 0, /* tp_str */
997 PyObject_GenericGetAttr, /* tp_getattro */
998 0, /* tp_setattro */
999 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +00001000 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +00001001 staticmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +00001002 (traverseproc)sm_traverse, /* tp_traverse */
1003 (inquiry)sm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001004 0, /* tp_richcompare */
1005 0, /* tp_weaklistoffset */
1006 0, /* tp_iter */
1007 0, /* tp_iternext */
1008 0, /* tp_methods */
1009 0, /* tp_members */
1010 0, /* tp_getset */
1011 0, /* tp_base */
1012 0, /* tp_dict */
1013 sm_descr_get, /* tp_descr_get */
1014 0, /* tp_descr_set */
1015 0, /* tp_dictoffset */
1016 sm_init, /* tp_init */
1017 PyType_GenericAlloc, /* tp_alloc */
1018 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +00001019 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001020};
1021
1022PyObject *
1023PyStaticMethod_New(PyObject *callable)
1024{
1025 staticmethod *sm = (staticmethod *)
1026 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1027 if (sm != NULL) {
1028 Py_INCREF(callable);
1029 sm->sm_callable = callable;
1030 }
1031 return (PyObject *)sm;
1032}