Mercurial > cpython-lru_cache-weakref
annotate Objects/funcobject.c @ 1590:ac2e5cc29949 legacy-trunk
Added tests for missing host to open_http and open_gopher
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 18 Apr 1994 09:39:56 +0000 |
| parents | 06570429f062 |
| children | b724599401ff |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
|
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); |
| 6 | 44 } |
| 45 return (object *)op; | |
| 46 } | |
| 47 | |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
48 object * |
|
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
49 getfunccode(op) |
| 6 | 50 object *op; |
| 51 { | |
| 52 if (!is_funcobject(op)) { | |
| 27 | 53 err_badcall(); |
| 6 | 54 return NULL; |
| 55 } | |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
56 return ((funcobject *) op) -> func_code; |
| 6 | 57 } |
| 58 | |
| 59 object * | |
| 60 getfuncglobals(op) | |
| 61 object *op; | |
| 62 { | |
| 63 if (!is_funcobject(op)) { | |
| 27 | 64 err_badcall(); |
| 6 | 65 return NULL; |
| 66 } | |
| 67 return ((funcobject *) op) -> func_globals; | |
| 68 } | |
| 69 | |
| 70 /* Methods */ | |
| 71 | |
| 86 | 72 #define OFF(x) offsetof(funcobject, x) |
| 73 | |
| 74 static struct memberlist func_memberlist[] = { | |
|
588
7d24de643fb5
Added READONLY specifier to data members.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
75 {"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
|
76 {"func_globals",T_OBJECT, OFF(func_globals), READONLY}, |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
77 {"func_name", T_OBJECT, OFF(func_name), READONLY}, |
| 86 | 78 {NULL} /* Sentinel */ |
| 79 }; | |
| 80 | |
| 81 static object * | |
| 82 func_getattr(op, name) | |
| 83 funcobject *op; | |
| 84 char *name; | |
| 85 { | |
| 86 return getmember((char *)op, func_memberlist, name); | |
| 87 } | |
| 88 | |
| 6 | 89 static void |
| 86 | 90 func_dealloc(op) |
| 6 | 91 funcobject *op; |
| 92 { | |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
93 DECREF(op->func_code); |
| 6 | 94 DECREF(op->func_globals); |
|
84
a79cbb2655ba
Function objects no longer contain a parse tree node, but intermediate
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
95 DEL(op); |
| 6 | 96 } |
| 97 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
98 static object* |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
99 func_repr(op) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
100 funcobject *op; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
101 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
102 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 (long)op); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
109 return newstringobject(buf); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
110 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
111 |
|
1399
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
112 static int |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
113 func_compare(f, g) |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
114 funcobject *f, *g; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
115 { |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
116 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
|
117 return (f->func_globals < g->func_globals) ? -1 : 1; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
118 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
|
119 } |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
120 |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
121 static long |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
122 func_hash(f) |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
123 funcobject *f; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
124 { |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
125 long h; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
126 h = hashobject(f->func_code); |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
127 if (h == -1) return h; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
128 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
|
129 if (h == -1) h = -2; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
130 return h; |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
131 } |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
132 |
| 6 | 133 typeobject Functype = { |
| 134 OB_HEAD_INIT(&Typetype) | |
| 135 0, | |
| 136 "function", | |
| 137 sizeof(funcobject), | |
| 138 0, | |
| 86 | 139 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
|
140 0, /*tp_print*/ |
| 86 | 141 func_getattr, /*tp_getattr*/ |
| 6 | 142 0, /*tp_setattr*/ |
|
1399
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
143 func_compare, /*tp_compare*/ |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
144 func_repr, /*tp_repr*/ |
|
1399
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
145 0, /*tp_as_number*/ |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
146 0, /*tp_as_sequence*/ |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
147 0, /*tp_as_mapping*/ |
|
cc172ce05561
Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
148 func_hash, /*tp_hash*/ |
| 6 | 149 }; |
