Mercurial > cpython-lru_cache-weakref
annotate Python/import.c @ 160:afa48a96a444 legacy-trunk
Create code string with initial size of 1000 instead of 0.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 03 Apr 1991 19:00:23 +0000 |
| parents | 853b736a6a9e |
| children | 28923e44254c |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
1 /*********************************************************** |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
3 Netherlands. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
4 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
5 All Rights Reserved |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
6 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
9 provided that the above copyright notice appear in all copies and that |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
11 supporting documentation, and that the names of Stichting Mathematisch |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
12 Centrum or CWI not be used in advertising or publicity pertaining to |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
13 distribution of the software without specific, written prior permission. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
14 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
22 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
23 ******************************************************************/ |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
141
diff
changeset
|
24 |
| 6 | 25 /* Module definition and import implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
| 6 | 28 |
| 29 #include "node.h" | |
| 30 #include "token.h" | |
| 31 #include "graminit.h" | |
| 32 #include "import.h" | |
| 33 #include "errcode.h" | |
| 34 #include "sysmodule.h" | |
| 86 | 35 #include "pythonrun.h" |
| 6 | 36 |
| 86 | 37 /* Define pathname separator used in file names */ |
| 6 | 38 |
| 39 #ifdef THINK_C | |
| 40 #define SEP ':' | |
| 41 #endif | |
| 42 | |
| 43 #ifndef SEP | |
| 44 #define SEP '/' | |
| 45 #endif | |
| 46 | |
| 86 | 47 static object *modules; |
| 48 | |
| 49 /* Initialization */ | |
| 6 | 50 |
| 51 void | |
| 52 initimport() | |
| 53 { | |
| 86 | 54 if ((modules = newdictobject()) == NULL) |
| 55 fatal("no mem for dictionary of modules"); | |
| 6 | 56 } |
| 57 | |
| 58 object * | |
| 86 | 59 get_modules() |
| 6 | 60 { |
| 86 | 61 return modules; |
| 6 | 62 } |
| 63 | |
| 86 | 64 object * |
| 65 add_module(name) | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
66 char *name; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
67 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
68 object *m; |
| 86 | 69 if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m)) |
| 70 return m; | |
| 71 m = newmoduleobject(name); | |
| 72 if (m == NULL) | |
| 6 | 73 return NULL; |
| 86 | 74 if (dictinsert(modules, name, m) != 0) { |
| 75 DECREF(m); | |
| 76 return NULL; | |
| 6 | 77 } |
| 86 | 78 DECREF(m); /* Yes, it still exists, in modules! */ |
| 79 return m; | |
| 6 | 80 } |
| 81 | |
| 82 static FILE * | |
| 86 | 83 open_module(name, suffix, namebuf) |
| 6 | 84 char *name; |
| 85 char *suffix; | |
| 86 | 86 char *namebuf; /* XXX No buffer overflow checks! */ |
| 6 | 87 { |
| 88 object *path; | |
| 89 FILE *fp; | |
| 90 | |
| 91 path = sysget("path"); | |
| 92 if (path == NULL || !is_listobject(path)) { | |
| 93 strcpy(namebuf, name); | |
| 94 strcat(namebuf, suffix); | |
| 95 fp = fopen(namebuf, "r"); | |
| 96 } | |
| 97 else { | |
| 98 int npath = getlistsize(path); | |
| 99 int i; | |
| 100 fp = NULL; | |
| 101 for (i = 0; i < npath; i++) { | |
| 102 object *v = getlistitem(path, i); | |
| 103 int len; | |
| 104 if (!is_stringobject(v)) | |
| 105 continue; | |
| 106 strcpy(namebuf, getstringvalue(v)); | |
| 107 len = getstringsize(v); | |
| 108 if (len > 0 && namebuf[len-1] != SEP) | |
| 109 namebuf[len++] = SEP; | |
| 86 | 110 strcpy(namebuf+len, name); |
| 111 strcat(namebuf, suffix); | |
| 6 | 112 fp = fopen(namebuf, "r"); |
| 113 if (fp != NULL) | |
| 114 break; | |
| 115 } | |
| 116 } | |
| 117 return fp; | |
| 118 } | |
| 119 | |
| 120 static object * | |
| 86 | 121 get_module(m, name, m_ret) |
| 122 /*module*/object *m; | |
| 6 | 123 char *name; |
| 86 | 124 object **m_ret; |
| 6 | 125 { |
| 86 | 126 object *d; |
| 6 | 127 FILE *fp; |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
128 node *n; |
| 6 | 129 int err; |
| 86 | 130 char namebuf[256]; |
| 6 | 131 |
| 86 | 132 fp = open_module(name, ".py", namebuf); |
| 133 if (fp == NULL) { | |
| 134 if (m == NULL) | |
| 135 err_setstr(NameError, name); | |
| 136 else | |
| 137 err_setstr(RuntimeError, "no module source file"); | |
| 6 | 138 return NULL; |
| 139 } | |
| 86 | 140 err = parse_file(fp, namebuf, file_input, &n); |
| 6 | 141 fclose(fp); |
| 142 if (err != E_DONE) { | |
| 86 | 143 err_input(err); |
| 6 | 144 return NULL; |
| 145 } | |
| 86 | 146 if (m == NULL) { |
| 147 m = add_module(name); | |
| 148 if (m == NULL) { | |
| 149 freetree(n); | |
| 150 return NULL; | |
| 151 } | |
| 152 *m_ret = m; | |
| 153 } | |
| 154 d = getmoduledict(m); | |
| 155 return run_node(n, namebuf, d, d); | |
| 156 } | |
| 157 | |
| 158 static object * | |
| 159 load_module(name) | |
| 160 char *name; | |
| 161 { | |
| 162 object *m, *v; | |
| 163 v = get_module((object *)NULL, name, &m); | |
| 164 if (v == NULL) | |
| 6 | 165 return NULL; |
| 86 | 166 DECREF(v); |
| 6 | 167 return m; |
| 168 } | |
| 169 | |
| 170 object * | |
| 86 | 171 import_module(name) |
| 6 | 172 char *name; |
| 173 { | |
| 174 object *m; | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
175 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
|
176 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
|
177 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
|
178 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
|
179 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
180 else { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
181 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
|
182 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
183 } |
| 6 | 184 return m; |
| 185 } | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
186 |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
187 object * |
| 86 | 188 reload_module(m) |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
189 object *m; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
190 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
191 if (m == NULL || !is_moduleobject(m)) { |
| 86 | 192 err_setstr(TypeError, "reload() argument must be module"); |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
193 return NULL; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
194 } |
| 86 | 195 /* XXX Ought to check for builtin modules -- can't reload these... */ |
| 196 return get_module(m, getmodulename(m), (object **)NULL); | |
| 197 } | |
| 198 | |
| 199 static void | |
| 200 cleardict(d) | |
| 201 object *d; | |
| 202 { | |
| 203 int i; | |
| 204 for (i = getdictsize(d); --i >= 0; ) { | |
| 205 char *k; | |
| 206 k = getdictkey(d, i); | |
| 207 if (k != NULL) | |
| 208 (void) dictremove(d, k); | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
209 } |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
210 } |
| 86 | 211 |
| 212 void | |
| 213 doneimport() | |
| 214 { | |
| 215 if (modules != NULL) { | |
| 216 int i; | |
| 217 /* Explicitly erase all modules; this is the safest way | |
| 218 to get rid of at least *some* circular dependencies */ | |
| 219 for (i = getdictsize(modules); --i >= 0; ) { | |
| 220 char *k; | |
| 221 k = getdictkey(modules, i); | |
| 222 if (k != NULL) { | |
| 223 object *m; | |
| 224 m = dictlookup(modules, k); | |
| 225 if (m != NULL && is_moduleobject(m)) { | |
| 226 object *d; | |
| 227 d = getmoduledict(m); | |
| 228 if (d != NULL && is_dictobject(d)) { | |
| 229 cleardict(d); | |
| 230 } | |
| 231 } | |
| 232 } | |
| 233 } | |
| 234 cleardict(modules); | |
| 235 } | |
| 236 DECREF(modules); | |
| 237 } | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
238 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
239 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
240 /* 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
|
241 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
242 extern struct { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
243 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
244 void (*initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
245 } inittab[]; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
246 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
247 static int |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
248 init_builtin(name) |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
249 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
250 { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
251 int i; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
252 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
|
253 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
|
254 (*inittab[i].initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
255 return 1; |
|
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 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
258 return 0; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
259 } |
