Mercurial > cpython-lru_cache-weakref
annotate Python/import.c @ 263:28f0ffd9facf legacy-trunk
Support ".pyc" files: cached compilation results.
(Similar to Emacs ".elc" files.)
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 04 Jun 1991 19:39:42 +0000 |
| parents | 634c1d3c7d39 |
| children | f900b692b37e |
| 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 |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
40 #ifdef THINK_C |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
41 #define macintosh |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
42 #endif |
| 6 | 43 |
| 86 | 44 /* Define pathname separator used in file names */ |
| 6 | 45 |
| 46 #ifdef THINK_C | |
| 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 | |
| 155 err_setstr(RuntimeError, "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; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
167 (void) rd_long(fpc); /* Reserved for magic word */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
168 pyc_mtime = rd_long(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
169 if (pyc_mtime != 0 && pyc_mtime != -1 && pyc_mtime == mtime) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
170 v = rd_object(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
171 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
|
172 err_clear(); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
173 XDECREF(v); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
174 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
175 else |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
176 co = (codeobject *)v; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
177 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
178 fclose(fpc); |
|
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 namebuf[namelen] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
181 if (co == NULL) |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
182 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
|
183 else |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
184 err = E_DONE; |
| 6 | 185 fclose(fp); |
| 186 if (err != E_DONE) { | |
| 86 | 187 err_input(err); |
| 6 | 188 return NULL; |
| 189 } | |
| 86 | 190 if (m == NULL) { |
| 191 m = add_module(name); | |
| 192 if (m == NULL) { | |
| 193 freetree(n); | |
| 194 return NULL; | |
| 195 } | |
| 196 *m_ret = m; | |
| 197 } | |
| 198 d = getmoduledict(m); | |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
199 if (co == NULL) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
200 co = compile(n, namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
201 freetree(n); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
202 if (co == NULL) |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
203 return NULL; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
204 /* 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
|
205 namebuf[namelen] = 'c'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
206 namebuf[namelen+1] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
207 fpc = fopen(namebuf, "wb"); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
208 if (fpc != NULL) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
209 wr_long(0L, fpc); /* Reserved for magic word */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
210 /* First write a 0 for mtime */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
211 wr_long(0L, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
212 wr_object((object *)co, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
213 if (ferror(fpc)) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
214 /* Don't keep partial file */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
215 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
216 (void) unlink(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
217 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
218 else { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
219 /* Now write the true mtime */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
220 fseek(fpc, 4L, 0); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
221 wr_long(mtime, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
222 fflush(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
223 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
224 } |
|
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 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
|
228 DECREF(co); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
229 return v; |
| 86 | 230 } |
| 231 | |
| 232 static object * | |
| 233 load_module(name) | |
| 234 char *name; | |
| 235 { | |
| 236 object *m, *v; | |
| 237 v = get_module((object *)NULL, name, &m); | |
| 238 if (v == NULL) | |
| 6 | 239 return NULL; |
| 86 | 240 DECREF(v); |
| 6 | 241 return m; |
| 242 } | |
| 243 | |
| 244 object * | |
| 86 | 245 import_module(name) |
| 6 | 246 char *name; |
| 247 { | |
| 248 object *m; | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 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
|
253 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
254 else { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
255 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
|
256 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
257 } |
| 6 | 258 return m; |
| 259 } | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
260 |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
261 object * |
| 86 | 262 reload_module(m) |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
263 object *m; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
264 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
265 if (m == NULL || !is_moduleobject(m)) { |
| 86 | 266 err_setstr(TypeError, "reload() argument must be module"); |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
267 return NULL; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
268 } |
| 86 | 269 /* XXX Ought to check for builtin modules -- can't reload these... */ |
| 270 return get_module(m, getmodulename(m), (object **)NULL); | |
| 271 } | |
| 272 | |
| 273 static void | |
| 274 cleardict(d) | |
| 275 object *d; | |
| 276 { | |
| 277 int i; | |
| 278 for (i = getdictsize(d); --i >= 0; ) { | |
| 279 char *k; | |
| 280 k = getdictkey(d, i); | |
| 281 if (k != NULL) | |
| 282 (void) dictremove(d, k); | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
283 } |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
284 } |
| 86 | 285 |
| 286 void | |
| 287 doneimport() | |
| 288 { | |
| 289 if (modules != NULL) { | |
| 290 int i; | |
| 291 /* Explicitly erase all modules; this is the safest way | |
| 292 to get rid of at least *some* circular dependencies */ | |
| 293 for (i = getdictsize(modules); --i >= 0; ) { | |
| 294 char *k; | |
| 295 k = getdictkey(modules, i); | |
| 296 if (k != NULL) { | |
| 297 object *m; | |
| 298 m = dictlookup(modules, k); | |
| 299 if (m != NULL && is_moduleobject(m)) { | |
| 300 object *d; | |
| 301 d = getmoduledict(m); | |
| 302 if (d != NULL && is_dictobject(d)) { | |
| 303 cleardict(d); | |
| 304 } | |
| 305 } | |
| 306 } | |
| 307 } | |
| 308 cleardict(modules); | |
| 309 } | |
| 310 DECREF(modules); | |
| 311 } | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
312 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
313 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
314 /* 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
|
315 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
316 extern struct { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
317 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
318 void (*initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
319 } inittab[]; |
|
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 static int |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
322 init_builtin(name) |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
323 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
324 { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
325 int i; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
326 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
|
327 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
|
328 (*inittab[i].initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
329 return 1; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
330 } |
|
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 return 0; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
333 } |
