blob: dca5543eb10b4a5cc3ba0ee1c224af4d8f6327a2 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum9bfef441993-03-29 10:43:31 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The 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
32 object *md_name;
33 object *md_dict;
34} moduleobject;
35
36object *
37newmoduleobject(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
52object *
53getmoduledict(m)
54 object *m;
55{
56 if (!is_moduleobject(m)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +000057 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 return NULL;
59 }
60 return ((moduleobject *)m) -> md_dict;
61}
62
Guido van Rossum0558a201990-10-26 15:00:11 +000063char *
64getmodulename(m)
65 object *m;
66{
67 if (!is_moduleobject(m)) {
68 err_badarg();
69 return NULL;
70 }
71 return getstringvalue(((moduleobject *)m) -> md_name);
72}
73
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074/* Methods */
75
76static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000077module_dealloc(m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +000088module_repr(m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089 moduleobject *m;
90{
91 char buf[100];
Guido van Rossum3f5da241990-12-20 15:06:42 +000092 sprintf(buf, "<module '%.80s'>", getstringvalue(m->md_name));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 return newstringobject(buf);
94}
95
96static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +000097module_getattr(m, name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098 moduleobject *m;
99 char *name;
100{
Guido van Rossume87203a1990-10-21 22:12:30 +0000101 object *res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102 if (strcmp(name, "__dict__") == 0) {
Guido van Rossume87203a1990-10-21 22:12:30 +0000103 INCREF(m->md_dict);
104 return m->md_dict;
105 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000106 if (strcmp(name, "__name__") == 0) {
107 INCREF(m->md_name);
108 return m->md_name;
109 }
Guido van Rossume87203a1990-10-21 22:12:30 +0000110 res = dictlookup(m->md_dict, name);
Guido van Rossum2b654f71990-10-14 20:03:32 +0000111 if (res == NULL)
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000112 err_setstr(AttributeError, name);
Guido van Rossum25831651993-05-19 14:50:45 +0000113 else {
114 if (is_accessobject(res))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000115 res = getaccessvalue(res, getglobals());
Guido van Rossum25831651993-05-19 14:50:45 +0000116 else
117 INCREF(res);
118 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119 return res;
120}
121
122static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123module_setattr(m, name, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 moduleobject *m;
125 char *name;
126 object *v;
127{
Guido van Rossum25831651993-05-19 14:50:45 +0000128 object *ac;
129 if (name[0] == '_' && name[1] == '_') {
130 int n = strlen(name);
131 if (name[n-1] == '_' && name[n-2] == '_') {
132 err_setstr(TypeError, "read-only special attribute");
133 return -1;
134 }
Guido van Rossume87203a1990-10-21 22:12:30 +0000135 }
Guido van Rossum25831651993-05-19 14:50:45 +0000136 ac = dictlookup(m->md_dict, name);
137 if (ac != NULL && is_accessobject(ac))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000138 return setaccessvalue(ac, getglobals(), v);
Guido van Rossum94472a01992-09-04 09:45:18 +0000139 if (v == NULL) {
140 int rv = dictremove(m->md_dict, name);
141 if (rv < 0)
142 err_setstr(AttributeError,
143 "delete non-existing module attribute");
144 return rv;
145 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 else
147 return dictinsert(m->md_dict, name, v);
148}
149
150typeobject Moduletype = {
151 OB_HEAD_INIT(&Typetype)
152 0, /*ob_size*/
153 "module", /*tp_name*/
154 sizeof(moduleobject), /*tp_size*/
155 0, /*tp_itemsize*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000156 module_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000157 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158 module_getattr, /*tp_getattr*/
159 module_setattr, /*tp_setattr*/
160 0, /*tp_compare*/
161 module_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162};