Mercurial > cpython-lru_cache-weakref
annotate Objects/funcobject.c @ 33091:48100a01ac7c 2.3
Ignore the locale's encoding if it is an empty string.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Thu, 12 Aug 2004 13:14:20 +0000 |
| parents | f6d503de45bd |
| children | d448cc261f69 |
| rev | line source |
|---|---|
| 145 | 1 |
| 6 | 2 /* Function object implementation */ |
| 3 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
4 #include "Python.h" |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
5 #include "compile.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 | 7 #include "structmember.h" |
| 6 | 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 | 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); |
| 6 | 14 if (op != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
15 PyObject *doc; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
16 PyObject *consts; |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
17 PyObject *module; |
|
17170
962196ce7231
Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents:
16891
diff
changeset
|
18 op->func_weakreflist = NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
19 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
|
20 op->func_code = code; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
21 Py_INCREF(globals); |
| 6 | 22 op->func_globals = globals; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
23 op->func_name = ((PyCodeObject *)code)->co_name; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
24 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
|
25 op->func_defaults = NULL; /* No default arguments */ |
|
16257
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
26 op->func_closure = NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
27 consts = ((PyCodeObject *)code)->co_consts; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
28 if (PyTuple_Size(consts) >= 1) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
29 doc = PyTuple_GetItem(consts, 0); |
| 12512 | 30 if (!PyString_Check(doc) && !PyUnicode_Check(doc)) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
31 doc = Py_None; |
|
2082
4afa7eb4734b
Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
32 } |
|
4afa7eb4734b
Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
33 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
34 doc = Py_None; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
35 Py_INCREF(doc); |
|
2082
4afa7eb4734b
Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
36 op->func_doc = doc; |
|
15894
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
37 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
|
38 op->func_module = NULL; |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
39 |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
40 /* __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
|
41 Otherwise, use None. |
|
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 = PyDict_GetItemString(globals, "__name__"); |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
44 if (module) { |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
45 Py_INCREF(module); |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
46 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
|
47 } |
| 6 | 48 } |
|
15894
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
49 else |
|
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
50 return NULL; |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
51 _PyObject_GC_TRACK(op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
52 return (PyObject *)op; |
| 6 | 53 } |
| 54 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
55 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
56 PyFunction_GetCode(PyObject *op) |
| 6 | 57 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
58 if (!PyFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
59 PyErr_BadInternalCall(); |
| 6 | 60 return NULL; |
| 61 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
62 return ((PyFunctionObject *) op) -> func_code; |
| 6 | 63 } |
| 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_GetGlobals(PyObject *op) |
| 6 | 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 | 70 return NULL; |
| 71 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
72 return ((PyFunctionObject *) op) -> func_globals; |
| 6 | 73 } |
| 74 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
75 PyObject * |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
76 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
|
77 { |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
78 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
|
79 PyErr_BadInternalCall(); |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
80 return NULL; |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
81 } |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
82 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
|
83 } |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
84 |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
85 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
86 PyFunction_GetDefaults(PyObject *op) |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
87 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
88 if (!PyFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
89 PyErr_BadInternalCall(); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
90 return NULL; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
91 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
92 return ((PyFunctionObject *) op) -> func_defaults; |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
93 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
94 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
95 int |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
96 PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
|
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 -1; |
|
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 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
|
103 defaults = NULL; |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5945
diff
changeset
|
104 else if (PyTuple_Check(defaults)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
105 Py_XINCREF(defaults); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5945
diff
changeset
|
106 } |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
107 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
108 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
|
109 return -1; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
110 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
111 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
112 ((PyFunctionObject *) op) -> func_defaults = defaults; |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
113 return 0; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
114 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
115 |
|
16257
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
116 PyObject * |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
117 PyFunction_GetClosure(PyObject *op) |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
118 { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
119 if (!PyFunction_Check(op)) { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
120 PyErr_BadInternalCall(); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
121 return NULL; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
122 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
123 return ((PyFunctionObject *) op) -> func_closure; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
124 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
125 |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
126 int |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
127 PyFunction_SetClosure(PyObject *op, PyObject *closure) |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
128 { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
129 if (!PyFunction_Check(op)) { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
130 PyErr_BadInternalCall(); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
131 return -1; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
132 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
133 if (closure == Py_None) |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
134 closure = NULL; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
135 else if (PyTuple_Check(closure)) { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
136 Py_XINCREF(closure); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
137 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
138 else { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
139 PyErr_SetString(PyExc_SystemError, "non-tuple closure"); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
140 return -1; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
141 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
142 Py_XDECREF(((PyFunctionObject *) op) -> func_closure); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
143 ((PyFunctionObject *) op) -> func_closure = closure; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
144 return 0; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
145 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
146 |
| 6 | 147 /* Methods */ |
| 148 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
149 #define OFF(x) offsetof(PyFunctionObject, x) |
| 86 | 150 |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
151 #define RR () |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
152 |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19948
diff
changeset
|
153 static PyMemberDef func_memberlist[] = { |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
154 {"func_closure", T_OBJECT, OFF(func_closure), |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
155 RESTRICTED|READONLY}, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
156 {"func_doc", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED}, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
157 {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED}, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
158 {"func_globals", T_OBJECT, OFF(func_globals), |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
159 RESTRICTED|READONLY}, |
|
16042
78bd97d8413c
Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents:
15894
diff
changeset
|
160 {"func_name", T_OBJECT, OFF(func_name), READONLY}, |
|
78bd97d8413c
Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents:
15894
diff
changeset
|
161 {"__name__", T_OBJECT, OFF(func_name), READONLY}, |
|
27941
14451dd3acb0
Make __module__ writable except in restricted mode (like for classic classes).
Guido van Rossum <guido@python.org>
parents:
27940
diff
changeset
|
162 {"__module__", T_OBJECT, OFF(func_module), WRITE_RESTRICTED}, |
|
16042
78bd97d8413c
Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents:
15894
diff
changeset
|
163 {NULL} /* Sentinel */ |
| 86 | 164 }; |
| 165 | |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
166 static int |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
167 restricted(void) |
| 86 | 168 { |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
169 if (!PyEval_GetRestricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
170 return 0; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
171 PyErr_SetString(PyExc_RuntimeError, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
172 "function attributes not accessible in restricted mode"); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
173 return 1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
174 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
175 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
176 static PyObject * |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
177 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
|
178 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
179 if (restricted()) |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2082
diff
changeset
|
180 return NULL; |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
181 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
|
182 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
|
183 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
|
184 return NULL; |
|
fbe2074c5fa5
func_getattro(), func_setattro(): Implement the new semantics for
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
185 } |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
186 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
|
187 return op->func_dict; |
| 86 | 188 } |
| 189 | |
|
8407
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
190 static int |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
191 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
|
192 { |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
193 PyObject *tmp; |
|
15894
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
194 |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
195 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
196 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
197 /* 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
|
198 if (value == NULL) { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
199 PyErr_SetString(PyExc_TypeError, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
200 "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
|
201 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
202 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
203 /* 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
|
204 if (!PyDict_Check(value)) { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
205 PyErr_SetString(PyExc_TypeError, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
206 "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
|
207 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
208 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
209 tmp = op->func_dict; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
210 Py_INCREF(value); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
211 op->func_dict = value; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
212 Py_XDECREF(tmp); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
213 return 0; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
214 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
215 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
216 static PyObject * |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
217 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
|
218 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
219 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
220 return NULL; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
221 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
|
222 return op->func_code; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
223 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
224 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
225 static int |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
226 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
|
227 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
228 PyObject *tmp; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
229 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
230 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
231 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
232 /* 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
|
233 * 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
|
234 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
|
235 PyErr_SetString(PyExc_TypeError, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
236 "func_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
|
237 return -1; |
|
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
238 } |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
239 tmp = op->func_code; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
240 Py_INCREF(value); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
241 op->func_code = value; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
242 Py_DECREF(tmp); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
243 return 0; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
244 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
245 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
246 static PyObject * |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
247 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
|
248 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
249 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
250 return NULL; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
251 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
|
252 Py_INCREF(Py_None); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
253 return Py_None; |
|
8407
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
254 } |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
255 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
|
256 return op->func_defaults; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
257 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
258 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
259 static int |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
260 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
|
261 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
262 PyObject *tmp; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
263 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
264 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
265 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
266 /* 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
|
267 * 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
|
268 if (value == Py_None) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
269 value = NULL; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
270 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
|
271 PyErr_SetString(PyExc_TypeError, |
|
8407
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
272 "func_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
|
273 return -1; |
|
16042
78bd97d8413c
Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents:
15894
diff
changeset
|
274 } |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
275 tmp = op->func_defaults; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
276 Py_XINCREF(value); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
277 op->func_defaults = value; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
278 Py_XDECREF(tmp); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
279 return 0; |
|
8407
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
280 } |
|
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
281 |
|
20015
ca7af2a5d915
Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
282 static PyGetSetDef func_getsetlist[] = { |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
283 {"func_code", (getter)func_get_code, (setter)func_set_code}, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
284 {"func_defaults", (getter)func_get_defaults, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
285 (setter)func_set_defaults}, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
286 {"func_dict", (getter)func_get_dict, (setter)func_set_dict}, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
287 {"__dict__", (getter)func_get_dict, (setter)func_set_dict}, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
288 {NULL} /* Sentinel */ |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
289 }; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
290 |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
291 PyDoc_STRVAR(func_doc, |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
292 "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
|
293 \n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
294 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
|
295 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
|
296 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
|
297 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
|
298 |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
299 /* 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
|
300 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
|
301 |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
302 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
|
303 closure = NULL |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
304 else: |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
305 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
|
306 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
|
307 */ |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
308 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
309 static PyObject * |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
310 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
|
311 { |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
312 PyCodeObject *code; |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
313 PyObject *globals; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
314 PyObject *name = Py_None; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
315 PyObject *defaults = Py_None; |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
316 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
|
317 PyFunctionObject *newfunc; |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
318 int nfree, nclosure; |
|
28986
41388e84261b
SF bug #692959: new.function ignores keyword arguments
Raymond Hettinger <python@rcn.com>
parents:
28537
diff
changeset
|
319 static char *kwlist[] = {"code", "globals", "name", |
|
41388e84261b
SF bug #692959: new.function ignores keyword arguments
Raymond Hettinger <python@rcn.com>
parents:
28537
diff
changeset
|
320 "argdefs", "closure", 0}; |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
321 |
|
28986
41388e84261b
SF bug #692959: new.function ignores keyword arguments
Raymond Hettinger <python@rcn.com>
parents:
28537
diff
changeset
|
322 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
|
323 kwlist, |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
324 &PyCode_Type, &code, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
325 &PyDict_Type, &globals, |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
326 &name, &defaults, &closure)) |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
327 return NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
328 if (name != Py_None && !PyString_Check(name)) { |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
329 PyErr_SetString(PyExc_TypeError, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
330 "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
|
331 return NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
332 } |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
333 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
|
334 PyErr_SetString(PyExc_TypeError, |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
335 "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
|
336 return NULL; |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
337 } |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
338 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
|
339 if (!PyTuple_Check(closure)) { |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
340 if (nfree && closure == Py_None) { |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
341 PyErr_SetString(PyExc_TypeError, |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
342 "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
|
343 return NULL; |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
344 } |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
345 else if (closure != Py_None) { |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
346 PyErr_SetString(PyExc_TypeError, |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
347 "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
|
348 return NULL; |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
349 } |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
350 } |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
351 |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
352 /* 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
|
353 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
|
354 if (nfree != nclosure) |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
355 return PyErr_Format(PyExc_ValueError, |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
356 "%s requires closure of length %d, not %d", |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
357 PyString_AS_STRING(code->co_name), |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
358 nfree, nclosure); |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
359 if (nclosure) { |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
360 int i; |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
361 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
|
362 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
|
363 if (!PyCell_Check(o)) { |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
364 return PyErr_Format(PyExc_TypeError, |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
365 "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
|
366 o->ob_type->tp_name); |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
367 } |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
368 } |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
369 } |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
370 |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
371 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
|
372 globals); |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
373 if (newfunc == NULL) |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
374 return NULL; |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
375 |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
376 if (name != Py_None) { |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
377 Py_INCREF(name); |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
378 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
|
379 newfunc->func_name = name; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
380 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
381 if (defaults != Py_None) { |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
382 Py_INCREF(defaults); |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
383 newfunc->func_defaults = defaults; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
384 } |
|
24193
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
385 if (closure != Py_None) { |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
386 Py_INCREF(closure); |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
387 newfunc->func_closure = closure; |
|
6eb74e83a6f1
Extend function() to support an optional closure argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23868
diff
changeset
|
388 } |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
389 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
390 return (PyObject *)newfunc; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
391 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
392 |
| 6 | 393 static void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
394 func_dealloc(PyFunctionObject *op) |
| 6 | 395 { |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
396 _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
|
397 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
|
398 PyObject_ClearWeakRefs((PyObject *) op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
399 Py_DECREF(op->func_code); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
400 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
|
401 Py_XDECREF(op->func_module); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
402 Py_DECREF(op->func_name); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
403 Py_XDECREF(op->func_defaults); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
404 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
|
405 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
|
406 Py_XDECREF(op->func_closure); |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
407 PyObject_GC_Del(op); |
| 6 | 408 } |
| 409 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
410 static PyObject* |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
411 func_repr(PyFunctionObject *op) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
412 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
413 if (op->func_name == Py_None) |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19244
diff
changeset
|
414 return PyString_FromFormat("<anonymous function at %p>", op); |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19244
diff
changeset
|
415 return PyString_FromFormat("<function %s at %p>", |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19244
diff
changeset
|
416 PyString_AsString(op->func_name), |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19244
diff
changeset
|
417 op); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
418 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
419 |
|
1399
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
420 static int |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
421 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
|
422 { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
423 int err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
424 if (f->func_code) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
425 err = visit(f->func_code, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
426 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
427 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
428 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
429 if (f->func_globals) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
430 err = visit(f->func_globals, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
431 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
432 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
433 } |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
434 if (f->func_module) { |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
435 err = visit(f->func_module, arg); |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
436 if (err) |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
437 return err; |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26818
diff
changeset
|
438 } |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
439 if (f->func_defaults) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
440 err = visit(f->func_defaults, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
441 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
442 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
443 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
444 if (f->func_doc) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
445 err = visit(f->func_doc, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
446 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
447 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
448 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
449 if (f->func_name) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
450 err = visit(f->func_name, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
451 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
452 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
453 } |
|
15894
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
454 if (f->func_dict) { |
|
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
455 err = visit(f->func_dict, arg); |
|
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
456 if (err) |
|
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
457 return err; |
|
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
458 } |
|
16891
806c6ae23103
Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16825
diff
changeset
|
459 if (f->func_closure) { |
|
806c6ae23103
Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16825
diff
changeset
|
460 err = visit(f->func_closure, arg); |
|
806c6ae23103
Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16825
diff
changeset
|
461 if (err) |
|
806c6ae23103
Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16825
diff
changeset
|
462 return err; |
|
806c6ae23103
Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16825
diff
changeset
|
463 } |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
464 return 0; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
465 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
466 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
467 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
468 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
|
469 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
470 PyObject *result; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
471 PyObject *argdefs; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
472 PyObject **d, **k; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
473 int nk, nd; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
474 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
475 argdefs = PyFunction_GET_DEFAULTS(func); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
476 if (argdefs != NULL && PyTuple_Check(argdefs)) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
477 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
|
478 nd = PyTuple_Size(argdefs); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
479 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
480 else { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
481 d = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
482 nd = 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
483 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
484 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
485 if (kw != NULL && PyDict_Check(kw)) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
486 int pos, i; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
487 nk = PyDict_Size(kw); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
488 k = PyMem_NEW(PyObject *, 2*nk); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
489 if (k == NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
490 PyErr_NoMemory(); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
491 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
492 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
493 pos = i = 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
494 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
|
495 i += 2; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
496 nk = i/2; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
497 /* 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
|
498 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
499 else { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
500 k = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
501 nk = 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
502 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
503 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
504 result = PyEval_EvalCodeEx( |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
505 (PyCodeObject *)PyFunction_GET_CODE(func), |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
506 PyFunction_GET_GLOBALS(func), (PyObject *)NULL, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
507 &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
|
508 k, nk, d, nd, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
509 PyFunction_GET_CLOSURE(func)); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
510 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
511 if (k != NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
512 PyMem_DEL(k); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
513 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
514 return result; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
515 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
516 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
517 /* Bind a function to an object */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
518 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
519 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
|
520 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
521 if (obj == Py_None) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
522 obj = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
523 return PyMethod_New(func, obj, type); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
524 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
525 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
526 PyTypeObject PyFunction_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
527 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 528 0, |
| 529 "function", | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
530 sizeof(PyFunctionObject), |
| 6 | 531 0, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
532 (destructor)func_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
533 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
534 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
535 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
536 0, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
537 (reprfunc)func_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
538 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
539 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
540 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
541 0, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
542 function_call, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
543 0, /* tp_str */ |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
544 PyObject_GenericGetAttr, /* tp_getattro */ |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
545 PyObject_GenericSetAttr, /* tp_setattro */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
546 0, /* tp_as_buffer */ |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
547 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
|
548 func_doc, /* tp_doc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
549 (traverseproc)func_traverse, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
550 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
551 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
|
552 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
|
553 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
554 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
555 0, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
556 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
|
557 func_getsetlist, /* tp_getset */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
558 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
559 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
560 func_descr_get, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
561 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
562 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
|
563 0, /* tp_init */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
564 0, /* tp_alloc */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
565 func_new, /* tp_new */ |
| 6 | 566 }; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
567 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
568 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
569 /* Class method object */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
570 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
571 /* 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
|
572 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
|
573 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
|
574 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
575 class C: |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
576 def f(cls, arg1, arg2, ...): ... |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
577 f = classmethod(f) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
578 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
579 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
|
580 (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
|
581 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
|
582 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
|
583 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
584 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
|
585 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
|
586 */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
587 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
588 typedef struct { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
589 PyObject_HEAD |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
590 PyObject *cm_callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
591 } classmethod; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
592 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
593 static void |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
594 cm_dealloc(classmethod *cm) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
595 { |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
596 _PyObject_GC_UNTRACK((PyObject *)cm); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
597 Py_XDECREF(cm->cm_callable); |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
20015
diff
changeset
|
598 cm->ob_type->tp_free((PyObject *)cm); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
599 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
600 |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
601 static int |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
602 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
|
603 { |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
604 if (!cm->cm_callable) |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
605 return 0; |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
606 return visit(cm->cm_callable, arg); |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
607 } |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
608 |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
609 static int |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
610 cm_clear(classmethod *cm) |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
611 { |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
612 Py_XDECREF(cm->cm_callable); |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
613 cm->cm_callable = NULL; |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
614 |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
615 return 0; |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
616 } |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
617 |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
618 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
619 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
620 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
|
621 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
622 classmethod *cm = (classmethod *)self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
623 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
624 if (cm->cm_callable == NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
625 PyErr_SetString(PyExc_RuntimeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
626 "uninitialized classmethod object"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
627 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
628 } |
|
22367
f2b6ccc00808
Fix for SF bug 528132 (Armin Rigo): classmethod().__get__() segfault
Guido van Rossum <guido@python.org>
parents:
21446
diff
changeset
|
629 if (type == NULL) |
|
f2b6ccc00808
Fix for SF bug 528132 (Armin Rigo): classmethod().__get__() segfault
Guido van Rossum <guido@python.org>
parents:
21446
diff
changeset
|
630 type = (PyObject *)(obj->ob_type); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
631 return PyMethod_New(cm->cm_callable, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
632 type, (PyObject *)(type->ob_type)); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
633 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
634 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
635 static int |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
636 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
|
637 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
638 classmethod *cm = (classmethod *)self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
639 PyObject *callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
640 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
24193
diff
changeset
|
641 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
|
642 return -1; |
|
29526
f6d503de45bd
SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents:
28986
diff
changeset
|
643 if (!PyCallable_Check(callable)) { |
|
f6d503de45bd
SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents:
28986
diff
changeset
|
644 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
|
645 callable->ob_type->tp_name); |
|
f6d503de45bd
SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents:
28986
diff
changeset
|
646 return -1; |
|
f6d503de45bd
SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents:
28986
diff
changeset
|
647 } |
|
f6d503de45bd
SF bug #753451: classmethod abuse --> SystemError
Raymond Hettinger <python@rcn.com>
parents:
28986
diff
changeset
|
648 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
649 Py_INCREF(callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
650 cm->cm_callable = callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
651 return 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
652 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
653 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22974
diff
changeset
|
654 PyDoc_STRVAR(classmethod_doc, |
|
21444
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
655 "classmethod(function) -> method\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
656 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
657 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
|
658 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
659 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
|
660 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
|
661 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
|
662 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
663 class C:\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
664 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
|
665 f = classmethod(f)\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
666 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
667 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
|
668 (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
|
669 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
|
670 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
|
671 \n\ |
|
21444
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
672 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
|
673 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
|
674 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
675 PyTypeObject PyClassMethod_Type = { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
676 PyObject_HEAD_INIT(&PyType_Type) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
677 0, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
678 "classmethod", |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
679 sizeof(classmethod), |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
680 0, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
681 (destructor)cm_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
682 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
683 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
684 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
685 0, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
686 0, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
687 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
688 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
689 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
690 0, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
691 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
692 0, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
693 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
694 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
695 0, /* tp_as_buffer */ |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
696 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
|
697 classmethod_doc, /* tp_doc */ |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
698 (traverseproc)cm_traverse, /* tp_traverse */ |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
699 (inquiry)cm_clear, /* tp_clear */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
700 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
701 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
702 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
703 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
704 0, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
705 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
706 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
707 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
708 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
709 cm_descr_get, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
710 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
711 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
712 cm_init, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
713 PyType_GenericAlloc, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
714 PyType_GenericNew, /* tp_new */ |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
715 PyObject_GC_Del, /* tp_free */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
716 }; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
717 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
718 PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
719 PyClassMethod_New(PyObject *callable) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
720 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
721 classmethod *cm = (classmethod *) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
722 PyType_GenericAlloc(&PyClassMethod_Type, 0); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
723 if (cm != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
724 Py_INCREF(callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
725 cm->cm_callable = callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
726 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
727 return (PyObject *)cm; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
728 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
729 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
730 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
731 /* Static method object */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
732 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
733 /* 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
|
734 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
|
735 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
736 class C: |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
737 def f(arg1, arg2, ...): ... |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
738 f = staticmethod(f) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
739 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
740 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
|
741 (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
|
742 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
743 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
|
744 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
|
745 */ |
|
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 typedef struct { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
748 PyObject_HEAD |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
749 PyObject *sm_callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
750 } staticmethod; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
751 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
752 static void |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
753 sm_dealloc(staticmethod *sm) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
754 { |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
755 _PyObject_GC_UNTRACK((PyObject *)sm); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
756 Py_XDECREF(sm->sm_callable); |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
20015
diff
changeset
|
757 sm->ob_type->tp_free((PyObject *)sm); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
758 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
759 |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
760 static int |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
761 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
|
762 { |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
763 if (!sm->sm_callable) |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
764 return 0; |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
765 return visit(sm->sm_callable, arg); |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
766 } |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
767 |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
768 static int |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
769 sm_clear(staticmethod *sm) |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
770 { |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
771 Py_XDECREF(sm->sm_callable); |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
772 sm->sm_callable = NULL; |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
773 |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
774 return 0; |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
775 } |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
776 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
777 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
778 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
|
779 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
780 staticmethod *sm = (staticmethod *)self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
781 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
782 if (sm->sm_callable == NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
783 PyErr_SetString(PyExc_RuntimeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
784 "uninitialized staticmethod object"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
785 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
786 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
787 Py_INCREF(sm->sm_callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
788 return sm->sm_callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
789 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
790 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
791 static int |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
792 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
|
793 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
794 staticmethod *sm = (staticmethod *)self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
795 PyObject *callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
796 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
24193
diff
changeset
|
797 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
|
798 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
799 Py_INCREF(callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
800 sm->sm_callable = callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
801 return 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
802 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
803 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22974
diff
changeset
|
804 PyDoc_STRVAR(staticmethod_doc, |
|
21444
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
805 "staticmethod(function) -> method\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
806 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
807 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
|
808 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
809 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
|
810 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
|
811 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
812 class C:\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
813 def f(arg1, arg2, ...): ...\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
814 f = staticmethod(f)\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
815 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
816 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
|
817 (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
|
818 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
819 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
|
820 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
|
821 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
822 PyTypeObject PyStaticMethod_Type = { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
823 PyObject_HEAD_INIT(&PyType_Type) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
824 0, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
825 "staticmethod", |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
826 sizeof(staticmethod), |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
827 0, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
828 (destructor)sm_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
829 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
830 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
831 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
832 0, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
833 0, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
834 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
835 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
836 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
837 0, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
838 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
839 0, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
840 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
841 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
842 0, /* tp_as_buffer */ |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
843 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
|
844 staticmethod_doc, /* tp_doc */ |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
845 (traverseproc)sm_traverse, /* tp_traverse */ |
|
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
846 (inquiry)sm_clear, /* tp_clear */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
847 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
848 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
849 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
850 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
851 0, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
852 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
853 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
854 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
855 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
856 sm_descr_get, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
857 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
858 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
859 sm_init, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
860 PyType_GenericAlloc, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
861 PyType_GenericNew, /* tp_new */ |
|
28537
c7bb254ab890
Make staticmethods and classmethods participate in GC.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27941
diff
changeset
|
862 PyObject_GC_Del, /* tp_free */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
863 }; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
864 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
865 PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
866 PyStaticMethod_New(PyObject *callable) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
867 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
868 staticmethod *sm = (staticmethod *) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
869 PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
870 if (sm != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
871 Py_INCREF(callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
872 sm->sm_callable = callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
873 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
874 return (PyObject *)sm; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
875 } |
