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