Mercurial > cpython-lru_cache-weakref
annotate Python/import.c @ 141:bf5a8539d8ba legacy-trunk
Call the init function of a built-in module here.
,
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 19 Feb 1991 12:23:57 +0000 |
| parents | a6934380c6e7 |
| children | 853b736a6a9e |
| rev | line source |
|---|---|
| 6 | 1 /* Module definition and import implementation */ |
| 2 | |
| 86 | 3 #include "allobjects.h" |
| 6 | 4 |
| 5 #include "node.h" | |
| 6 #include "token.h" | |
| 7 #include "graminit.h" | |
| 8 #include "import.h" | |
| 9 #include "errcode.h" | |
| 10 #include "sysmodule.h" | |
| 86 | 11 #include "pythonrun.h" |
| 6 | 12 |
| 86 | 13 /* Define pathname separator used in file names */ |
| 6 | 14 |
| 15 #ifdef THINK_C | |
| 16 #define SEP ':' | |
| 17 #endif | |
| 18 | |
| 19 #ifndef SEP | |
| 20 #define SEP '/' | |
| 21 #endif | |
| 22 | |
| 86 | 23 static object *modules; |
| 24 | |
| 25 /* Initialization */ | |
| 6 | 26 |
| 27 void | |
| 28 initimport() | |
| 29 { | |
| 86 | 30 if ((modules = newdictobject()) == NULL) |
| 31 fatal("no mem for dictionary of modules"); | |
| 6 | 32 } |
| 33 | |
| 34 object * | |
| 86 | 35 get_modules() |
| 6 | 36 { |
| 86 | 37 return modules; |
| 6 | 38 } |
| 39 | |
| 86 | 40 object * |
| 41 add_module(name) | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
42 char *name; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
43 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
44 object *m; |
| 86 | 45 if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m)) |
| 46 return m; | |
| 47 m = newmoduleobject(name); | |
| 48 if (m == NULL) | |
| 6 | 49 return NULL; |
| 86 | 50 if (dictinsert(modules, name, m) != 0) { |
| 51 DECREF(m); | |
| 52 return NULL; | |
| 6 | 53 } |
| 86 | 54 DECREF(m); /* Yes, it still exists, in modules! */ |
| 55 return m; | |
| 6 | 56 } |
| 57 | |
| 58 static FILE * | |
| 86 | 59 open_module(name, suffix, namebuf) |
| 6 | 60 char *name; |
| 61 char *suffix; | |
| 86 | 62 char *namebuf; /* XXX No buffer overflow checks! */ |
| 6 | 63 { |
| 64 object *path; | |
| 65 FILE *fp; | |
| 66 | |
| 67 path = sysget("path"); | |
| 68 if (path == NULL || !is_listobject(path)) { | |
| 69 strcpy(namebuf, name); | |
| 70 strcat(namebuf, suffix); | |
| 71 fp = fopen(namebuf, "r"); | |
| 72 } | |
| 73 else { | |
| 74 int npath = getlistsize(path); | |
| 75 int i; | |
| 76 fp = NULL; | |
| 77 for (i = 0; i < npath; i++) { | |
| 78 object *v = getlistitem(path, i); | |
| 79 int len; | |
| 80 if (!is_stringobject(v)) | |
| 81 continue; | |
| 82 strcpy(namebuf, getstringvalue(v)); | |
| 83 len = getstringsize(v); | |
| 84 if (len > 0 && namebuf[len-1] != SEP) | |
| 85 namebuf[len++] = SEP; | |
| 86 | 86 strcpy(namebuf+len, name); |
| 87 strcat(namebuf, suffix); | |
| 6 | 88 fp = fopen(namebuf, "r"); |
| 89 if (fp != NULL) | |
| 90 break; | |
| 91 } | |
| 92 } | |
| 93 return fp; | |
| 94 } | |
| 95 | |
| 96 static object * | |
| 86 | 97 get_module(m, name, m_ret) |
| 98 /*module*/object *m; | |
| 6 | 99 char *name; |
| 86 | 100 object **m_ret; |
| 6 | 101 { |
| 86 | 102 object *d; |
| 6 | 103 FILE *fp; |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
104 node *n; |
| 6 | 105 int err; |
| 86 | 106 char namebuf[256]; |
| 6 | 107 |
| 86 | 108 fp = open_module(name, ".py", namebuf); |
| 109 if (fp == NULL) { | |
| 110 if (m == NULL) | |
| 111 err_setstr(NameError, name); | |
| 112 else | |
| 113 err_setstr(RuntimeError, "no module source file"); | |
| 6 | 114 return NULL; |
| 115 } | |
| 86 | 116 err = parse_file(fp, namebuf, file_input, &n); |
| 6 | 117 fclose(fp); |
| 118 if (err != E_DONE) { | |
| 86 | 119 err_input(err); |
| 6 | 120 return NULL; |
| 121 } | |
| 86 | 122 if (m == NULL) { |
| 123 m = add_module(name); | |
| 124 if (m == NULL) { | |
| 125 freetree(n); | |
| 126 return NULL; | |
| 127 } | |
| 128 *m_ret = m; | |
| 129 } | |
| 130 d = getmoduledict(m); | |
| 131 return run_node(n, namebuf, d, d); | |
| 132 } | |
| 133 | |
| 134 static object * | |
| 135 load_module(name) | |
| 136 char *name; | |
| 137 { | |
| 138 object *m, *v; | |
| 139 v = get_module((object *)NULL, name, &m); | |
| 140 if (v == NULL) | |
| 6 | 141 return NULL; |
| 86 | 142 DECREF(v); |
| 6 | 143 return m; |
| 144 } | |
| 145 | |
| 146 object * | |
| 86 | 147 import_module(name) |
| 6 | 148 char *name; |
| 149 { | |
| 150 object *m; | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 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
|
155 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
156 else { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
157 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
|
158 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
159 } |
| 6 | 160 return m; |
| 161 } | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
162 |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
163 object * |
| 86 | 164 reload_module(m) |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
165 object *m; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
166 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
167 if (m == NULL || !is_moduleobject(m)) { |
| 86 | 168 err_setstr(TypeError, "reload() argument must be module"); |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
169 return NULL; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
170 } |
| 86 | 171 /* XXX Ought to check for builtin modules -- can't reload these... */ |
| 172 return get_module(m, getmodulename(m), (object **)NULL); | |
| 173 } | |
| 174 | |
| 175 static void | |
| 176 cleardict(d) | |
| 177 object *d; | |
| 178 { | |
| 179 int i; | |
| 180 for (i = getdictsize(d); --i >= 0; ) { | |
| 181 char *k; | |
| 182 k = getdictkey(d, i); | |
| 183 if (k != NULL) | |
| 184 (void) dictremove(d, k); | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
185 } |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
186 } |
| 86 | 187 |
| 188 void | |
| 189 doneimport() | |
| 190 { | |
| 191 if (modules != NULL) { | |
| 192 int i; | |
| 193 /* Explicitly erase all modules; this is the safest way | |
| 194 to get rid of at least *some* circular dependencies */ | |
| 195 for (i = getdictsize(modules); --i >= 0; ) { | |
| 196 char *k; | |
| 197 k = getdictkey(modules, i); | |
| 198 if (k != NULL) { | |
| 199 object *m; | |
| 200 m = dictlookup(modules, k); | |
| 201 if (m != NULL && is_moduleobject(m)) { | |
| 202 object *d; | |
| 203 d = getmoduledict(m); | |
| 204 if (d != NULL && is_dictobject(d)) { | |
| 205 cleardict(d); | |
| 206 } | |
| 207 } | |
| 208 } | |
| 209 } | |
| 210 cleardict(modules); | |
| 211 } | |
| 212 DECREF(modules); | |
| 213 } | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
214 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
215 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
216 /* 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
|
217 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
218 extern struct { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
219 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
220 void (*initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
221 } inittab[]; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
222 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
223 static int |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
224 init_builtin(name) |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
225 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
226 { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
227 int i; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
228 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
|
229 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
|
230 (*inittab[i].initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
231 return 1; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
232 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
233 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
234 return 0; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
235 } |
