Mercurial > cpython-lru_cache-weakref
annotate Objects/moduleobject.c @ 1330:409ea74feb21 legacy-trunk
Minor fixes / changes for Mac compatibility.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 29 Jul 1993 08:25:09 +0000 |
| parents | ac3c80abbf43 |
| children | 315272ec4d5b |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
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:
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_name; | |
| 33 object *md_dict; | |
| 34 } moduleobject; | |
| 35 | |
| 36 object * | |
| 37 newmoduleobject(name) | |
| 38 char *name; | |
| 39 { | |
| 40 moduleobject *m = NEWOBJ(moduleobject, &Moduletype); | |
| 41 if (m == NULL) | |
| 42 return NULL; | |
| 43 m->md_name = newstringobject(name); | |
| 44 m->md_dict = newdictobject(); | |
| 45 if (m->md_name == NULL || m->md_dict == NULL) { | |
| 46 DECREF(m); | |
| 47 return NULL; | |
| 48 } | |
| 49 return (object *)m; | |
| 50 } | |
| 51 | |
| 52 object * | |
| 53 getmoduledict(m) | |
| 54 object *m; | |
| 55 { | |
| 56 if (!is_moduleobject(m)) { | |
| 86 | 57 err_badcall(); |
| 6 | 58 return NULL; |
| 59 } | |
| 60 return ((moduleobject *)m) -> md_dict; | |
| 61 } | |
| 62 | |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
63 char * |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
64 getmodulename(m) |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
65 object *m; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
66 { |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
67 if (!is_moduleobject(m)) { |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
68 err_badarg(); |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
69 return NULL; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
70 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
71 return getstringvalue(((moduleobject *)m) -> md_name); |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
72 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
73 |
| 6 | 74 /* Methods */ |
| 75 | |
| 76 static void | |
| 86 | 77 module_dealloc(m) |
| 6 | 78 moduleobject *m; |
| 79 { | |
| 80 if (m->md_name != NULL) | |
| 81 DECREF(m->md_name); | |
| 82 if (m->md_dict != NULL) | |
| 83 DECREF(m->md_dict); | |
| 84 free((char *)m); | |
| 85 } | |
| 86 | |
| 87 static object * | |
| 86 | 88 module_repr(m) |
| 6 | 89 moduleobject *m; |
| 90 { | |
| 91 char buf[100]; | |
| 86 | 92 sprintf(buf, "<module '%.80s'>", getstringvalue(m->md_name)); |
| 6 | 93 return newstringobject(buf); |
| 94 } | |
| 95 | |
| 96 static object * | |
| 86 | 97 module_getattr(m, name) |
| 6 | 98 moduleobject *m; |
| 99 char *name; | |
| 100 { | |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
101 object *res; |
| 86 | 102 if (strcmp(name, "__dict__") == 0) { |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
103 INCREF(m->md_dict); |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
104 return m->md_dict; |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
105 } |
| 86 | 106 if (strcmp(name, "__name__") == 0) { |
| 107 INCREF(m->md_name); | |
| 108 return m->md_name; | |
| 109 } | |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
110 res = dictlookup(m->md_dict, name); |
|
14
874c8fd565fa
New error handling in getattr().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
111 if (res == NULL) |
| 475 | 112 err_setstr(AttributeError, name); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
113 else { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
114 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
|
115 res = getaccessvalue(res, getglobals()); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
116 else |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
117 INCREF(res); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
118 } |
| 6 | 119 return res; |
| 120 } | |
| 121 | |
| 122 static int | |
| 86 | 123 module_setattr(m, name, v) |
| 6 | 124 moduleobject *m; |
| 125 char *name; | |
| 126 object *v; | |
| 127 { | |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
128 object *ac; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
129 if (name[0] == '_' && name[1] == '_') { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
130 int n = strlen(name); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
131 if (name[n-1] == '_' && name[n-2] == '_') { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
132 err_setstr(TypeError, "read-only special attribute"); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
133 return -1; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
134 } |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
135 } |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
136 ac = dictlookup(m->md_dict, name); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
137 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
|
138 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
|
139 if (v == NULL) { |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
140 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
|
141 if (rv < 0) |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
142 err_setstr(AttributeError, |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
143 "delete non-existing module attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
144 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
145 } |
| 6 | 146 else |
| 147 return dictinsert(m->md_dict, name, v); | |
| 148 } | |
| 149 | |
| 150 typeobject Moduletype = { | |
| 151 OB_HEAD_INIT(&Typetype) | |
| 152 0, /*ob_size*/ | |
| 153 "module", /*tp_name*/ | |
| 154 sizeof(moduleobject), /*tp_size*/ | |
| 155 0, /*tp_itemsize*/ | |
| 86 | 156 module_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
157 0, /*tp_print*/ |
| 86 | 158 module_getattr, /*tp_getattr*/ |
| 159 module_setattr, /*tp_setattr*/ | |
| 160 0, /*tp_compare*/ | |
| 161 module_repr, /*tp_repr*/ | |
| 6 | 162 }; |
