Mercurial > cpython-lru_cache-weakref
annotate Objects/moduleobject.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:
1410
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:
1051
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 /* Module object implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
28 #include "ceval.h" |
| 6 | 29 |
| 30 typedef struct { | |
| 31 OB_HEAD | |
| 32 object *md_dict; | |
| 33 } moduleobject; | |
| 34 | |
| 35 object * | |
| 36 newmoduleobject(name) | |
| 37 char *name; | |
| 38 { | |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
39 moduleobject *m; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
40 object *nameobj; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
41 m = NEWOBJ(moduleobject, &Moduletype); |
| 6 | 42 if (m == NULL) |
| 43 return NULL; | |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
44 nameobj = newstringobject(name); |
| 6 | 45 m->md_dict = newdictobject(); |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
46 if (m->md_dict == NULL || nameobj == NULL) |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
47 goto fail; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
48 if (dictinsert(m->md_dict, "__name__", nameobj) != 0) |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
49 goto fail; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
50 DECREF(nameobj); |
| 6 | 51 return (object *)m; |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
52 |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
53 fail: |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
54 XDECREF(nameobj); |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
55 DECREF(m); |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
56 return NULL; |
| 6 | 57 } |
| 58 | |
| 59 object * | |
| 60 getmoduledict(m) | |
| 61 object *m; | |
| 62 { | |
| 63 if (!is_moduleobject(m)) { | |
| 86 | 64 err_badcall(); |
| 6 | 65 return NULL; |
| 66 } | |
| 67 return ((moduleobject *)m) -> md_dict; | |
| 68 } | |
| 69 | |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
70 char * |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
71 getmodulename(m) |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
72 object *m; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
73 { |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
74 object *nameobj; |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
75 if (!is_moduleobject(m)) { |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
76 err_badarg(); |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
77 return NULL; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
78 } |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
79 nameobj = dictlookup(((moduleobject *)m)->md_dict, "__name__"); |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
80 if (nameobj == NULL || !is_stringobject(nameobj)) { |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
81 err_setstr(SystemError, "nameless module"); |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
82 return NULL; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
83 } |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
84 return getstringvalue(nameobj); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
85 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
86 |
| 6 | 87 /* Methods */ |
| 88 | |
| 89 static void | |
| 86 | 90 module_dealloc(m) |
| 6 | 91 moduleobject *m; |
| 92 { | |
| 93 if (m->md_dict != NULL) | |
| 94 DECREF(m->md_dict); | |
| 95 free((char *)m); | |
| 96 } | |
| 97 | |
| 98 static object * | |
| 86 | 99 module_repr(m) |
| 6 | 100 moduleobject *m; |
| 101 { | |
| 102 char buf[100]; | |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
103 char *name = getmodulename((object *)m); |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
104 if (name == NULL) { |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
105 err_clear(); |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
106 name = "?"; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
107 } |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
108 sprintf(buf, "<module '%.80s'>", name); |
| 6 | 109 return newstringobject(buf); |
| 110 } | |
| 111 | |
| 112 static object * | |
| 86 | 113 module_getattr(m, name) |
| 6 | 114 moduleobject *m; |
| 115 char *name; | |
| 116 { | |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
117 object *res; |
| 86 | 118 if (strcmp(name, "__dict__") == 0) { |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
119 INCREF(m->md_dict); |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
120 return m->md_dict; |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
121 } |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
122 res = dictlookup(m->md_dict, name); |
|
14
874c8fd565fa
New error handling in getattr().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
123 if (res == NULL) |
| 475 | 124 err_setstr(AttributeError, name); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
125 else { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
126 if (is_accessobject(res)) |
|
1293
ac3c80abbf43
* classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
127 res = getaccessvalue(res, getglobals()); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
128 else |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
129 INCREF(res); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
130 } |
| 6 | 131 return res; |
| 132 } | |
| 133 | |
| 134 static int | |
| 86 | 135 module_setattr(m, name, v) |
| 6 | 136 moduleobject *m; |
| 137 char *name; | |
| 138 object *v; | |
| 139 { | |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
140 object *ac; |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
141 if (name[0] == '_' && strcmp(name, "__dict__") == 0) { |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
142 err_setstr(TypeError, "read-only special attribute"); |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
143 return -1; |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
144 } |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
145 ac = dictlookup(m->md_dict, name); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
146 if (ac != NULL && is_accessobject(ac)) |
|
1293
ac3c80abbf43
* classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
147 return setaccessvalue(ac, getglobals(), v); |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
148 if (v == NULL) { |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
149 int rv = dictremove(m->md_dict, name); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
150 if (rv < 0) |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
151 err_setstr(AttributeError, |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
152 "delete non-existing module attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
153 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
154 } |
| 6 | 155 else |
| 156 return dictinsert(m->md_dict, name, v); | |
| 157 } | |
| 158 | |
| 159 typeobject Moduletype = { | |
| 160 OB_HEAD_INIT(&Typetype) | |
| 161 0, /*ob_size*/ | |
| 162 "module", /*tp_name*/ | |
| 163 sizeof(moduleobject), /*tp_size*/ | |
| 164 0, /*tp_itemsize*/ | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
165 (destructor)module_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
166 0, /*tp_print*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
167 (getattrfunc)module_getattr, /*tp_getattr*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
168 (setattrfunc)module_setattr, /*tp_setattr*/ |
| 86 | 169 0, /*tp_compare*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
170 (reprfunc)module_repr, /*tp_repr*/ |
| 6 | 171 }; |
