Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Module object implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
| 29 | typedef struct { |
| 30 | OB_HEAD |
| 31 | object *md_name; |
| 32 | object *md_dict; |
| 33 | } moduleobject; |
| 34 | |
| 35 | object * |
| 36 | newmoduleobject(name) |
| 37 | char *name; |
| 38 | { |
| 39 | moduleobject *m = NEWOBJ(moduleobject, &Moduletype); |
| 40 | if (m == NULL) |
| 41 | return NULL; |
| 42 | m->md_name = newstringobject(name); |
| 43 | m->md_dict = newdictobject(); |
| 44 | if (m->md_name == NULL || m->md_dict == NULL) { |
| 45 | DECREF(m); |
| 46 | return NULL; |
| 47 | } |
| 48 | return (object *)m; |
| 49 | } |
| 50 | |
| 51 | object * |
| 52 | getmoduledict(m) |
| 53 | object *m; |
| 54 | { |
| 55 | if (!is_moduleobject(m)) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 56 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | return NULL; |
| 58 | } |
| 59 | return ((moduleobject *)m) -> md_dict; |
| 60 | } |
| 61 | |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 62 | char * |
| 63 | getmodulename(m) |
| 64 | object *m; |
| 65 | { |
| 66 | if (!is_moduleobject(m)) { |
| 67 | err_badarg(); |
| 68 | return NULL; |
| 69 | } |
| 70 | return getstringvalue(((moduleobject *)m) -> md_name); |
| 71 | } |
| 72 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | /* Methods */ |
| 74 | |
| 75 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 76 | module_dealloc(m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | moduleobject *m; |
| 78 | { |
| 79 | if (m->md_name != NULL) |
| 80 | DECREF(m->md_name); |
| 81 | if (m->md_dict != NULL) |
| 82 | DECREF(m->md_dict); |
| 83 | free((char *)m); |
| 84 | } |
| 85 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 87 | module_repr(m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | moduleobject *m; |
| 89 | { |
| 90 | char buf[100]; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | sprintf(buf, "<module '%.80s'>", getstringvalue(m->md_name)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | return newstringobject(buf); |
| 93 | } |
| 94 | |
| 95 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 96 | module_getattr(m, name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | moduleobject *m; |
| 98 | char *name; |
| 99 | { |
Guido van Rossum | e87203a | 1990-10-21 22:12:30 +0000 | [diff] [blame] | 100 | object *res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 101 | if (strcmp(name, "__dict__") == 0) { |
Guido van Rossum | e87203a | 1990-10-21 22:12:30 +0000 | [diff] [blame] | 102 | INCREF(m->md_dict); |
| 103 | return m->md_dict; |
| 104 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 105 | if (strcmp(name, "__name__") == 0) { |
| 106 | INCREF(m->md_name); |
| 107 | return m->md_name; |
| 108 | } |
Guido van Rossum | e87203a | 1990-10-21 22:12:30 +0000 | [diff] [blame] | 109 | res = dictlookup(m->md_dict, name); |
Guido van Rossum | 2b654f7 | 1990-10-14 20:03:32 +0000 | [diff] [blame] | 110 | if (res == NULL) |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 111 | err_setstr(AttributeError, name); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame^] | 112 | else { |
| 113 | if (is_accessobject(res)) |
| 114 | res = getaccessvalue(res, (object *)NULL); |
| 115 | else |
| 116 | INCREF(res); |
| 117 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | return res; |
| 119 | } |
| 120 | |
| 121 | static int |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 122 | module_setattr(m, name, v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | moduleobject *m; |
| 124 | char *name; |
| 125 | object *v; |
| 126 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame^] | 127 | object *ac; |
| 128 | if (name[0] == '_' && name[1] == '_') { |
| 129 | int n = strlen(name); |
| 130 | if (name[n-1] == '_' && name[n-2] == '_') { |
| 131 | err_setstr(TypeError, "read-only special attribute"); |
| 132 | return -1; |
| 133 | } |
Guido van Rossum | e87203a | 1990-10-21 22:12:30 +0000 | [diff] [blame] | 134 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame^] | 135 | ac = dictlookup(m->md_dict, name); |
| 136 | if (ac != NULL && is_accessobject(ac)) |
| 137 | return setaccessvalue(ac, (object *)NULL, v); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 138 | if (v == NULL) { |
| 139 | int rv = dictremove(m->md_dict, name); |
| 140 | if (rv < 0) |
| 141 | err_setstr(AttributeError, |
| 142 | "delete non-existing module attribute"); |
| 143 | return rv; |
| 144 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | else |
| 146 | return dictinsert(m->md_dict, name, v); |
| 147 | } |
| 148 | |
| 149 | typeobject Moduletype = { |
| 150 | OB_HEAD_INIT(&Typetype) |
| 151 | 0, /*ob_size*/ |
| 152 | "module", /*tp_name*/ |
| 153 | sizeof(moduleobject), /*tp_size*/ |
| 154 | 0, /*tp_itemsize*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 155 | module_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 156 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 157 | module_getattr, /*tp_getattr*/ |
| 158 | module_setattr, /*tp_setattr*/ |
| 159 | 0, /*tp_compare*/ |
| 160 | module_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | }; |