blob: 09bbae947804a753fd7a0b7bb106d5e6fc2cecbf [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
65/* Methods */
66
67static void
68moduledealloc(m)
69 moduleobject *m;
70{
71 if (m->md_name != NULL)
72 DECREF(m->md_name);
73 if (m->md_dict != NULL)
74 DECREF(m->md_dict);
75 free((char *)m);
76}
77
78static void
79moduleprint(m, fp, flags)
80 moduleobject *m;
81 FILE *fp;
82 int flags;
83{
84 fprintf(fp, "<module %s>", getstringvalue(m->md_name));
85}
86
87static object *
88modulerepr(m)
89 moduleobject *m;
90{
91 char buf[100];
92 sprintf(buf, "<module %.80s>", getstringvalue(m->md_name));
93 return newstringobject(buf);
94}
95
96static object *
97modulegetattr(m, name)
98 moduleobject *m;
99 char *name;
100{
Guido van Rossume87203a1990-10-21 22:12:30 +0000101 object *res;
102 if (strcmp(name, "__dict") == 0) {
103 INCREF(m->md_dict);
104 return m->md_dict;
105 }
106 res = dictlookup(m->md_dict, name);
Guido van Rossum2b654f71990-10-14 20:03:32 +0000107 if (res == NULL)
108 err_setstr(NameError, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 else
110 INCREF(res);
111 return res;
112}
113
114static int
115modulesetattr(m, name, v)
116 moduleobject *m;
117 char *name;
118 object *v;
119{
Guido van Rossume87203a1990-10-21 22:12:30 +0000120 if (strcmp(name, "__dict") == 0) {
121 /* Can't allow assignment to __dict, it would screw up
122 module's functions which still use the old dictionary. */
123 err_setstr(NameError, "__dict is a reserved member name");
124 return NULL;
125 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 if (v == NULL)
127 return dictremove(m->md_dict, name);
128 else
129 return dictinsert(m->md_dict, name, v);
130}
131
132typeobject Moduletype = {
133 OB_HEAD_INIT(&Typetype)
134 0, /*ob_size*/
135 "module", /*tp_name*/
136 sizeof(moduleobject), /*tp_size*/
137 0, /*tp_itemsize*/
138 moduledealloc, /*tp_dealloc*/
139 moduleprint, /*tp_print*/
140 modulegetattr, /*tp_getattr*/
141 modulesetattr, /*tp_setattr*/
142 0, /*tp_compare*/
143 modulerepr, /*tp_repr*/
144};