blob: d1da0015824f7c3ec28df6a69653e98f46e710fe [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
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 definition and import implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030#include "token.h"
31#include "graminit.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032#include "import.h"
33#include "errcode.h"
34#include "sysmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035#include "pythonrun.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
Guido van Rossum3f5da241990-12-20 15:06:42 +000037/* Define pathname separator used in file names */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038
39#ifdef THINK_C
40#define SEP ':'
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041#endif
42
43#ifndef SEP
44#define SEP '/'
45#endif
46
Guido van Rossum3f5da241990-12-20 15:06:42 +000047static object *modules;
48
Guido van Rossum66f1fa81991-04-03 19:03:52 +000049/* Forward */
50static int init_builtin PROTO((char *));
51
Guido van Rossum3f5da241990-12-20 15:06:42 +000052/* Initialization */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053
54void
55initimport()
56{
Guido van Rossum3f5da241990-12-20 15:06:42 +000057 if ((modules = newdictobject()) == NULL)
58 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059}
60
61object *
Guido van Rossum3f5da241990-12-20 15:06:42 +000062get_modules()
63{
64 return modules;
65}
66
67object *
68add_module(name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 char *name;
70{
71 object *m;
Guido van Rossum3f5da241990-12-20 15:06:42 +000072 if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m))
73 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 m = newmoduleobject(name);
75 if (m == NULL)
76 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000077 if (dictinsert(modules, name, m) != 0) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 DECREF(m);
79 return NULL;
80 }
Guido van Rossum3f5da241990-12-20 15:06:42 +000081 DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 return m;
83}
84
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085static FILE *
Guido van Rossum3f5da241990-12-20 15:06:42 +000086open_module(name, suffix, namebuf)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087 char *name;
88 char *suffix;
Guido van Rossum3f5da241990-12-20 15:06:42 +000089 char *namebuf; /* XXX No buffer overflow checks! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
91 object *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 FILE *fp;
93
94 path = sysget("path");
95 if (path == NULL || !is_listobject(path)) {
96 strcpy(namebuf, name);
97 strcat(namebuf, suffix);
98 fp = fopen(namebuf, "r");
99 }
100 else {
101 int npath = getlistsize(path);
102 int i;
103 fp = NULL;
104 for (i = 0; i < npath; i++) {
105 object *v = getlistitem(path, i);
106 int len;
107 if (!is_stringobject(v))
108 continue;
109 strcpy(namebuf, getstringvalue(v));
110 len = getstringsize(v);
111 if (len > 0 && namebuf[len-1] != SEP)
112 namebuf[len++] = SEP;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113 strcpy(namebuf+len, name);
114 strcat(namebuf, suffix);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 fp = fopen(namebuf, "r");
116 if (fp != NULL)
117 break;
118 }
119 }
120 return fp;
121}
122
123static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124get_module(m, name, m_ret)
125 /*module*/object *m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127 object **m_ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000129 object *d;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000130 FILE *fp;
131 node *n;
132 int err;
133 char namebuf[256];
134
135 fp = open_module(name, ".py", namebuf);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000136 if (fp == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000137 if (m == NULL)
138 err_setstr(NameError, name);
139 else
140 err_setstr(RuntimeError, "no module source file");
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000141 return NULL;
142 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000143 err = parse_file(fp, namebuf, file_input, &n);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000144 fclose(fp);
145 if (err != E_DONE) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146 err_input(err);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000147 return NULL;
148 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000149 if (m == NULL) {
150 m = add_module(name);
151 if (m == NULL) {
152 freetree(n);
153 return NULL;
154 }
155 *m_ret = m;
156 }
157 d = getmoduledict(m);
158 return run_node(n, namebuf, d, d);
159}
160
161static object *
162load_module(name)
163 char *name;
164{
165 object *m, *v;
166 v = get_module((object *)NULL, name, &m);
167 if (v == NULL)
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000168 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000169 DECREF(v);
170 return m;
171}
172
173object *
174import_module(name)
175 char *name;
176{
177 object *m;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000178 if ((m = dictlookup(modules, name)) == NULL) {
179 if (init_builtin(name)) {
180 if ((m = dictlookup(modules, name)) == NULL)
181 err_setstr(SystemError, "builtin module missing");
182 }
183 else {
184 m = load_module(name);
185 }
186 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000187 return m;
188}
189
190object *
191reload_module(m)
192 object *m;
193{
194 if (m == NULL || !is_moduleobject(m)) {
195 err_setstr(TypeError, "reload() argument must be module");
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000196 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000197 }
198 /* XXX Ought to check for builtin modules -- can't reload these... */
199 return get_module(m, getmodulename(m), (object **)NULL);
200}
201
202static void
203cleardict(d)
204 object *d;
205{
206 int i;
207 for (i = getdictsize(d); --i >= 0; ) {
208 char *k;
209 k = getdictkey(d, i);
210 if (k != NULL)
211 (void) dictremove(d, k);
212 }
213}
214
215void
216doneimport()
217{
218 if (modules != NULL) {
219 int i;
220 /* Explicitly erase all modules; this is the safest way
221 to get rid of at least *some* circular dependencies */
222 for (i = getdictsize(modules); --i >= 0; ) {
223 char *k;
224 k = getdictkey(modules, i);
225 if (k != NULL) {
226 object *m;
227 m = dictlookup(modules, k);
228 if (m != NULL && is_moduleobject(m)) {
229 object *d;
230 d = getmoduledict(m);
231 if (d != NULL && is_dictobject(d)) {
232 cleardict(d);
233 }
234 }
235 }
236 }
237 cleardict(modules);
238 }
239 DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000240}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000241
242
243/* Initialize built-in modules when first imported */
244
245extern struct {
246 char *name;
247 void (*initfunc)();
248} inittab[];
249
250static int
251init_builtin(name)
252 char *name;
253{
254 int i;
255 for (i = 0; inittab[i].name != NULL; i++) {
256 if (strcmp(name, inittab[i].name) == 0) {
257 (*inittab[i].initfunc)();
258 return 1;
259 }
260 }
261 return 0;
262}