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