blob: 8ab2f455c301bd14a2f22c13bba8080861359a2d [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;
50 DECREF(nameobj);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 return (object *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000052
53 fail:
54 XDECREF(nameobj);
55 DECREF(m);
56 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
59object *
60getmoduledict(m)
61 object *m;
62{
63 if (!is_moduleobject(m)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +000064 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065 return NULL;
66 }
67 return ((moduleobject *)m) -> md_dict;
68}
69
Guido van Rossum0558a201990-10-26 15:00:11 +000070char *
71getmodulename(m)
72 object *m;
73{
Guido van Rossumc45611d1993-11-17 22:58:56 +000074 object *nameobj;
Guido van Rossum0558a201990-10-26 15:00:11 +000075 if (!is_moduleobject(m)) {
76 err_badarg();
77 return NULL;
78 }
Guido van Rossumc45611d1993-11-17 22:58:56 +000079 nameobj = dictlookup(((moduleobject *)m)->md_dict, "__name__");
80 if (nameobj == NULL || !is_stringobject(nameobj)) {
81 err_setstr(SystemError, "nameless module");
82 return NULL;
83 }
84 return getstringvalue(nameobj);
Guido van Rossum0558a201990-10-26 15:00:11 +000085}
86
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087/* Methods */
88
89static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000090module_dealloc(m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091 moduleobject *m;
92{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 if (m->md_dict != NULL)
94 DECREF(m->md_dict);
95 free((char *)m);
96}
97
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +000099module_repr(m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100 moduleobject *m;
101{
102 char buf[100];
Guido van Rossumc45611d1993-11-17 22:58:56 +0000103 char *name = getmodulename((object *)m);
104 if (name == NULL) {
105 err_clear();
106 name = "?";
107 }
108 sprintf(buf, "<module '%.80s'>", name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 return newstringobject(buf);
110}
111
112static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113module_getattr(m, name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 moduleobject *m;
115 char *name;
116{
Guido van Rossume87203a1990-10-21 22:12:30 +0000117 object *res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000118 if (strcmp(name, "__dict__") == 0) {
Guido van Rossume87203a1990-10-21 22:12:30 +0000119 INCREF(m->md_dict);
120 return m->md_dict;
121 }
122 res = dictlookup(m->md_dict, name);
Guido van Rossum2b654f71990-10-14 20:03:32 +0000123 if (res == NULL)
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000124 err_setstr(AttributeError, name);
Guido van Rossum25831651993-05-19 14:50:45 +0000125 else {
126 if (is_accessobject(res))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000127 res = getaccessvalue(res, getglobals());
Guido van Rossum25831651993-05-19 14:50:45 +0000128 else
129 INCREF(res);
130 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131 return res;
132}
133
134static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000135module_setattr(m, name, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136 moduleobject *m;
137 char *name;
138 object *v;
139{
Guido van Rossum25831651993-05-19 14:50:45 +0000140 object *ac;
Guido van Rossumc45611d1993-11-17 22:58:56 +0000141 if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
142 err_setstr(TypeError, "read-only special attribute");
143 return -1;
Guido van Rossume87203a1990-10-21 22:12:30 +0000144 }
Guido van Rossum25831651993-05-19 14:50:45 +0000145 ac = dictlookup(m->md_dict, name);
146 if (ac != NULL && is_accessobject(ac))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000147 return setaccessvalue(ac, getglobals(), v);
Guido van Rossum94472a01992-09-04 09:45:18 +0000148 if (v == NULL) {
149 int rv = dictremove(m->md_dict, name);
150 if (rv < 0)
151 err_setstr(AttributeError,
152 "delete non-existing module attribute");
153 return rv;
154 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 else
156 return dictinsert(m->md_dict, name, v);
157}
158
159typeobject Moduletype = {
160 OB_HEAD_INIT(&Typetype)
161 0, /*ob_size*/
162 "module", /*tp_name*/
163 sizeof(moduleobject), /*tp_size*/
164 0, /*tp_itemsize*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000165 (destructor)module_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000166 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000167 (getattrfunc)module_getattr, /*tp_getattr*/
168 (setattrfunc)module_setattr, /*tp_setattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000169 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000170 (reprfunc)module_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171};