Mercurial > cpython-lru_cache-weakref
annotate Python/import.c @ 595:f9cdeebfcb67 legacy-trunk
New version from the net.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 14 Jan 1992 18:42:23 +0000 |
| parents | cbcdfda05bc4 |
| children | 0ad5b35e3ad8 |
| 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) { | |
|
510
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
152 if (m == NULL) { |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
153 sprintf(namebuf, "no module named %.200s", name); |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
154 err_setstr(ImportError, namebuf); |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
155 } |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
156 else { |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
157 sprintf(namebuf, "no source for module %.200s", name); |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
158 err_setstr(ImportError, namebuf); |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
159 } |
| 6 | 160 return NULL; |
| 161 } | |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
162 /* Get mtime -- always useful */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
163 mtime = getmtime(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
164 /* Check ".pyc" file first */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
165 namelen = strlen(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
166 namebuf[namelen] = 'c'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
167 namebuf[namelen+1] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
168 fpc = fopen(namebuf, "rb"); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
169 if (fpc != NULL) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
170 long pyc_mtime; |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
171 long magic; |
|
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
172 magic = rd_long(fpc); |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
173 pyc_mtime = rd_long(fpc); |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
174 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
|
175 v = rd_object(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
176 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
|
177 err_clear(); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
178 XDECREF(v); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
179 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
180 else |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
181 co = (codeobject *)v; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
182 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
183 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
184 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
185 namebuf[namelen] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
186 if (co == NULL) |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
187 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
|
188 else |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
189 err = E_DONE; |
| 6 | 190 fclose(fp); |
| 191 if (err != E_DONE) { | |
| 86 | 192 err_input(err); |
| 6 | 193 return NULL; |
| 194 } | |
| 86 | 195 if (m == NULL) { |
| 196 m = add_module(name); | |
| 197 if (m == NULL) { | |
| 198 freetree(n); | |
| 199 return NULL; | |
| 200 } | |
| 201 *m_ret = m; | |
| 202 } | |
| 203 d = getmoduledict(m); | |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
204 if (co == NULL) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
205 co = compile(n, namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
206 freetree(n); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
207 if (co == NULL) |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
208 return NULL; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
209 /* 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
|
210 namebuf[namelen] = 'c'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
211 namebuf[namelen+1] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
212 fpc = fopen(namebuf, "wb"); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
213 if (fpc != NULL) { |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
214 wr_long(MAGIC, fpc); |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
215 /* First write a 0 for mtime */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
216 wr_long(0L, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
217 wr_object((object *)co, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
218 if (ferror(fpc)) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
219 /* Don't keep partial file */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
220 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
221 (void) unlink(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
222 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
223 else { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
224 /* Now write the true mtime */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
225 fseek(fpc, 4L, 0); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
226 wr_long(mtime, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
227 fflush(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
228 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
229 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
230 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
231 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
232 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
|
233 DECREF(co); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
234 return v; |
| 86 | 235 } |
| 236 | |
| 237 static object * | |
| 238 load_module(name) | |
| 239 char *name; | |
| 240 { | |
| 241 object *m, *v; | |
| 242 v = get_module((object *)NULL, name, &m); | |
| 243 if (v == NULL) | |
| 6 | 244 return NULL; |
| 86 | 245 DECREF(v); |
| 6 | 246 return m; |
| 247 } | |
| 248 | |
| 249 object * | |
| 86 | 250 import_module(name) |
| 6 | 251 char *name; |
| 252 { | |
| 253 object *m; | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
254 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
|
255 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
|
256 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
|
257 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
|
258 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
259 else { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
260 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
|
261 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
262 } |
| 6 | 263 return m; |
| 264 } | |
|
40
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 object * |
| 86 | 267 reload_module(m) |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
268 object *m; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
269 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
270 if (m == NULL || !is_moduleobject(m)) { |
| 86 | 271 err_setstr(TypeError, "reload() argument must be module"); |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
272 return NULL; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
273 } |
| 86 | 274 /* XXX Ought to check for builtin modules -- can't reload these... */ |
| 275 return get_module(m, getmodulename(m), (object **)NULL); | |
| 276 } | |
| 277 | |
| 278 static void | |
| 279 cleardict(d) | |
| 280 object *d; | |
| 281 { | |
| 282 int i; | |
| 283 for (i = getdictsize(d); --i >= 0; ) { | |
| 284 char *k; | |
| 285 k = getdictkey(d, i); | |
| 286 if (k != NULL) | |
| 287 (void) dictremove(d, k); | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
288 } |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
289 } |
| 86 | 290 |
| 291 void | |
| 292 doneimport() | |
| 293 { | |
| 294 if (modules != NULL) { | |
| 295 int i; | |
| 296 /* Explicitly erase all modules; this is the safest way | |
| 297 to get rid of at least *some* circular dependencies */ | |
| 298 for (i = getdictsize(modules); --i >= 0; ) { | |
|
340
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
299 object *k; |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
300 k = getdict2key(modules, i); |
| 86 | 301 if (k != NULL) { |
| 302 object *m; | |
|
340
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
303 m = dict2lookup(modules, k); |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
304 if (m == NULL) |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
305 err_clear(); |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
306 else if (is_moduleobject(m)) { |
| 86 | 307 object *d; |
| 308 d = getmoduledict(m); | |
| 309 if (d != NULL && is_dictobject(d)) { | |
| 310 cleardict(d); | |
| 311 } | |
| 312 } | |
| 313 } | |
| 314 } | |
| 315 cleardict(modules); | |
| 316 } | |
| 317 DECREF(modules); | |
| 318 } | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
319 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
320 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
321 /* 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
|
322 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
323 extern struct { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
324 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
325 void (*initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
326 } inittab[]; |
|
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 static int |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
329 init_builtin(name) |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
330 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
331 { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
332 int i; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
333 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
|
334 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
|
335 (*inittab[i].initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
336 return 1; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
337 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
338 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
339 return 0; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
340 } |
