Mercurial > cpython-lru_cache-weakref
annotate Python/import.c @ 507:fbff2e7cc49d legacy-trunk
EOF while parsing should raise EOFError, not SyntaxError.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 24 Dec 1991 13:26:15 +0000 |
| parents | 2ce6a2a31886 |
| children | cbcdfda05bc4 |
| 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" |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
36 #include "marshal.h" |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
37 #include "compile.h" |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
38 #include "ceval.h" |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
39 |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
40 /* Magic word to reject pre-0.9.4 .pyc files */ |
|
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
41 |
|
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
42 #define MAGIC 0x949494L |
|
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
43 |
| 86 | 44 /* Define pathname separator used in file names */ |
| 6 | 45 |
|
293
f900b692b37e
Generalize to macintosh.
Guido van Rossum <guido@python.org>
parents:
263
diff
changeset
|
46 #ifdef macintosh |
| 6 | 47 #define SEP ':' |
| 48 #endif | |
| 49 | |
|
228
634c1d3c7d39
Pre-define MS-DOS separator
Guido van Rossum <guido@python.org>
parents:
166
diff
changeset
|
50 #ifdef MSDOS |
|
634c1d3c7d39
Pre-define MS-DOS separator
Guido van Rossum <guido@python.org>
parents:
166
diff
changeset
|
51 #define SEP '\\' |
|
634c1d3c7d39
Pre-define MS-DOS separator
Guido van Rossum <guido@python.org>
parents:
166
diff
changeset
|
52 #endif |
|
634c1d3c7d39
Pre-define MS-DOS separator
Guido van Rossum <guido@python.org>
parents:
166
diff
changeset
|
53 |
| 6 | 54 #ifndef SEP |
| 55 #define SEP '/' | |
| 56 #endif | |
| 57 | |
| 86 | 58 static object *modules; |
| 59 | |
|
166
28923e44254c
Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
60 /* Forward */ |
|
28923e44254c
Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
61 static int init_builtin PROTO((char *)); |
|
28923e44254c
Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
62 |
| 86 | 63 /* Initialization */ |
| 6 | 64 |
| 65 void | |
| 66 initimport() | |
| 67 { | |
| 86 | 68 if ((modules = newdictobject()) == NULL) |
| 69 fatal("no mem for dictionary of modules"); | |
| 6 | 70 } |
| 71 | |
| 72 object * | |
| 86 | 73 get_modules() |
| 6 | 74 { |
| 86 | 75 return modules; |
| 6 | 76 } |
| 77 | |
| 86 | 78 object * |
| 79 add_module(name) | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
80 char *name; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
81 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
82 object *m; |
| 86 | 83 if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m)) |
| 84 return m; | |
| 85 m = newmoduleobject(name); | |
| 86 if (m == NULL) | |
| 6 | 87 return NULL; |
| 86 | 88 if (dictinsert(modules, name, m) != 0) { |
| 89 DECREF(m); | |
| 90 return NULL; | |
| 6 | 91 } |
| 86 | 92 DECREF(m); /* Yes, it still exists, in modules! */ |
| 93 return m; | |
| 6 | 94 } |
| 95 | |
| 96 static FILE * | |
| 86 | 97 open_module(name, suffix, namebuf) |
| 6 | 98 char *name; |
| 99 char *suffix; | |
| 86 | 100 char *namebuf; /* XXX No buffer overflow checks! */ |
| 6 | 101 { |
| 102 object *path; | |
| 103 FILE *fp; | |
| 104 | |
| 105 path = sysget("path"); | |
| 106 if (path == NULL || !is_listobject(path)) { | |
| 107 strcpy(namebuf, name); | |
| 108 strcat(namebuf, suffix); | |
| 109 fp = fopen(namebuf, "r"); | |
| 110 } | |
| 111 else { | |
| 112 int npath = getlistsize(path); | |
| 113 int i; | |
| 114 fp = NULL; | |
| 115 for (i = 0; i < npath; i++) { | |
| 116 object *v = getlistitem(path, i); | |
| 117 int len; | |
| 118 if (!is_stringobject(v)) | |
| 119 continue; | |
| 120 strcpy(namebuf, getstringvalue(v)); | |
| 121 len = getstringsize(v); | |
| 122 if (len > 0 && namebuf[len-1] != SEP) | |
| 123 namebuf[len++] = SEP; | |
| 86 | 124 strcpy(namebuf+len, name); |
| 125 strcat(namebuf, suffix); | |
| 6 | 126 fp = fopen(namebuf, "r"); |
| 127 if (fp != NULL) | |
| 128 break; | |
| 129 } | |
| 130 } | |
| 131 return fp; | |
| 132 } | |
| 133 | |
| 134 static object * | |
| 86 | 135 get_module(m, name, m_ret) |
| 136 /*module*/object *m; | |
| 6 | 137 char *name; |
| 86 | 138 object **m_ret; |
| 6 | 139 { |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
140 codeobject *co = NULL; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
141 object *v, *d; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
142 FILE *fp, *fpc; |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
143 node *n; |
| 6 | 144 int err; |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
145 char namebuf[258]; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
146 int namelen; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
147 long mtime; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
148 extern long getmtime(); |
| 6 | 149 |
| 86 | 150 fp = open_module(name, ".py", namebuf); |
| 151 if (fp == NULL) { | |
| 152 if (m == NULL) | |
| 153 err_setstr(NameError, name); | |
| 154 else | |
|
497
2ce6a2a31886
Change RuntimeError into IOError when module file not found on reload.
Guido van Rossum <guido@python.org>
parents:
487
diff
changeset
|
155 err_setstr(IOError, "no module source file"); |
| 6 | 156 return NULL; |
| 157 } | |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
158 /* Get mtime -- always useful */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
159 mtime = getmtime(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
160 /* Check ".pyc" file first */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
161 namelen = strlen(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
162 namebuf[namelen] = 'c'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
163 namebuf[namelen+1] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
164 fpc = fopen(namebuf, "rb"); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
165 if (fpc != NULL) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
166 long pyc_mtime; |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
167 long magic; |
|
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
168 magic = rd_long(fpc); |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
169 pyc_mtime = rd_long(fpc); |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
170 if (magic == MAGIC && pyc_mtime == mtime && mtime != 0 && mtime != -1) { |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
171 v = rd_object(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
172 if (v == NULL || err_occurred() || !is_codeobject(v)) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
173 err_clear(); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
174 XDECREF(v); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
175 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
176 else |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
177 co = (codeobject *)v; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
178 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
179 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
180 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
181 namebuf[namelen] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
182 if (co == NULL) |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
183 err = parse_file(fp, namebuf, file_input, &n); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
184 else |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
185 err = E_DONE; |
| 6 | 186 fclose(fp); |
| 187 if (err != E_DONE) { | |
| 86 | 188 err_input(err); |
| 6 | 189 return NULL; |
| 190 } | |
| 86 | 191 if (m == NULL) { |
| 192 m = add_module(name); | |
| 193 if (m == NULL) { | |
| 194 freetree(n); | |
| 195 return NULL; | |
| 196 } | |
| 197 *m_ret = m; | |
| 198 } | |
| 199 d = getmoduledict(m); | |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
200 if (co == NULL) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
201 co = compile(n, namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
202 freetree(n); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
203 if (co == NULL) |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
204 return NULL; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
205 /* Now write the code object to the ".pyc" file */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
206 namebuf[namelen] = 'c'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
207 namebuf[namelen+1] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
208 fpc = fopen(namebuf, "wb"); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
209 if (fpc != NULL) { |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
210 wr_long(MAGIC, fpc); |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
211 /* First write a 0 for mtime */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
212 wr_long(0L, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
213 wr_object((object *)co, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
214 if (ferror(fpc)) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
215 /* Don't keep partial file */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
216 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
217 (void) unlink(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
218 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
219 else { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
220 /* Now write the true mtime */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
221 fseek(fpc, 4L, 0); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
222 wr_long(mtime, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
223 fflush(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
224 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
225 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
226 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
227 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
228 v = eval_code(co, d, d, (object *)NULL); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
229 DECREF(co); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
230 return v; |
| 86 | 231 } |
| 232 | |
| 233 static object * | |
| 234 load_module(name) | |
| 235 char *name; | |
| 236 { | |
| 237 object *m, *v; | |
| 238 v = get_module((object *)NULL, name, &m); | |
| 239 if (v == NULL) | |
| 6 | 240 return NULL; |
| 86 | 241 DECREF(v); |
| 6 | 242 return m; |
| 243 } | |
| 244 | |
| 245 object * | |
| 86 | 246 import_module(name) |
| 6 | 247 char *name; |
| 248 { | |
| 249 object *m; | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
250 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
|
251 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
|
252 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
|
253 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
|
254 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
255 else { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
256 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
|
257 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
258 } |
| 6 | 259 return m; |
| 260 } | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
261 |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
262 object * |
| 86 | 263 reload_module(m) |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
264 object *m; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
265 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
266 if (m == NULL || !is_moduleobject(m)) { |
| 86 | 267 err_setstr(TypeError, "reload() argument must be module"); |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
268 return NULL; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
269 } |
| 86 | 270 /* XXX Ought to check for builtin modules -- can't reload these... */ |
| 271 return get_module(m, getmodulename(m), (object **)NULL); | |
| 272 } | |
| 273 | |
| 274 static void | |
| 275 cleardict(d) | |
| 276 object *d; | |
| 277 { | |
| 278 int i; | |
| 279 for (i = getdictsize(d); --i >= 0; ) { | |
| 280 char *k; | |
| 281 k = getdictkey(d, i); | |
| 282 if (k != NULL) | |
| 283 (void) dictremove(d, k); | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
284 } |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
285 } |
| 86 | 286 |
| 287 void | |
| 288 doneimport() | |
| 289 { | |
| 290 if (modules != NULL) { | |
| 291 int i; | |
| 292 /* Explicitly erase all modules; this is the safest way | |
| 293 to get rid of at least *some* circular dependencies */ | |
| 294 for (i = getdictsize(modules); --i >= 0; ) { | |
|
340
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
295 object *k; |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
296 k = getdict2key(modules, i); |
| 86 | 297 if (k != NULL) { |
| 298 object *m; | |
|
340
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
299 m = dict2lookup(modules, k); |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
300 if (m == NULL) |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
301 err_clear(); |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
302 else if (is_moduleobject(m)) { |
| 86 | 303 object *d; |
| 304 d = getmoduledict(m); | |
| 305 if (d != NULL && is_dictobject(d)) { | |
| 306 cleardict(d); | |
| 307 } | |
| 308 } | |
| 309 } | |
| 310 } | |
| 311 cleardict(modules); | |
| 312 } | |
| 313 DECREF(modules); | |
| 314 } | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
315 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
316 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
317 /* 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
|
318 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
319 extern struct { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
320 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
321 void (*initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
322 } inittab[]; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
323 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
324 static int |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
325 init_builtin(name) |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
326 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
327 { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
328 int i; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
329 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
|
330 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
|
331 (*inittab[i].initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
332 return 1; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
333 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
334 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
335 return 0; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
336 } |
