blob: 22a793f417c300d554c81947361e3e20845a58b7 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Module object implementation */
2
3#include <stdio.h>
4
5#include "PROTO.h"
6#include "object.h"
7#include "stringobject.h"
8#include "dictobject.h"
9#include "moduleobject.h"
10#include "objimpl.h"
Guido van Rossum2b654f71990-10-14 20:03:32 +000011#include "errors.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
13typedef struct {
14 OB_HEAD
15 object *md_name;
16 object *md_dict;
17} moduleobject;
18
19object *
20newmoduleobject(name)
21 char *name;
22{
23 moduleobject *m = NEWOBJ(moduleobject, &Moduletype);
24 if (m == NULL)
25 return NULL;
26 m->md_name = newstringobject(name);
27 m->md_dict = newdictobject();
28 if (m->md_name == NULL || m->md_dict == NULL) {
29 DECREF(m);
30 return NULL;
31 }
32 return (object *)m;
33}
34
35object *
36getmoduledict(m)
37 object *m;
38{
39 if (!is_moduleobject(m)) {
Guido van Rossume87203a1990-10-21 22:12:30 +000040 err_badarg();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041 return NULL;
42 }
43 return ((moduleobject *)m) -> md_dict;
44}
45
46int
47setmoduledict(m, v)
48 object *m;
49 object *v;
50{
Guido van Rossume87203a1990-10-21 22:12:30 +000051 if (!is_moduleobject(m)) {
52 err_badarg();
53 return -1;
54 }
55 if (!is_dictobject(v)) {
56 err_badarg();
57 return -1;
58 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 DECREF(((moduleobject *)m) -> md_dict);
60 INCREF(v);
61 ((moduleobject *)m) -> md_dict = v;
62 return 0;
63}
64
Guido van Rossum0558a201990-10-26 15:00:11 +000065char *
66getmodulename(m)
67 object *m;
68{
69 if (!is_moduleobject(m)) {
70 err_badarg();
71 return NULL;
72 }
73 return getstringvalue(((moduleobject *)m) -> md_name);
74}
75
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076/* Methods */
77
78static void
79moduledealloc(m)
80 moduleobject *m;
81{
82 if (m->md_name != NULL)
83 DECREF(m->md_name);
84 if (m->md_dict != NULL)
85 DECREF(m->md_dict);
86 free((char *)m);
87}
88
89static void
90moduleprint(m, fp, flags)
91 moduleobject *m;
92 FILE *fp;
93 int flags;
94{
95 fprintf(fp, "<module %s>", getstringvalue(m->md_name));
96}
97
98static object *
99modulerepr(m)
100 moduleobject *m;
101{
102 char buf[100];
103 sprintf(buf, "<module %.80s>", getstringvalue(m->md_name));
104 return newstringobject(buf);
105}
106
107static object *
108modulegetattr(m, name)
109 moduleobject *m;
110 char *name;
111{
Guido van Rossume87203a1990-10-21 22:12:30 +0000112 object *res;
113 if (strcmp(name, "__dict") == 0) {
114 INCREF(m->md_dict);
115 return m->md_dict;
116 }
117 res = dictlookup(m->md_dict, name);
Guido van Rossum2b654f71990-10-14 20:03:32 +0000118 if (res == NULL)
119 err_setstr(NameError, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 else
121 INCREF(res);
122 return res;
123}
124
125static int
126modulesetattr(m, name, v)
127 moduleobject *m;
128 char *name;
129 object *v;
130{
Guido van Rossume87203a1990-10-21 22:12:30 +0000131 if (strcmp(name, "__dict") == 0) {
132 /* Can't allow assignment to __dict, it would screw up
133 module's functions which still use the old dictionary. */
134 err_setstr(NameError, "__dict is a reserved member name");
135 return NULL;
136 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137 if (v == NULL)
138 return dictremove(m->md_dict, name);
139 else
140 return dictinsert(m->md_dict, name, v);
141}
142
143typeobject Moduletype = {
144 OB_HEAD_INIT(&Typetype)
145 0, /*ob_size*/
146 "module", /*tp_name*/
147 sizeof(moduleobject), /*tp_size*/
148 0, /*tp_itemsize*/
149 moduledealloc, /*tp_dealloc*/
150 moduleprint, /*tp_print*/
151 modulegetattr, /*tp_getattr*/
152 modulesetattr, /*tp_setattr*/
153 0, /*tp_compare*/
154 modulerepr, /*tp_repr*/
155};