blob: 7377a98235a7b870314a0627c99c6d342c87b64d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Module object implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum81daa321993-05-20 14:24:46 +000028#include "ceval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029
30typedef struct {
31 OB_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032 object *md_dict;
33} moduleobject;
34
35object *
36newmoduleobject(name)
37 char *name;
38{
Guido van Rossumc45611d1993-11-17 22:58:56 +000039 moduleobject *m;
40 object *nameobj;
41 m = NEWOBJ(moduleobject, &Moduletype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042 if (m == NULL)
43 return NULL;
Guido van Rossumc45611d1993-11-17 22:58:56 +000044 nameobj = newstringobject(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045 m->md_dict = newdictobject();
Guido van Rossumc45611d1993-11-17 22:58:56 +000046 if (m->md_dict == NULL || nameobj == NULL)
47 goto fail;
48 if (dictinsert(m->md_dict, "__name__", nameobj) != 0)
49 goto fail;
Guido van Rossum8b14b4c1995-01-07 11:59:29 +000050 if (dictinsert(m->md_dict, "__doc__", None) != 0)
51 goto fail;
Guido van Rossumc45611d1993-11-17 22:58:56 +000052 DECREF(nameobj);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053 return (object *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000054
55 fail:
56 XDECREF(nameobj);
57 DECREF(m);
58 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059}
60
61object *
62getmoduledict(m)
63 object *m;
64{
65 if (!is_moduleobject(m)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +000066 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 return NULL;
68 }
69 return ((moduleobject *)m) -> md_dict;
70}
71
Guido van Rossum0558a201990-10-26 15:00:11 +000072char *
73getmodulename(m)
74 object *m;
75{
Guido van Rossumc45611d1993-11-17 22:58:56 +000076 object *nameobj;
Guido van Rossum0558a201990-10-26 15:00:11 +000077 if (!is_moduleobject(m)) {
78 err_badarg();
79 return NULL;
80 }
Guido van Rossumc45611d1993-11-17 22:58:56 +000081 nameobj = dictlookup(((moduleobject *)m)->md_dict, "__name__");
82 if (nameobj == NULL || !is_stringobject(nameobj)) {
83 err_setstr(SystemError, "nameless module");
84 return NULL;
85 }
86 return getstringvalue(nameobj);
Guido van Rossum0558a201990-10-26 15:00:11 +000087}
88
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089/* Methods */
90
91static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000092module_dealloc(m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 moduleobject *m;
94{
Guido van Rossum27e916f1995-01-26 00:39:00 +000095 if (m->md_dict != NULL) {
96 mappingclear(m->md_dict);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 DECREF(m->md_dict);
Guido van Rossum27e916f1995-01-26 00:39:00 +000098 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 free((char *)m);
100}
101
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103module_repr(m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 moduleobject *m;
105{
106 char buf[100];
Guido van Rossumc45611d1993-11-17 22:58:56 +0000107 char *name = getmodulename((object *)m);
108 if (name == NULL) {
109 err_clear();
110 name = "?";
111 }
112 sprintf(buf, "<module '%.80s'>", name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113 return newstringobject(buf);
114}
115
116static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117module_getattr(m, name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118 moduleobject *m;
119 char *name;
120{
Guido van Rossume87203a1990-10-21 22:12:30 +0000121 object *res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122 if (strcmp(name, "__dict__") == 0) {
Guido van Rossume87203a1990-10-21 22:12:30 +0000123 INCREF(m->md_dict);
124 return m->md_dict;
125 }
126 res = dictlookup(m->md_dict, name);
Guido van Rossum2b654f71990-10-14 20:03:32 +0000127 if (res == NULL)
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000128 err_setstr(AttributeError, name);
Guido van Rossum25831651993-05-19 14:50:45 +0000129 else {
130 if (is_accessobject(res))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000131 res = getaccessvalue(res, getglobals());
Guido van Rossum25831651993-05-19 14:50:45 +0000132 else
133 INCREF(res);
134 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135 return res;
136}
137
138static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000139module_setattr(m, name, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 moduleobject *m;
141 char *name;
142 object *v;
143{
Guido van Rossum25831651993-05-19 14:50:45 +0000144 object *ac;
Guido van Rossumc45611d1993-11-17 22:58:56 +0000145 if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
146 err_setstr(TypeError, "read-only special attribute");
147 return -1;
Guido van Rossume87203a1990-10-21 22:12:30 +0000148 }
Guido van Rossum25831651993-05-19 14:50:45 +0000149 ac = dictlookup(m->md_dict, name);
150 if (ac != NULL && is_accessobject(ac))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000151 return setaccessvalue(ac, getglobals(), v);
Guido van Rossum94472a01992-09-04 09:45:18 +0000152 if (v == NULL) {
153 int rv = dictremove(m->md_dict, name);
154 if (rv < 0)
155 err_setstr(AttributeError,
156 "delete non-existing module attribute");
157 return rv;
158 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 else
160 return dictinsert(m->md_dict, name, v);
161}
162
163typeobject Moduletype = {
164 OB_HEAD_INIT(&Typetype)
165 0, /*ob_size*/
166 "module", /*tp_name*/
167 sizeof(moduleobject), /*tp_size*/
168 0, /*tp_itemsize*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000169 (destructor)module_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000170 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000171 (getattrfunc)module_getattr, /*tp_getattr*/
172 (setattrfunc)module_setattr, /*tp_setattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000173 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000174 (reprfunc)module_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175};