Mercurial > cpython-lru_cache-weakref
annotate Objects/funcobject.c @ 1944:8e8fddcbdfd2 legacy-trunk
Hopefully the last edits for 1.1
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 06 Oct 1994 16:15:09 +0000 |
| parents | b724599401ff |
| children | 866c81068dbd |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
3 Amsterdam, The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
| 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 not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 6 | 25 /* Function object implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
28 #include "compile.h" |
| 86 | 29 #include "structmember.h" |
| 6 | 30 |
| 31 object * | |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
32 newfuncobject(code, globals) |
|
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
33 object *code; |
| 6 | 34 object *globals; |
| 35 { | |
| 36 funcobject *op = NEWOBJ(funcobject, &Functype); | |
| 37 if (op != NULL) { | |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
38 INCREF(code); |
|
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
39 op->func_code = code; |
|
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
40 INCREF(globals); |
| 6 | 41 op->func_globals = globals; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
42 op->func_name = ((codeobject*)(op->func_code))->co_name; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
43 INCREF(op->func_name); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
44 op->func_argcount = -1; /* Unknown */ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
45 op->func_argdefs = NULL; /* No default arguments */ |
| 6 | 46 } |
| 47 return (object *)op; | |
| 48 } | |
| 49 | |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
50 object * |
|
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
51 getfunccode(op) |
| 6 | 52 object *op; |
| 53 { | |
| 54 if (!is_funcobject(op)) { | |
| 27 | 55 err_badcall(); |
| 6 | 56 return NULL; |
| 57 } | |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
58 return ((funcobject *) op) -> func_code; |
| 6 | 59 } |
| 60 | |
| 61 object * | |
| 62 getfuncglobals(op) | |
| 63 object *op; | |
| 64 { | |
| 65 if (!is_funcobject(op)) { | |
| 27 | 66 err_badcall(); |
| 6 | 67 return NULL; |
| 68 } | |
| 69 return ((funcobject *) op) -> func_globals; | |
| 70 } | |
| 71 | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
72 object * |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
73 getfuncargstuff(op, argcount_return) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
74 object *op; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
75 int *argcount_return; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
76 { |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
77 if (!is_funcobject(op)) { |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
78 err_badcall(); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
79 return NULL; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
80 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
81 *argcount_return = ((funcobject *) op) -> func_argcount; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
82 return ((funcobject *) op) -> func_argdefs; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
83 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
84 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
85 int |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
86 setfuncargstuff(op, argcount, argdefs) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
87 object *op; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
88 int argcount; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
89 object *argdefs; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
90 { |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
91 if (!is_funcobject(op) || |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
92 argdefs != NULL && !is_tupleobject(argdefs)) { |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
93 err_badcall(); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
94 return -1; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
95 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
96 if (argdefs == None) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
97 argdefs = NULL; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
98 else if (is_tupleobject(argdefs)) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
99 XINCREF(argdefs); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
100 else { |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
101 err_setstr(SystemError, "non-tuple default args"); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
102 return -1; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
103 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
104 ((funcobject *) op) -> func_argcount = argcount; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
105 XDECREF(((funcobject *) op) -> func_argdefs); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
106 ((funcobject *) op) -> func_argdefs = argdefs; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
107 return 0; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
108 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
109 |
| 6 | 110 /* Methods */ |
| 111 | |
| 86 | 112 #define OFF(x) offsetof(funcobject, x) |
| 113 | |
| 114 static struct memberlist func_memberlist[] = { | |
|
588
7d24de643fb5
Added READONLY specifier to data members.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
115 {"func_code", T_OBJECT, OFF(func_code), READONLY}, |
|
7d24de643fb5
Added READONLY specifier to data members.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
116 {"func_globals",T_OBJECT, OFF(func_globals), READONLY}, |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
117 {"func_name", T_OBJECT, OFF(func_name), READONLY}, |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
118 {"func_argcount",T_INT, OFF(func_argcount), READONLY}, |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
119 {"func_argdefs",T_OBJECT, OFF(func_argdefs), READONLY}, |
| 86 | 120 {NULL} /* Sentinel */ |
| 121 }; | |
| 122 | |
| 123 static object * | |
| 124 func_getattr(op, name) | |
| 125 funcobject *op; | |
| 126 char *name; | |
| 127 { | |
| 128 return getmember((char *)op, func_memberlist, name); | |
| 129 } | |
| 130 | |
| 6 | 131 static void |
| 86 | 132 func_dealloc(op) |
| 6 | 133 funcobject *op; |
| 134 { | |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
135 DECREF(op->func_code); |
| 6 | 136 DECREF(op->func_globals); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
137 XDECREF(op->func_argdefs); |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
138 DEL(op); |
| 6 | 139 } |
| 140 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
141 static object* |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
142 func_repr(op) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
143 funcobject *op; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
144 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
145 char buf[140]; |
|
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
|
146 if (op->func_name == None) |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1399
diff
changeset
|
147 sprintf(buf, "<anonymous function at %lx>", (long)op); |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1399
diff
changeset
|
148 else |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1399
diff
changeset
|
149 sprintf(buf, "<function %.100s at %lx>", |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1399
diff
changeset
|
150 getstringvalue(op->func_name), |
|
06570429f062
* import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents:
1399
diff
changeset
|
151 (long)op); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
152 return newstringobject(buf); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
153 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
154 |
|
1399
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
155 static int |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
156 func_compare(f, g) |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
157 funcobject *f, *g; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
158 { |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
159 int c; |
|
1399
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
160 if (f->func_globals != g->func_globals) |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
161 return (f->func_globals < g->func_globals) ? -1 : 1; |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
162 c = f->func_argcount < g->func_argcount; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
163 if (c != 0) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
164 return c < 0 ? -1 : 1; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
165 c = cmpobject(f->func_argdefs, g->func_argdefs); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
166 if (c != 0) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
167 return c; |
|
1399
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
168 return cmpobject(f->func_code, g->func_code); |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
169 } |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
170 |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
171 static long |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
172 func_hash(f) |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
173 funcobject *f; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
174 { |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
175 long h; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
176 h = hashobject(f->func_code); |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
177 if (h == -1) return h; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
178 h = h ^ (long)f->func_globals; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
179 if (h == -1) h = -2; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
180 return h; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
181 } |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
182 |
| 6 | 183 typeobject Functype = { |
| 184 OB_HEAD_INIT(&Typetype) | |
| 185 0, | |
| 186 "function", | |
| 187 sizeof(funcobject), | |
| 188 0, | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
189 (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
|
190 0, /*tp_print*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
191 (getattrfunc)func_getattr, /*tp_getattr*/ |
| 6 | 192 0, /*tp_setattr*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
193 (cmpfunc)func_compare, /*tp_compare*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
194 (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
|
195 0, /*tp_as_number*/ |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
196 0, /*tp_as_sequence*/ |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
197 0, /*tp_as_mapping*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1415
diff
changeset
|
198 (hashfunc)func_hash, /*tp_hash*/ |
| 6 | 199 }; |
