annotate Objects/funcobject.c @ 52039:bebc4eef9e89

Blocked revisions 70166-70167,70169,70176,70178,70197 via svnmerge ........ r70166 | georg.brandl | 2009-03-04 12:24:41 -0600 (Wed, 04 Mar 2009) | 2 lines Remove obsolete stuff from string module docs. ........ r70167 | ronald.oussoren | 2009-03-04 15:07:19 -0600 (Wed, 04 Mar 2009) | 2 lines Fix issue 5224. ........ r70169 | ronald.oussoren | 2009-03-04 15:12:17 -0600 (Wed, 04 Mar 2009) | 2 lines Fix for issue 5226. ........ r70176 | ronald.oussoren | 2009-03-04 15:35:05 -0600 (Wed, 04 Mar 2009) | 2 lines Fixes issues 3883 and 5194 ........ r70178 | ronald.oussoren | 2009-03-04 16:49:36 -0600 (Wed, 04 Mar 2009) | 2 lines Fix for issue #1113328. ........ r70197 | jesus.cea | 2009-03-05 13:37:37 -0600 (Thu, 05 Mar 2009) | 1 line Minor bsddb documentation glitch ........
author Benjamin Peterson <benjamin@python.org>
date Thu, 05 Mar 2009 22:53:54 +0000
parents 87fbbf22adf3
children f3c871bdea57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Function object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
4 #include "Python.h"
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 34598
diff changeset
5 #include "code.h"
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
6 #include "eval.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
7 #include "structmember.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
9 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
10 PyFunction_New(PyObject *code, PyObject *globals)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
12 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
13 &PyFunction_Type);
31780
d448cc261f69 Intern __name__.
Martin v. Löwis <martin@v.loewis.de>
parents: 29526
diff changeset
14 static PyObject *__name__ = 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 if (op != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
16 PyObject *doc;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
17 PyObject *consts;
27559
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
18 PyObject *module;
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 16891
diff changeset
19 op->func_weakreflist = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
20 Py_INCREF(code);
84
a79cbb2655ba Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
21 op->func_code = code;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
22 Py_INCREF(globals);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 op->func_globals = globals;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
24 op->func_name = ((PyCodeObject *)code)->co_name;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
25 Py_INCREF(op->func_name);
2842
420874f7d8bd changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents: 2205
diff changeset
26 op->func_defaults = NULL; /* No default arguments */
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
27 op->func_kwdefaults = NULL; /* No keyword only defaults */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
28 op->func_closure = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
29 consts = ((PyCodeObject *)code)->co_consts;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
30 if (PyTuple_Size(consts) >= 1) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
31 doc = PyTuple_GetItem(consts, 0);
42851
6380f88e3dee Use unicode and stop supporting str8
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
32 if (!PyUnicode_Check(doc))
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
33 doc = Py_None;
2082
4afa7eb4734b Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
34 }
4afa7eb4734b Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
35 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
36 doc = Py_None;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
37 Py_INCREF(doc);
2082
4afa7eb4734b Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
38 op->func_doc = doc;
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
39 op->func_dict = NULL;
27559
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
40 op->func_module = NULL;
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
41 op->func_annotations = NULL;
27559
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
42
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
43 /* __module__: If module name is in globals, use it.
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
44 Otherwise, use None.
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
45 */
31780
d448cc261f69 Intern __name__.
Martin v. Löwis <martin@v.loewis.de>
parents: 29526
diff changeset
46 if (!__name__) {
42851
6380f88e3dee Use unicode and stop supporting str8
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
47 __name__ = PyUnicode_InternFromString("__name__");
31780
d448cc261f69 Intern __name__.
Martin v. Löwis <martin@v.loewis.de>
parents: 29526
diff changeset
48 if (!__name__) {
d448cc261f69 Intern __name__.
Martin v. Löwis <martin@v.loewis.de>
parents: 29526
diff changeset
49 Py_DECREF(op);
d448cc261f69 Intern __name__.
Martin v. Löwis <martin@v.loewis.de>
parents: 29526
diff changeset
50 return NULL;
d448cc261f69 Intern __name__.
Martin v. Löwis <martin@v.loewis.de>
parents: 29526
diff changeset
51 }
d448cc261f69 Intern __name__.
Martin v. Löwis <martin@v.loewis.de>
parents: 29526
diff changeset
52 }
d448cc261f69 Intern __name__.
Martin v. Löwis <martin@v.loewis.de>
parents: 29526
diff changeset
53 module = PyDict_GetItem(globals, __name__);
27559
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
54 if (module) {
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
55 Py_INCREF(module);
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
56 op->func_module = module;
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
57 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 }
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
59 else
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
60 return NULL;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
61 _PyObject_GC_TRACK(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
62 return (PyObject *)op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
65 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
66 PyFunction_GetCode(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
68 if (!PyFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
69 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
72 return ((PyFunctionObject *) op) -> func_code;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
75 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
76 PyFunction_GetGlobals(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
78 if (!PyFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
79 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
82 return ((PyFunctionObject *) op) -> func_globals;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
85 PyObject *
27559
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
86 PyFunction_GetModule(PyObject *op)
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
87 {
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
88 if (!PyFunction_Check(op)) {
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
89 PyErr_BadInternalCall();
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
90 return NULL;
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
91 }
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
92 return ((PyFunctionObject *) op) -> func_module;
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
93 }
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
94
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
95 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
96 PyFunction_GetDefaults(PyObject *op)
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
97 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
98 if (!PyFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
99 PyErr_BadInternalCall();
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
100 return NULL;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
101 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
102 return ((PyFunctionObject *) op) -> func_defaults;
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
103 }
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
104
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
105 int
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
106 PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
107 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
108 if (!PyFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
109 PyErr_BadInternalCall();
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
110 return -1;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
111 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
112 if (defaults == Py_None)
2842
420874f7d8bd changes for keyword arguments and fast function call; added abstract.c
Guido van Rossum <guido@python.org>
parents: 2205
diff changeset
113 defaults = NULL;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
114 else if (defaults && PyTuple_Check(defaults)) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
115 Py_INCREF(defaults);
7981
09963a2a24db Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 5945
diff changeset
116 }
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
117 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
118 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
119 return -1;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
120 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
121 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
122 ((PyFunctionObject *) op) -> func_defaults = defaults;
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
123 return 0;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
124 }
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
125
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
126 PyObject *
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
127 PyFunction_GetKwDefaults(PyObject *op)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
128 {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
129 if (!PyFunction_Check(op)) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
130 PyErr_BadInternalCall();
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
131 return NULL;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
132 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
133 return ((PyFunctionObject *) op) -> func_kwdefaults;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
134 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
135
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
136 int
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
137 PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
138 {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
139 if (!PyFunction_Check(op)) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
140 PyErr_BadInternalCall();
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
141 return -1;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
142 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
143 if (defaults == Py_None)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
144 defaults = NULL;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
145 else if (defaults && PyDict_Check(defaults)) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
146 Py_INCREF(defaults);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
147 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
148 else {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
149 PyErr_SetString(PyExc_SystemError,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
150 "non-dict keyword only default args");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
151 return -1;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
152 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
153 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
154 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
155 return 0;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
156 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
157
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
158 PyObject *
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
159 PyFunction_GetClosure(PyObject *op)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
160 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
161 if (!PyFunction_Check(op)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
162 PyErr_BadInternalCall();
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
163 return NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
164 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
165 return ((PyFunctionObject *) op) -> func_closure;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
166 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
167
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
168 int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
169 PyFunction_SetClosure(PyObject *op, PyObject *closure)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
170 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
171 if (!PyFunction_Check(op)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
172 PyErr_BadInternalCall();
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
173 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
174 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
175 if (closure == Py_None)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
176 closure = NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
177 else if (PyTuple_Check(closure)) {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
178 Py_INCREF(closure);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
179 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
180 else {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 34598
diff changeset
181 PyErr_Format(PyExc_SystemError,
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 34598
diff changeset
182 "expected tuple for closure, got '%.100s'",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 34598
diff changeset
183 closure->ob_type->tp_name);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
184 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
185 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
186 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
187 ((PyFunctionObject *) op) -> func_closure = closure;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
188 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
189 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
190
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
191 PyObject *
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
192 PyFunction_GetAnnotations(PyObject *op)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
193 {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
194 if (!PyFunction_Check(op)) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
195 PyErr_BadInternalCall();
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
196 return NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
197 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
198 return ((PyFunctionObject *) op) -> func_annotations;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
199 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
200
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
201 int
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
202 PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
203 {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
204 if (!PyFunction_Check(op)) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
205 PyErr_BadInternalCall();
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
206 return -1;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
207 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
208 if (annotations == Py_None)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
209 annotations = NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
210 else if (annotations && PyDict_Check(annotations)) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
211 Py_INCREF(annotations);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
212 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
213 else {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
214 PyErr_SetString(PyExc_SystemError,
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
215 "non-dict annotations");
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
216 return -1;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
217 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
218 Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
219 ((PyFunctionObject *) op) -> func_annotations = annotations;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
220 return 0;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
221 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
222
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
225 #define OFF(x) offsetof(PyFunctionObject, x)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
226
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19948
diff changeset
227 static PyMemberDef func_memberlist[] = {
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
228 {"__closure__", T_OBJECT, OFF(func_closure),
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
229 RESTRICTED|READONLY},
43893
483e958d13c7 Fixed some build issues and updated docs.
Christian Heimes <christian@cheimes.de>
parents: 42851
diff changeset
230 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
231 {"__globals__", T_OBJECT, OFF(func_globals),
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
232 RESTRICTED|READONLY},
43893
483e958d13c7 Fixed some build issues and updated docs.
Christian Heimes <christian@cheimes.de>
parents: 42851
diff changeset
233 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
234 {NULL} /* Sentinel */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
235 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
236
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
237 static PyObject *
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
238 func_get_dict(PyFunctionObject *op)
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
239 {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
240 if (op->func_dict == NULL) {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
241 op->func_dict = PyDict_New();
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
242 if (op->func_dict == NULL)
19244
fbe2074c5fa5 func_getattro(), func_setattro(): Implement the new semantics for
Barry Warsaw <barry@python.org>
parents: 18933
diff changeset
243 return NULL;
fbe2074c5fa5 func_getattro(), func_setattro(): Implement the new semantics for
Barry Warsaw <barry@python.org>
parents: 18933
diff changeset
244 }
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
245 Py_INCREF(op->func_dict);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
246 return op->func_dict;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
247 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
248
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
249 static int
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
250 func_set_dict(PyFunctionObject *op, PyObject *value)
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
251 {
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
252 PyObject *tmp;
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
253
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
254 /* It is illegal to del f.func_dict */
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
255 if (value == NULL) {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
256 PyErr_SetString(PyExc_TypeError,
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
257 "function's dictionary may not be deleted");
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
258 return -1;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
259 }
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
260 /* Can only set func_dict to a dictionary */
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
261 if (!PyDict_Check(value)) {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
262 PyErr_SetString(PyExc_TypeError,
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
263 "setting function's dictionary to a non-dict");
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
264 return -1;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
265 }
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
266 tmp = op->func_dict;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
267 Py_INCREF(value);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
268 op->func_dict = value;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
269 Py_XDECREF(tmp);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
270 return 0;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
271 }
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
272
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
273 static PyObject *
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
274 func_get_code(PyFunctionObject *op)
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
275 {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
276 Py_INCREF(op->func_code);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
277 return op->func_code;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
278 }
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
279
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
280 static int
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
281 func_set_code(PyFunctionObject *op, PyObject *value)
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
282 {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
283 PyObject *tmp;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
284 Py_ssize_t nfree, nclosure;
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
285
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
286 /* Not legal to del f.func_code or to set it to anything
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
287 * other than a code object. */
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
288 if (value == NULL || !PyCode_Check(value)) {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
289 PyErr_SetString(PyExc_TypeError,
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
290 "__code__ must be set to a code object");
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
291 return -1;
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
292 }
33856
60c60dc75c77 Wrote down the invariants of some common objects whose structure is
Armin Rigo <arigo@tunes.org>
parents: 33102
diff changeset
293 nfree = PyCode_GetNumFree((PyCodeObject *)value);
60c60dc75c77 Wrote down the invariants of some common objects whose structure is
Armin Rigo <arigo@tunes.org>
parents: 33102
diff changeset
294 nclosure = (op->func_closure == NULL ? 0 :
60c60dc75c77 Wrote down the invariants of some common objects whose structure is
Armin Rigo <arigo@tunes.org>
parents: 33102
diff changeset
295 PyTuple_GET_SIZE(op->func_closure));
60c60dc75c77 Wrote down the invariants of some common objects whose structure is
Armin Rigo <arigo@tunes.org>
parents: 33102
diff changeset
296 if (nclosure != nfree) {
60c60dc75c77 Wrote down the invariants of some common objects whose structure is
Armin Rigo <arigo@tunes.org>
parents: 33102
diff changeset
297 PyErr_Format(PyExc_ValueError,
36510
0f3894bb2ada Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents: 36509
diff changeset
298 "%s() requires a code object with %zd free vars,"
0f3894bb2ada Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents: 36509
diff changeset
299 " not %zd",
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 44351
diff changeset
300 _PyUnicode_AsString(op->func_name),
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
301 nclosure, nfree);
33856
60c60dc75c77 Wrote down the invariants of some common objects whose structure is
Armin Rigo <arigo@tunes.org>
parents: 33102
diff changeset
302 return -1;
60c60dc75c77 Wrote down the invariants of some common objects whose structure is
Armin Rigo <arigo@tunes.org>
parents: 33102
diff changeset
303 }
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
304 tmp = op->func_code;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
305 Py_INCREF(value);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
306 op->func_code = value;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
307 Py_DECREF(tmp);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
308 return 0;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
309 }
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
310
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
311 static PyObject *
33102
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
312 func_get_name(PyFunctionObject *op)
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
313 {
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
314 Py_INCREF(op->func_name);
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
315 return op->func_name;
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
316 }
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
317
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
318 static int
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
319 func_set_name(PyFunctionObject *op, PyObject *value)
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
320 {
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
321 PyObject *tmp;
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
322
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
323 /* Not legal to del f.func_name or to set it to anything
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
324 * other than a string object. */
42851
6380f88e3dee Use unicode and stop supporting str8
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
325 if (value == NULL || !PyUnicode_Check(value)) {
33102
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
326 PyErr_SetString(PyExc_TypeError,
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
327 "__name__ must be set to a string object");
33102
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
328 return -1;
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
329 }
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
330 tmp = op->func_name;
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
331 Py_INCREF(value);
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
332 op->func_name = value;
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
333 Py_DECREF(tmp);
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
334 return 0;
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
335 }
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
336
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
337 static PyObject *
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
338 func_get_defaults(PyFunctionObject *op)
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
339 {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
340 if (op->func_defaults == NULL) {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
341 Py_INCREF(Py_None);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
342 return Py_None;
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
343 }
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
344 Py_INCREF(op->func_defaults);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
345 return op->func_defaults;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
346 }
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
347
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
348 static int
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
349 func_set_defaults(PyFunctionObject *op, PyObject *value)
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
350 {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
351 PyObject *tmp;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
352
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
353 /* Legal to del f.func_defaults.
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
354 * Can only set func_defaults to NULL or a tuple. */
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
355 if (value == Py_None)
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
356 value = NULL;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
357 if (value != NULL && !PyTuple_Check(value)) {
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
358 PyErr_SetString(PyExc_TypeError,
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
359 "__defaults__ must be set to a tuple object");
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
360 return -1;
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
361 }
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
362 tmp = op->func_defaults;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
363 Py_XINCREF(value);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
364 op->func_defaults = value;
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
365 Py_XDECREF(tmp);
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
366 return 0;
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
367 }
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
368
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
369 static PyObject *
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
370 func_get_kwdefaults(PyFunctionObject *op)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
371 {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
372 if (op->func_kwdefaults == NULL) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
373 Py_INCREF(Py_None);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
374 return Py_None;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
375 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
376 Py_INCREF(op->func_kwdefaults);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
377 return op->func_kwdefaults;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
378 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
379
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
380 static int
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
381 func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
382 {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
383 PyObject *tmp;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
384
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
385 if (value == Py_None)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
386 value = NULL;
40923
25a84900e205 Fix refleaks in function objects caused by kwdefaults not being allocated.
Georg Brandl <georg@python.org>
parents: 40910
diff changeset
387 /* Legal to del f.func_kwdefaults.
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
388 * Can only set func_kwdefaults to NULL or a dict. */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
389 if (value != NULL && !PyDict_Check(value)) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
390 PyErr_SetString(PyExc_TypeError,
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
391 "__kwdefaults__ must be set to a dict object");
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
392 return -1;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
393 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
394 tmp = op->func_kwdefaults;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
395 Py_XINCREF(value);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
396 op->func_kwdefaults = value;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
397 Py_XDECREF(tmp);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
398 return 0;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
399 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
400
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
401 static PyObject *
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
402 func_get_annotations(PyFunctionObject *op)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
403 {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
404 if (op->func_annotations == NULL) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
405 op->func_annotations = PyDict_New();
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
406 if (op->func_annotations == NULL)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
407 return NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
408 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
409 Py_INCREF(op->func_annotations);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
410 return op->func_annotations;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
411 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
412
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
413 static int
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
414 func_set_annotations(PyFunctionObject *op, PyObject *value)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
415 {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
416 PyObject *tmp;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
417
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
418 if (value == Py_None)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
419 value = NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
420 /* Legal to del f.func_annotations.
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
421 * Can only set func_annotations to NULL (through C api)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
422 * or a dict. */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
423 if (value != NULL && !PyDict_Check(value)) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
424 PyErr_SetString(PyExc_TypeError,
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
425 "__annotations__ must be set to a dict object");
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
426 return -1;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
427 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
428 tmp = op->func_annotations;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
429 Py_XINCREF(value);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
430 op->func_annotations = value;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
431 Py_XDECREF(tmp);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
432 return 0;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
433 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
434
20015
ca7af2a5d915 Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
435 static PyGetSetDef func_getsetlist[] = {
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
436 {"__code__", (getter)func_get_code, (setter)func_set_code},
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
437 {"__defaults__", (getter)func_get_defaults,
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
438 (setter)func_set_defaults},
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
439 {"__kwdefaults__", (getter)func_get_kwdefaults,
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
440 (setter)func_set_kwdefaults},
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40529
diff changeset
441 {"__annotations__", (getter)func_get_annotations,
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
442 (setter)func_set_annotations},
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
443 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
33102
f27935b46985 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 32531
diff changeset
444 {"__name__", (getter)func_get_name, (setter)func_set_name},
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
445 {NULL} /* Sentinel */
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
446 };
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
447
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
448 PyDoc_STRVAR(func_doc,
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
449 "function(code, globals[, name[, argdefs[, closure]]])\n\
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
450 \n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
451 Create a function object from a code object and a dictionary.\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
452 The optional name string overrides the name from the code object.\n\
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
453 The optional argdefs tuple specifies the default argument values.\n\
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
454 The optional closure tuple supplies the bindings for free variables.");
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
455
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
456 /* func_new() maintains the following invariants for closures. The
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
457 closure must correspond to the free variables of the code object.
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
458
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
459 if len(code.co_freevars) == 0:
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
460 closure = NULL
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
461 else:
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
462 len(closure) == len(code.co_freevars)
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
463 for every elt in closure, type(elt) == cell
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
464 */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
465
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
466 static PyObject *
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
467 func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
468 {
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
469 PyCodeObject *code;
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
470 PyObject *globals;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
471 PyObject *name = Py_None;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
472 PyObject *defaults = Py_None;
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
473 PyObject *closure = Py_None;
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
474 PyFunctionObject *newfunc;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
475 Py_ssize_t nfree, nclosure;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36641
diff changeset
476 static char *kwlist[] = {"code", "globals", "name",
28986
41388e84261b SF bug #692959: new.function ignores keyword arguments
Raymond Hettinger <python@rcn.com>
parents: 28537
diff changeset
477 "argdefs", "closure", 0};
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
478
28986
41388e84261b SF bug #692959: new.function ignores keyword arguments
Raymond Hettinger <python@rcn.com>
parents: 28537
diff changeset
479 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
41388e84261b SF bug #692959: new.function ignores keyword arguments
Raymond Hettinger <python@rcn.com>
parents: 28537
diff changeset
480 kwlist,
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
481 &PyCode_Type, &code,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
482 &PyDict_Type, &globals,
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
483 &name, &defaults, &closure))
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
484 return NULL;
42851
6380f88e3dee Use unicode and stop supporting str8
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
485 if (name != Py_None && !PyUnicode_Check(name)) {
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
486 PyErr_SetString(PyExc_TypeError,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
487 "arg 3 (name) must be None or string");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
488 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
489 }
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
490 if (defaults != Py_None && !PyTuple_Check(defaults)) {
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
491 PyErr_SetString(PyExc_TypeError,
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
492 "arg 4 (defaults) must be None or tuple");
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
493 return NULL;
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
494 }
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
495 nfree = PyTuple_GET_SIZE(code->co_freevars);
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
496 if (!PyTuple_Check(closure)) {
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
497 if (nfree && closure == Py_None) {
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
498 PyErr_SetString(PyExc_TypeError,
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
499 "arg 5 (closure) must be tuple");
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
500 return NULL;
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
501 }
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
502 else if (closure != Py_None) {
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
503 PyErr_SetString(PyExc_TypeError,
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
504 "arg 5 (closure) must be None or tuple");
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
505 return NULL;
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
506 }
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
507 }
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
508
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
509 /* check that the closure is well-formed */
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
510 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
511 if (nfree != nclosure)
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
512 return PyErr_Format(PyExc_ValueError,
42038
056627cb0e40 Simplify error formatting (no default encoding required).
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
513 "%U requires closure of length %zd, not %zd",
056627cb0e40 Simplify error formatting (no default encoding required).
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
514 code->co_name, nfree, nclosure);
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
515 if (nclosure) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
516 Py_ssize_t i;
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
517 for (i = 0; i < nclosure; i++) {
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
518 PyObject *o = PyTuple_GET_ITEM(closure, i);
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
519 if (!PyCell_Check(o)) {
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
520 return PyErr_Format(PyExc_TypeError,
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
521 "arg 5 (closure) expected cell, found %s",
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
522 o->ob_type->tp_name);
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
523 }
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
524 }
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
525 }
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
526
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
527 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
528 globals);
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
529 if (newfunc == NULL)
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
530 return NULL;
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
531
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
532 if (name != Py_None) {
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
533 Py_INCREF(name);
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
534 Py_DECREF(newfunc->func_name);
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
535 newfunc->func_name = name;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
536 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
537 if (defaults != Py_None) {
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
538 Py_INCREF(defaults);
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
539 newfunc->func_defaults = defaults;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
540 }
24193
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
541 if (closure != Py_None) {
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
542 Py_INCREF(closure);
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
543 newfunc->func_closure = closure;
6eb74e83a6f1 Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23868
diff changeset
544 }
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
545
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
546 return (PyObject *)newfunc;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
547 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
548
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 static void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
550 func_dealloc(PyFunctionObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
552 _PyObject_GC_UNTRACK(op);
20698
b229208a2a72 Be smarter about clearing the weakref lists for instances, instance methods,
Fred Drake <fdrake@acm.org>
parents: 20277
diff changeset
553 if (op->func_weakreflist != NULL)
b229208a2a72 Be smarter about clearing the weakref lists for instances, instance methods,
Fred Drake <fdrake@acm.org>
parents: 20277
diff changeset
554 PyObject_ClearWeakRefs((PyObject *) op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
555 Py_DECREF(op->func_code);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
556 Py_DECREF(op->func_globals);
27559
faabd9867fb8 Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26818
diff changeset
557 Py_XDECREF(op->func_module);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
558 Py_DECREF(op->func_name);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
559 Py_XDECREF(op->func_defaults);
40923
25a84900e205 Fix refleaks in function objects caused by kwdefaults not being allocated.
Georg Brandl <georg@python.org>
parents: 40910
diff changeset
560 Py_XDECREF(op->func_kwdefaults);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
561 Py_XDECREF(op->func_doc);
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
562 Py_XDECREF(op->func_dict);
16891
806c6ae23103 Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16825
diff changeset
563 Py_XDECREF(op->func_closure);
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
564 Py_XDECREF(op->func_annotations);
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
565 PyObject_GC_Del(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
567
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
568 static PyObject*
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
569 func_repr(PyFunctionObject *op)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
570 {
42851
6380f88e3dee Use unicode and stop supporting str8
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
571 return PyUnicode_FromFormat("<function %U at %p>",
6380f88e3dee Use unicode and stop supporting str8
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
572 op->func_name, op);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
573 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
574
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
575 static int
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
576 func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
577 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
578 Py_VISIT(f->func_code);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
579 Py_VISIT(f->func_globals);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
580 Py_VISIT(f->func_module);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
581 Py_VISIT(f->func_defaults);
40923
25a84900e205 Fix refleaks in function objects caused by kwdefaults not being allocated.
Georg Brandl <georg@python.org>
parents: 40910
diff changeset
582 Py_VISIT(f->func_kwdefaults);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
583 Py_VISIT(f->func_doc);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
584 Py_VISIT(f->func_name);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
585 Py_VISIT(f->func_dict);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
586 Py_VISIT(f->func_closure);
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40215
diff changeset
587 Py_VISIT(f->func_annotations);
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
588 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
589 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
590
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
591 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
592 function_call(PyObject *func, PyObject *arg, PyObject *kw)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
593 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
594 PyObject *result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
595 PyObject *argdefs;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
596 PyObject **d, **k;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
597 Py_ssize_t nk, nd;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
598
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
599 argdefs = PyFunction_GET_DEFAULTS(func);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
600 if (argdefs != NULL && PyTuple_Check(argdefs)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
601 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
602 nd = PyTuple_Size(argdefs);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
603 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
604 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
605 d = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
606 nd = 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
607 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
608
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
609 if (kw != NULL && PyDict_Check(kw)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
610 Py_ssize_t pos, i;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
611 nk = PyDict_Size(kw);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
612 k = PyMem_NEW(PyObject *, 2*nk);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
613 if (k == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
614 PyErr_NoMemory();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
615 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
616 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
617 pos = i = 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
618 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
619 i += 2;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
620 nk = i/2;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
621 /* XXX This is broken if the caller deletes dict items! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
622 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
623 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
624 k = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
625 nk = 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
626 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
627
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
628 result = PyEval_EvalCodeEx(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
629 (PyCodeObject *)PyFunction_GET_CODE(func),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
630 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
631 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
632 k, nk, d, nd,
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
633 PyFunction_GET_KW_DEFAULTS(func),
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
634 PyFunction_GET_CLOSURE(func));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
635
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
636 if (k != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
637 PyMem_DEL(k);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
638
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
639 return result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
640 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
641
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
642 /* Bind a function to an object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
643 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
644 func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
645 {
44021
ff5df1fc0fcc Patch from Georg Brandl and me for #1493
Christian Heimes <christian@cheimes.de>
parents: 43893
diff changeset
646 if (obj == Py_None || obj == NULL) {
ff5df1fc0fcc Patch from Georg Brandl and me for #1493
Christian Heimes <christian@cheimes.de>
parents: 43893
diff changeset
647 Py_INCREF(func);
ff5df1fc0fcc Patch from Georg Brandl and me for #1493
Christian Heimes <christian@cheimes.de>
parents: 43893
diff changeset
648 return func;
ff5df1fc0fcc Patch from Georg Brandl and me for #1493
Christian Heimes <christian@cheimes.de>
parents: 43893
diff changeset
649 }
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 44021
diff changeset
650 return PyMethod_New(func, obj);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
651 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
652
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
653 PyTypeObject PyFunction_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42038
diff changeset
654 PyVarObject_HEAD_INIT(&PyType_Type, 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 "function",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
656 sizeof(PyFunctionObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 0,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
658 (destructor)func_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
659 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
660 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
661 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48660
diff changeset
662 0, /* tp_reserved */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
663 (reprfunc)func_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
664 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
665 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
666 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
667 0, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
668 function_call, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
669 0, /* tp_str */
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
670 PyObject_GenericGetAttr, /* tp_getattro */
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
671 PyObject_GenericSetAttr, /* tp_setattro */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
672 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
673 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
674 func_doc, /* tp_doc */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
675 (traverseproc)func_traverse, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
676 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
677 0, /* tp_richcompare */
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 16891
diff changeset
678 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
679 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
680 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
681 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
682 func_memberlist, /* tp_members */
19948
eaf1203b85cf Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
683 func_getsetlist, /* tp_getset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
684 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
685 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
686 func_descr_get, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
687 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
688 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
689 0, /* tp_init */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
690 0, /* tp_alloc */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
691 func_new, /* tp_new */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 };
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
693
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
694
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
695 /* Class method object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
696
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
697 /* A class method receives the class as implicit first argument,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
698 just like an instance method receives the instance.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
699 To declare a class method, use this idiom:
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
700
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
701 class C:
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
702 def f(cls, arg1, arg2, ...): ...
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
703 f = classmethod(f)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
704
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
705 It can be called either on the class (e.g. C.f()) or on an instance
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
706 (e.g. C().f()); the instance is ignored except for its class.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
707 If a class method is called for a derived class, the derived class
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
708 object is passed as the implied first argument.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
709
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
710 Class methods are different than C++ or Java static methods.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
711 If you want those, see static methods below.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
712 */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
713
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
714 typedef struct {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
715 PyObject_HEAD
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
716 PyObject *cm_callable;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
717 } classmethod;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
718
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
719 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
720 cm_dealloc(classmethod *cm)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
721 {
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
722 _PyObject_GC_UNTRACK((PyObject *)cm);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
723 Py_XDECREF(cm->cm_callable);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44026
diff changeset
724 Py_TYPE(cm)->tp_free((PyObject *)cm);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
725 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
726
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
727 static int
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
728 cm_traverse(classmethod *cm, visitproc visit, void *arg)
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
729 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
730 Py_VISIT(cm->cm_callable);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
731 return 0;
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
732 }
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
733
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
734 static int
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
735 cm_clear(classmethod *cm)
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
736 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
737 Py_CLEAR(cm->cm_callable);
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
738 return 0;
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
739 }
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
740
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
741
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
742 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
743 cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
744 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
745 classmethod *cm = (classmethod *)self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
746
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
747 if (cm->cm_callable == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
748 PyErr_SetString(PyExc_RuntimeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
749 "uninitialized classmethod object");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
750 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
751 }
22367
f2b6ccc00808 Fix for SF bug 528132 (Armin Rigo): classmethod().__get__() segfault
Guido van Rossum <guido@python.org>
parents: 21446
diff changeset
752 if (type == NULL)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44026
diff changeset
753 type = (PyObject *)(Py_TYPE(obj));
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 44021
diff changeset
754 return PyMethod_New(cm->cm_callable, type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
755 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
756
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
757 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
758 cm_init(PyObject *self, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
759 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
760 classmethod *cm = (classmethod *)self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
761 PyObject *callable;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
762
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 24193
diff changeset
763 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
764 return -1;
36641
36ae3d929832 Make staticmethod and classmethod complain about keyword args.
Georg Brandl <georg@python.org>
parents: 36511
diff changeset
765 if (!_PyArg_NoKeywords("classmethod", kwds))
36ae3d929832 Make staticmethod and classmethod complain about keyword args.
Georg Brandl <georg@python.org>
parents: 36511
diff changeset
766 return -1;
29526
f6d503de45bd SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents: 28986
diff changeset
767 if (!PyCallable_Check(callable)) {
f6d503de45bd SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents: 28986
diff changeset
768 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
f6d503de45bd SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents: 28986
diff changeset
769 callable->ob_type->tp_name);
f6d503de45bd SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents: 28986
diff changeset
770 return -1;
f6d503de45bd SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents: 28986
diff changeset
771 }
f6d503de45bd SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents: 28986
diff changeset
772
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
773 Py_INCREF(callable);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
774 cm->cm_callable = callable;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
775 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
776 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
777
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22974
diff changeset
778 PyDoc_STRVAR(classmethod_doc,
21444
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
779 "classmethod(function) -> method\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
780 \n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
781 Convert a function to be a class method.\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
782 \n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
783 A class method receives the class as implicit first argument,\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
784 just like an instance method receives the instance.\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
785 To declare a class method, use this idiom:\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
786 \n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
787 class C:\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
788 def f(cls, arg1, arg2, ...): ...\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
789 f = classmethod(f)\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
790 \n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
791 It can be called either on the class (e.g. C.f()) or on an instance\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
792 (e.g. C().f()). The instance is ignored except for its class.\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
793 If a class method is called for a derived class, the derived class\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
794 object is passed as the implied first argument.\n\
21446
77c7476f41bd Portability fix: Not every compiler implements the extension of
Sjoerd Mullender <sjoerd@acm.org>
parents: 21444
diff changeset
795 \n\
21444
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
796 Class methods are different than C++ or Java static methods.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22974
diff changeset
797 If you want those, see the staticmethod builtin.");
21444
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
798
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
799 PyTypeObject PyClassMethod_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42038
diff changeset
800 PyVarObject_HEAD_INIT(&PyType_Type, 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
801 "classmethod",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
802 sizeof(classmethod),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
803 0,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
804 (destructor)cm_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
805 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
806 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
807 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48660
diff changeset
808 0, /* tp_reserved */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
809 0, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
810 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
811 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
812 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
813 0, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
814 0, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
815 0, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
816 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
817 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
818 0, /* tp_as_buffer */
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
819 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
21444
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
820 classmethod_doc, /* tp_doc */
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
821 (traverseproc)cm_traverse, /* tp_traverse */
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
822 (inquiry)cm_clear, /* tp_clear */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
823 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
824 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
825 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
826 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
827 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
828 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
829 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
830 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
831 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
832 cm_descr_get, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
833 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
834 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
835 cm_init, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
836 PyType_GenericAlloc, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
837 PyType_GenericNew, /* tp_new */
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
838 PyObject_GC_Del, /* tp_free */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
839 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
840
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
841 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
842 PyClassMethod_New(PyObject *callable)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
843 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
844 classmethod *cm = (classmethod *)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
845 PyType_GenericAlloc(&PyClassMethod_Type, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
846 if (cm != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
847 Py_INCREF(callable);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
848 cm->cm_callable = callable;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
849 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
850 return (PyObject *)cm;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
851 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
852
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
853
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
854 /* Static method object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
855
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
856 /* A static method does not receive an implicit first argument.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
857 To declare a static method, use this idiom:
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
858
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
859 class C:
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
860 def f(arg1, arg2, ...): ...
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
861 f = staticmethod(f)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
862
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
863 It can be called either on the class (e.g. C.f()) or on an instance
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
864 (e.g. C().f()); the instance is ignored except for its class.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
865
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
866 Static methods in Python are similar to those found in Java or C++.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
867 For a more advanced concept, see class methods above.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
868 */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
869
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
870 typedef struct {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
871 PyObject_HEAD
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
872 PyObject *sm_callable;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
873 } staticmethod;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
874
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
875 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
876 sm_dealloc(staticmethod *sm)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
877 {
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
878 _PyObject_GC_UNTRACK((PyObject *)sm);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
879 Py_XDECREF(sm->sm_callable);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44026
diff changeset
880 Py_TYPE(sm)->tp_free((PyObject *)sm);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
881 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
882
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
883 static int
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
884 sm_traverse(staticmethod *sm, visitproc visit, void *arg)
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
885 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
886 Py_VISIT(sm->sm_callable);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
887 return 0;
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
888 }
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
889
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
890 static int
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
891 sm_clear(staticmethod *sm)
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
892 {
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
893 Py_XDECREF(sm->sm_callable);
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
894 sm->sm_callable = NULL;
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
895
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
896 return 0;
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
897 }
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
898
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
899 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
900 sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
901 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
902 staticmethod *sm = (staticmethod *)self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
903
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
904 if (sm->sm_callable == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
905 PyErr_SetString(PyExc_RuntimeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
906 "uninitialized staticmethod object");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
907 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
908 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
909 Py_INCREF(sm->sm_callable);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
910 return sm->sm_callable;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
911 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
912
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
913 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
914 sm_init(PyObject *self, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
915 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
916 staticmethod *sm = (staticmethod *)self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
917 PyObject *callable;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
918
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 24193
diff changeset
919 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
920 return -1;
36641
36ae3d929832 Make staticmethod and classmethod complain about keyword args.
Georg Brandl <georg@python.org>
parents: 36511
diff changeset
921 if (!_PyArg_NoKeywords("staticmethod", kwds))
36ae3d929832 Make staticmethod and classmethod complain about keyword args.
Georg Brandl <georg@python.org>
parents: 36511
diff changeset
922 return -1;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
923 Py_INCREF(callable);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
924 sm->sm_callable = callable;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
925 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
926 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
927
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22974
diff changeset
928 PyDoc_STRVAR(staticmethod_doc,
21444
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
929 "staticmethod(function) -> method\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
930 \n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
931 Convert a function to be a static method.\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
932 \n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
933 A static method does not receive an implicit first argument.\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
934 To declare a static method, use this idiom:\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
935 \n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
936 class C:\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
937 def f(arg1, arg2, ...): ...\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
938 f = staticmethod(f)\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
939 \n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
940 It can be called either on the class (e.g. C.f()) or on an instance\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
941 (e.g. C().f()). The instance is ignored except for its class.\n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
942 \n\
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
943 Static methods in Python are similar to those found in Java or C++.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22974
diff changeset
944 For a more advanced concept, see the classmethod builtin.");
21444
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
945
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
946 PyTypeObject PyStaticMethod_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42038
diff changeset
947 PyVarObject_HEAD_INIT(&PyType_Type, 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
948 "staticmethod",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
949 sizeof(staticmethod),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
950 0,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
951 (destructor)sm_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
952 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
953 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
954 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48660
diff changeset
955 0, /* tp_reserved */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
956 0, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
957 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
958 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
959 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
960 0, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
961 0, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
962 0, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
963 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
964 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
965 0, /* tp_as_buffer */
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
966 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
21444
5193290aef0a SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents: 21167
diff changeset
967 staticmethod_doc, /* tp_doc */
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
968 (traverseproc)sm_traverse, /* tp_traverse */
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
969 (inquiry)sm_clear, /* tp_clear */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
970 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
971 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
972 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
973 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
974 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
975 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
976 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
977 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
978 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
979 sm_descr_get, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
980 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
981 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
982 sm_init, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
983 PyType_GenericAlloc, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
984 PyType_GenericNew, /* tp_new */
28537
c7bb254ab890 Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27941
diff changeset
985 PyObject_GC_Del, /* tp_free */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
986 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
987
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
988 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
989 PyStaticMethod_New(PyObject *callable)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
990 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
991 staticmethod *sm = (staticmethod *)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
992 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
993 if (sm != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
994 Py_INCREF(callable);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
995 sm->sm_callable = callable;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
996 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
997 return (PyObject *)sm;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
998 }