blob: e2ba2a97d6e5749b5d1ed5d03eb94234edbf893e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Function object implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum9bfef441993-03-29 10:43:31 +000028#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000029#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031object *
Guido van Rossum846e4311990-11-18 17:44:06 +000032newfuncobject(code, globals)
33 object *code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034 object *globals;
35{
36 funcobject *op = NEWOBJ(funcobject, &Functype);
37 if (op != NULL) {
Guido van Rossum5bd38051995-01-07 12:01:30 +000038 object *doc;
39 object *consts;
Guido van Rossum846e4311990-11-18 17:44:06 +000040 INCREF(code);
41 op->func_code = code;
42 INCREF(globals);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043 op->func_globals = globals;
Guido van Rossum5bd38051995-01-07 12:01:30 +000044 op->func_name = ((codeobject *)code)->co_name;
Guido van Rossum25831651993-05-19 14:50:45 +000045 INCREF(op->func_name);
Guido van Rossum1d5735e1994-08-30 08:27:36 +000046 op->func_argcount = -1; /* Unknown */
47 op->func_argdefs = NULL; /* No default arguments */
Guido van Rossum5bd38051995-01-07 12:01:30 +000048 consts = ((codeobject *)code)->co_consts;
49 if (gettuplesize(consts) >= 1) {
50 doc = gettupleitem(consts, 0);
51 if (!is_stringobject(doc))
52 doc = None;
53 }
54 else
55 doc = None;
56 INCREF(doc);
57 op->func_doc = doc;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 }
59 return (object *)op;
60}
61
Guido van Rossum846e4311990-11-18 17:44:06 +000062object *
63getfunccode(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 object *op;
65{
66 if (!is_funcobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000067 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068 return NULL;
69 }
Guido van Rossum846e4311990-11-18 17:44:06 +000070 return ((funcobject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}
72
73object *
74getfuncglobals(op)
75 object *op;
76{
77 if (!is_funcobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000078 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 return NULL;
80 }
81 return ((funcobject *) op) -> func_globals;
82}
83
Guido van Rossum1d5735e1994-08-30 08:27:36 +000084object *
85getfuncargstuff(op, argcount_return)
86 object *op;
87 int *argcount_return;
88{
89 if (!is_funcobject(op)) {
90 err_badcall();
91 return NULL;
92 }
93 *argcount_return = ((funcobject *) op) -> func_argcount;
94 return ((funcobject *) op) -> func_argdefs;
95}
96
97int
98setfuncargstuff(op, argcount, argdefs)
99 object *op;
100 int argcount;
101 object *argdefs;
102{
103 if (!is_funcobject(op) ||
104 argdefs != NULL && !is_tupleobject(argdefs)) {
105 err_badcall();
106 return -1;
107 }
108 if (argdefs == None)
109 argdefs = NULL;
110 else if (is_tupleobject(argdefs))
111 XINCREF(argdefs);
112 else {
113 err_setstr(SystemError, "non-tuple default args");
114 return -1;
115 }
116 ((funcobject *) op) -> func_argcount = argcount;
117 XDECREF(((funcobject *) op) -> func_argdefs);
118 ((funcobject *) op) -> func_argdefs = argdefs;
119 return 0;
120}
121
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122/* Methods */
123
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124#define OFF(x) offsetof(funcobject, x)
125
126static struct memberlist func_memberlist[] = {
Guido van Rossuma38c0ff1992-01-14 18:32:20 +0000127 {"func_code", T_OBJECT, OFF(func_code), READONLY},
128 {"func_globals",T_OBJECT, OFF(func_globals), READONLY},
Guido van Rossum25831651993-05-19 14:50:45 +0000129 {"func_name", T_OBJECT, OFF(func_name), READONLY},
Guido van Rossum10393b11995-01-10 10:39:49 +0000130 {"__name__", T_OBJECT, OFF(func_name), READONLY},
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000131 {"func_argcount",T_INT, OFF(func_argcount), READONLY},
132 {"func_argdefs",T_OBJECT, OFF(func_argdefs), READONLY},
Guido van Rossum5bd38051995-01-07 12:01:30 +0000133 {"func_doc", T_OBJECT, OFF(func_doc)},
134 {"__doc__", T_OBJECT, OFF(func_doc)},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000135 {NULL} /* Sentinel */
136};
137
138static object *
139func_getattr(op, name)
140 funcobject *op;
141 char *name;
142{
Guido van Rossum10393b11995-01-10 10:39:49 +0000143 if (name[0] != '_' && getrestricted()) {
144 err_setstr(RuntimeError,
145 "function attributes not accessible in restricted mode");
146 return NULL;
147 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000148 return getmember((char *)op, func_memberlist, name);
149}
150
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151static void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000152func_dealloc(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 funcobject *op;
154{
Guido van Rossum846e4311990-11-18 17:44:06 +0000155 DECREF(op->func_code);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156 DECREF(op->func_globals);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000157 XDECREF(op->func_argdefs);
Guido van Rossum5bd38051995-01-07 12:01:30 +0000158 XDECREF(op->func_doc);
Guido van Rossum846e4311990-11-18 17:44:06 +0000159 DEL(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}
161
Guido van Rossum9bfef441993-03-29 10:43:31 +0000162static object*
163func_repr(op)
164 funcobject *op;
165{
166 char buf[140];
Guido van Rossum590baa41993-11-30 13:40:46 +0000167 if (op->func_name == None)
168 sprintf(buf, "<anonymous function at %lx>", (long)op);
169 else
170 sprintf(buf, "<function %.100s at %lx>",
171 getstringvalue(op->func_name),
172 (long)op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000173 return newstringobject(buf);
174}
175
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000176static int
177func_compare(f, g)
178 funcobject *f, *g;
179{
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000180 int c;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000181 if (f->func_globals != g->func_globals)
182 return (f->func_globals < g->func_globals) ? -1 : 1;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000183 c = f->func_argcount < g->func_argcount;
184 if (c != 0)
185 return c < 0 ? -1 : 1;
186 c = cmpobject(f->func_argdefs, g->func_argdefs);
187 if (c != 0)
188 return c;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000189 return cmpobject(f->func_code, g->func_code);
190}
191
192static long
193func_hash(f)
194 funcobject *f;
195{
196 long h;
197 h = hashobject(f->func_code);
198 if (h == -1) return h;
199 h = h ^ (long)f->func_globals;
200 if (h == -1) h = -2;
201 return h;
202}
203
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204typeobject Functype = {
205 OB_HEAD_INIT(&Typetype)
206 0,
207 "function",
208 sizeof(funcobject),
209 0,
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000210 (destructor)func_dealloc, /*tp_dealloc*/
Guido van Rossum846e4311990-11-18 17:44:06 +0000211 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000212 (getattrfunc)func_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213 0, /*tp_setattr*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000214 (cmpfunc)func_compare, /*tp_compare*/
215 (reprfunc)func_repr, /*tp_repr*/
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000216 0, /*tp_as_number*/
217 0, /*tp_as_sequence*/
218 0, /*tp_as_mapping*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000219 (hashfunc)func_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220};