Improved some error messages for command line processing.
[python/dscho.git] / Objects / funcobject.c
blobc9dd1398a2bae88fe4c93e913baf1a6197143461
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Function object implementation */
34 #include "Python.h"
35 #include "compile.h"
36 #include "structmember.h"
38 PyObject *
39 PyFunction_New(code, globals)
40 PyObject *code;
41 PyObject *globals;
43 PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
44 &PyFunction_Type);
45 if (op != NULL) {
46 PyObject *doc;
47 PyObject *consts;
48 Py_INCREF(code);
49 op->func_code = code;
50 Py_INCREF(globals);
51 op->func_globals = globals;
52 op->func_name = ((PyCodeObject *)code)->co_name;
53 Py_INCREF(op->func_name);
54 op->func_defaults = NULL; /* No default arguments */
55 consts = ((PyCodeObject *)code)->co_consts;
56 if (PyTuple_Size(consts) >= 1) {
57 doc = PyTuple_GetItem(consts, 0);
58 if (!PyString_Check(doc))
59 doc = Py_None;
61 else
62 doc = Py_None;
63 Py_INCREF(doc);
64 op->func_doc = doc;
66 return (PyObject *)op;
69 PyObject *
70 PyFunction_GetCode(op)
71 PyObject *op;
73 if (!PyFunction_Check(op)) {
74 PyErr_BadInternalCall();
75 return NULL;
77 return ((PyFunctionObject *) op) -> func_code;
80 PyObject *
81 PyFunction_GetGlobals(op)
82 PyObject *op;
84 if (!PyFunction_Check(op)) {
85 PyErr_BadInternalCall();
86 return NULL;
88 return ((PyFunctionObject *) op) -> func_globals;
91 PyObject *
92 PyFunction_GetDefaults(op)
93 PyObject *op;
95 if (!PyFunction_Check(op)) {
96 PyErr_BadInternalCall();
97 return NULL;
99 return ((PyFunctionObject *) op) -> func_defaults;
103 PyFunction_SetDefaults(op, defaults)
104 PyObject *op;
105 PyObject *defaults;
107 if (!PyFunction_Check(op)) {
108 PyErr_BadInternalCall();
109 return -1;
111 if (defaults == Py_None)
112 defaults = NULL;
113 else if (PyTuple_Check(defaults)) {
114 Py_XINCREF(defaults);
116 else {
117 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
118 return -1;
120 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
121 ((PyFunctionObject *) op) -> func_defaults = defaults;
122 return 0;
125 /* Methods */
127 #define OFF(x) offsetof(PyFunctionObject, x)
129 static struct memberlist func_memberlist[] = {
130 {"func_code", T_OBJECT, OFF(func_code)},
131 {"func_globals",T_OBJECT, OFF(func_globals), READONLY},
132 {"func_name", T_OBJECT, OFF(func_name), READONLY},
133 {"__name__", T_OBJECT, OFF(func_name), READONLY},
134 {"func_defaults",T_OBJECT, OFF(func_defaults)},
135 {"func_doc", T_OBJECT, OFF(func_doc)},
136 {"__doc__", T_OBJECT, OFF(func_doc)},
137 {NULL} /* Sentinel */
140 static PyObject *
141 func_getattr(op, name)
142 PyFunctionObject *op;
143 char *name;
145 if (name[0] != '_' && PyEval_GetRestricted()) {
146 PyErr_SetString(PyExc_RuntimeError,
147 "function attributes not accessible in restricted mode");
148 return NULL;
150 return PyMember_Get((char *)op, func_memberlist, name);
153 static int
154 func_setattr(op, name, value)
155 PyFunctionObject *op;
156 char *name;
157 PyObject *value;
159 if (PyEval_GetRestricted()) {
160 PyErr_SetString(PyExc_RuntimeError,
161 "function attributes not settable in restricted mode");
162 return -1;
164 if (strcmp(name, "func_code") == 0) {
165 if (value == NULL || !PyCode_Check(value)) {
166 PyErr_SetString(
167 PyExc_TypeError,
168 "func_code must be set to a code object");
169 return -1;
172 else if (strcmp(name, "func_defaults") == 0) {
173 if (value != Py_None && !PyTuple_Check(value)) {
174 PyErr_SetString(
175 PyExc_TypeError,
176 "func_defaults must be set to a tuple object");
177 return -1;
179 if (value == Py_None)
180 value = NULL;
182 return PyMember_Set((char *)op, func_memberlist, name, value);
185 static void
186 func_dealloc(op)
187 PyFunctionObject *op;
189 Py_DECREF(op->func_code);
190 Py_DECREF(op->func_globals);
191 Py_DECREF(op->func_name);
192 Py_XDECREF(op->func_defaults);
193 Py_XDECREF(op->func_doc);
194 PyMem_DEL(op);
197 static PyObject*
198 func_repr(op)
199 PyFunctionObject *op;
201 char buf[140];
202 if (op->func_name == Py_None)
203 sprintf(buf, "<anonymous function at %lx>", (long)op);
204 else
205 sprintf(buf, "<function %.100s at %lx>",
206 PyString_AsString(op->func_name),
207 (long)op);
208 return PyString_FromString(buf);
211 static int
212 func_compare(f, g)
213 PyFunctionObject *f, *g;
215 int c;
216 if (f->func_globals != g->func_globals)
217 return (f->func_globals < g->func_globals) ? -1 : 1;
218 if (f->func_defaults != g->func_defaults) {
219 if (f->func_defaults == NULL)
220 return -1;
221 if (g->func_defaults == NULL)
222 return 1;
223 c = PyObject_Compare(f->func_defaults, g->func_defaults);
224 if (c != 0)
225 return c;
227 return PyObject_Compare(f->func_code, g->func_code);
230 static long
231 func_hash(f)
232 PyFunctionObject *f;
234 long h;
235 h = PyObject_Hash(f->func_code);
236 if (h == -1) return h;
237 h = h ^ (long)f->func_globals;
238 if (h == -1) h = -2;
239 return h;
242 PyTypeObject PyFunction_Type = {
243 PyObject_HEAD_INIT(&PyType_Type)
245 "function",
246 sizeof(PyFunctionObject),
248 (destructor)func_dealloc, /*tp_dealloc*/
249 0, /*tp_print*/
250 (getattrfunc)func_getattr, /*tp_getattr*/
251 (setattrfunc)func_setattr, /*tp_setattr*/
252 (cmpfunc)func_compare, /*tp_compare*/
253 (reprfunc)func_repr, /*tp_repr*/
254 0, /*tp_as_number*/
255 0, /*tp_as_sequence*/
256 0, /*tp_as_mapping*/
257 (hashfunc)func_hash, /*tp_hash*/