blob: aedba35bca5b8b124cb47776abbf5e661f11cd89 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
4
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 Rossum85a5fbb1990-10-14 12:07:46 +000028
29typedef struct {
30 OB_HEAD
31 object *md_name;
32 object *md_dict;
33} moduleobject;
34
35object *
36newmoduleobject(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
51object *
52getmoduledict(m)
53 object *m;
54{
55 if (!is_moduleobject(m)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +000056 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057 return NULL;
58 }
59 return ((moduleobject *)m) -> md_dict;
60}
61
Guido van Rossum0558a201990-10-26 15:00:11 +000062char *
63getmodulename(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 Rossum85a5fbb1990-10-14 12:07:46 +000073/* Methods */
74
75static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000076module_dealloc(m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 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 Rossum2a591661992-03-27 17:26:38 +000086/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +000087static int
Guido van Rossum3f5da241990-12-20 15:06:42 +000088module_print(m, fp, flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089 moduleobject *m;
90 FILE *fp;
Guido van Rossum2a591661992-03-27 17:26:38 +000091 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092{
Guido van Rossum3f5da241990-12-20 15:06:42 +000093 fprintf(fp, "<module '%s'>", getstringvalue(m->md_name));
Guido van Rossum90933611991-06-07 16:10:43 +000094 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095}
96
97static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +000098module_repr(m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 moduleobject *m;
100{
101 char buf[100];
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102 sprintf(buf, "<module '%.80s'>", getstringvalue(m->md_name));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 return newstringobject(buf);
104}
105
106static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107module_getattr(m, name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108 moduleobject *m;
109 char *name;
110{
Guido van Rossume87203a1990-10-21 22:12:30 +0000111 object *res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112 if (strcmp(name, "__dict__") == 0) {
Guido van Rossume87203a1990-10-21 22:12:30 +0000113 INCREF(m->md_dict);
114 return m->md_dict;
115 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000116 if (strcmp(name, "__name__") == 0) {
117 INCREF(m->md_name);
118 return m->md_name;
119 }
Guido van Rossume87203a1990-10-21 22:12:30 +0000120 res = dictlookup(m->md_dict, name);
Guido van Rossum2b654f71990-10-14 20:03:32 +0000121 if (res == NULL)
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000122 err_setstr(AttributeError, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123 else
124 INCREF(res);
125 return res;
126}
127
128static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129module_setattr(m, name, v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130 moduleobject *m;
131 char *name;
132 object *v;
133{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134 if (strcmp(name, "__dict__") == 0 || strcmp(name, "__name__") == 0) {
Guido van Rossum94472a01992-09-04 09:45:18 +0000135 err_setstr(TypeError, "read-only special attribute");
Guido van Rossum73531a31990-12-20 23:12:40 +0000136 return -1;
Guido van Rossume87203a1990-10-21 22:12:30 +0000137 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000138 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 Rossum85a5fbb1990-10-14 12:07:46 +0000145 else
146 return dictinsert(m->md_dict, name, v);
147}
148
149typeobject 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 Rossum3f5da241990-12-20 15:06:42 +0000155 module_dealloc, /*tp_dealloc*/
156 module_print, /*tp_print*/
157 module_getattr, /*tp_getattr*/
158 module_setattr, /*tp_setattr*/
159 0, /*tp_compare*/
160 module_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161};