Mercurial > cpython-lru_cache-weakref
annotate Objects/moduleobject.c @ 3044:7e7bb00a0a2e legacy-trunk
Added functionality: locate CW projects, copy them, empty them and
binhex them.
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Thu, 31 Aug 1995 13:47:14 +0000 |
| parents | b5dcba7b732d |
| children | 138fa15a1032 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2059
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
3 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; |
|
2081
afecdec996ff
initialize __doc__ to None
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
50 if (dictinsert(m->md_dict, "__doc__", None) != 0) |
|
afecdec996ff
initialize __doc__ to None
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
51 goto fail; |
|
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 DECREF(nameobj); |
| 6 | 53 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
|
54 |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
55 fail: |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
56 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
|
57 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
|
58 return NULL; |
| 6 | 59 } |
| 60 | |
| 61 object * | |
| 62 getmoduledict(m) | |
| 63 object *m; | |
| 64 { | |
| 65 if (!is_moduleobject(m)) { | |
| 86 | 66 err_badcall(); |
| 6 | 67 return NULL; |
| 68 } | |
| 69 return ((moduleobject *)m) -> md_dict; | |
| 70 } | |
| 71 | |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
72 char * |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
73 getmodulename(m) |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
74 object *m; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
75 { |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
76 object *nameobj; |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
77 if (!is_moduleobject(m)) { |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
78 err_badarg(); |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
79 return NULL; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
80 } |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 } |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
86 return getstringvalue(nameobj); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
87 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
88 |
| 6 | 89 /* Methods */ |
| 90 | |
| 91 static void | |
| 86 | 92 module_dealloc(m) |
| 6 | 93 moduleobject *m; |
| 94 { | |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
95 if (m->md_dict != NULL) { |
|
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
96 mappingclear(m->md_dict); |
| 6 | 97 DECREF(m->md_dict); |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
98 } |
| 6 | 99 free((char *)m); |
| 100 } | |
| 101 | |
| 102 static object * | |
| 86 | 103 module_repr(m) |
| 6 | 104 moduleobject *m; |
| 105 { | |
| 106 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
|
107 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
|
108 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
|
109 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
|
110 name = "?"; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
111 } |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
112 sprintf(buf, "<module '%.80s'>", name); |
| 6 | 113 return newstringobject(buf); |
| 114 } | |
| 115 | |
| 116 static object * | |
| 86 | 117 module_getattr(m, name) |
| 6 | 118 moduleobject *m; |
| 119 char *name; | |
| 120 { | |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
121 object *res; |
| 86 | 122 if (strcmp(name, "__dict__") == 0) { |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
123 INCREF(m->md_dict); |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
124 return m->md_dict; |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
125 } |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
126 res = dictlookup(m->md_dict, name); |
|
14
874c8fd565fa
New error handling in getattr().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
127 if (res == NULL) |
| 475 | 128 err_setstr(AttributeError, name); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
129 else { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
130 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
|
131 res = getaccessvalue(res, getglobals()); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
132 else |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
133 INCREF(res); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
134 } |
| 6 | 135 return res; |
| 136 } | |
| 137 | |
| 138 static int | |
| 86 | 139 module_setattr(m, name, v) |
| 6 | 140 moduleobject *m; |
| 141 char *name; | |
| 142 object *v; | |
| 143 { | |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 return -1; |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
148 } |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
149 ac = dictlookup(m->md_dict, name); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
150 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
|
151 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
|
152 if (v == NULL) { |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
153 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
|
154 if (rv < 0) |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
155 err_setstr(AttributeError, |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
156 "delete non-existing module attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
157 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
158 } |
| 6 | 159 else |
| 160 return dictinsert(m->md_dict, name, v); | |
| 161 } | |
| 162 | |
| 163 typeobject Moduletype = { | |
| 164 OB_HEAD_INIT(&Typetype) | |
| 165 0, /*ob_size*/ | |
| 166 "module", /*tp_name*/ | |
| 167 sizeof(moduleobject), /*tp_size*/ | |
| 168 0, /*tp_itemsize*/ | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
169 (destructor)module_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
170 0, /*tp_print*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
171 (getattrfunc)module_getattr, /*tp_getattr*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
172 (setattrfunc)module_setattr, /*tp_setattr*/ |
| 86 | 173 0, /*tp_compare*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
174 (reprfunc)module_repr, /*tp_repr*/ |
| 6 | 175 }; |
