blob: 8e94170be3a4be43e4aa84b1cf7b3fba7b1d5167 [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");
45}
46
47object *
48new_module(name)
49 char *name;
50{
51 object *m;
52 object *mtab;
53 mtab = sysget("modules");
54 if (mtab == NULL || !is_dictobject(mtab)) {
55 errno = EBADF;
56 return NULL;
57 }
58 m = newmoduleobject(name);
59 if (m == NULL)
60 return NULL;
61 if (dictinsert(mtab, name, m) != 0) {
62 DECREF(m);
63 return NULL;
64 }
65 return m;
66}
67
68void
69define_module(ctx, name)
70 context *ctx;
71 char *name;
72{
73 object *m, *d;
74 m = new_module(name);
75 if (m == NULL) {
76 puterrno(ctx);
77 return;
78 }
79 d = getmoduledict(m);
80 INCREF(d);
81 DECREF(ctx->ctx_locals);
82 ctx->ctx_locals = d;
83 INCREF(d);
84 DECREF(ctx->ctx_globals);
85 ctx->ctx_globals = d;
86 DECREF(m);
87}
88
89object *
90parsepath(path, delim)
91 char *path;
92 int delim;
93{
94 int i, n;
95 char *p;
96 object *v, *w;
97
98 n = 1;
99 p = path;
100 while ((p = strchr(p, delim)) != NULL) {
101 n++;
102 p++;
103 }
104 v = newlistobject(n);
105 if (v == NULL)
106 return NULL;
107 for (i = 0; ; i++) {
108 p = strchr(path, delim);
109 if (p == NULL)
110 p = strchr(path, '\0'); /* End of string */
111 w = newsizedstringobject(path, (int) (p - path));
112 if (w == NULL) {
113 DECREF(v);
114 return NULL;
115 }
116 setlistitem(v, i, w);
117 if (*p == '\0')
118 break;
119 path = p+1;
120 }
121 return v;
122}
123
124void
125setpythonpath(path)
126 char *path;
127{
128 object *v;
129 if ((v = parsepath(path, DELIM)) != NULL) {
130 if (sysset("path", v) != 0)
131 fatal("can't assign sys.path");
132 DECREF(v);
133 }
134}
135
136static FILE *
137open_module(name, suffix)
138 char *name;
139 char *suffix;
140{
141 object *path;
142 char namebuf[256];
143 FILE *fp;
144
145 path = sysget("path");
146 if (path == NULL || !is_listobject(path)) {
147 strcpy(namebuf, name);
148 strcat(namebuf, suffix);
149 fp = fopen(namebuf, "r");
150 }
151 else {
152 int npath = getlistsize(path);
153 int i;
154 fp = NULL;
155 for (i = 0; i < npath; i++) {
156 object *v = getlistitem(path, i);
157 int len;
158 if (!is_stringobject(v))
159 continue;
160 strcpy(namebuf, getstringvalue(v));
161 len = getstringsize(v);
162 if (len > 0 && namebuf[len-1] != SEP)
163 namebuf[len++] = SEP;
164 strcpy(namebuf+len, name); /* XXX check for overflow */
165 strcat(namebuf, suffix); /* XXX ditto */
166 fp = fopen(namebuf, "r");
167 if (fp != NULL)
168 break;
169 }
170 }
171 return fp;
172}
173
174static object *
175load_module(ctx, name)
176 context *ctx;
177 char *name;
178{
179 object *m;
180 char **p;
181 FILE *fp;
182 node *n, *mh;
183 int err;
184 object *mtab;
185 object *save_locals, *save_globals;
186
187 mtab = sysget("modules");
188 if (mtab == NULL || !is_dictobject(mtab)) {
189 errno = EBADF;
190 return NULL;
191 }
192 fp = open_module(name, ".py");
193 if (fp == NULL) {
194 /* XXX Compatibility hack */
195 fprintf(stderr,
196 "Can't find '%s.py' in sys.path, trying '%s'\n",
197 name, name);
198 fp = open_module(name, "");
199 }
200 if (fp == NULL) {
201 name_error(ctx, name);
202 return NULL;
203 }
204#ifdef DEBUG
205 fprintf(stderr, "Reading module %s from file '%s'\n", name, namebuf);
206#endif
207 err = parseinput(fp, file_input, &n);
208 fclose(fp);
209 if (err != E_DONE) {
210 input_error(ctx, err);
211 return NULL;
212 }
213 save_locals = ctx->ctx_locals;
214 INCREF(save_locals);
215 save_globals = ctx->ctx_globals;
216 INCREF(save_globals);
217 define_module(ctx, name);
218 exec_node(ctx, n);
219 DECREF(ctx->ctx_locals);
220 ctx->ctx_locals = save_locals;
221 DECREF(ctx->ctx_globals);
222 ctx->ctx_globals = save_globals;
223 /* XXX need to free the tree n here; except referenced defs */
224 if (ctx->ctx_exception) {
225 dictremove(mtab, name); /* Undefine the module */
226 return NULL;
227 }
228 m = dictlookup(mtab, name);
229 if (m == NULL) {
230 error(ctx, "module not defined after loading");
231 return NULL;
232 }
233 return m;
234}
235
236object *
237import_module(ctx, name)
238 context *ctx;
239 char *name;
240{
241 object *m;
242 object *mtab;
243 mtab = sysget("modules");
244 if (mtab == NULL || !is_dictobject(mtab)) {
245 error(ctx, "bad sys.modules");
246 return NULL;
247 }
248 if ((m = dictlookup(mtab, name)) == NULL) {
249 m = load_module(ctx, name);
250 }
251 return m;
252}