Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Module definition and import implementation */ |
| 33 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | #include "node.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | #include "token.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | #include "errcode.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 39 | #include "marshal.h" |
| 40 | #include "compile.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 41 | #include "eval.h" |
Guido van Rossum | d8bac6d | 1992-02-26 15:19:13 +0000 | [diff] [blame] | 42 | #include "osdefs.h" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 43 | #include "importdl.h" |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 44 | #ifdef macintosh |
| 45 | #include "macglue.h" |
| 46 | #endif |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 48 | #ifdef HAVE_UNISTD_H |
| 49 | #include <unistd.h> |
| 50 | #endif |
| 51 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 52 | /* We expect that stat exists on most systems. |
| 53 | It's confirmed on Unix, Mac and Windows. |
| 54 | If you don't have it, add #define DONT_HAVE_STAT to your config.h. */ |
| 55 | #ifndef DONT_HAVE_STAT |
| 56 | #define HAVE_STAT |
| 57 | |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 58 | #ifndef DONT_HAVE_SYS_TYPES_H |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 59 | #include <sys/types.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 60 | #endif |
| 61 | #ifndef DONT_HAVE_SYS_STAT_H |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 62 | #include <sys/stat.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 63 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 595d7ba | 1997-12-05 21:45:29 +0000 | [diff] [blame] | 65 | #if defined(PYCC_VACPP) |
| 66 | /* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */ |
| 67 | #define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG) |
| 68 | #endif |
| 69 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 70 | #ifndef S_ISDIR |
| 71 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) |
| 72 | #endif |
| 73 | |
| 74 | #endif |
| 75 | |
| 76 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 77 | extern long PyOS_GetLastModificationTime(); /* In getmtime.c */ |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 6c84969 | 1994-09-26 15:47:17 +0000 | [diff] [blame] | 79 | /* Magic word to reject .pyc files generated by other Python versions */ |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 80 | /* Change for each incompatible change */ |
| 81 | /* The value of CR and LF is incorporated so if you ever read or write |
| 82 | a .pyc file in text mode the magic number will be wrong; also, the |
| 83 | Apple MPW compiler swaps their values, botching string constants */ |
| 84 | /* XXX Perhaps the magic number should be frozen and a version field |
| 85 | added to the .pyc file header? */ |
Guido van Rossum | dd5db43 | 1997-01-17 21:06:11 +0000 | [diff] [blame] | 86 | /* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */ |
Guido van Rossum | 99d1825 | 1997-01-24 03:44:53 +0000 | [diff] [blame] | 87 | #define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24)) |
Guido van Rossum | 3ddee71 | 1991-12-16 13:06:34 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 89 | /* See _PyImport_FixupExtension() below */ |
| 90 | static PyObject *extensions = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 92 | /* This table is defined in config.c: */ |
| 93 | extern struct _inittab _PyImport_Inittab[]; |
| 94 | |
| 95 | struct _inittab *PyImport_Inittab = _PyImport_Inittab; |
Guido van Rossum | 66f1fa8 | 1991-04-03 19:03:52 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 97 | /* Initialize things */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | |
| 99 | void |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 100 | _PyImport_Init() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | { |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 102 | if (Py_OptimizeFlag) { |
| 103 | /* Replace ".pyc" with ".pyo" in import_filetab */ |
| 104 | struct filedescr *p; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 105 | for (p = _PyImport_Filetab; p->suffix != NULL; p++) { |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 106 | if (strcmp(p->suffix, ".pyc") == 0) |
| 107 | p->suffix = ".pyo"; |
| 108 | } |
| 109 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 112 | void |
| 113 | _PyImport_Fini() |
| 114 | { |
| 115 | Py_XDECREF(extensions); |
| 116 | extensions = NULL; |
| 117 | } |
| 118 | |
| 119 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 120 | /* Locking primitives to prevent parallel imports of the same module |
| 121 | in different threads to return with a partially loaded module. |
| 122 | These calls are serialized by the global interpreter lock. */ |
| 123 | |
| 124 | #ifdef WITH_THREAD |
| 125 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 126 | #include "pythread.h" |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 128 | static PyThread_type_lock import_lock = 0; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 129 | static long import_lock_thread = -1; |
| 130 | static int import_lock_level = 0; |
| 131 | |
| 132 | static void |
| 133 | lock_import() |
| 134 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 135 | long me = PyThread_get_thread_ident(); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 136 | if (me == -1) |
| 137 | return; /* Too bad */ |
| 138 | if (import_lock == NULL) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 139 | import_lock = PyThread_allocate_lock(); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 140 | if (import_lock_thread == me) { |
| 141 | import_lock_level++; |
| 142 | return; |
| 143 | } |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 144 | if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) { |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 145 | PyThreadState *tstate = PyEval_SaveThread(); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 146 | PyThread_acquire_lock(import_lock, 1); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 147 | PyEval_RestoreThread(tstate); |
| 148 | } |
| 149 | import_lock_thread = me; |
| 150 | import_lock_level = 1; |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | unlock_import() |
| 155 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 156 | long me = PyThread_get_thread_ident(); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 157 | if (me == -1) |
| 158 | return; /* Too bad */ |
| 159 | if (import_lock_thread != me) |
| 160 | Py_FatalError("unlock_import: not holding the import lock"); |
| 161 | import_lock_level--; |
| 162 | if (import_lock_level == 0) { |
| 163 | import_lock_thread = -1; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 164 | PyThread_release_lock(import_lock); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | #else |
| 169 | |
| 170 | #define lock_import() |
| 171 | #define unlock_import() |
| 172 | |
| 173 | #endif |
| 174 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 175 | /* Helper for sys */ |
| 176 | |
| 177 | PyObject * |
| 178 | PyImport_GetModuleDict() |
| 179 | { |
| 180 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 181 | if (interp->modules == NULL) |
| 182 | Py_FatalError("PyImport_GetModuleDict: no module dictionary!"); |
| 183 | return interp->modules; |
| 184 | } |
| 185 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 187 | /* List of names to clear in sys */ |
| 188 | static char* sys_deletes[] = { |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 189 | "path", "argv", "ps1", "ps2", "exitfunc", |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 190 | "exc_type", "exc_value", "exc_traceback", |
| 191 | "last_type", "last_value", "last_traceback", |
| 192 | NULL |
| 193 | }; |
| 194 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 195 | static char* sys_files[] = { |
| 196 | "stdin", "__stdin__", |
| 197 | "stdout", "__stdout__", |
| 198 | "stderr", "__stderr__", |
| 199 | NULL |
| 200 | }; |
| 201 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 203 | /* Un-initialize things, as good as we can */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 205 | void |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 206 | PyImport_Cleanup() |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 207 | { |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 208 | int pos, ndone; |
| 209 | char *name; |
| 210 | PyObject *key, *value, *dict; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 211 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 212 | PyObject *modules = interp->modules; |
| 213 | |
| 214 | if (modules == NULL) |
| 215 | return; /* Already done */ |
| 216 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 217 | /* Delete some special variables first. These are common |
| 218 | places where user values hide and people complain when their |
| 219 | destructors fail. Since the modules containing them are |
| 220 | deleted *last* of all, they would come too late in the normal |
| 221 | destruction order. Sigh. */ |
| 222 | |
| 223 | value = PyDict_GetItemString(modules, "__builtin__"); |
| 224 | if (value != NULL && PyModule_Check(value)) { |
| 225 | dict = PyModule_GetDict(value); |
| 226 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 227 | PySys_WriteStderr("# clear __builtin__._\n"); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 228 | PyDict_SetItemString(dict, "_", Py_None); |
| 229 | } |
| 230 | value = PyDict_GetItemString(modules, "sys"); |
| 231 | if (value != NULL && PyModule_Check(value)) { |
| 232 | char **p; |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 233 | PyObject *v; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 234 | dict = PyModule_GetDict(value); |
| 235 | for (p = sys_deletes; *p != NULL; p++) { |
| 236 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 237 | PySys_WriteStderr("# clear sys.%s\n", *p); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 238 | PyDict_SetItemString(dict, *p, Py_None); |
| 239 | } |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 240 | for (p = sys_files; *p != NULL; p+=2) { |
| 241 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 242 | PySys_WriteStderr("# restore sys.%s\n", *p); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 243 | v = PyDict_GetItemString(dict, *(p+1)); |
| 244 | if (v == NULL) |
| 245 | v = Py_None; |
| 246 | PyDict_SetItemString(dict, *p, v); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* First, delete __main__ */ |
| 251 | value = PyDict_GetItemString(modules, "__main__"); |
| 252 | if (value != NULL && PyModule_Check(value)) { |
| 253 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 254 | PySys_WriteStderr("# cleanup __main__\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 255 | _PyModule_Clear(value); |
| 256 | PyDict_SetItemString(modules, "__main__", Py_None); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 259 | /* The special treatment of __builtin__ here is because even |
| 260 | when it's not referenced as a module, its dictionary is |
| 261 | referenced by almost every module's __builtins__. Since |
| 262 | deleting a module clears its dictionary (even if there are |
| 263 | references left to it), we need to delete the __builtin__ |
| 264 | module last. Likewise, we don't delete sys until the very |
| 265 | end because it is implicitly referenced (e.g. by print). |
| 266 | |
| 267 | Also note that we 'delete' modules by replacing their entry |
| 268 | in the modules dict with None, rather than really deleting |
| 269 | them; this avoids a rehash of the modules dictionary and |
| 270 | also marks them as "non existent" so they won't be |
| 271 | re-imported. */ |
| 272 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 273 | /* Next, repeatedly delete modules with a reference count of |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 274 | one (skipping __builtin__ and sys) and delete them */ |
| 275 | do { |
| 276 | ndone = 0; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 277 | pos = 0; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 278 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 279 | if (value->ob_refcnt != 1) |
| 280 | continue; |
Guido van Rossum | 566373e | 1998-10-01 15:24:50 +0000 | [diff] [blame] | 281 | if (PyString_Check(key) && PyModule_Check(value)) { |
| 282 | name = PyString_AS_STRING(key); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 283 | if (strcmp(name, "__builtin__") == 0) |
| 284 | continue; |
| 285 | if (strcmp(name, "sys") == 0) |
| 286 | continue; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 287 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 288 | PySys_WriteStderr( |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 289 | "# cleanup[1] %s\n", name); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 290 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 291 | PyDict_SetItem(modules, key, Py_None); |
| 292 | ndone++; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 295 | } while (ndone > 0); |
| 296 | |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 297 | /* Next, delete all modules (still skipping __builtin__ and sys) */ |
| 298 | pos = 0; |
| 299 | while (PyDict_Next(modules, &pos, &key, &value)) { |
Guido van Rossum | 566373e | 1998-10-01 15:24:50 +0000 | [diff] [blame] | 300 | if (PyString_Check(key) && PyModule_Check(value)) { |
| 301 | name = PyString_AS_STRING(key); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 302 | if (strcmp(name, "__builtin__") == 0) |
| 303 | continue; |
| 304 | if (strcmp(name, "sys") == 0) |
| 305 | continue; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 306 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 307 | PySys_WriteStderr("# cleanup[2] %s\n", name); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 308 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 309 | PyDict_SetItem(modules, key, Py_None); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /* Next, delete sys and __builtin__ (in that order) */ |
| 314 | value = PyDict_GetItemString(modules, "sys"); |
| 315 | if (value != NULL && PyModule_Check(value)) { |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 316 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 317 | PySys_WriteStderr("# cleanup sys\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 318 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 319 | PyDict_SetItemString(modules, "sys", Py_None); |
| 320 | } |
| 321 | value = PyDict_GetItemString(modules, "__builtin__"); |
| 322 | if (value != NULL && PyModule_Check(value)) { |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 323 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 324 | PySys_WriteStderr("# cleanup __builtin__\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 325 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 326 | PyDict_SetItemString(modules, "__builtin__", Py_None); |
| 327 | } |
| 328 | |
| 329 | /* Finally, clear and delete the modules directory */ |
| 330 | PyDict_Clear(modules); |
| 331 | interp->modules = NULL; |
| 332 | Py_DECREF(modules); |
Guido van Rossum | 8d15b5d | 1990-10-26 14:58:58 +0000 | [diff] [blame] | 333 | } |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 334 | |
| 335 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 336 | /* Helper for pythonrun.c -- return magic number */ |
| 337 | |
| 338 | long |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 339 | PyImport_GetMagicNumber() |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 340 | { |
| 341 | return MAGIC; |
| 342 | } |
| 343 | |
| 344 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 345 | /* Magic for extension modules (built-in as well as dynamically |
| 346 | loaded). To prevent initializing an extension module more than |
| 347 | once, we keep a static dictionary 'extensions' keyed by module name |
| 348 | (for built-in modules) or by filename (for dynamically loaded |
| 349 | modules), containing these modules. A copy od the module's |
| 350 | dictionary is stored by calling _PyImport_FixupExtension() |
| 351 | immediately after the module initialization function succeeds. A |
| 352 | copy can be retrieved from there by calling |
| 353 | _PyImport_FindExtension(). */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 354 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 355 | PyObject * |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 356 | _PyImport_FixupExtension(name, filename) |
| 357 | char *name; |
| 358 | char *filename; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 359 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 360 | PyObject *modules, *mod, *dict, *copy; |
| 361 | if (extensions == NULL) { |
| 362 | extensions = PyDict_New(); |
| 363 | if (extensions == NULL) |
| 364 | return NULL; |
| 365 | } |
| 366 | modules = PyImport_GetModuleDict(); |
| 367 | mod = PyDict_GetItemString(modules, name); |
| 368 | if (mod == NULL || !PyModule_Check(mod)) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 369 | PyErr_Format(PyExc_SystemError, |
| 370 | "_PyImport_FixupExtension: module %.200s not loaded", name); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
| 373 | dict = PyModule_GetDict(mod); |
| 374 | if (dict == NULL) |
| 375 | return NULL; |
| 376 | copy = PyObject_CallMethod(dict, "copy", ""); |
| 377 | if (copy == NULL) |
| 378 | return NULL; |
| 379 | PyDict_SetItemString(extensions, filename, copy); |
| 380 | Py_DECREF(copy); |
| 381 | return copy; |
| 382 | } |
| 383 | |
| 384 | PyObject * |
| 385 | _PyImport_FindExtension(name, filename) |
| 386 | char *name; |
| 387 | char *filename; |
| 388 | { |
| 389 | PyObject *dict, *mod, *mdict, *result; |
| 390 | if (extensions == NULL) |
| 391 | return NULL; |
| 392 | dict = PyDict_GetItemString(extensions, filename); |
| 393 | if (dict == NULL) |
| 394 | return NULL; |
| 395 | mod = PyImport_AddModule(name); |
| 396 | if (mod == NULL) |
| 397 | return NULL; |
| 398 | mdict = PyModule_GetDict(mod); |
| 399 | if (mdict == NULL) |
| 400 | return NULL; |
| 401 | result = PyObject_CallMethod(mdict, "update", "O", dict); |
| 402 | if (result == NULL) |
| 403 | return NULL; |
| 404 | Py_DECREF(result); |
| 405 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 406 | PySys_WriteStderr("import %s # previously loaded (%s)\n", |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 407 | name, filename); |
| 408 | return mod; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | |
| 412 | /* Get the module object corresponding to a module name. |
| 413 | First check the modules dictionary if there's one there, |
| 414 | if not, create a new one and insert in in the modules dictionary. |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 415 | Because the former action is most common, THIS DOES NOT RETURN A |
| 416 | 'NEW' REFERENCE! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 418 | PyObject * |
| 419 | PyImport_AddModule(name) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 420 | char *name; |
| 421 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 422 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 423 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 425 | if ((m = PyDict_GetItemString(modules, name)) != NULL && |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 426 | PyModule_Check(m)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 427 | return m; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 428 | m = PyModule_New(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 429 | if (m == NULL) |
| 430 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 431 | if (PyDict_SetItemString(modules, name, m) != 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 432 | Py_DECREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 433 | return NULL; |
| 434 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 435 | Py_DECREF(m); /* Yes, it still exists, in modules! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 436 | |
| 437 | return m; |
| 438 | } |
| 439 | |
| 440 | |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 441 | /* Execute a code object in a module and return the module object |
| 442 | WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 443 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 444 | PyObject * |
| 445 | PyImport_ExecCodeModule(name, co) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 446 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 447 | PyObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 448 | { |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 449 | return PyImport_ExecCodeModuleEx(name, co, (char *)NULL); |
| 450 | } |
| 451 | |
| 452 | PyObject * |
| 453 | PyImport_ExecCodeModuleEx(name, co, pathname) |
| 454 | char *name; |
| 455 | PyObject *co; |
| 456 | char *pathname; |
| 457 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 458 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 459 | PyObject *m, *d, *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 460 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 461 | m = PyImport_AddModule(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 462 | if (m == NULL) |
| 463 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 464 | d = PyModule_GetDict(m); |
| 465 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 466 | if (PyDict_SetItemString(d, "__builtins__", |
| 467 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 468 | return NULL; |
| 469 | } |
Guido van Rossum | 9c9a07c | 1996-05-16 20:43:40 +0000 | [diff] [blame] | 470 | /* Remember the filename as the __file__ attribute */ |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 471 | v = NULL; |
| 472 | if (pathname != NULL) { |
| 473 | v = PyString_FromString(pathname); |
| 474 | if (v == NULL) |
| 475 | PyErr_Clear(); |
| 476 | } |
| 477 | if (v == NULL) { |
| 478 | v = ((PyCodeObject *)co)->co_filename; |
| 479 | Py_INCREF(v); |
| 480 | } |
| 481 | if (PyDict_SetItemString(d, "__file__", v) != 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 482 | PyErr_Clear(); /* Not important enough to report */ |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 483 | Py_DECREF(v); |
| 484 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 485 | v = PyEval_EvalCode((PyCodeObject *)co, d, d); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 486 | if (v == NULL) |
| 487 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 488 | Py_DECREF(v); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 489 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 490 | if ((m = PyDict_GetItemString(modules, name)) == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 491 | PyErr_Format(PyExc_ImportError, |
| 492 | "Loaded module %.200s not found in sys.modules", |
| 493 | name); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 494 | return NULL; |
| 495 | } |
| 496 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 497 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 498 | |
| 499 | return m; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | /* Given a pathname for a Python source file, fill a buffer with the |
| 504 | pathname for the corresponding compiled file. Return the pathname |
| 505 | for the compiled file, or NULL if there's no space in the buffer. |
| 506 | Doesn't set an exception. */ |
| 507 | |
| 508 | static char * |
| 509 | make_compiled_pathname(pathname, buf, buflen) |
| 510 | char *pathname; |
| 511 | char *buf; |
| 512 | int buflen; |
| 513 | { |
| 514 | int len; |
| 515 | |
| 516 | len = strlen(pathname); |
| 517 | if (len+2 > buflen) |
| 518 | return NULL; |
| 519 | strcpy(buf, pathname); |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 520 | strcpy(buf+len, Py_OptimizeFlag ? "o" : "c"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 521 | |
| 522 | return buf; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | /* Given a pathname for a Python source file, its time of last |
| 527 | modification, and a pathname for a compiled file, check whether the |
| 528 | compiled file represents the same version of the source. If so, |
| 529 | return a FILE pointer for the compiled file, positioned just after |
| 530 | the header; if not, return NULL. |
| 531 | Doesn't set an exception. */ |
| 532 | |
| 533 | static FILE * |
| 534 | check_compiled_module(pathname, mtime, cpathname) |
| 535 | char *pathname; |
| 536 | long mtime; |
| 537 | char *cpathname; |
| 538 | { |
| 539 | FILE *fp; |
| 540 | long magic; |
| 541 | long pyc_mtime; |
| 542 | |
| 543 | fp = fopen(cpathname, "rb"); |
| 544 | if (fp == NULL) |
| 545 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 546 | magic = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 547 | if (magic != MAGIC) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 548 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 549 | PySys_WriteStderr("# %s has bad magic\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 550 | fclose(fp); |
| 551 | return NULL; |
| 552 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 553 | pyc_mtime = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 554 | if (pyc_mtime != mtime) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 555 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 556 | PySys_WriteStderr("# %s has bad mtime\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 557 | fclose(fp); |
| 558 | return NULL; |
| 559 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 560 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 561 | PySys_WriteStderr("# %s matches %s\n", cpathname, pathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 562 | return fp; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | /* Read a code object from a file and check it for validity */ |
| 567 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 568 | static PyCodeObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 569 | read_compiled_module(cpathname, fp) |
| 570 | char *cpathname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 571 | FILE *fp; |
| 572 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 573 | PyObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 574 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 575 | co = PyMarshal_ReadObjectFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 576 | /* Ugly: rd_object() may return NULL with or without error */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 577 | if (co == NULL || !PyCode_Check(co)) { |
| 578 | if (!PyErr_Occurred()) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 579 | PyErr_Format(PyExc_ImportError, |
| 580 | "Non-code object in %.200s", cpathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 581 | Py_XDECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 582 | return NULL; |
| 583 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 584 | return (PyCodeObject *)co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
| 588 | /* Load a module from a compiled file, execute it, and return its |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 589 | module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 590 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 591 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 592 | load_compiled_module(name, cpathname, fp) |
| 593 | char *name; |
| 594 | char *cpathname; |
| 595 | FILE *fp; |
| 596 | { |
| 597 | long magic; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 598 | PyCodeObject *co; |
| 599 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 600 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 601 | magic = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 602 | if (magic != MAGIC) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 603 | PyErr_Format(PyExc_ImportError, |
| 604 | "Bad magic number in %.200s", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 605 | return NULL; |
| 606 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 607 | (void) PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 608 | co = read_compiled_module(cpathname, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 609 | if (co == NULL) |
| 610 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 611 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 612 | PySys_WriteStderr("import %s # precompiled from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 613 | name, cpathname); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 614 | m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 615 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 616 | |
| 617 | return m; |
| 618 | } |
| 619 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 620 | /* Parse a source file and return the corresponding code object */ |
| 621 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 622 | static PyCodeObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 623 | parse_source_module(pathname, fp) |
| 624 | char *pathname; |
| 625 | FILE *fp; |
| 626 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 627 | PyCodeObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 628 | node *n; |
| 629 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 630 | n = PyParser_SimpleParseFile(fp, pathname, Py_file_input); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 631 | if (n == NULL) |
| 632 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 633 | co = PyNode_Compile(n, pathname); |
| 634 | PyNode_Free(n); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 635 | |
| 636 | return co; |
| 637 | } |
| 638 | |
| 639 | |
| 640 | /* Write a compiled module to a file, placing the time of last |
| 641 | modification of its source into the header. |
| 642 | Errors are ignored, if a write error occurs an attempt is made to |
| 643 | remove the file. */ |
| 644 | |
| 645 | static void |
| 646 | write_compiled_module(co, cpathname, mtime) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 647 | PyCodeObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 648 | char *cpathname; |
| 649 | long mtime; |
| 650 | { |
| 651 | FILE *fp; |
| 652 | |
| 653 | fp = fopen(cpathname, "wb"); |
| 654 | if (fp == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 655 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 656 | PySys_WriteStderr( |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 657 | "# can't create %s\n", cpathname); |
| 658 | return; |
| 659 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 660 | PyMarshal_WriteLongToFile(MAGIC, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 661 | /* First write a 0 for mtime */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 662 | PyMarshal_WriteLongToFile(0L, fp); |
| 663 | PyMarshal_WriteObjectToFile((PyObject *)co, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 664 | if (ferror(fp)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 665 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 666 | PySys_WriteStderr("# can't write %s\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 667 | /* Don't keep partial file */ |
| 668 | fclose(fp); |
| 669 | (void) unlink(cpathname); |
| 670 | return; |
| 671 | } |
| 672 | /* Now write the true mtime */ |
| 673 | fseek(fp, 4L, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 674 | PyMarshal_WriteLongToFile(mtime, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 675 | fflush(fp); |
| 676 | fclose(fp); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 677 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 678 | PySys_WriteStderr("# wrote %s\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 679 | #ifdef macintosh |
Guido van Rossum | be1a6e2 | 1996-02-21 15:29:20 +0000 | [diff] [blame] | 680 | setfiletype(cpathname, 'Pyth', 'PYC '); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 681 | #endif |
| 682 | } |
| 683 | |
| 684 | |
| 685 | /* Load a source module from a given file and return its module |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 686 | object WITH INCREMENTED REFERENCE COUNT. If there's a matching |
| 687 | byte-compiled file, use that instead. */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 688 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 689 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 690 | load_source_module(name, pathname, fp) |
| 691 | char *name; |
| 692 | char *pathname; |
| 693 | FILE *fp; |
| 694 | { |
| 695 | long mtime; |
| 696 | FILE *fpc; |
| 697 | char buf[MAXPATHLEN+1]; |
| 698 | char *cpathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 699 | PyCodeObject *co; |
| 700 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 701 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 702 | mtime = PyOS_GetLastModificationTime(pathname, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 703 | cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1); |
| 704 | if (cpathname != NULL && |
| 705 | (fpc = check_compiled_module(pathname, mtime, cpathname))) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 706 | co = read_compiled_module(cpathname, fpc); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 707 | fclose(fpc); |
| 708 | if (co == NULL) |
| 709 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 710 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 711 | PySys_WriteStderr("import %s # precompiled from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 712 | name, cpathname); |
Guido van Rossum | afd3dae | 1998-08-25 18:44:34 +0000 | [diff] [blame] | 713 | pathname = cpathname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 714 | } |
| 715 | else { |
| 716 | co = parse_source_module(pathname, fp); |
| 717 | if (co == NULL) |
| 718 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 719 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 720 | PySys_WriteStderr("import %s # from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 721 | name, pathname); |
| 722 | write_compiled_module(co, cpathname, mtime); |
| 723 | } |
Guido van Rossum | afd3dae | 1998-08-25 18:44:34 +0000 | [diff] [blame] | 724 | m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 725 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 726 | |
| 727 | return m; |
| 728 | } |
| 729 | |
| 730 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 731 | /* Forward */ |
| 732 | static PyObject *load_module Py_PROTO((char *, FILE *, char *, int)); |
| 733 | static struct filedescr *find_module Py_PROTO((char *, PyObject *, |
| 734 | char *, int, FILE **)); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 735 | static struct _frozen *find_frozen Py_PROTO((char *name)); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 736 | |
| 737 | /* Load a package and return its module object WITH INCREMENTED |
| 738 | REFERENCE COUNT */ |
| 739 | |
| 740 | static PyObject * |
| 741 | load_package(name, pathname) |
| 742 | char *name; |
| 743 | char *pathname; |
| 744 | { |
| 745 | PyObject *m, *d, *file, *path; |
| 746 | int err; |
| 747 | char buf[MAXPATHLEN+1]; |
| 748 | FILE *fp = NULL; |
| 749 | struct filedescr *fdp; |
| 750 | |
| 751 | m = PyImport_AddModule(name); |
| 752 | if (m == NULL) |
| 753 | return NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 754 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 755 | PySys_WriteStderr("import %s # directory %s\n", |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 756 | name, pathname); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 757 | d = PyModule_GetDict(m); |
| 758 | file = PyString_FromString(pathname); |
| 759 | if (file == NULL) |
| 760 | return NULL; |
| 761 | path = Py_BuildValue("[O]", file); |
| 762 | if (path == NULL) { |
| 763 | Py_DECREF(file); |
| 764 | return NULL; |
| 765 | } |
| 766 | err = PyDict_SetItemString(d, "__file__", file); |
| 767 | if (err == 0) |
| 768 | err = PyDict_SetItemString(d, "__path__", path); |
| 769 | if (err != 0) { |
| 770 | m = NULL; |
| 771 | goto cleanup; |
| 772 | } |
| 773 | buf[0] = '\0'; |
| 774 | fdp = find_module("__init__", path, buf, sizeof(buf), &fp); |
| 775 | if (fdp == NULL) { |
| 776 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 777 | PyErr_Clear(); |
| 778 | } |
| 779 | else |
| 780 | m = NULL; |
| 781 | goto cleanup; |
| 782 | } |
| 783 | m = load_module(name, fp, buf, fdp->type); |
| 784 | if (fp != NULL) |
| 785 | fclose(fp); |
| 786 | cleanup: |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 787 | Py_XDECREF(path); |
| 788 | Py_XDECREF(file); |
| 789 | return m; |
| 790 | } |
| 791 | |
| 792 | |
| 793 | /* Helper to test for built-in module */ |
| 794 | |
| 795 | static int |
| 796 | is_builtin(name) |
| 797 | char *name; |
| 798 | { |
| 799 | int i; |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 800 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
| 801 | if (strcmp(name, PyImport_Inittab[i].name) == 0) { |
| 802 | if (PyImport_Inittab[i].initfunc == NULL) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 803 | return -1; |
| 804 | else |
| 805 | return 1; |
| 806 | } |
| 807 | } |
| 808 | return 0; |
| 809 | } |
| 810 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 811 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 812 | /* Search the path (default sys.path) for a module. Return the |
| 813 | corresponding filedescr struct, and (via return arguments) the |
| 814 | pathname and an open file. Return NULL if the module is not found. */ |
| 815 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 816 | #ifdef MS_COREDLL |
| 817 | extern FILE *PyWin_FindRegisteredModule(); |
| 818 | #endif |
| 819 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 820 | #ifdef CHECK_IMPORT_CASE |
| 821 | static int check_case(char *, int, int, char *); |
| 822 | #endif |
| 823 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 824 | static int find_init_module Py_PROTO((char *)); /* Forward */ |
| 825 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 826 | static struct filedescr * |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 827 | find_module(realname, path, buf, buflen, p_fp) |
| 828 | char *realname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 829 | PyObject *path; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 830 | /* Output parameters: */ |
| 831 | char *buf; |
| 832 | int buflen; |
| 833 | FILE **p_fp; |
| 834 | { |
| 835 | int i, npath, len, namelen; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 836 | struct _frozen *f; |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 837 | struct filedescr *fdp = NULL; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 838 | FILE *fp = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 839 | struct stat statbuf; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 840 | static struct filedescr fd_frozen = {"", "", PY_FROZEN}; |
| 841 | static struct filedescr fd_builtin = {"", "", C_BUILTIN}; |
| 842 | static struct filedescr fd_package = {"", "", PKG_DIRECTORY}; |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 843 | char name[MAXPATHLEN+1]; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 845 | strcpy(name, realname); |
| 846 | |
| 847 | if (path != NULL && PyString_Check(path)) { |
| 848 | /* Submodule of "frozen" package: |
| 849 | Set name to the fullname, path to NULL |
| 850 | and continue as "usual" */ |
| 851 | if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) { |
| 852 | PyErr_SetString(PyExc_ImportError, |
| 853 | "full frozen module name too long"); |
| 854 | return NULL; |
| 855 | } |
| 856 | strcpy(buf, PyString_AsString(path)); |
| 857 | strcat(buf, "."); |
| 858 | strcat(buf, name); |
| 859 | strcpy(name, buf); |
| 860 | path = NULL; |
| 861 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 862 | if (path == NULL) { |
| 863 | if (is_builtin(name)) { |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 864 | strcpy(buf, name); |
| 865 | return &fd_builtin; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 866 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 867 | if ((f = find_frozen(name)) != NULL) { |
| 868 | strcpy(buf, name); |
| 869 | return &fd_frozen; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 870 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 871 | |
Guido van Rossum | ac27910 | 1996-08-22 23:10:58 +0000 | [diff] [blame] | 872 | #ifdef MS_COREDLL |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 873 | fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen); |
| 874 | if (fp != NULL) { |
| 875 | *p_fp = fp; |
| 876 | return fdp; |
| 877 | } |
Guido van Rossum | a5a3db7 | 1996-04-09 02:39:59 +0000 | [diff] [blame] | 878 | #endif |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 879 | path = PySys_GetObject("path"); |
| 880 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 881 | if (path == NULL || !PyList_Check(path)) { |
| 882 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 883 | "sys.path must be a list of directory names"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 884 | return NULL; |
| 885 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 886 | npath = PyList_Size(path); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 887 | namelen = strlen(name); |
| 888 | for (i = 0; i < npath; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 889 | PyObject *v = PyList_GetItem(path, i); |
| 890 | if (!PyString_Check(v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 891 | continue; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 892 | len = PyString_Size(v); |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 893 | if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 894 | continue; /* Too long */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 895 | strcpy(buf, PyString_AsString(v)); |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 896 | if ((int)strlen(buf) != len) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 897 | continue; /* v contains '\0' */ |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 898 | #ifdef macintosh |
Guido van Rossum | 741689d | 1997-08-12 14:53:39 +0000 | [diff] [blame] | 899 | #ifdef INTERN_STRINGS |
| 900 | /* |
| 901 | ** Speedup: each sys.path item is interned, and |
| 902 | ** FindResourceModule remembers which items refer to |
| 903 | ** folders (so we don't have to bother trying to look |
| 904 | ** into them for resources). |
| 905 | */ |
| 906 | PyString_InternInPlace(&PyList_GET_ITEM(path, i)); |
| 907 | v = PyList_GET_ITEM(path, i); |
| 908 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 909 | if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 910 | static struct filedescr resfiledescr = |
| 911 | {"", "", PY_RESOURCE}; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 912 | |
| 913 | return &resfiledescr; |
| 914 | } |
Guido van Rossum | 0f84a34 | 1998-08-06 13:36:01 +0000 | [diff] [blame] | 915 | if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) { |
| 916 | static struct filedescr resfiledescr = |
| 917 | {"", "", PY_CODERESOURCE}; |
| 918 | |
| 919 | return &resfiledescr; |
| 920 | } |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 921 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 922 | if (len > 0 && buf[len-1] != SEP |
| 923 | #ifdef ALTSEP |
| 924 | && buf[len-1] != ALTSEP |
| 925 | #endif |
| 926 | ) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 927 | buf[len++] = SEP; |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 928 | #ifdef IMPORT_8x3_NAMES |
Guido van Rossum | e8c27bb | 1997-12-17 18:57:16 +0000 | [diff] [blame] | 929 | /* see if we are searching in directory dos-8x3 */ |
| 930 | if (len > 7 && !strncmp(buf + len - 8, "dos-8x3", 7)){ |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 931 | int j; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 932 | char ch; /* limit name to 8 lower-case characters */ |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 933 | for (j = 0; (ch = name[j]) && j < 8; j++) |
| 934 | if (isupper(ch)) |
| 935 | buf[len++] = tolower(ch); |
| 936 | else |
| 937 | buf[len++] = ch; |
| 938 | } |
Guido van Rossum | e8c27bb | 1997-12-17 18:57:16 +0000 | [diff] [blame] | 939 | else /* Not in dos-8x3, use the full name */ |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 940 | #endif |
Guido van Rossum | 0e41c8c | 1996-06-20 14:18:34 +0000 | [diff] [blame] | 941 | { |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 942 | strcpy(buf+len, name); |
| 943 | len += namelen; |
| 944 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 945 | #ifdef HAVE_STAT |
| 946 | if (stat(buf, &statbuf) == 0) { |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 947 | if (S_ISDIR(statbuf.st_mode)) { |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 948 | if (find_init_module(buf)) { |
| 949 | #ifdef CHECK_IMPORT_CASE |
| 950 | if (!check_case(buf, len, namelen, |
| 951 | name)) |
| 952 | return NULL; |
| 953 | #endif |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 954 | return &fd_package; |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 955 | } |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 956 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 957 | } |
| 958 | #else |
| 959 | /* XXX How are you going to test for directories? */ |
| 960 | #endif |
Guido van Rossum | 9a61dc9 | 1997-10-08 15:25:08 +0000 | [diff] [blame] | 961 | #ifdef macintosh |
| 962 | fdp = PyMac_FindModuleExtension(buf, &len, name); |
| 963 | if (fdp) |
| 964 | fp = fopen(buf, fdp->mode); |
| 965 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 966 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 967 | strcpy(buf+len, fdp->suffix); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 968 | if (Py_VerboseFlag > 1) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 969 | PySys_WriteStderr("# trying %s\n", buf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 970 | fp = fopen(buf, fdp->mode); |
| 971 | if (fp != NULL) |
| 972 | break; |
| 973 | } |
Guido van Rossum | 741689d | 1997-08-12 14:53:39 +0000 | [diff] [blame] | 974 | #endif /* !macintosh */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 975 | if (fp != NULL) |
| 976 | break; |
| 977 | } |
| 978 | if (fp == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 979 | PyErr_Format(PyExc_ImportError, |
| 980 | "No module named %.200s", name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 981 | return NULL; |
| 982 | } |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 983 | #ifdef CHECK_IMPORT_CASE |
| 984 | if (!check_case(buf, len, namelen, name)) { |
| 985 | fclose(fp); |
| 986 | return NULL; |
| 987 | } |
| 988 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 989 | |
| 990 | *p_fp = fp; |
| 991 | return fdp; |
| 992 | } |
| 993 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 994 | #ifdef CHECK_IMPORT_CASE |
| 995 | |
| 996 | #ifdef MS_WIN32 |
| 997 | #include <windows.h> |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 998 | #include <ctype.h> |
| 999 | |
| 1000 | static int |
| 1001 | allcaps8x3(s) |
| 1002 | char *s; |
| 1003 | { |
| 1004 | /* Return 1 if s is an 8.3 filename in ALLCAPS */ |
| 1005 | char c; |
| 1006 | char *dot = strchr(s, '.'); |
| 1007 | char *end = strchr(s, '\0'); |
| 1008 | if (dot != NULL) { |
| 1009 | if (dot-s > 8) |
| 1010 | return 1; /* More than 8 before '.' */ |
| 1011 | if (end-dot > 4) |
| 1012 | return 1; /* More than 3 after '.' */ |
| 1013 | end = strchr(dot+1, '.'); |
| 1014 | if (end != NULL) |
| 1015 | return 1; /* More than one dot */ |
| 1016 | } |
| 1017 | else if (end-s > 8) |
| 1018 | return 1; /* More than 8 and no dot */ |
| 1019 | while ((c = *s++)) { |
| 1020 | if (islower(c)) |
| 1021 | return 0; |
| 1022 | } |
| 1023 | return 1; |
| 1024 | } |
| 1025 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1026 | static int |
| 1027 | check_case(char *buf, int len, int namelen, char *name) |
| 1028 | { |
| 1029 | WIN32_FIND_DATA data; |
| 1030 | HANDLE h; |
| 1031 | if (getenv("PYTHONCASEOK") != NULL) |
| 1032 | return 1; |
| 1033 | h = FindFirstFile(buf, &data); |
| 1034 | if (h == INVALID_HANDLE_VALUE) { |
| 1035 | PyErr_Format(PyExc_NameError, |
| 1036 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1037 | name, buf); |
| 1038 | return 0; |
| 1039 | } |
| 1040 | FindClose(h); |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 1041 | if (allcaps8x3(data.cFileName)) { |
| 1042 | /* Skip the test if the filename is ALL.CAPS. This can |
| 1043 | happen in certain circumstances beyond our control, |
| 1044 | e.g. when software is installed under NT on a FAT |
| 1045 | filesystem and then the same FAT filesystem is used |
| 1046 | under Windows 95. */ |
| 1047 | return 1; |
| 1048 | } |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1049 | if (strncmp(data.cFileName, name, namelen) != 0) { |
| 1050 | strcpy(buf+len-namelen, data.cFileName); |
| 1051 | PyErr_Format(PyExc_NameError, |
| 1052 | "Case mismatch for module name %.100s\n(filename %.300s)", |
| 1053 | name, buf); |
| 1054 | return 0; |
| 1055 | } |
| 1056 | return 1; |
| 1057 | } |
| 1058 | #endif /* MS_WIN32 */ |
| 1059 | |
| 1060 | #ifdef macintosh |
| 1061 | #include <TextUtils.h> |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 1062 | #ifdef USE_GUSI |
| 1063 | #include "TFileSpec.h" /* for Path2FSSpec() */ |
| 1064 | #endif |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1065 | static int |
| 1066 | check_case(char *buf, int len, int namelen, char *name) |
| 1067 | { |
| 1068 | FSSpec fss; |
| 1069 | OSErr err; |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 1070 | #ifndef USE_GUSI |
| 1071 | err = FSMakeFSSpec(0, 0, Pstring(buf), &fss); |
| 1072 | #else |
| 1073 | /* GUSI's Path2FSSpec() resolves all possible aliases nicely on |
| 1074 | the way, which is fine for all directories, but here we need |
| 1075 | the original name of the alias file (say, Dlg.ppc.slb, not |
| 1076 | toolboxmodules.ppc.slb). */ |
| 1077 | char *colon; |
| 1078 | err = Path2FSSpec(buf, &fss); |
| 1079 | if (err == noErr) { |
| 1080 | colon = strrchr(buf, ':'); /* find filename */ |
| 1081 | if (colon != NULL) |
| 1082 | err = FSMakeFSSpec(fss.vRefNum, fss.parID, |
| 1083 | Pstring(colon+1), &fss); |
| 1084 | else |
| 1085 | err = FSMakeFSSpec(fss.vRefNum, fss.parID, |
| 1086 | fss.name, &fss); |
| 1087 | } |
| 1088 | #endif |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1089 | if (err) { |
| 1090 | PyErr_Format(PyExc_NameError, |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 1091 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1092 | name, buf); |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | p2cstr(fss.name); |
| 1096 | if ( strncmp(name, (char *)fss.name, namelen) != 0 ) { |
| 1097 | PyErr_Format(PyExc_NameError, |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 1098 | "Case mismatch for module name %.100s\n(filename %.300s)", |
| 1099 | name, fss.name); |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1100 | return 0; |
| 1101 | } |
| 1102 | return 1; |
| 1103 | } |
| 1104 | #endif /* macintosh */ |
| 1105 | |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1106 | #ifdef DJGPP |
Guido van Rossum | aeca79b | 1998-02-18 16:21:00 +0000 | [diff] [blame] | 1107 | #include <dir.h> |
| 1108 | |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1109 | static int |
| 1110 | check_case(char *buf, int len, int namelen, char *name) |
| 1111 | { |
| 1112 | struct ffblk ffblk; |
| 1113 | int done; |
| 1114 | |
| 1115 | if (getenv("PYTHONCASEOK") != NULL) |
| 1116 | return 1; |
Guido van Rossum | aeca79b | 1998-02-18 16:21:00 +0000 | [diff] [blame] | 1117 | done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC); |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1118 | if (done) { |
| 1119 | PyErr_Format(PyExc_NameError, |
| 1120 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1121 | name, buf); |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | if (strncmp(ffblk.ff_name, name, namelen) != 0) { |
| 1126 | strcpy(buf+len-namelen, ffblk.ff_name); |
| 1127 | PyErr_Format(PyExc_NameError, |
| 1128 | "Case mismatch for module name %.100s\n(filename %.300s)", |
| 1129 | name, buf); |
| 1130 | return 0; |
| 1131 | } |
| 1132 | return 1; |
| 1133 | } |
| 1134 | #endif |
| 1135 | |
Guido van Rossum | 8766a77 | 1998-04-10 23:44:32 +0000 | [diff] [blame] | 1136 | #endif /* CHECK_IMPORT_CASE */ |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1137 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1138 | #ifdef HAVE_STAT |
| 1139 | /* Helper to look for __init__.py or __init__.py[co] in potential package */ |
| 1140 | static int |
| 1141 | find_init_module(buf) |
| 1142 | char *buf; |
| 1143 | { |
| 1144 | int save_len = strlen(buf); |
| 1145 | int i = save_len; |
| 1146 | struct stat statbuf; |
| 1147 | |
| 1148 | if (save_len + 13 >= MAXPATHLEN) |
| 1149 | return 0; |
| 1150 | buf[i++] = SEP; |
| 1151 | strcpy(buf+i, "__init__.py"); |
| 1152 | if (stat(buf, &statbuf) == 0) { |
| 1153 | buf[save_len] = '\0'; |
| 1154 | return 1; |
| 1155 | } |
| 1156 | i += strlen(buf+i); |
| 1157 | if (Py_OptimizeFlag) |
| 1158 | strcpy(buf+i, "o"); |
| 1159 | else |
| 1160 | strcpy(buf+i, "c"); |
| 1161 | if (stat(buf, &statbuf) == 0) { |
| 1162 | buf[save_len] = '\0'; |
| 1163 | return 1; |
| 1164 | } |
| 1165 | buf[save_len] = '\0'; |
| 1166 | return 0; |
| 1167 | } |
| 1168 | #endif /* HAVE_STAT */ |
| 1169 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1170 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1171 | static int init_builtin Py_PROTO((char *)); /* Forward */ |
| 1172 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1173 | /* Load an external module using the default search path and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1174 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1175 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1176 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1177 | load_module(name, fp, buf, type) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1178 | char *name; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1179 | FILE *fp; |
| 1180 | char *buf; |
| 1181 | int type; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1182 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1183 | PyObject *modules; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1184 | PyObject *m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1185 | int err; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1186 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1187 | /* First check that there's an open file (if we need one) */ |
| 1188 | switch (type) { |
| 1189 | case PY_SOURCE: |
| 1190 | case PY_COMPILED: |
| 1191 | if (fp == NULL) { |
| 1192 | PyErr_Format(PyExc_ValueError, |
| 1193 | "file object required for import (type code %d)", |
| 1194 | type); |
| 1195 | return NULL; |
| 1196 | } |
| 1197 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1198 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1199 | switch (type) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1200 | |
| 1201 | case PY_SOURCE: |
| 1202 | m = load_source_module(name, buf, fp); |
| 1203 | break; |
| 1204 | |
| 1205 | case PY_COMPILED: |
| 1206 | m = load_compiled_module(name, buf, fp); |
| 1207 | break; |
| 1208 | |
| 1209 | case C_EXTENSION: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1210 | m = _PyImport_LoadDynamicModule(name, buf, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1211 | break; |
| 1212 | |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 1213 | #ifdef macintosh |
| 1214 | case PY_RESOURCE: |
| 1215 | m = PyMac_LoadResourceModule(name, buf); |
| 1216 | break; |
Guido van Rossum | 0f84a34 | 1998-08-06 13:36:01 +0000 | [diff] [blame] | 1217 | case PY_CODERESOURCE: |
| 1218 | m = PyMac_LoadCodeResourceModule(name, buf); |
| 1219 | break; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 1220 | #endif |
| 1221 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1222 | case PKG_DIRECTORY: |
| 1223 | m = load_package(name, buf); |
| 1224 | break; |
| 1225 | |
| 1226 | case C_BUILTIN: |
| 1227 | case PY_FROZEN: |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1228 | if (buf != NULL && buf[0] != '\0') |
| 1229 | name = buf; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1230 | if (type == C_BUILTIN) |
| 1231 | err = init_builtin(name); |
| 1232 | else |
| 1233 | err = PyImport_ImportFrozenModule(name); |
| 1234 | if (err < 0) |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1235 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1236 | if (err == 0) { |
| 1237 | PyErr_Format(PyExc_ImportError, |
| 1238 | "Purported %s module %.200s not found", |
| 1239 | type == C_BUILTIN ? |
| 1240 | "builtin" : "frozen", |
| 1241 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1242 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1243 | } |
| 1244 | modules = PyImport_GetModuleDict(); |
| 1245 | m = PyDict_GetItemString(modules, name); |
| 1246 | if (m == NULL) { |
| 1247 | PyErr_Format( |
| 1248 | PyExc_ImportError, |
| 1249 | "%s module %.200s not properly initialized", |
| 1250 | type == C_BUILTIN ? |
| 1251 | "builtin" : "frozen", |
| 1252 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1253 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1254 | } |
| 1255 | Py_INCREF(m); |
| 1256 | break; |
| 1257 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1258 | default: |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1259 | PyErr_Format(PyExc_ImportError, |
| 1260 | "Don't know how to import %.200s (type code %d)", |
| 1261 | name, type); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1262 | m = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1263 | |
| 1264 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1265 | |
| 1266 | return m; |
| 1267 | } |
| 1268 | |
| 1269 | |
| 1270 | /* Initialize a built-in module. |
| 1271 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 1272 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1273 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1274 | static int |
| 1275 | init_builtin(name) |
| 1276 | char *name; |
| 1277 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1278 | struct _inittab *p; |
| 1279 | PyObject *mod; |
| 1280 | |
| 1281 | if ((mod = _PyImport_FindExtension(name, name)) != NULL) |
| 1282 | return 1; |
| 1283 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 1284 | for (p = PyImport_Inittab; p->name != NULL; p++) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1285 | if (strcmp(name, p->name) == 0) { |
| 1286 | if (p->initfunc == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1287 | PyErr_Format(PyExc_ImportError, |
| 1288 | "Cannot re-init internal module %.200s", |
| 1289 | name); |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1290 | return -1; |
| 1291 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1292 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1293 | PySys_WriteStderr("import %s # builtin\n", name); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1294 | (*p->initfunc)(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1295 | if (PyErr_Occurred()) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1296 | return -1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1297 | if (_PyImport_FixupExtension(name, name) == NULL) |
| 1298 | return -1; |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1299 | return 1; |
| 1300 | } |
| 1301 | } |
| 1302 | return 0; |
| 1303 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1304 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1305 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1306 | /* Frozen modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1307 | |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1308 | static struct _frozen * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1309 | find_frozen(name) |
| 1310 | char *name; |
| 1311 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1312 | struct _frozen *p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1313 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1314 | for (p = PyImport_FrozenModules; ; p++) { |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1315 | if (p->name == NULL) |
| 1316 | return NULL; |
| 1317 | if (strcmp(p->name, name) == 0) |
| 1318 | break; |
| 1319 | } |
| 1320 | return p; |
| 1321 | } |
| 1322 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1323 | static PyObject * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1324 | get_frozen_object(name) |
| 1325 | char *name; |
| 1326 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1327 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1328 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1329 | |
| 1330 | if (p == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1331 | PyErr_Format(PyExc_ImportError, |
| 1332 | "No such frozen object named %.200s", |
| 1333 | name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1334 | return NULL; |
| 1335 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1336 | size = p->size; |
| 1337 | if (size < 0) |
| 1338 | size = -size; |
| 1339 | return PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* Initialize a frozen module. |
| 1343 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 1344 | an exception set if the initialization failed. |
| 1345 | This function is also used from frozenmain.c */ |
Guido van Rossum | 0b34490 | 1995-02-07 15:35:27 +0000 | [diff] [blame] | 1346 | |
| 1347 | int |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1348 | PyImport_ImportFrozenModule(name) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1349 | char *name; |
| 1350 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1351 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1352 | PyObject *co; |
| 1353 | PyObject *m; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1354 | int ispackage; |
| 1355 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (p == NULL) |
| 1358 | return 0; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1359 | size = p->size; |
| 1360 | ispackage = (size < 0); |
| 1361 | if (ispackage) |
| 1362 | size = -size; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1363 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1364 | PySys_WriteStderr("import %s # frozen%s\n", |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1365 | name, ispackage ? " package" : ""); |
| 1366 | co = PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1367 | if (co == NULL) |
| 1368 | return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1369 | if (!PyCode_Check(co)) { |
| 1370 | Py_DECREF(co); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1371 | PyErr_Format(PyExc_TypeError, |
| 1372 | "frozen object %.200s is not a code object", |
| 1373 | name); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1374 | return -1; |
| 1375 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1376 | if (ispackage) { |
| 1377 | /* Set __path__ to the package name */ |
| 1378 | PyObject *d, *s; |
| 1379 | int err; |
| 1380 | m = PyImport_AddModule(name); |
| 1381 | if (m == NULL) |
| 1382 | return -1; |
| 1383 | d = PyModule_GetDict(m); |
| 1384 | s = PyString_InternFromString(name); |
| 1385 | if (s == NULL) |
| 1386 | return -1; |
| 1387 | err = PyDict_SetItemString(d, "__path__", s); |
| 1388 | Py_DECREF(s); |
| 1389 | if (err != 0) |
| 1390 | return err; |
| 1391 | } |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 1392 | m = PyImport_ExecCodeModuleEx(name, co, "<frozen>"); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1393 | Py_DECREF(co); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1394 | if (m == NULL) |
| 1395 | return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1396 | Py_DECREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1397 | return 1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1398 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1399 | |
| 1400 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1401 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1402 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1403 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1404 | PyObject * |
| 1405 | PyImport_ImportModule(name) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1406 | char *name; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1407 | { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1408 | static PyObject *fromlist = NULL; |
| 1409 | if (fromlist == NULL && strchr(name, '.') != NULL) { |
| 1410 | fromlist = Py_BuildValue("[s]", "*"); |
| 1411 | if (fromlist == NULL) |
| 1412 | return NULL; |
| 1413 | } |
| 1414 | return PyImport_ImportModuleEx(name, NULL, NULL, fromlist); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1417 | /* Forward declarations for helper routines */ |
| 1418 | static PyObject *get_parent Py_PROTO((PyObject *globals, |
| 1419 | char *buf, int *p_buflen)); |
| 1420 | static PyObject *load_next Py_PROTO((PyObject *mod, PyObject *altmod, |
| 1421 | char **p_name, char *buf, int *p_buflen)); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1422 | static int mark_miss Py_PROTO((char *name)); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1423 | static int ensure_fromlist Py_PROTO((PyObject *mod, PyObject *fromlist, |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1424 | char *buf, int buflen, int recursive)); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1425 | static PyObject * import_submodule Py_PROTO((PyObject *mod, |
| 1426 | char *name, char *fullname)); |
| 1427 | |
| 1428 | /* The Magnum Opus of dotted-name import :-) */ |
| 1429 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1430 | static PyObject * |
| 1431 | import_module_ex(name, globals, locals, fromlist) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1432 | char *name; |
| 1433 | PyObject *globals; |
| 1434 | PyObject *locals; |
| 1435 | PyObject *fromlist; |
| 1436 | { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1437 | char buf[MAXPATHLEN+1]; |
| 1438 | int buflen = 0; |
| 1439 | PyObject *parent, *head, *next, *tail; |
| 1440 | |
| 1441 | parent = get_parent(globals, buf, &buflen); |
| 1442 | if (parent == NULL) |
| 1443 | return NULL; |
| 1444 | |
| 1445 | head = load_next(parent, Py_None, &name, buf, &buflen); |
| 1446 | if (head == NULL) |
| 1447 | return NULL; |
| 1448 | |
| 1449 | tail = head; |
| 1450 | Py_INCREF(tail); |
| 1451 | while (name) { |
| 1452 | next = load_next(tail, tail, &name, buf, &buflen); |
| 1453 | Py_DECREF(tail); |
| 1454 | if (next == NULL) { |
| 1455 | Py_DECREF(head); |
| 1456 | return NULL; |
| 1457 | } |
| 1458 | tail = next; |
| 1459 | } |
| 1460 | |
| 1461 | if (fromlist != NULL) { |
| 1462 | if (fromlist == Py_None || !PyObject_IsTrue(fromlist)) |
| 1463 | fromlist = NULL; |
| 1464 | } |
| 1465 | |
| 1466 | if (fromlist == NULL) { |
| 1467 | Py_DECREF(tail); |
| 1468 | return head; |
| 1469 | } |
| 1470 | |
| 1471 | Py_DECREF(head); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1472 | if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1473 | Py_DECREF(tail); |
| 1474 | return NULL; |
| 1475 | } |
| 1476 | |
| 1477 | return tail; |
| 1478 | } |
| 1479 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1480 | PyObject * |
| 1481 | PyImport_ImportModuleEx(name, globals, locals, fromlist) |
| 1482 | char *name; |
| 1483 | PyObject *globals; |
| 1484 | PyObject *locals; |
| 1485 | PyObject *fromlist; |
| 1486 | { |
| 1487 | PyObject *result; |
| 1488 | lock_import(); |
Guido van Rossum | d65911b | 1998-03-03 22:33:27 +0000 | [diff] [blame] | 1489 | result = import_module_ex(name, globals, locals, fromlist); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1490 | unlock_import(); |
| 1491 | return result; |
| 1492 | } |
| 1493 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1494 | static PyObject * |
| 1495 | get_parent(globals, buf, p_buflen) |
| 1496 | PyObject *globals; |
| 1497 | char *buf; |
| 1498 | int *p_buflen; |
| 1499 | { |
| 1500 | static PyObject *namestr = NULL; |
| 1501 | static PyObject *pathstr = NULL; |
| 1502 | PyObject *modname, *modpath, *modules, *parent; |
| 1503 | |
| 1504 | if (globals == NULL || !PyDict_Check(globals)) |
| 1505 | return Py_None; |
| 1506 | |
| 1507 | if (namestr == NULL) { |
| 1508 | namestr = PyString_InternFromString("__name__"); |
| 1509 | if (namestr == NULL) |
| 1510 | return NULL; |
| 1511 | } |
| 1512 | if (pathstr == NULL) { |
| 1513 | pathstr = PyString_InternFromString("__path__"); |
| 1514 | if (pathstr == NULL) |
| 1515 | return NULL; |
| 1516 | } |
| 1517 | |
| 1518 | *buf = '\0'; |
| 1519 | *p_buflen = 0; |
| 1520 | modname = PyDict_GetItem(globals, namestr); |
| 1521 | if (modname == NULL || !PyString_Check(modname)) |
| 1522 | return Py_None; |
| 1523 | |
| 1524 | modpath = PyDict_GetItem(globals, pathstr); |
| 1525 | if (modpath != NULL) { |
| 1526 | int len = PyString_GET_SIZE(modname); |
| 1527 | if (len > MAXPATHLEN) { |
| 1528 | PyErr_SetString(PyExc_ValueError, |
| 1529 | "Module name too long"); |
| 1530 | return NULL; |
| 1531 | } |
| 1532 | strcpy(buf, PyString_AS_STRING(modname)); |
| 1533 | *p_buflen = len; |
| 1534 | } |
| 1535 | else { |
| 1536 | char *start = PyString_AS_STRING(modname); |
| 1537 | char *lastdot = strrchr(start, '.'); |
| 1538 | int len; |
| 1539 | if (lastdot == NULL) |
| 1540 | return Py_None; |
| 1541 | len = lastdot - start; |
| 1542 | if (len >= MAXPATHLEN) { |
| 1543 | PyErr_SetString(PyExc_ValueError, |
| 1544 | "Module name too long"); |
| 1545 | return NULL; |
| 1546 | } |
| 1547 | strncpy(buf, start, len); |
| 1548 | buf[len] = '\0'; |
| 1549 | *p_buflen = len; |
| 1550 | } |
| 1551 | |
| 1552 | modules = PyImport_GetModuleDict(); |
| 1553 | parent = PyDict_GetItemString(modules, buf); |
| 1554 | if (parent == NULL) |
| 1555 | parent = Py_None; |
| 1556 | return parent; |
| 1557 | /* We expect, but can't guarantee, if parent != None, that: |
| 1558 | - parent.__name__ == buf |
| 1559 | - parent.__dict__ is globals |
| 1560 | If this is violated... Who cares? */ |
| 1561 | } |
| 1562 | |
| 1563 | static PyObject * |
| 1564 | load_next(mod, altmod, p_name, buf, p_buflen) |
| 1565 | PyObject *mod; |
| 1566 | PyObject *altmod; /* Either None or same as mod */ |
| 1567 | char **p_name; |
| 1568 | char *buf; |
| 1569 | int *p_buflen; |
| 1570 | { |
| 1571 | char *name = *p_name; |
| 1572 | char *dot = strchr(name, '.'); |
| 1573 | int len; |
| 1574 | char *p; |
| 1575 | PyObject *result; |
| 1576 | |
| 1577 | if (dot == NULL) { |
| 1578 | *p_name = NULL; |
| 1579 | len = strlen(name); |
| 1580 | } |
| 1581 | else { |
| 1582 | *p_name = dot+1; |
| 1583 | len = dot-name; |
| 1584 | } |
Guido van Rossum | 111c20b | 1998-04-11 17:38:22 +0000 | [diff] [blame] | 1585 | if (len == 0) { |
| 1586 | PyErr_SetString(PyExc_ValueError, |
| 1587 | "Empty module name"); |
| 1588 | return NULL; |
| 1589 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1590 | |
| 1591 | p = buf + *p_buflen; |
| 1592 | if (p != buf) |
| 1593 | *p++ = '.'; |
| 1594 | if (p+len-buf >= MAXPATHLEN) { |
| 1595 | PyErr_SetString(PyExc_ValueError, |
| 1596 | "Module name too long"); |
| 1597 | return NULL; |
| 1598 | } |
| 1599 | strncpy(p, name, len); |
| 1600 | p[len] = '\0'; |
| 1601 | *p_buflen = p+len-buf; |
| 1602 | |
| 1603 | result = import_submodule(mod, p, buf); |
| 1604 | if (result == Py_None && altmod != mod) { |
| 1605 | Py_DECREF(result); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1606 | /* Here, altmod must be None and mod must not be None */ |
Guido van Rossum | 0c81945 | 1997-09-07 06:16:57 +0000 | [diff] [blame] | 1607 | result = import_submodule(altmod, p, p); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1608 | if (result != NULL && result != Py_None) { |
| 1609 | if (mark_miss(buf) != 0) { |
| 1610 | Py_DECREF(result); |
| 1611 | return NULL; |
| 1612 | } |
| 1613 | strncpy(buf, name, len); |
| 1614 | buf[len] = '\0'; |
| 1615 | *p_buflen = len; |
| 1616 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1617 | } |
| 1618 | if (result == NULL) |
| 1619 | return NULL; |
| 1620 | |
| 1621 | if (result == Py_None) { |
| 1622 | Py_DECREF(result); |
| 1623 | PyErr_Format(PyExc_ImportError, |
| 1624 | "No module named %.200s", name); |
| 1625 | return NULL; |
| 1626 | } |
| 1627 | |
| 1628 | return result; |
| 1629 | } |
| 1630 | |
| 1631 | static int |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1632 | mark_miss(name) |
| 1633 | char *name; |
| 1634 | { |
| 1635 | PyObject *modules = PyImport_GetModuleDict(); |
| 1636 | return PyDict_SetItemString(modules, name, Py_None); |
| 1637 | } |
| 1638 | |
| 1639 | static int |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1640 | ensure_fromlist(mod, fromlist, buf, buflen, recursive) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1641 | PyObject *mod; |
| 1642 | PyObject *fromlist; |
| 1643 | char *buf; |
| 1644 | int buflen; |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1645 | int recursive; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1646 | { |
| 1647 | int i; |
| 1648 | |
| 1649 | if (!PyObject_HasAttrString(mod, "__path__")) |
| 1650 | return 1; |
| 1651 | |
| 1652 | for (i = 0; ; i++) { |
| 1653 | PyObject *item = PySequence_GetItem(fromlist, i); |
| 1654 | int hasit; |
| 1655 | if (item == NULL) { |
| 1656 | if (PyErr_ExceptionMatches(PyExc_IndexError)) { |
| 1657 | PyErr_Clear(); |
| 1658 | return 1; |
| 1659 | } |
| 1660 | return 0; |
| 1661 | } |
| 1662 | if (!PyString_Check(item)) { |
| 1663 | PyErr_SetString(PyExc_TypeError, |
| 1664 | "Item in ``from list'' not a string"); |
| 1665 | Py_DECREF(item); |
| 1666 | return 0; |
| 1667 | } |
| 1668 | if (PyString_AS_STRING(item)[0] == '*') { |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1669 | PyObject *all; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1670 | Py_DECREF(item); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1671 | /* See if the package defines __all__ */ |
| 1672 | if (recursive) |
| 1673 | continue; /* Avoid endless recursion */ |
| 1674 | all = PyObject_GetAttrString(mod, "__all__"); |
| 1675 | if (all == NULL) |
| 1676 | PyErr_Clear(); |
| 1677 | else { |
| 1678 | if (!ensure_fromlist(mod, all, buf, buflen, 1)) |
| 1679 | return 0; |
| 1680 | Py_DECREF(all); |
| 1681 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1682 | continue; |
| 1683 | } |
| 1684 | hasit = PyObject_HasAttr(mod, item); |
| 1685 | if (!hasit) { |
| 1686 | char *subname = PyString_AS_STRING(item); |
| 1687 | PyObject *submod; |
| 1688 | char *p; |
| 1689 | if (buflen + strlen(subname) >= MAXPATHLEN) { |
| 1690 | PyErr_SetString(PyExc_ValueError, |
| 1691 | "Module name too long"); |
| 1692 | Py_DECREF(item); |
| 1693 | return 0; |
| 1694 | } |
| 1695 | p = buf + buflen; |
| 1696 | *p++ = '.'; |
| 1697 | strcpy(p, subname); |
| 1698 | submod = import_submodule(mod, subname, buf); |
| 1699 | Py_XDECREF(submod); |
| 1700 | if (submod == NULL) { |
| 1701 | Py_DECREF(item); |
| 1702 | return 0; |
| 1703 | } |
| 1704 | } |
| 1705 | Py_DECREF(item); |
| 1706 | } |
| 1707 | |
Guido van Rossum | a7f2e81 | 1997-10-03 15:33:32 +0000 | [diff] [blame] | 1708 | /* NOTREACHED */ |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | static PyObject * |
| 1712 | import_submodule(mod, subname, fullname) |
| 1713 | PyObject *mod; /* May be None */ |
| 1714 | char *subname; |
| 1715 | char *fullname; |
| 1716 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1717 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1718 | PyObject *m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1719 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1720 | /* Require: |
| 1721 | if mod == None: subname == fullname |
| 1722 | else: mod.__name__ + "." + subname == fullname |
| 1723 | */ |
| 1724 | |
| 1725 | if ((m = PyDict_GetItemString(modules, fullname)) != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1726 | Py_INCREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1727 | } |
| 1728 | else { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1729 | PyObject *path; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1730 | char buf[MAXPATHLEN+1]; |
| 1731 | struct filedescr *fdp; |
| 1732 | FILE *fp = NULL; |
| 1733 | |
Guido van Rossum | 9c0afe5 | 1998-05-19 15:09:05 +0000 | [diff] [blame] | 1734 | if (mod == Py_None) |
| 1735 | path = NULL; |
| 1736 | else { |
| 1737 | path = PyObject_GetAttrString(mod, "__path__"); |
| 1738 | if (path == NULL) { |
| 1739 | PyErr_Clear(); |
| 1740 | Py_INCREF(Py_None); |
| 1741 | return Py_None; |
| 1742 | } |
| 1743 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1744 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1745 | buf[0] = '\0'; |
Guido van Rossum | b68cd42 | 1998-07-01 17:36:26 +0000 | [diff] [blame] | 1746 | fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp); |
| 1747 | Py_XDECREF(path); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1748 | if (fdp == NULL) { |
| 1749 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) |
| 1750 | return NULL; |
| 1751 | PyErr_Clear(); |
| 1752 | Py_INCREF(Py_None); |
| 1753 | return Py_None; |
| 1754 | } |
| 1755 | m = load_module(fullname, fp, buf, fdp->type); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1756 | if (fp) |
| 1757 | fclose(fp); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1758 | if (m != NULL && mod != Py_None) { |
| 1759 | if (PyObject_SetAttrString(mod, subname, m) < 0) { |
| 1760 | Py_DECREF(m); |
| 1761 | m = NULL; |
| 1762 | } |
| 1763 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1764 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1765 | |
| 1766 | return m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1769 | |
| 1770 | /* Re-import a module of any kind and return its module object, WITH |
| 1771 | INCREMENTED REFERENCE COUNT */ |
| 1772 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1773 | PyObject * |
| 1774 | PyImport_ReloadModule(m) |
| 1775 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1776 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1777 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1778 | PyObject *path = NULL; |
| 1779 | char *name, *subname; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1780 | char buf[MAXPATHLEN+1]; |
| 1781 | struct filedescr *fdp; |
| 1782 | FILE *fp = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1783 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1784 | if (m == NULL || !PyModule_Check(m)) { |
| 1785 | PyErr_SetString(PyExc_TypeError, |
| 1786 | "reload() argument must be module"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1787 | return NULL; |
| 1788 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1789 | name = PyModule_GetName(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1790 | if (name == NULL) |
| 1791 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1792 | if (m != PyDict_GetItemString(modules, name)) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1793 | PyErr_Format(PyExc_ImportError, |
| 1794 | "reload(): module %.200s not in sys.modules", |
| 1795 | name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1796 | return NULL; |
| 1797 | } |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1798 | subname = strrchr(name, '.'); |
| 1799 | if (subname == NULL) |
| 1800 | subname = name; |
| 1801 | else { |
| 1802 | PyObject *parentname, *parent; |
| 1803 | parentname = PyString_FromStringAndSize(name, (subname-name)); |
| 1804 | if (parentname == NULL) |
| 1805 | return NULL; |
| 1806 | parent = PyDict_GetItem(modules, parentname); |
Barry Warsaw | 3879333 | 1999-01-27 17:54:20 +0000 | [diff] [blame] | 1807 | Py_DECREF(parentname); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1808 | if (parent == NULL) { |
| 1809 | PyErr_Format(PyExc_ImportError, |
| 1810 | "reload(): parent %.200s not in sys.modules", |
| 1811 | name); |
| 1812 | return NULL; |
| 1813 | } |
| 1814 | subname++; |
| 1815 | path = PyObject_GetAttrString(parent, "__path__"); |
| 1816 | if (path == NULL) |
| 1817 | PyErr_Clear(); |
| 1818 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1819 | buf[0] = '\0'; |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1820 | fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp); |
| 1821 | Py_XDECREF(path); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1822 | if (fdp == NULL) |
| 1823 | return NULL; |
| 1824 | m = load_module(name, fp, buf, fdp->type); |
| 1825 | if (fp) |
| 1826 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1827 | return m; |
| 1828 | } |
| 1829 | |
| 1830 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1831 | /* Higher-level import emulator which emulates the "import" statement |
| 1832 | more accurately -- it invokes the __import__() function from the |
| 1833 | builtins of the current globals. This means that the import is |
| 1834 | done using whatever import hooks are installed in the current |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 1835 | environment, e.g. by "rexec". |
| 1836 | A dummy list ["__doc__"] is passed as the 4th argument so that |
| 1837 | e.g. PyImport_Import(PyString_FromString("win32com.client.gencache")) |
| 1838 | will return <module "gencache"> instead of <module "win32com">. */ |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1839 | |
| 1840 | PyObject * |
| 1841 | PyImport_Import(module_name) |
| 1842 | PyObject *module_name; |
| 1843 | { |
| 1844 | static PyObject *silly_list = NULL; |
| 1845 | static PyObject *builtins_str = NULL; |
| 1846 | static PyObject *import_str = NULL; |
| 1847 | static PyObject *standard_builtins = NULL; |
| 1848 | PyObject *globals = NULL; |
| 1849 | PyObject *import = NULL; |
| 1850 | PyObject *builtins = NULL; |
| 1851 | PyObject *r = NULL; |
| 1852 | |
| 1853 | /* Initialize constant string objects */ |
| 1854 | if (silly_list == NULL) { |
| 1855 | import_str = PyString_InternFromString("__import__"); |
| 1856 | if (import_str == NULL) |
| 1857 | return NULL; |
| 1858 | builtins_str = PyString_InternFromString("__builtins__"); |
| 1859 | if (builtins_str == NULL) |
| 1860 | return NULL; |
| 1861 | silly_list = Py_BuildValue("[s]", "__doc__"); |
| 1862 | if (silly_list == NULL) |
| 1863 | return NULL; |
| 1864 | } |
| 1865 | |
| 1866 | /* Get the builtins from current globals */ |
| 1867 | globals = PyEval_GetGlobals(); |
| 1868 | if(globals != NULL) { |
Guido van Rossum | 6646856 | 1998-10-22 15:46:50 +0000 | [diff] [blame] | 1869 | Py_INCREF(globals); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1870 | builtins = PyObject_GetItem(globals, builtins_str); |
| 1871 | if (builtins == NULL) |
| 1872 | goto err; |
| 1873 | } |
| 1874 | else { |
| 1875 | /* No globals -- use standard builtins, and fake globals */ |
| 1876 | PyErr_Clear(); |
| 1877 | |
| 1878 | if (standard_builtins == NULL) { |
| 1879 | standard_builtins = |
| 1880 | PyImport_ImportModule("__builtin__"); |
| 1881 | if (standard_builtins == NULL) |
| 1882 | return NULL; |
| 1883 | } |
| 1884 | |
| 1885 | builtins = standard_builtins; |
| 1886 | Py_INCREF(builtins); |
| 1887 | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
| 1888 | if (globals == NULL) |
| 1889 | goto err; |
| 1890 | } |
| 1891 | |
| 1892 | /* Get the __import__ function from the builtins */ |
| 1893 | if (PyDict_Check(builtins)) |
| 1894 | import=PyObject_GetItem(builtins, import_str); |
| 1895 | else |
| 1896 | import=PyObject_GetAttr(builtins, import_str); |
| 1897 | if (import == NULL) |
| 1898 | goto err; |
| 1899 | |
| 1900 | /* Call the _import__ function with the proper argument list */ |
| 1901 | r = PyObject_CallFunction(import, "OOOO", |
| 1902 | module_name, globals, globals, silly_list); |
| 1903 | |
| 1904 | err: |
| 1905 | Py_XDECREF(globals); |
| 1906 | Py_XDECREF(builtins); |
| 1907 | Py_XDECREF(import); |
| 1908 | |
| 1909 | return r; |
| 1910 | } |
| 1911 | |
| 1912 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1913 | /* Module 'imp' provides Python access to the primitives used for |
| 1914 | importing modules. |
| 1915 | */ |
| 1916 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1917 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1918 | imp_get_magic(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1919 | PyObject *self; |
| 1920 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1921 | { |
| 1922 | char buf[4]; |
| 1923 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1924 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1925 | return NULL; |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 1926 | buf[0] = (char) ((MAGIC >> 0) & 0xff); |
| 1927 | buf[1] = (char) ((MAGIC >> 8) & 0xff); |
| 1928 | buf[2] = (char) ((MAGIC >> 16) & 0xff); |
| 1929 | buf[3] = (char) ((MAGIC >> 24) & 0xff); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1930 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1931 | return PyString_FromStringAndSize(buf, 4); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1934 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1935 | imp_get_suffixes(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1936 | PyObject *self; |
| 1937 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1938 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1939 | PyObject *list; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1940 | struct filedescr *fdp; |
| 1941 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1942 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1943 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1944 | list = PyList_New(0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1945 | if (list == NULL) |
| 1946 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1947 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
| 1948 | PyObject *item = Py_BuildValue("ssi", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1949 | fdp->suffix, fdp->mode, fdp->type); |
| 1950 | if (item == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1951 | Py_DECREF(list); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1952 | return NULL; |
| 1953 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1954 | if (PyList_Append(list, item) < 0) { |
| 1955 | Py_DECREF(list); |
| 1956 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1957 | return NULL; |
| 1958 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1959 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1960 | } |
| 1961 | return list; |
| 1962 | } |
| 1963 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1964 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1965 | call_find_module(name, path) |
| 1966 | char *name; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1967 | PyObject *path; /* list or None or NULL */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1968 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1969 | extern int fclose Py_PROTO((FILE *)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1970 | PyObject *fob, *ret; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1971 | struct filedescr *fdp; |
| 1972 | char pathname[MAXPATHLEN+1]; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1973 | FILE *fp = NULL; |
| 1974 | |
| 1975 | pathname[0] = '\0'; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1976 | if (path == Py_None) |
| 1977 | path = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1978 | fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp); |
| 1979 | if (fdp == NULL) |
| 1980 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1981 | if (fp != NULL) { |
| 1982 | fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); |
| 1983 | if (fob == NULL) { |
| 1984 | fclose(fp); |
| 1985 | return NULL; |
| 1986 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1987 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1988 | else { |
| 1989 | fob = Py_None; |
| 1990 | Py_INCREF(fob); |
| 1991 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1992 | ret = Py_BuildValue("Os(ssi)", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1993 | fob, pathname, fdp->suffix, fdp->mode, fdp->type); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1994 | Py_DECREF(fob); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1995 | return ret; |
| 1996 | } |
| 1997 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1998 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1999 | imp_find_module(self, args) |
| 2000 | PyObject *self; |
| 2001 | PyObject *args; |
| 2002 | { |
| 2003 | char *name; |
| 2004 | PyObject *path = NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2005 | if (!PyArg_ParseTuple(args, "s|O", &name, &path)) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2006 | return NULL; |
| 2007 | return call_find_module(name, path); |
| 2008 | } |
| 2009 | |
| 2010 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2011 | imp_init_builtin(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2012 | PyObject *self; |
| 2013 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2014 | { |
| 2015 | char *name; |
| 2016 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2017 | PyObject *m; |
| 2018 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2019 | return NULL; |
| 2020 | ret = init_builtin(name); |
| 2021 | if (ret < 0) |
| 2022 | return NULL; |
| 2023 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2024 | Py_INCREF(Py_None); |
| 2025 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2026 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2027 | m = PyImport_AddModule(name); |
| 2028 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2029 | return m; |
| 2030 | } |
| 2031 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2032 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2033 | imp_init_frozen(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2034 | PyObject *self; |
| 2035 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2036 | { |
| 2037 | char *name; |
| 2038 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2039 | PyObject *m; |
| 2040 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2041 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2042 | ret = PyImport_ImportFrozenModule(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2043 | if (ret < 0) |
| 2044 | return NULL; |
| 2045 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2046 | Py_INCREF(Py_None); |
| 2047 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2048 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2049 | m = PyImport_AddModule(name); |
| 2050 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2051 | return m; |
| 2052 | } |
| 2053 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2054 | static PyObject * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2055 | imp_get_frozen_object(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2056 | PyObject *self; |
| 2057 | PyObject *args; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2058 | { |
| 2059 | char *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 2060 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2061 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2062 | return NULL; |
| 2063 | return get_frozen_object(name); |
| 2064 | } |
| 2065 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2066 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2067 | imp_is_builtin(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2068 | PyObject *self; |
| 2069 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2070 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2071 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2072 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2073 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2074 | return PyInt_FromLong(is_builtin(name)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2077 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2078 | imp_is_frozen(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2079 | PyObject *self; |
| 2080 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2081 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2082 | char *name; |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 2083 | struct _frozen *p; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2084 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2085 | return NULL; |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 2086 | p = find_frozen(name); |
| 2087 | return PyInt_FromLong((long) (p == NULL ? 0 : p->size)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | static FILE * |
| 2091 | get_file(pathname, fob, mode) |
| 2092 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2093 | PyObject *fob; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2094 | char *mode; |
| 2095 | { |
| 2096 | FILE *fp; |
| 2097 | if (fob == NULL) { |
| 2098 | fp = fopen(pathname, mode); |
| 2099 | if (fp == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2100 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2101 | } |
| 2102 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2103 | fp = PyFile_AsFile(fob); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2104 | if (fp == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2105 | PyErr_SetString(PyExc_ValueError, |
| 2106 | "bad/closed file object"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2107 | } |
| 2108 | return fp; |
| 2109 | } |
| 2110 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2111 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2112 | imp_load_compiled(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2113 | PyObject *self; |
| 2114 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2115 | { |
| 2116 | char *name; |
| 2117 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2118 | PyObject *fob = NULL; |
| 2119 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2120 | FILE *fp; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2121 | if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2122 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2123 | return NULL; |
| 2124 | fp = get_file(pathname, fob, "rb"); |
| 2125 | if (fp == NULL) |
| 2126 | return NULL; |
| 2127 | m = load_compiled_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2128 | if (fob == NULL) |
| 2129 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2130 | return m; |
| 2131 | } |
| 2132 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2133 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2134 | imp_load_dynamic(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2135 | PyObject *self; |
| 2136 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2137 | { |
| 2138 | char *name; |
| 2139 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2140 | PyObject *fob = NULL; |
| 2141 | PyObject *m; |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2142 | FILE *fp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2143 | if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, |
| 2144 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2145 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2146 | if (fob) { |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2147 | fp = get_file(pathname, fob, "r"); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2148 | if (fp == NULL) |
| 2149 | return NULL; |
| 2150 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2151 | m = _PyImport_LoadDynamicModule(name, pathname, fp); |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2152 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2155 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2156 | imp_load_source(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2157 | PyObject *self; |
| 2158 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2159 | { |
| 2160 | char *name; |
| 2161 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2162 | PyObject *fob = NULL; |
| 2163 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2164 | FILE *fp; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2165 | if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2166 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2167 | return NULL; |
| 2168 | fp = get_file(pathname, fob, "r"); |
| 2169 | if (fp == NULL) |
| 2170 | return NULL; |
| 2171 | m = load_source_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2172 | if (fob == NULL) |
| 2173 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2174 | return m; |
| 2175 | } |
| 2176 | |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2177 | #ifdef macintosh |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2178 | static PyObject * |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2179 | imp_load_resource(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2180 | PyObject *self; |
| 2181 | PyObject *args; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2182 | { |
| 2183 | char *name; |
| 2184 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2185 | PyObject *m; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2186 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2187 | if (!PyArg_ParseTuple(args, "ss", &name, &pathname)) |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2188 | return NULL; |
| 2189 | m = PyMac_LoadResourceModule(name, pathname); |
| 2190 | return m; |
| 2191 | } |
| 2192 | #endif /* macintosh */ |
| 2193 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2194 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2195 | imp_load_module(self, args) |
| 2196 | PyObject *self; |
| 2197 | PyObject *args; |
| 2198 | { |
| 2199 | char *name; |
| 2200 | PyObject *fob; |
| 2201 | char *pathname; |
| 2202 | char *suffix; /* Unused */ |
| 2203 | char *mode; |
| 2204 | int type; |
| 2205 | FILE *fp; |
| 2206 | |
| 2207 | if (!PyArg_ParseTuple(args, "sOs(ssi)", |
| 2208 | &name, &fob, &pathname, |
| 2209 | &suffix, &mode, &type)) |
| 2210 | return NULL; |
| 2211 | if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) { |
| 2212 | PyErr_Format(PyExc_ValueError, |
| 2213 | "invalid file open mode %.200s", mode); |
| 2214 | return NULL; |
| 2215 | } |
| 2216 | if (fob == Py_None) |
| 2217 | fp = NULL; |
| 2218 | else { |
| 2219 | if (!PyFile_Check(fob)) { |
| 2220 | PyErr_SetString(PyExc_ValueError, |
| 2221 | "load_module arg#2 should be a file or None"); |
| 2222 | return NULL; |
| 2223 | } |
| 2224 | fp = get_file(pathname, fob, mode); |
| 2225 | if (fp == NULL) |
| 2226 | return NULL; |
| 2227 | } |
| 2228 | return load_module(name, fp, pathname, type); |
| 2229 | } |
| 2230 | |
| 2231 | static PyObject * |
| 2232 | imp_load_package(self, args) |
| 2233 | PyObject *self; |
| 2234 | PyObject *args; |
| 2235 | { |
| 2236 | char *name; |
| 2237 | char *pathname; |
| 2238 | if (!PyArg_ParseTuple(args, "ss", &name, &pathname)) |
| 2239 | return NULL; |
| 2240 | return load_package(name, pathname); |
| 2241 | } |
| 2242 | |
| 2243 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2244 | imp_new_module(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2245 | PyObject *self; |
| 2246 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2247 | { |
| 2248 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2249 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2250 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2251 | return PyModule_New(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2254 | /* Doc strings */ |
| 2255 | |
| 2256 | static char doc_imp[] = "\ |
| 2257 | This module provides the components needed to build your own\n\ |
| 2258 | __import__ function. Undocumented functions are obsolete.\n\ |
| 2259 | "; |
| 2260 | |
| 2261 | static char doc_find_module[] = "\ |
| 2262 | find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\ |
| 2263 | Search for a module. If path is omitted or None, search for a\n\ |
| 2264 | built-in, frozen or special module and continue search in sys.path.\n\ |
| 2265 | The module name cannot contain '.'; to search for a submodule of a\n\ |
| 2266 | package, pass the submodule name and the package's __path__.\ |
| 2267 | "; |
| 2268 | |
| 2269 | static char doc_load_module[] = "\ |
| 2270 | load_module(name, file, filename, (suffix, mode, type)) -> module\n\ |
| 2271 | Load a module, given information returned by find_module().\n\ |
| 2272 | The module name must include the full package name, if any.\ |
| 2273 | "; |
| 2274 | |
| 2275 | static char doc_get_magic[] = "\ |
| 2276 | get_magic() -> string\n\ |
| 2277 | Return the magic number for .pyc or .pyo files.\ |
| 2278 | "; |
| 2279 | |
| 2280 | static char doc_get_suffixes[] = "\ |
| 2281 | get_suffixes() -> [(suffix, mode, type), ...]\n\ |
| 2282 | Return a list of (suffix, mode, type) tuples describing the files\n\ |
| 2283 | that find_module() looks for.\ |
| 2284 | "; |
| 2285 | |
| 2286 | static char doc_new_module[] = "\ |
| 2287 | new_module(name) -> module\n\ |
| 2288 | Create a new module. Do not enter it in sys.modules.\n\ |
| 2289 | The module name must include the full package name, if any.\ |
| 2290 | "; |
| 2291 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2292 | static PyMethodDef imp_methods[] = { |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2293 | {"find_module", imp_find_module, 1, doc_find_module}, |
| 2294 | {"get_magic", imp_get_magic, 1, doc_get_magic}, |
| 2295 | {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes}, |
| 2296 | {"load_module", imp_load_module, 1, doc_load_module}, |
| 2297 | {"new_module", imp_new_module, 1, doc_new_module}, |
| 2298 | /* The rest are obsolete */ |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2299 | {"get_frozen_object", imp_get_frozen_object, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2300 | {"init_builtin", imp_init_builtin, 1}, |
| 2301 | {"init_frozen", imp_init_frozen, 1}, |
| 2302 | {"is_builtin", imp_is_builtin, 1}, |
| 2303 | {"is_frozen", imp_is_frozen, 1}, |
| 2304 | {"load_compiled", imp_load_compiled, 1}, |
| 2305 | {"load_dynamic", imp_load_dynamic, 1}, |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2306 | {"load_package", imp_load_package, 1}, |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2307 | #ifdef macintosh |
| 2308 | {"load_resource", imp_load_resource, 1}, |
| 2309 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2310 | {"load_source", imp_load_source, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2311 | {NULL, NULL} /* sentinel */ |
| 2312 | }; |
| 2313 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 2314 | static int |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2315 | setint(d, name, value) |
| 2316 | PyObject *d; |
| 2317 | char *name; |
| 2318 | int value; |
| 2319 | { |
| 2320 | PyObject *v; |
| 2321 | int err; |
| 2322 | |
| 2323 | v = PyInt_FromLong((long)value); |
| 2324 | err = PyDict_SetItemString(d, name, v); |
| 2325 | Py_XDECREF(v); |
| 2326 | return err; |
| 2327 | } |
| 2328 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2329 | void |
| 2330 | initimp() |
| 2331 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2332 | PyObject *m, *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2333 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2334 | m = Py_InitModule4("imp", imp_methods, doc_imp, |
| 2335 | NULL, PYTHON_API_VERSION); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2336 | d = PyModule_GetDict(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2337 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2338 | if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; |
| 2339 | if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; |
| 2340 | if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure; |
| 2341 | if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure; |
| 2342 | if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure; |
| 2343 | if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure; |
| 2344 | if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure; |
| 2345 | if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure; |
Guido van Rossum | 0f84a34 | 1998-08-06 13:36:01 +0000 | [diff] [blame] | 2346 | if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2347 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2348 | failure: |
| 2349 | ; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2350 | } |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2351 | |
| 2352 | |
| 2353 | /* API for embedding applications that want to add their own entries to the |
| 2354 | table of built-in modules. This should normally be called *before* |
| 2355 | Py_Initialize(). When the malloc() or realloc() call fails, -1 is returned |
| 2356 | and the existing table is unchanged. |
| 2357 | |
| 2358 | After a similar function by Just van Rossum. */ |
| 2359 | |
| 2360 | int |
| 2361 | PyImport_ExtendInittab(newtab) |
| 2362 | struct _inittab *newtab; |
| 2363 | { |
| 2364 | static struct _inittab *our_copy = NULL; |
| 2365 | struct _inittab *p; |
| 2366 | int i, n; |
| 2367 | |
| 2368 | /* Count the number of entries in both tables */ |
| 2369 | for (n = 0; newtab[n].name != NULL; n++) |
| 2370 | ; |
| 2371 | if (n == 0) |
| 2372 | return 0; /* Nothing to do */ |
| 2373 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) |
| 2374 | ; |
| 2375 | |
| 2376 | /* Allocate new memory for the combined table */ |
| 2377 | if (our_copy == NULL) |
| 2378 | p = malloc((i+n+1) * sizeof(struct _inittab)); |
| 2379 | else |
| 2380 | p = realloc(our_copy, (i+n+1) * sizeof(struct _inittab)); |
| 2381 | if (p == NULL) |
| 2382 | return -1; |
| 2383 | |
| 2384 | /* Copy the tables into the new memory */ |
| 2385 | if (our_copy != PyImport_Inittab) |
| 2386 | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
| 2387 | PyImport_Inittab = our_copy = p; |
| 2388 | memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); |
| 2389 | |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
| 2393 | /* Shorthand to add a single entry given a name and a function */ |
| 2394 | |
| 2395 | int |
| 2396 | PyImport_AppendInittab(name, initfunc) |
| 2397 | char *name; |
| 2398 | void (*initfunc)(); |
| 2399 | { |
| 2400 | struct _inittab newtab[2]; |
| 2401 | |
| 2402 | memset(newtab, '\0', sizeof newtab); |
| 2403 | |
| 2404 | newtab[0].name = name; |
| 2405 | newtab[0].initfunc = initfunc; |
| 2406 | |
| 2407 | return PyImport_ExtendInittab(newtab); |
| 2408 | } |