Mercurial > cpython-lru_cache-weakref
annotate Python/import.c @ 208:f352ea4dff70 legacy-trunk
Initial revision
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 16 Apr 1991 08:45:40 +0000 |
| parents | 28923e44254c |
| children | 634c1d3c7d39 |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
1 /*********************************************************** |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
3 Netherlands. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
4 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
5 All Rights Reserved |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
6 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
9 provided that the above copyright notice appear in all copies and that |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
11 supporting documentation, and that the names of Stichting Mathematisch |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
12 Centrum or CWI not be used in advertising or publicity pertaining to |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
13 distribution of the software without specific, written prior permission. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
14 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
22 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
23 ******************************************************************/ |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
24 |
| 6 | 25 /* Module definition and import implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
| 6 | 28 |
| 29 #include "node.h" | |
| 30 #include "token.h" | |
| 31 #include "graminit.h" | |
| 32 #include "import.h" | |
| 33 #include "errcode.h" | |
| 34 #include "sysmodule.h" | |
| 86 | 35 #include "pythonrun.h" |
| 6 | 36 |
| 86 | 37 /* Define pathname separator used in file names */ |
| 6 | 38 |
| 39 #ifdef THINK_C | |
| 40 #define SEP ':' | |
| 41 #endif | |
| 42 | |
| 43 #ifndef SEP | |
| 44 #define SEP '/' | |
| 45 #endif | |
| 46 | |
| 86 | 47 static object *modules; |
| 48 | |
|
166
28923e44254c
Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
49 /* Forward */ |
|
28923e44254c
Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
50 static int init_builtin PROTO((char *)); |
|
28923e44254c
Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
51 |
| 86 | 52 /* Initialization */ |
| 6 | 53 |
| 54 void | |
| 55 initimport() | |
| 56 { | |
| 86 | 57 if ((modules = newdictobject()) == NULL) |
| 58 fatal("no mem for dictionary of modules"); | |
| 6 | 59 } |
| 60 | |
| 61 object * | |
| 86 | 62 get_modules() |
| 6 | 63 { |
| 86 | 64 return modules; |
| 6 | 65 } |
| 66 | |
| 86 | 67 object * |
| 68 add_module(name) | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
69 char *name; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
70 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
71 object *m; |
| 86 | 72 if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m)) |
| 73 return m; | |
| 74 m = newmoduleobject(name); | |
| 75 if (m == NULL) | |
| 6 | 76 return NULL; |
| 86 | 77 if (dictinsert(modules, name, m) != 0) { |
| 78 DECREF(m); | |
| 79 return NULL; | |
| 6 | 80 } |
| 86 | 81 DECREF(m); /* Yes, it still exists, in modules! */ |
| 82 return m; | |
| 6 | 83 } |
| 84 | |
| 85 static FILE * | |
| 86 | 86 open_module(name, suffix, namebuf) |
| 6 | 87 char *name; |
| 88 char *suffix; | |
| 86 | 89 char *namebuf; /* XXX No buffer overflow checks! */ |
| 6 | 90 { |
| 91 object *path; | |
| 92 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; | |
| 86 | 113 strcpy(namebuf+len, name); |
| 114 strcat(namebuf, suffix); | |
| 6 | 115 fp = fopen(namebuf, "r"); |
| 116 if (fp != NULL) | |
| 117 break; | |
| 118 } | |
| 119 } | |
| 120 return fp; | |
| 121 } | |
| 122 | |
| 123 static object * | |
| 86 | 124 get_module(m, name, m_ret) |
| 125 /*module*/object *m; | |
| 6 | 126 char *name; |
| 86 | 127 object **m_ret; |
| 6 | 128 { |
| 86 | 129 object *d; |
| 6 | 130 FILE *fp; |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
131 node *n; |
| 6 | 132 int err; |
| 86 | 133 char namebuf[256]; |
| 6 | 134 |
| 86 | 135 fp = open_module(name, ".py", namebuf); |
| 136 if (fp == NULL) { | |
| 137 if (m == NULL) | |
| 138 err_setstr(NameError, name); | |
| 139 else | |
| 140 err_setstr(RuntimeError, "no module source file"); | |
| 6 | 141 return NULL; |
| 142 } | |
| 86 | 143 err = parse_file(fp, namebuf, file_input, &n); |
| 6 | 144 fclose(fp); |
| 145 if (err != E_DONE) { | |
| 86 | 146 err_input(err); |
| 6 | 147 return NULL; |
| 148 } | |
| 86 | 149 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 | |
| 161 static object * | |
| 162 load_module(name) | |
| 163 char *name; | |
| 164 { | |
| 165 object *m, *v; | |
| 166 v = get_module((object *)NULL, name, &m); | |
| 167 if (v == NULL) | |
| 6 | 168 return NULL; |
| 86 | 169 DECREF(v); |
| 6 | 170 return m; |
| 171 } | |
| 172 | |
| 173 object * | |
| 86 | 174 import_module(name) |
| 6 | 175 char *name; |
| 176 { | |
| 177 object *m; | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
178 if ((m = dictlookup(modules, name)) == NULL) { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
179 if (init_builtin(name)) { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
180 if ((m = dictlookup(modules, name)) == NULL) |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
181 err_setstr(SystemError, "builtin module missing"); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
182 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
183 else { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
184 m = load_module(name); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
185 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
186 } |
| 6 | 187 return m; |
| 188 } | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
189 |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
190 object * |
| 86 | 191 reload_module(m) |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
192 object *m; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
193 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
194 if (m == NULL || !is_moduleobject(m)) { |
| 86 | 195 err_setstr(TypeError, "reload() argument must be module"); |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
196 return NULL; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
197 } |
| 86 | 198 /* XXX Ought to check for builtin modules -- can't reload these... */ |
| 199 return get_module(m, getmodulename(m), (object **)NULL); | |
| 200 } | |
| 201 | |
| 202 static void | |
| 203 cleardict(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); | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
212 } |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
213 } |
| 86 | 214 |
| 215 void | |
| 216 doneimport() | |
| 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); | |
| 240 } | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
241 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
242 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
243 /* Initialize built-in modules when first imported */ |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
244 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
245 extern struct { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
246 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
247 void (*initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
248 } inittab[]; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
249 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
250 static int |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
251 init_builtin(name) |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
252 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
253 { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
254 int i; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
255 for (i = 0; inittab[i].name != NULL; i++) { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
256 if (strcmp(name, inittab[i].name) == 0) { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
257 (*inittab[i].initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
258 return 1; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
259 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
260 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
261 return 0; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
262 } |
