annotate Objects/funcobject.c @ 17581:c1937faabb23 2.1

Added support for optional explanation parameter to the \versionadded macro. Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most of the work in a helper function, with the do_cmd_*() wrappers just supplying a portion of the replacement text.
author Fred Drake <fdrake@acm.org>
date Wed, 18 Apr 2001 17:29:14 +0000
parents 962196ce7231
children d7d05103c023
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Function object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
4 #include "Python.h"
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
5 #include "compile.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
6 #include "structmember.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
8 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
9 PyFunction_New(PyObject *code, PyObject *globals)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
11 PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
12 &PyFunction_Type);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 if (op != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
14 PyObject *doc;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
15 PyObject *consts;
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 16891
diff changeset
16 op->func_weakreflist = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
17 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
18 op->func_code = code;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
19 Py_INCREF(globals);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 op->func_globals = globals;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
21 op->func_name = ((PyCodeObject *)code)->co_name;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
22 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
23 op->func_defaults = NULL; /* No default arguments */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
24 op->func_closure = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
25 consts = ((PyCodeObject *)code)->co_consts;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
26 if (PyTuple_Size(consts) >= 1) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
27 doc = PyTuple_GetItem(consts, 0);
12512
a61157328b12 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 8407
diff changeset
28 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
29 doc = Py_None;
2082
4afa7eb4734b Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
30 }
4afa7eb4734b Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
31 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
32 doc = Py_None;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
33 Py_INCREF(doc);
2082
4afa7eb4734b Added __doc__ attribute (alias func_doc), initialized from first
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
34 op->func_doc = doc;
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
35 op->func_dict = NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 }
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
37 else
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
38 return NULL;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13141
diff changeset
39 PyObject_GC_Init(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
40 return (PyObject *)op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
43 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
44 PyFunction_GetCode(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
46 if (!PyFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
47 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
50 return ((PyFunctionObject *) op) -> func_code;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
53 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
54 PyFunction_GetGlobals(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
56 if (!PyFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
57 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
60 return ((PyFunctionObject *) op) -> func_globals;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
63 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
64 PyFunction_GetDefaults(PyObject *op)
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
65 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
66 if (!PyFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
67 PyErr_BadInternalCall();
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
68 return NULL;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
69 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
70 return ((PyFunctionObject *) op) -> func_defaults;
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
71 }
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 int
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
74 PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
75 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
76 if (!PyFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
77 PyErr_BadInternalCall();
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
78 return -1;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
79 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
80 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
81 defaults = NULL;
7981
09963a2a24db Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 5945
diff changeset
82 else if (PyTuple_Check(defaults)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
83 Py_XINCREF(defaults);
7981
09963a2a24db Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 5945
diff changeset
84 }
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
85 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
86 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
87 return -1;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
88 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
89 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
90 ((PyFunctionObject *) op) -> func_defaults = defaults;
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
91 return 0;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
92 }
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
93
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
94 PyObject *
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
95 PyFunction_GetClosure(PyObject *op)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
96 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
97 if (!PyFunction_Check(op)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
98 PyErr_BadInternalCall();
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
99 return NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
100 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
101 return ((PyFunctionObject *) op) -> func_closure;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
102 }
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 int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
105 PyFunction_SetClosure(PyObject *op, PyObject *closure)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
106 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
107 if (!PyFunction_Check(op)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
108 PyErr_BadInternalCall();
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
109 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
110 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
111 if (closure == Py_None)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
112 closure = NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
113 else if (PyTuple_Check(closure)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
114 Py_XINCREF(closure);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
115 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
116 else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
117 PyErr_SetString(PyExc_SystemError, "non-tuple closure");
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
118 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
119 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
120 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
121 ((PyFunctionObject *) op) -> func_closure = closure;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
122 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
123 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16042
diff changeset
124
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
127 #define OFF(x) offsetof(PyFunctionObject, x)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
128
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
129 static struct memberlist func_memberlist[] = {
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
130 {"func_code", T_OBJECT, OFF(func_code)},
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
131 {"func_globals", T_OBJECT, OFF(func_globals), READONLY},
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
132 {"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
133 {"__name__", T_OBJECT, OFF(func_name), READONLY},
16825
51cf451faae4 Fix SF buf 404774 submitted by Gregory H. Ball
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16328
diff changeset
134 {"func_closure", T_OBJECT, OFF(func_closure), READONLY},
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
135 {"func_defaults", T_OBJECT, OFF(func_defaults)},
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
136 {"func_doc", T_OBJECT, OFF(func_doc)},
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
137 {"__doc__", T_OBJECT, OFF(func_doc)},
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
138 {"func_dict", T_OBJECT, OFF(func_dict)},
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
139 {"__dict__", T_OBJECT, OFF(func_dict)},
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
140 {NULL} /* Sentinel */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
141 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
142
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
143 static PyObject *
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
144 func_getattro(PyFunctionObject *op, PyObject *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
145 {
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
146 PyObject *rtn;
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
147 char *sname = PyString_AsString(name);
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
148
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
149 if (sname[0] != '_' && PyEval_GetRestricted()) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
150 PyErr_SetString(PyExc_RuntimeError,
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2082
diff changeset
151 "function attributes not accessible in restricted mode");
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2082
diff changeset
152 return NULL;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2082
diff changeset
153 }
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
154
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
155 /* no API for PyMember_HasAttr() */
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
156 rtn = PyMember_Get((char *)op, func_memberlist, sname);
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
157
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
158 if (rtn == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
159 PyErr_Clear();
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
160 if (op->func_dict != NULL) {
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
161 rtn = PyDict_GetItem(op->func_dict, name);
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
162 Py_XINCREF(rtn);
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
163 }
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
164 if (rtn == NULL)
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
165 PyErr_SetObject(PyExc_AttributeError, name);
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
166 }
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
167 return rtn;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
168 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 84
diff changeset
169
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
170 static int
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
171 func_setattro(PyFunctionObject *op, PyObject *name, PyObject *value)
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
172 {
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
173 int rtn;
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
174 char *sname = PyString_AsString(name);
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
175
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
176 if (PyEval_GetRestricted()) {
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
177 PyErr_SetString(PyExc_RuntimeError,
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
178 "function attributes not settable in restricted mode");
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
179 return -1;
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
180 }
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
181 if (strcmp(sname, "func_code") == 0) {
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
182 /* not legal to del f.func_code or to set it to anything
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
183 * other than a code object.
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
184 */
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
185 if (value == NULL || !PyCode_Check(value)) {
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
186 PyErr_SetString(
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
187 PyExc_TypeError,
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
188 "func_code must be set to a code object");
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
189 return -1;
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
190 }
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
191 }
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
192 else if (strcmp(sname, "func_defaults") == 0) {
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
193 /* legal to del f.func_defaults. Can only set
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
194 * func_defaults to NULL or a tuple.
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
195 */
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
196 if (value == Py_None)
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
197 value = NULL;
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
198 if (value != NULL && !PyTuple_Check(value)) {
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
199 PyErr_SetString(
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
200 PyExc_TypeError,
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
201 "func_defaults must be set to a tuple object");
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
202 return -1;
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
203 }
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
204 }
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
205 else if (!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) {
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
206 /* legal to del f.func_dict. Can only set func_dict to
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
207 * NULL or a dictionary.
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
208 */
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
209 if (value == Py_None)
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
210 value = NULL;
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
211 if (value != NULL && !PyDict_Check(value)) {
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
212 PyErr_SetString(
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
213 PyExc_TypeError,
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
214 "func_dict must be set to a dict object");
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
215 return -1;
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
216 }
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
217 }
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
218
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
219 rtn = PyMember_Set((char *)op, func_memberlist, sname, value);
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
220 if (rtn < 0 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
221 PyErr_Clear();
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
222 if (op->func_dict == NULL) {
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
223 /* don't create the dict if we're deleting an
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
224 * attribute. In that case, we know we'll get an
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
225 * AttributeError.
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
226 */
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
227 if (value == NULL) {
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
228 PyErr_SetString(PyExc_AttributeError, sname);
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
229 return -1;
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
230 }
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
231 op->func_dict = PyDict_New();
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
232 if (op->func_dict == NULL)
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
233 return -1;
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
234 }
16042
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
235 if (value == NULL)
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
236 rtn = PyDict_DelItem(op->func_dict, name);
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
237 else
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
238 rtn = PyDict_SetItem(op->func_dict, name, value);
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
239 /* transform KeyError into AttributeError */
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
240 if (rtn < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
78bd97d8413c Application and elaboration of patch #103305 to fix core dumps when
Barry Warsaw <barry@python.org>
parents: 15894
diff changeset
241 PyErr_SetString(PyExc_AttributeError, sname);
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
242 }
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
243 return rtn;
8407
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
244 }
096857b0e4b8 Make function objects somewhat mutable -- the members func_code,
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
245
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 static void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
247 func_dealloc(PyFunctionObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 {
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 16891
diff changeset
249 PyObject_ClearWeakRefs((PyObject *) op);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13141
diff changeset
250 PyObject_GC_Fini(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
251 Py_DECREF(op->func_code);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
252 Py_DECREF(op->func_globals);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
253 Py_DECREF(op->func_name);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
254 Py_XDECREF(op->func_defaults);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
255 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
256 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
257 Py_XDECREF(op->func_closure);
13265
e8bb0d536633 Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
258 op = (PyFunctionObject *) PyObject_AS_GC(op);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12512
diff changeset
259 PyObject_DEL(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
262 static PyObject*
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
263 func_repr(PyFunctionObject *op)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
264 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
265 char buf[140];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
266 if (op->func_name == Py_None)
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13183
diff changeset
267 sprintf(buf, "<anonymous function at %p>", op);
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1399
diff changeset
268 else
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13183
diff changeset
269 sprintf(buf, "<function %.100s at %p>",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
270 PyString_AsString(op->func_name),
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13183
diff changeset
271 op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
272 return PyString_FromString(buf);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
273 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
274
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
275 static int
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
276 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
277 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
278 int err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
279 if (f->func_code) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
280 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
281 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
282 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
283 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
284 if (f->func_globals) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
285 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
286 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
287 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
288 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
289 if (f->func_defaults) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
290 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
291 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
292 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
293 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
294 if (f->func_doc) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
295 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
296 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
297 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
298 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
299 if (f->func_name) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
300 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
301 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
302 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
303 }
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
304 if (f->func_dict) {
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
305 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
306 if (err)
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
307 return err;
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
308 }
16891
806c6ae23103 Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16825
diff changeset
309 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
310 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
311 if (err)
806c6ae23103 Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16825
diff changeset
312 return err;
806c6ae23103 Visit the closure during traversal and XDECREF it on during deallocation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16825
diff changeset
313 }
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
314 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
315 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
316
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
317 PyTypeObject PyFunction_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
318 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 "function",
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13141
diff changeset
321 sizeof(PyFunctionObject) + PyGC_HEAD_SIZE,
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 0,
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
323 (destructor)func_dealloc, /*tp_dealloc*/
84
a79cbb2655ba Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
324 0, /*tp_print*/
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
325 0, /*tp_getattr*/
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
326 0, /*tp_setattr*/
16328
abd383f1905c The one thing I love more then writing code is deleting code.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 16257
diff changeset
327 0, /*tp_compare*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
328 (reprfunc)func_repr, /*tp_repr*/
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
329 0, /*tp_as_number*/
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
330 0, /*tp_as_sequence*/
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
331 0, /*tp_as_mapping*/
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 16891
diff changeset
332 0, /*tp_hash*/
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
333 0, /*tp_call*/
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
334 0, /*tp_str*/
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
335 (getattrofunc)func_getattro, /*tp_getattro*/
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 14375
diff changeset
336 (setattrofunc)func_setattro, /*tp_setattro*/
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
337 0, /* tp_as_buffer */
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 16891
diff changeset
338 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC | Py_TPFLAGS_HAVE_WEAKREFS,
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
339 0, /* tp_doc */
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
340 (traverseproc)func_traverse, /* tp_traverse */
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 16891
diff changeset
341 0, /* tp_clear */
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 16891
diff changeset
342 0, /* tp_richcompare */
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 16891
diff changeset
343 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 };