Mercurial > cpython-lru_cache-weakref
annotate Python/import.c @ 662:aaff650d7e13 legacy-trunk
Get rid of comment about open().
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 05 Feb 1992 11:17:52 +0000 |
| parents | 04ed4765b17c |
| children | 5aff9f8fd299 |
| 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 |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
40 #ifdef DEBUG |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
41 #define D(x) x |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
42 #else |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
43 #define D(x) |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
44 #endif |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
45 |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
46 #ifdef USE_DL |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
47 #include "dl.h" |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
48 |
|
630
04ed4765b17c
getbinaryname is now part of dl_loadmod.
Guido van Rossum <guido@python.org>
parents:
609
diff
changeset
|
49 extern char *argv0; |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
50 #endif |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
51 |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
52 /* 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
|
53 |
|
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
54 #define MAGIC 0x949494L |
|
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
55 |
| 86 | 56 /* Define pathname separator used in file names */ |
| 6 | 57 |
|
293
f900b692b37e
Generalize to macintosh.
Guido van Rossum <guido@python.org>
parents:
263
diff
changeset
|
58 #ifdef macintosh |
| 6 | 59 #define SEP ':' |
| 60 #endif | |
| 61 | |
|
228
634c1d3c7d39
Pre-define MS-DOS separator
Guido van Rossum <guido@python.org>
parents:
166
diff
changeset
|
62 #ifdef MSDOS |
|
634c1d3c7d39
Pre-define MS-DOS separator
Guido van Rossum <guido@python.org>
parents:
166
diff
changeset
|
63 #define SEP '\\' |
|
634c1d3c7d39
Pre-define MS-DOS separator
Guido van Rossum <guido@python.org>
parents:
166
diff
changeset
|
64 #endif |
|
634c1d3c7d39
Pre-define MS-DOS separator
Guido van Rossum <guido@python.org>
parents:
166
diff
changeset
|
65 |
| 6 | 66 #ifndef SEP |
| 67 #define SEP '/' | |
| 68 #endif | |
| 69 | |
| 86 | 70 static object *modules; |
| 71 | |
|
166
28923e44254c
Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
72 /* Forward */ |
|
28923e44254c
Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
73 static int init_builtin PROTO((char *)); |
|
28923e44254c
Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
74 |
| 86 | 75 /* Initialization */ |
| 6 | 76 |
| 77 void | |
| 78 initimport() | |
| 79 { | |
| 86 | 80 if ((modules = newdictobject()) == NULL) |
| 81 fatal("no mem for dictionary of modules"); | |
| 6 | 82 } |
| 83 | |
| 84 object * | |
| 86 | 85 get_modules() |
| 6 | 86 { |
| 86 | 87 return modules; |
| 6 | 88 } |
| 89 | |
| 86 | 90 object * |
| 91 add_module(name) | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
92 char *name; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
93 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
94 object *m; |
| 86 | 95 if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m)) |
| 96 return m; | |
| 97 m = newmoduleobject(name); | |
| 98 if (m == NULL) | |
| 6 | 99 return NULL; |
| 86 | 100 if (dictinsert(modules, name, m) != 0) { |
| 101 DECREF(m); | |
| 102 return NULL; | |
| 6 | 103 } |
| 86 | 104 DECREF(m); /* Yes, it still exists, in modules! */ |
| 105 return m; | |
| 6 | 106 } |
| 107 | |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
108 /* Suffixes used by open_module: */ |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
109 |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
110 #define PY_SUFFIX ".py" |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
111 #ifdef USE_DL |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
112 #define O_SUFFIX "module.o" |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
113 #endif |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
114 |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
115 /* Find and open a module file, using sys.path. |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
116 Return a NULL pointer if no module file is found. |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
117 When dynamic loading is enabled, the contents of namebuf |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
118 is important when NULL is returned: if namebuf[0] != '\0' |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
119 a dl-able object file was found and namebuf is its pathname. */ |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
120 |
| 6 | 121 static FILE * |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
122 open_module(name, namebuf) |
| 6 | 123 char *name; |
| 86 | 124 char *namebuf; /* XXX No buffer overflow checks! */ |
| 6 | 125 { |
| 126 object *path; | |
| 127 FILE *fp; | |
| 128 | |
| 129 path = sysget("path"); | |
| 130 if (path == NULL || !is_listobject(path)) { | |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
131 /* No path -- at least try current directory */ |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
132 #ifdef USE_DL |
| 6 | 133 strcpy(namebuf, name); |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
134 strcat(namebuf, O_SUFFIX); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
135 if (getmtime(namebuf) > 0) |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
136 return NULL; |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
137 #endif |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
138 strcpy(namebuf, name); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
139 strcat(namebuf, PY_SUFFIX); |
| 6 | 140 fp = fopen(namebuf, "r"); |
| 141 } | |
| 142 else { | |
| 143 int npath = getlistsize(path); | |
| 144 int i; | |
| 145 fp = NULL; | |
| 146 for (i = 0; i < npath; i++) { | |
| 147 object *v = getlistitem(path, i); | |
| 148 int len; | |
| 149 if (!is_stringobject(v)) | |
| 150 continue; | |
| 151 strcpy(namebuf, getstringvalue(v)); | |
| 152 len = getstringsize(v); | |
| 153 if (len > 0 && namebuf[len-1] != SEP) | |
| 154 namebuf[len++] = SEP; | |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
155 #ifdef USE_DL |
| 86 | 156 strcpy(namebuf+len, name); |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
157 strcat(namebuf, O_SUFFIX); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
158 if (getmtime(namebuf) > 0) |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
159 return NULL; |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
160 #endif |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
161 strcpy(namebuf+len, name); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
162 strcat(namebuf, PY_SUFFIX); |
| 6 | 163 fp = fopen(namebuf, "r"); |
| 164 if (fp != NULL) | |
| 165 break; | |
| 166 } | |
| 167 } | |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
168 if (fp == NULL) |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
169 namebuf[0] = '\0'; |
| 6 | 170 return fp; |
| 171 } | |
| 172 | |
| 173 static object * | |
| 86 | 174 get_module(m, name, m_ret) |
| 175 /*module*/object *m; | |
| 6 | 176 char *name; |
| 86 | 177 object **m_ret; |
| 6 | 178 { |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
179 codeobject *co = NULL; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
180 object *v, *d; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
181 FILE *fp, *fpc; |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
182 node *n; |
| 6 | 183 int err; |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
184 char namebuf[258]; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
185 int namelen; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
186 long mtime; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
187 extern long getmtime(); |
| 6 | 188 |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
189 fp = open_module(name, namebuf); |
| 86 | 190 if (fp == NULL) { |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
191 #ifdef USE_DL |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
192 if (namebuf[0] != '\0') { |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
193 char funcname[258]; |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
194 dl_funcptr p; |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
195 D(fprintf(stderr, "Found %s\n", namebuf)); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
196 sprintf(funcname, "init%s", name); |
|
630
04ed4765b17c
getbinaryname is now part of dl_loadmod.
Guido van Rossum <guido@python.org>
parents:
609
diff
changeset
|
197 p = dl_loadmod(argv0, namebuf, funcname); |
|
609
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
198 if (p == NULL) { |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
199 D(fprintf(stderr, "dl_loadmod failed\n")); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
200 } |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
201 else { |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
202 (*p)(); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
203 *m_ret = m = dictlookup(modules, name); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
204 if (m == NULL) { |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
205 err_setstr(SystemError, |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
206 "dynamic module missing"); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
207 return NULL; |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
208 } |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
209 else { |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
210 D(fprintf(stderr, |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
211 "module %s loaded!\n", name)); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
212 INCREF(None); |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
213 return None; |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
214 } |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
215 } |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
216 } |
|
0ad5b35e3ad8
Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents:
510
diff
changeset
|
217 #endif |
|
510
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
218 if (m == NULL) { |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
219 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
|
220 err_setstr(ImportError, namebuf); |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
221 } |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
222 else { |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
223 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
|
224 err_setstr(ImportError, namebuf); |
|
cbcdfda05bc4
Use IOError and ImportError when import fails.
Guido van Rossum <guido@python.org>
parents:
497
diff
changeset
|
225 } |
| 6 | 226 return NULL; |
| 227 } | |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
228 /* Get mtime -- always useful */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
229 mtime = getmtime(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
230 /* Check ".pyc" file first */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
231 namelen = strlen(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
232 namebuf[namelen] = 'c'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
233 namebuf[namelen+1] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
234 fpc = fopen(namebuf, "rb"); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
235 if (fpc != NULL) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
236 long pyc_mtime; |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
237 long magic; |
|
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
238 magic = rd_long(fpc); |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
239 pyc_mtime = rd_long(fpc); |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
240 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
|
241 v = rd_object(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
242 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
|
243 err_clear(); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
244 XDECREF(v); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
245 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
246 else |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
247 co = (codeobject *)v; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
248 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
249 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
250 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
251 namebuf[namelen] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
252 if (co == NULL) |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
253 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
|
254 else |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
255 err = E_DONE; |
| 6 | 256 fclose(fp); |
| 257 if (err != E_DONE) { | |
| 86 | 258 err_input(err); |
| 6 | 259 return NULL; |
| 260 } | |
| 86 | 261 if (m == NULL) { |
| 262 m = add_module(name); | |
| 263 if (m == NULL) { | |
| 264 freetree(n); | |
| 265 return NULL; | |
| 266 } | |
| 267 *m_ret = m; | |
| 268 } | |
| 269 d = getmoduledict(m); | |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
270 if (co == NULL) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
271 co = compile(n, namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
272 freetree(n); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
273 if (co == NULL) |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
274 return NULL; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
275 /* 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
|
276 namebuf[namelen] = 'c'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
277 namebuf[namelen+1] = '\0'; |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
278 fpc = fopen(namebuf, "wb"); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
279 if (fpc != NULL) { |
|
487
77b80a91d357
New magic word; and check it.
Guido van Rossum <guido@python.org>
parents:
340
diff
changeset
|
280 wr_long(MAGIC, fpc); |
|
263
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
281 /* First write a 0 for mtime */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
282 wr_long(0L, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
283 wr_object((object *)co, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
284 if (ferror(fpc)) { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
285 /* Don't keep partial file */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
286 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
287 (void) unlink(namebuf); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
288 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
289 else { |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
290 /* Now write the true mtime */ |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
291 fseek(fpc, 4L, 0); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
292 wr_long(mtime, fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
293 fflush(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
294 fclose(fpc); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
295 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
296 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
297 } |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
298 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
|
299 DECREF(co); |
|
28f0ffd9facf
Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents:
228
diff
changeset
|
300 return v; |
| 86 | 301 } |
| 302 | |
| 303 static object * | |
| 304 load_module(name) | |
| 305 char *name; | |
| 306 { | |
| 307 object *m, *v; | |
| 308 v = get_module((object *)NULL, name, &m); | |
| 309 if (v == NULL) | |
| 6 | 310 return NULL; |
| 86 | 311 DECREF(v); |
| 6 | 312 return m; |
| 313 } | |
| 314 | |
| 315 object * | |
| 86 | 316 import_module(name) |
| 6 | 317 char *name; |
| 318 { | |
| 319 object *m; | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
320 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
|
321 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
|
322 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
|
323 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
|
324 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
325 else { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
326 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
|
327 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
328 } |
| 6 | 329 return m; |
| 330 } | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
331 |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
332 object * |
| 86 | 333 reload_module(m) |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
334 object *m; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
335 { |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
336 if (m == NULL || !is_moduleobject(m)) { |
| 86 | 337 err_setstr(TypeError, "reload() argument must be module"); |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
338 return NULL; |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
339 } |
| 86 | 340 /* XXX Ought to check for builtin modules -- can't reload these... */ |
| 341 return get_module(m, getmodulename(m), (object **)NULL); | |
| 342 } | |
| 343 | |
| 344 static void | |
| 345 cleardict(d) | |
| 346 object *d; | |
| 347 { | |
| 348 int i; | |
| 349 for (i = getdictsize(d); --i >= 0; ) { | |
| 350 char *k; | |
| 351 k = getdictkey(d, i); | |
| 352 if (k != NULL) | |
| 353 (void) dictremove(d, k); | |
|
40
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
354 } |
|
f28ac7f621c7
Added reload() functionality.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
355 } |
| 86 | 356 |
| 357 void | |
| 358 doneimport() | |
| 359 { | |
| 360 if (modules != NULL) { | |
| 361 int i; | |
| 362 /* Explicitly erase all modules; this is the safest way | |
| 363 to get rid of at least *some* circular dependencies */ | |
| 364 for (i = getdictsize(modules); --i >= 0; ) { | |
|
340
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
365 object *k; |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
366 k = getdict2key(modules, i); |
| 86 | 367 if (k != NULL) { |
| 368 object *m; | |
|
340
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
369 m = dict2lookup(modules, k); |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
370 if (m == NULL) |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
371 err_clear(); |
|
e3941efd7310
Use more dict2 functions./
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
372 else if (is_moduleobject(m)) { |
| 86 | 373 object *d; |
| 374 d = getmoduledict(m); | |
| 375 if (d != NULL && is_dictobject(d)) { | |
| 376 cleardict(d); | |
| 377 } | |
| 378 } | |
| 379 } | |
| 380 } | |
| 381 cleardict(modules); | |
| 382 } | |
| 383 DECREF(modules); | |
| 384 } | |
|
141
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
385 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
386 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
387 /* 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
|
388 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
389 extern struct { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
390 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
391 void (*initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
392 } inittab[]; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
393 |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
394 static int |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
395 init_builtin(name) |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
396 char *name; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
397 { |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
398 int i; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
399 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
|
400 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
|
401 (*inittab[i].initfunc)(); |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
402 return 1; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
403 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
404 } |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
405 return 0; |
|
bf5a8539d8ba
Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
406 } |
