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