Mercurial > cpython-lru_cache-weakref
annotate Objects/funcobject.c @ 24042:63fbbed10714 legacy-trunk
Simplify HTTPSConnection constructor.
See discussion in SF bug 458463.
| author | Jeremy Hylton <jeremy@alum.mit.edu> |
|---|---|
| date | Fri, 28 Jun 2002 23:38:14 +0000 |
| parents | 8e6760bf1884 |
| children | 6eb74e83a6f1 |
| 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; |
|
17170
962196ce7231
Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents:
16891
diff
changeset
|
17 op->func_weakreflist = NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
18 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
|
19 op->func_code = code; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
20 Py_INCREF(globals); |
| 6 | 21 op->func_globals = globals; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
22 op->func_name = ((PyCodeObject *)code)->co_name; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
23 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
|
24 op->func_defaults = NULL; /* No default arguments */ |
|
16257
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
25 op->func_closure = NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
26 consts = ((PyCodeObject *)code)->co_consts; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
27 if (PyTuple_Size(consts) >= 1) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
28 doc = PyTuple_GetItem(consts, 0); |
| 12512 | 29 if (!PyString_Check(doc) && !PyUnicode_Check(doc)) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
30 doc = Py_None; |
|
2082
4afa7eb4734b
Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
31 } |
|
4afa7eb4734b
Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
32 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
33 doc = Py_None; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
34 Py_INCREF(doc); |
|
2082
4afa7eb4734b
Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
35 op->func_doc = doc; |
|
15894
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
36 op->func_dict = NULL; |
| 6 | 37 } |
|
15894
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
38 else |
|
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
39 return NULL; |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
40 _PyObject_GC_TRACK(op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
41 return (PyObject *)op; |
| 6 | 42 } |
| 43 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
44 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
45 PyFunction_GetCode(PyObject *op) |
| 6 | 46 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
47 if (!PyFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
48 PyErr_BadInternalCall(); |
| 6 | 49 return NULL; |
| 50 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
51 return ((PyFunctionObject *) op) -> func_code; |
| 6 | 52 } |
| 53 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
54 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
55 PyFunction_GetGlobals(PyObject *op) |
| 6 | 56 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
57 if (!PyFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
58 PyErr_BadInternalCall(); |
| 6 | 59 return NULL; |
| 60 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
61 return ((PyFunctionObject *) op) -> func_globals; |
| 6 | 62 } |
| 63 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
64 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
65 PyFunction_GetDefaults(PyObject *op) |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
66 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
67 if (!PyFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
68 PyErr_BadInternalCall(); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
69 return NULL; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
70 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
71 return ((PyFunctionObject *) op) -> func_defaults; |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
72 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
73 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
74 int |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
75 PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
76 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
77 if (!PyFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
78 PyErr_BadInternalCall(); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
79 return -1; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
80 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
81 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
|
82 defaults = NULL; |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5945
diff
changeset
|
83 else if (PyTuple_Check(defaults)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
84 Py_XINCREF(defaults); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5945
diff
changeset
|
85 } |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
86 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
87 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
|
88 return -1; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
89 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
90 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
91 ((PyFunctionObject *) op) -> func_defaults = defaults; |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
92 return 0; |
|
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 |
|
16257
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
95 PyObject * |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
96 PyFunction_GetClosure(PyObject *op) |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
97 { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
98 if (!PyFunction_Check(op)) { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
99 PyErr_BadInternalCall(); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
100 return NULL; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
101 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
102 return ((PyFunctionObject *) op) -> func_closure; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
103 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
104 |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
105 int |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
106 PyFunction_SetClosure(PyObject *op, PyObject *closure) |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
107 { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
108 if (!PyFunction_Check(op)) { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
109 PyErr_BadInternalCall(); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
110 return -1; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
111 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
112 if (closure == Py_None) |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
113 closure = NULL; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
114 else if (PyTuple_Check(closure)) { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
115 Py_XINCREF(closure); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
116 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
117 else { |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
118 PyErr_SetString(PyExc_SystemError, "non-tuple closure"); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
119 return -1; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
120 } |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
121 Py_XDECREF(((PyFunctionObject *) op) -> func_closure); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
122 ((PyFunctionObject *) op) -> func_closure = closure; |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16042
diff
changeset
|
123 return 0; |
|
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 |
| 6 | 126 /* Methods */ |
| 127 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
128 #define OFF(x) offsetof(PyFunctionObject, x) |
| 86 | 129 |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
130 #define RR () |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
131 |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19948
diff
changeset
|
132 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
|
133 {"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
|
134 RESTRICTED|READONLY}, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
135 {"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
|
136 {"__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
|
137 {"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
|
138 RESTRICTED|READONLY}, |
|
16042
78bd97d8413c
Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents:
15894
diff
changeset
|
139 {"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
|
140 {"__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
|
141 {NULL} /* Sentinel */ |
| 86 | 142 }; |
| 143 | |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
144 static int |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
145 restricted(void) |
| 86 | 146 { |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
147 if (!PyEval_GetRestricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
148 return 0; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
149 PyErr_SetString(PyExc_RuntimeError, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
150 "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
|
151 return 1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
152 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
153 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
154 static PyObject * |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
155 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
|
156 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
157 if (restricted()) |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2082
diff
changeset
|
158 return NULL; |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 return NULL; |
|
fbe2074c5fa5
func_getattro(), func_setattro(): Implement the new semantics for
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
163 } |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
164 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
|
165 return op->func_dict; |
| 86 | 166 } |
| 167 | |
|
8407
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
168 static int |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
169 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
|
170 { |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
171 PyObject *tmp; |
|
15894
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
172 |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
173 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
174 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
175 /* 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
|
176 if (value == NULL) { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
177 PyErr_SetString(PyExc_TypeError, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
178 "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
|
179 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
180 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
181 /* 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
|
182 if (!PyDict_Check(value)) { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
183 PyErr_SetString(PyExc_TypeError, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
184 "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
|
185 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
186 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
187 tmp = op->func_dict; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
188 Py_INCREF(value); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
189 op->func_dict = value; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
190 Py_XDECREF(tmp); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
191 return 0; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
192 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
193 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
194 static PyObject * |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
195 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
|
196 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
197 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
198 return NULL; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
199 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
|
200 return op->func_code; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
201 } |
|
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 static int |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
204 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
|
205 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
206 PyObject *tmp; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
207 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
208 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
209 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
210 /* 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
|
211 * 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
|
212 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
|
213 PyErr_SetString(PyExc_TypeError, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
214 "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
|
215 return -1; |
|
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
216 } |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
217 tmp = op->func_code; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
218 Py_INCREF(value); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
219 op->func_code = value; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
220 Py_DECREF(tmp); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
221 return 0; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
222 } |
|
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 static PyObject * |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
225 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
|
226 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
227 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
228 return NULL; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
229 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
|
230 Py_INCREF(Py_None); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
231 return Py_None; |
|
8407
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
232 } |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
233 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
|
234 return op->func_defaults; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
235 } |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
236 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
237 static int |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
238 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
|
239 { |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
240 PyObject *tmp; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
241 |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
242 if (restricted()) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
243 return -1; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
244 /* 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
|
245 * 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
|
246 if (value == Py_None) |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
247 value = NULL; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
248 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
|
249 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
|
250 "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
|
251 return -1; |
|
16042
78bd97d8413c
Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents:
15894
diff
changeset
|
252 } |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
253 tmp = op->func_defaults; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
254 Py_XINCREF(value); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
255 op->func_defaults = value; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
256 Py_XDECREF(tmp); |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
257 return 0; |
|
8407
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
258 } |
|
096857b0e4b8
Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
259 |
|
20015
ca7af2a5d915
Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
260 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
|
261 {"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
|
262 {"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
|
263 (setter)func_set_defaults}, |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
264 {"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
|
265 {"__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
|
266 {NULL} /* Sentinel */ |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
267 }; |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
268 |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
269 PyDoc_STRVAR(func_doc, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
270 "function(code, globals[, name[, argdefs]])\n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
271 \n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
272 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
|
273 The optional name string overrides the name from the code object.\n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
274 The optional argdefs tuple specifies the default argument values."); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
275 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
276 static PyObject * |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
277 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
|
278 { |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
279 PyObject *code; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
280 PyObject *globals; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
281 PyObject *name = Py_None; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
282 PyObject *defaults = Py_None; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
283 PyFunctionObject *newfunc; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
284 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
285 if (!PyArg_ParseTuple(args, "O!O!|OO!:function", |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
286 &PyCode_Type, &code, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
287 &PyDict_Type, &globals, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
288 &name, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
289 &PyTuple_Type, &defaults)) |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
290 return NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
291 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
|
292 PyErr_SetString(PyExc_TypeError, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
293 "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
|
294 return NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
295 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
296 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
297 newfunc = (PyFunctionObject *)PyFunction_New(code, globals); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
298 if (newfunc == NULL) |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
299 return NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
300 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
301 if (name != Py_None) { |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
302 Py_XINCREF(name); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
303 Py_XDECREF(newfunc->func_name); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
304 newfunc->func_name = name; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
305 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
306 if (defaults != Py_None) { |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
307 Py_XINCREF(defaults); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
308 Py_XDECREF(newfunc->func_defaults); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
309 newfunc->func_defaults = defaults; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
310 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
311 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
312 return (PyObject *)newfunc; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
313 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
314 |
| 6 | 315 static void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
316 func_dealloc(PyFunctionObject *op) |
| 6 | 317 { |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
318 _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
|
319 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
|
320 PyObject_ClearWeakRefs((PyObject *) op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
321 Py_DECREF(op->func_code); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
322 Py_DECREF(op->func_globals); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
323 Py_DECREF(op->func_name); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
324 Py_XDECREF(op->func_defaults); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
325 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
|
326 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
|
327 Py_XDECREF(op->func_closure); |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
328 PyObject_GC_Del(op); |
| 6 | 329 } |
| 330 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
331 static PyObject* |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
332 func_repr(PyFunctionObject *op) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
333 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
334 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
|
335 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
|
336 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
|
337 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
|
338 op); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
339 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
340 |
|
1399
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
341 static int |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
342 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
|
343 { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
344 int err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
345 if (f->func_code) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
346 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
|
347 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
348 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
349 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
350 if (f->func_globals) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
351 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
|
352 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
353 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
354 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
355 if (f->func_defaults) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
356 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
|
357 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
358 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
359 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
360 if (f->func_doc) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
361 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
|
362 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
363 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
364 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
365 if (f->func_name) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
366 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
|
367 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
368 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
369 } |
|
15894
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
370 if (f->func_dict) { |
|
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
371 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
|
372 if (err) |
|
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
373 return err; |
|
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
14375
diff
changeset
|
374 } |
|
16891
806c6ae23103
Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16825
diff
changeset
|
375 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
|
376 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
|
377 if (err) |
|
806c6ae23103
Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16825
diff
changeset
|
378 return err; |
|
806c6ae23103
Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16825
diff
changeset
|
379 } |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
380 return 0; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
381 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
382 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
383 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
384 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
|
385 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
386 PyObject *result; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
387 PyObject *argdefs; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
388 PyObject **d, **k; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
389 int nk, nd; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
390 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
391 argdefs = PyFunction_GET_DEFAULTS(func); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
392 if (argdefs != NULL && PyTuple_Check(argdefs)) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
393 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
|
394 nd = PyTuple_Size(argdefs); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
395 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
396 else { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
397 d = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
398 nd = 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
399 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
400 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
401 if (kw != NULL && PyDict_Check(kw)) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
402 int pos, i; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
403 nk = PyDict_Size(kw); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
404 k = PyMem_NEW(PyObject *, 2*nk); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
405 if (k == NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
406 PyErr_NoMemory(); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
407 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
408 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
409 pos = i = 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
410 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
|
411 i += 2; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
412 nk = i/2; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
413 /* 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
|
414 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
415 else { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
416 k = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
417 nk = 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
418 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
419 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
420 result = PyEval_EvalCodeEx( |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
421 (PyCodeObject *)PyFunction_GET_CODE(func), |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
422 PyFunction_GET_GLOBALS(func), (PyObject *)NULL, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
423 &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
|
424 k, nk, d, nd, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
425 PyFunction_GET_CLOSURE(func)); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
426 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
427 if (k != NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
428 PyMem_DEL(k); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
429 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
430 return result; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
431 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
432 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
433 /* Bind a function to an object */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
434 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
435 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
|
436 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
437 if (obj == Py_None) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
438 obj = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
439 return PyMethod_New(func, obj, type); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
440 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
441 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
442 PyTypeObject PyFunction_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
443 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 444 0, |
| 445 "function", | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
446 sizeof(PyFunctionObject), |
| 6 | 447 0, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
448 (destructor)func_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
449 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
450 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
451 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
452 0, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
453 (reprfunc)func_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
454 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
455 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
456 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
457 0, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
458 function_call, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
459 0, /* tp_str */ |
|
19948
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
460 PyObject_GenericGetAttr, /* tp_getattro */ |
|
eaf1203b85cf
Rewrite function attributes to use the generic routines properly.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
461 PyObject_GenericSetAttr, /* tp_setattro */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
462 0, /* tp_as_buffer */ |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
463 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
|
464 func_doc, /* tp_doc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
465 (traverseproc)func_traverse, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
466 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
467 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
|
468 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
|
469 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
470 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
471 0, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
472 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
|
473 func_getsetlist, /* tp_getset */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
474 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
475 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
476 func_descr_get, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
477 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
478 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
|
479 0, /* tp_init */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
480 0, /* tp_alloc */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
481 func_new, /* tp_new */ |
| 6 | 482 }; |
|
18933
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 /* Class method object */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
486 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
487 /* 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
|
488 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
|
489 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
|
490 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
491 class C: |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
492 def f(cls, arg1, arg2, ...): ... |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
493 f = classmethod(f) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
494 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
495 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
|
496 (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
|
497 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
|
498 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
|
499 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
500 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
|
501 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
|
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 typedef struct { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
505 PyObject_HEAD |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
506 PyObject *cm_callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
507 } classmethod; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
508 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
509 static void |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
510 cm_dealloc(classmethod *cm) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
511 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
512 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
|
513 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
|
514 } |
|
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 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
517 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
|
518 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
519 classmethod *cm = (classmethod *)self; |
|
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 (cm->cm_callable == NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
522 PyErr_SetString(PyExc_RuntimeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
523 "uninitialized classmethod object"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
524 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
525 } |
|
22367
f2b6ccc00808
Fix for SF bug 528132 (Armin Rigo): classmethod().__get__() segfault
Guido van Rossum <guido@python.org>
parents:
21446
diff
changeset
|
526 if (type == NULL) |
|
f2b6ccc00808
Fix for SF bug 528132 (Armin Rigo): classmethod().__get__() segfault
Guido van Rossum <guido@python.org>
parents:
21446
diff
changeset
|
527 type = (PyObject *)(obj->ob_type); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
528 return PyMethod_New(cm->cm_callable, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
529 type, (PyObject *)(type->ob_type)); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
530 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
531 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
532 static int |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
533 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
|
534 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
535 classmethod *cm = (classmethod *)self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
536 PyObject *callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
537 |
|
22782
d99a1ddf9518
Fix the names of the classmethod and staticmethod constructors as passed to
Fred Drake <fdrake@acm.org>
parents:
22367
diff
changeset
|
538 if (!PyArg_ParseTuple(args, "O:classmethod", &callable)) |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
539 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
540 Py_INCREF(callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
541 cm->cm_callable = callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
542 return 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
543 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
544 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22974
diff
changeset
|
545 PyDoc_STRVAR(classmethod_doc, |
|
21444
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
546 "classmethod(function) -> method\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
547 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
548 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
|
549 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
550 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
|
551 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
|
552 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
|
553 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
554 class C:\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
555 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
|
556 f = classmethod(f)\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
557 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
558 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
|
559 (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
|
560 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
|
561 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
|
562 \n\ |
|
21444
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
563 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
|
564 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
|
565 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
566 PyTypeObject PyClassMethod_Type = { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
567 PyObject_HEAD_INIT(&PyType_Type) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
568 0, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
569 "classmethod", |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
570 sizeof(classmethod), |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
571 0, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
572 (destructor)cm_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
573 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
574 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
575 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
576 0, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
577 0, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
578 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
579 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
580 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
581 0, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
582 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
583 0, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
584 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
585 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
586 0, /* tp_as_buffer */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
587 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
21444
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
588 classmethod_doc, /* tp_doc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
589 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
590 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
591 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
592 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
593 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
594 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
595 0, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
596 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
597 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
598 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
599 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
600 cm_descr_get, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
601 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
602 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
603 cm_init, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
604 PyType_GenericAlloc, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
605 PyType_GenericNew, /* tp_new */ |
|
22974
72778ec94dec
PyObject_Del can now be used as a function designator.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22782
diff
changeset
|
606 PyObject_Del, /* tp_free */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
607 }; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
608 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
609 PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
610 PyClassMethod_New(PyObject *callable) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
611 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
612 classmethod *cm = (classmethod *) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
613 PyType_GenericAlloc(&PyClassMethod_Type, 0); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
614 if (cm != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
615 Py_INCREF(callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
616 cm->cm_callable = callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
617 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
618 return (PyObject *)cm; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
619 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
620 |
|
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 /* Static method object */ |
|
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 /* 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
|
625 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
|
626 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
627 class C: |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
628 def f(arg1, arg2, ...): ... |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
629 f = staticmethod(f) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
630 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
631 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
|
632 (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
|
633 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
634 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
|
635 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
|
636 */ |
|
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 typedef struct { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
639 PyObject_HEAD |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
640 PyObject *sm_callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
641 } staticmethod; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
642 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
643 static void |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
644 sm_dealloc(staticmethod *sm) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
645 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
646 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
|
647 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
|
648 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
649 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
650 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
651 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
|
652 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
653 staticmethod *sm = (staticmethod *)self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
654 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
655 if (sm->sm_callable == NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
656 PyErr_SetString(PyExc_RuntimeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
657 "uninitialized staticmethod object"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
658 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
659 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
660 Py_INCREF(sm->sm_callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
661 return sm->sm_callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
662 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
663 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
664 static int |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
665 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
|
666 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
667 staticmethod *sm = (staticmethod *)self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
668 PyObject *callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
669 |
|
22782
d99a1ddf9518
Fix the names of the classmethod and staticmethod constructors as passed to
Fred Drake <fdrake@acm.org>
parents:
22367
diff
changeset
|
670 if (!PyArg_ParseTuple(args, "O:staticmethod", &callable)) |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
671 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
672 Py_INCREF(callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
673 sm->sm_callable = callable; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
674 return 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
675 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
676 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22974
diff
changeset
|
677 PyDoc_STRVAR(staticmethod_doc, |
|
21444
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
678 "staticmethod(function) -> method\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
679 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
680 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
|
681 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
682 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
|
683 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
|
684 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
685 class C:\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
686 def f(arg1, arg2, ...): ...\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
687 f = staticmethod(f)\n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
688 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
689 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
|
690 (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
|
691 \n\ |
|
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
692 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
|
693 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
|
694 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
695 PyTypeObject PyStaticMethod_Type = { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
696 PyObject_HEAD_INIT(&PyType_Type) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
697 0, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
698 "staticmethod", |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
699 sizeof(staticmethod), |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
700 0, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
701 (destructor)sm_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
702 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
703 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
704 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
705 0, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
706 0, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
707 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
708 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
709 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
710 0, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
711 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
712 0, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
713 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
714 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
715 0, /* tp_as_buffer */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
716 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
21444
5193290aef0a
SF patch #493452: docstrings for staticmethod/classmethod (Skip
Guido van Rossum <guido@python.org>
parents:
21167
diff
changeset
|
717 staticmethod_doc, /* tp_doc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
718 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
719 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
720 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
721 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
722 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
723 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
724 0, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
725 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
726 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
727 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
728 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
729 sm_descr_get, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
730 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
731 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
732 sm_init, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
733 PyType_GenericAlloc, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
734 PyType_GenericNew, /* tp_new */ |
|
22974
72778ec94dec
PyObject_Del can now be used as a function designator.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22782
diff
changeset
|
735 PyObject_Del, /* tp_free */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
736 }; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
737 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
738 PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
739 PyStaticMethod_New(PyObject *callable) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
740 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
741 staticmethod *sm = (staticmethod *) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
742 PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
743 if (sm != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
744 Py_INCREF(callable); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
745 sm->sm_callable = callable; |
|
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 return (PyObject *)sm; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
748 } |
