blob: fd912ba27c98e2d8c5770ff4c9b733d4d59a646c [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Module definition and import implementation */
2
3#include <stdio.h>
4#include "string.h"
5
6#include "PROTO.h"
7#include "object.h"
8#include "stringobject.h"
9#include "listobject.h"
10#include "dictobject.h"
11#include "moduleobject.h"
12#include "node.h"
13#include "context.h"
14#include "token.h"
15#include "graminit.h"
16#include "run.h"
17#include "support.h"
18#include "import.h"
19#include "errcode.h"
20#include "sysmodule.h"
21
22/* Define pathname separator and delimiter in $PYTHONPATH */
23
24#ifdef THINK_C
25#define SEP ':'
26#define DELIM ' '
27#endif
28
29#ifndef SEP
30#define SEP '/'
31#endif
32
33#ifndef DELIM
34#define DELIM ':'
35#endif
36
37void
38initimport()
39{
40 object *v;
41 if ((v = newdictobject()) == NULL)
42 fatal("no mem for module table");
43 if (sysset("modules", v) != 0)
44 fatal("can't assign sys.modules");
Guido van Rossum33049751990-11-18 17:36:26 +000045 DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046}
47
48object *
49new_module(name)
50 char *name;
51{
52 object *m;
53 object *mtab;
54 mtab = sysget("modules");
55 if (mtab == NULL || !is_dictobject(mtab)) {
56 errno = EBADF;
57 return NULL;
58 }
59 m = newmoduleobject(name);
60 if (m == NULL)
61 return NULL;
62 if (dictinsert(mtab, name, m) != 0) {
63 DECREF(m);
64 return NULL;
65 }
66 return m;
67}
68
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000069static void
70use_module(ctx, d)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071 context *ctx;
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000072 object *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 INCREF(d);
75 DECREF(ctx->ctx_locals);
76 ctx->ctx_locals = d;
77 INCREF(d);
78 DECREF(ctx->ctx_globals);
79 ctx->ctx_globals = d;
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000080}
81
82static void
83define_module(ctx, name)
84 context *ctx;
85 char *name;
86{
87 object *m;
88 m = new_module(name);
89 if (m == NULL) {
90 puterrno(ctx);
91 return;
92 }
93 use_module(ctx, getmoduledict(m));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094 DECREF(m);
95}
96
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000097static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098parsepath(path, delim)
99 char *path;
100 int delim;
101{
102 int i, n;
103 char *p;
104 object *v, *w;
105
106 n = 1;
107 p = path;
108 while ((p = strchr(p, delim)) != NULL) {
109 n++;
110 p++;
111 }
112 v = newlistobject(n);
113 if (v == NULL)
114 return NULL;
115 for (i = 0; ; i++) {
116 p = strchr(path, delim);
117 if (p == NULL)
118 p = strchr(path, '\0'); /* End of string */
119 w = newsizedstringobject(path, (int) (p - path));
120 if (w == NULL) {
121 DECREF(v);
122 return NULL;
123 }
124 setlistitem(v, i, w);
125 if (*p == '\0')
126 break;
127 path = p+1;
128 }
129 return v;
130}
131
132void
133setpythonpath(path)
134 char *path;
135{
136 object *v;
137 if ((v = parsepath(path, DELIM)) != NULL) {
138 if (sysset("path", v) != 0)
139 fatal("can't assign sys.path");
140 DECREF(v);
141 }
142}
143
144static FILE *
145open_module(name, suffix)
146 char *name;
147 char *suffix;
148{
149 object *path;
150 char namebuf[256];
151 FILE *fp;
152
153 path = sysget("path");
154 if (path == NULL || !is_listobject(path)) {
155 strcpy(namebuf, name);
156 strcat(namebuf, suffix);
157 fp = fopen(namebuf, "r");
158 }
159 else {
160 int npath = getlistsize(path);
161 int i;
162 fp = NULL;
163 for (i = 0; i < npath; i++) {
164 object *v = getlistitem(path, i);
165 int len;
166 if (!is_stringobject(v))
167 continue;
168 strcpy(namebuf, getstringvalue(v));
169 len = getstringsize(v);
170 if (len > 0 && namebuf[len-1] != SEP)
171 namebuf[len++] = SEP;
172 strcpy(namebuf+len, name); /* XXX check for overflow */
173 strcat(namebuf, suffix); /* XXX ditto */
174 fp = fopen(namebuf, "r");
175 if (fp != NULL)
176 break;
177 }
178 }
179 return fp;
180}
181
182static object *
183load_module(ctx, name)
184 context *ctx;
185 char *name;
186{
187 object *m;
188 char **p;
189 FILE *fp;
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000190 node *n;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191 int err;
192 object *mtab;
193 object *save_locals, *save_globals;
194
195 mtab = sysget("modules");
196 if (mtab == NULL || !is_dictobject(mtab)) {
197 errno = EBADF;
198 return NULL;
199 }
200 fp = open_module(name, ".py");
201 if (fp == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202 name_error(ctx, name);
203 return NULL;
204 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205 err = parseinput(fp, file_input, &n);
206 fclose(fp);
207 if (err != E_DONE) {
208 input_error(ctx, err);
209 return NULL;
210 }
211 save_locals = ctx->ctx_locals;
212 INCREF(save_locals);
213 save_globals = ctx->ctx_globals;
214 INCREF(save_globals);
215 define_module(ctx, name);
216 exec_node(ctx, n);
217 DECREF(ctx->ctx_locals);
218 ctx->ctx_locals = save_locals;
219 DECREF(ctx->ctx_globals);
220 ctx->ctx_globals = save_globals;
221 /* XXX need to free the tree n here; except referenced defs */
222 if (ctx->ctx_exception) {
223 dictremove(mtab, name); /* Undefine the module */
224 return NULL;
225 }
226 m = dictlookup(mtab, name);
227 if (m == NULL) {
228 error(ctx, "module not defined after loading");
229 return NULL;
230 }
231 return m;
232}
233
234object *
235import_module(ctx, name)
236 context *ctx;
237 char *name;
238{
239 object *m;
240 object *mtab;
241 mtab = sysget("modules");
242 if (mtab == NULL || !is_dictobject(mtab)) {
243 error(ctx, "bad sys.modules");
244 return NULL;
245 }
246 if ((m = dictlookup(mtab, name)) == NULL) {
247 m = load_module(ctx, name);
248 }
249 return m;
250}
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000251
252object *
253reload_module(ctx, m)
254 context *ctx;
255 object *m;
256{
257 char *name;
258 FILE *fp;
259 node *n;
260 int err;
261 object *d;
262 object *save_locals, *save_globals;
263 if (m == NULL || !is_moduleobject(m)) {
264 type_error(ctx, "reload() argument must be module");
265 return NULL;
266 }
267 /* XXX Ought to check for builtin module */
268 name = getmodulename(m);
269 fp = open_module(name, ".py");
270 if (fp == NULL) {
271 error(ctx, "reload() cannot find module source file");
272 return NULL;
273 }
274 err = parseinput(fp, file_input, &n);
275 fclose(fp);
276 if (err != E_DONE) {
277 input_error(ctx, err);
278 return NULL;
279 }
280 d = newdictobject();
281 if (d == NULL)
282 return NULL;
283 setmoduledict(m, d);
284 save_locals = ctx->ctx_locals;
285 INCREF(save_locals);
286 save_globals = ctx->ctx_globals;
287 INCREF(save_globals);
288 use_module(ctx, d);
289 exec_node(ctx, n);
290 DECREF(ctx->ctx_locals);
291 ctx->ctx_locals = save_locals;
292 DECREF(ctx->ctx_globals);
293 ctx->ctx_globals = save_globals;
294 if (ctx->ctx_exception)
295 return NULL;
296 INCREF(None);
297 return None;
298}