blob: 9d8e1820f332d8950164e856f5dc0850fc17ab81 [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 Rossum85a5fbb1990-10-14 12:07:46 +000095 if (m->md_dict != NULL)
96 DECREF(m->md_dict);
97 free((char *)m);
98}
99
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000101module_repr(m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 moduleobject *m;
103{
104 char buf[100];
Guido van Rossumc45611d1993-11-17 22:58:56 +0000105 char *name = getmodulename((object *)m);
106 if (name == NULL) {
107 err_clear();
108 name = "?";
109 }
110 sprintf(buf, "<module '%.80s'>", name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 return newstringobject(buf);
112}
113
114static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000115module_getattr(m, name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116 moduleobject *m;
117 char *name;
118{
Guido van Rossume87203a1990-10-21 22:12:30 +0000119 object *res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 if (strcmp(name, "__dict__") == 0) {
Guido van Rossume87203a1990-10-21 22:12:30 +0000121 INCREF(m->md_dict);
122 return m->md_dict;
123 }
124 res = dictlookup(m->md_dict, name);
Guido van Rossum2b654f71990-10-14 20:03:32 +0000125 if (res == NULL)
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000126 err_setstr(AttributeError, name);
Guido van Rossum25831651993-05-19 14:50:45 +0000127 else {
128 if (is_accessobject(res))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000129 res = getaccessvalue(res, getglobals());
Guido van Rossum25831651993-05-19 14:50:45 +0000130 else
131 INCREF(res);
132 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133 return res;
134}
135
136static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000137module_setattr(m, name, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 moduleobject *m;
139 char *name;
140 object *v;
141{
Guido van Rossum25831651993-05-19 14:50:45 +0000142 object *ac;
Guido van Rossumc45611d1993-11-17 22:58:56 +0000143 if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
144 err_setstr(TypeError, "read-only special attribute");
145 return -1;
Guido van Rossume87203a1990-10-21 22:12:30 +0000146 }
Guido van Rossum25831651993-05-19 14:50:45 +0000147 ac = dictlookup(m->md_dict, name);
148 if (ac != NULL && is_accessobject(ac))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000149 return setaccessvalue(ac, getglobals(), v);
Guido van Rossum94472a01992-09-04 09:45:18 +0000150 if (v == NULL) {
151 int rv = dictremove(m->md_dict, name);
152 if (rv < 0)
153 err_setstr(AttributeError,
154 "delete non-existing module attribute");
155 return rv;
156 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 else
158 return dictinsert(m->md_dict, name, v);
159}
160
161typeobject Moduletype = {
162 OB_HEAD_INIT(&Typetype)
163 0, /*ob_size*/
164 "module", /*tp_name*/
165 sizeof(moduleobject), /*tp_size*/
166 0, /*tp_itemsize*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000167 (destructor)module_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000168 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000169 (getattrfunc)module_getattr, /*tp_getattr*/
170 (setattrfunc)module_setattr, /*tp_setattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000171 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000172 (reprfunc)module_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173};