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 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "Python-ast.h" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 7 | #undef Yield /* undefine macro conflicting with winbase.h */ |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 8 | #include "pyarena.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 9 | #include "pythonrun.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | #include "errcode.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 11 | #include "marshal.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 12 | #include "code.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 13 | #include "compile.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 14 | #include "eval.h" |
Guido van Rossum | d8bac6d | 1992-02-26 15:19:13 +0000 | [diff] [blame] | 15 | #include "osdefs.h" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 16 | #include "importdl.h" |
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 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 21 | #ifdef __cplusplus |
| 22 | extern "C" { |
| 23 | #endif |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 24 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 25 | #ifdef MS_WINDOWS |
| 26 | /* for stat.st_mode */ |
| 27 | typedef unsigned short mode_t; |
| 28 | #endif |
| 29 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 30 | extern time_t PyOS_GetLastModificationTime(char *, FILE *); |
| 31 | /* In getmtime.c */ |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 32 | |
Jeremy Hylton | d4ceb31 | 2004-04-01 02:45:22 +0000 | [diff] [blame] | 33 | /* Magic word to reject .pyc files generated by other Python versions. |
| 34 | It should change for each incompatible change to the bytecode. |
| 35 | |
| 36 | The value of CR and LF is incorporated so if you ever read or write |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 37 | 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] | 38 | Apple MPW compiler swaps their values, botching string constants. |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 40 | The magic numbers must be spaced apart at least 2 values, as the |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 41 | -U interpeter flag will cause MAGIC+1 being used. They have been |
| 42 | odd numbers for some time now. |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 43 | |
Jeremy Hylton | d4ceb31 | 2004-04-01 02:45:22 +0000 | [diff] [blame] | 44 | There were a variety of old schemes for setting the magic number. |
| 45 | The current working scheme is to increment the previous value by |
| 46 | 10. |
Guido van Rossum | f689492 | 2002-08-31 15:16:14 +0000 | [diff] [blame] | 47 | |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 48 | Known values: |
| 49 | Python 1.5: 20121 |
| 50 | Python 1.5.1: 20121 |
| 51 | Python 1.5.2: 20121 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 52 | Python 1.6: 50428 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 53 | Python 2.0: 50823 |
| 54 | Python 2.0.1: 50823 |
| 55 | Python 2.1: 60202 |
| 56 | Python 2.1.1: 60202 |
| 57 | Python 2.1.2: 60202 |
| 58 | Python 2.2: 60717 |
Neal Norwitz | 7fdcb41 | 2002-06-14 01:07:39 +0000 | [diff] [blame] | 59 | Python 2.3a0: 62011 |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 60 | Python 2.3a0: 62021 |
Guido van Rossum | f689492 | 2002-08-31 15:16:14 +0000 | [diff] [blame] | 61 | Python 2.3a0: 62011 (!) |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 62 | Python 2.4a0: 62041 |
Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 63 | Python 2.4a3: 62051 |
Raymond Hettinger | 2c31a05 | 2004-09-22 18:44:21 +0000 | [diff] [blame] | 64 | Python 2.4b1: 62061 |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 65 | Python 2.5a0: 62071 |
Michael W. Hudson | aee2e28 | 2005-10-21 11:32:20 +0000 | [diff] [blame] | 66 | Python 2.5a0: 62081 (ast-branch) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 67 | Python 2.5a0: 62091 (with) |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 68 | Python 2.5a0: 62092 (changed WITH_CLEANUP opcode) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | Python 2.5b3: 62101 (fix wrong code: for x, in ...) |
| 70 | Python 2.5b3: 62111 (fix wrong code: x += yield) |
| 71 | Python 2.5c1: 62121 (fix wrong lnotab with for loops and |
| 72 | storing constants that should have been removed) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 73 | Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 74 | Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 75 | Python 2.6a1: 62161 (WITH_CLEANUP optimization) |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 76 | Python 3000: 3000 |
Brett Cannon | e2e23ef | 2006-08-25 05:05:30 +0000 | [diff] [blame] | 77 | 3010 (removed UNARY_CONVERT) |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 78 | 3020 (added BUILD_SET) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 79 | 3030 (added keyword-only parameters) |
Guido van Rossum | 3203bdc | 2006-12-28 21:03:31 +0000 | [diff] [blame] | 80 | 3040 (added signature annotations) |
Guido van Rossum | 452bf51 | 2007-02-09 05:32:43 +0000 | [diff] [blame] | 81 | 3050 (print becomes a function) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 82 | 3060 (PEP 3115 metaclass syntax) |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 83 | 3070 (PEP 3109 raise changes) |
| 84 | 3080 (PEP 3137 make __file__ and __name__ unicode) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 85 | 3090 (kill str8 interning) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 86 | 3100 (merge from 2.6a0, see 62151) |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 87 | 3102 (__file__ points to source file) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 88 | Python 3.0a4: 3110 (WITH_CLEANUP optimization). |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 89 | Python 3.0a5: 3130 (lexical exception stacking, including POP_EXCEPT) |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 90 | */ |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 91 | #define MAGIC (3130 | ((long)'\r'<<16) | ((long)'\n'<<24)) |
Guido van Rossum | 3ddee71 | 1991-12-16 13:06:34 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 93 | /* Magic word as global; note that _PyImport_Init() can change the |
Jeremy Hylton | 9262b8a | 2000-06-30 04:59:17 +0000 | [diff] [blame] | 94 | value of this global to accommodate for alterations of how the |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 95 | compiler works which are enabled by command line switches. */ |
| 96 | static long pyc_magic = MAGIC; |
| 97 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 98 | /* See _PyImport_FixupExtension() below */ |
| 99 | static PyObject *extensions = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 101 | /* This table is defined in config.c: */ |
| 102 | extern struct _inittab _PyImport_Inittab[]; |
| 103 | |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 104 | /* Method from Parser/tokenizer.c */ |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 105 | extern char * PyTokenizer_FindEncoding(int); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 107 | struct _inittab *PyImport_Inittab = _PyImport_Inittab; |
Guido van Rossum | 66f1fa8 | 1991-04-03 19:03:52 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 109 | /* these tables define the module suffixes that Python recognizes */ |
| 110 | struct filedescr * _PyImport_Filetab = NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 112 | static const struct filedescr _PyImport_StandardFiletab[] = { |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 113 | {".py", "U", PY_SOURCE}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 114 | #ifdef MS_WINDOWS |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 115 | {".pyw", "U", PY_SOURCE}, |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 116 | #endif |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 117 | {".pyc", "rb", PY_COMPILED}, |
| 118 | {0, 0} |
| 119 | }; |
| 120 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 122 | /* Initialize things */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | |
| 124 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 125 | _PyImport_Init(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | { |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 127 | const struct filedescr *scan; |
| 128 | struct filedescr *filetab; |
| 129 | int countD = 0; |
| 130 | int countS = 0; |
| 131 | |
| 132 | /* prepare _PyImport_Filetab: copy entries from |
| 133 | _PyImport_DynLoadFiletab and _PyImport_StandardFiletab. |
| 134 | */ |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 135 | #ifdef HAVE_DYNAMIC_LOADING |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 136 | for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan) |
| 137 | ++countD; |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 138 | #endif |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 139 | for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan) |
| 140 | ++countS; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 141 | filetab = PyMem_NEW(struct filedescr, countD + countS + 1); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 142 | if (filetab == NULL) |
| 143 | Py_FatalError("Can't initialize import file table."); |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 144 | #ifdef HAVE_DYNAMIC_LOADING |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 145 | memcpy(filetab, _PyImport_DynLoadFiletab, |
| 146 | countD * sizeof(struct filedescr)); |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 147 | #endif |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 148 | memcpy(filetab + countD, _PyImport_StandardFiletab, |
| 149 | countS * sizeof(struct filedescr)); |
| 150 | filetab[countD + countS].suffix = NULL; |
| 151 | |
| 152 | _PyImport_Filetab = filetab; |
| 153 | |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 154 | if (Py_OptimizeFlag) { |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 155 | /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */ |
| 156 | for (; filetab->suffix != NULL; filetab++) { |
| 157 | if (strcmp(filetab->suffix, ".pyc") == 0) |
| 158 | filetab->suffix = ".pyo"; |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 162 | { |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 163 | /* Fix the pyc_magic so that byte compiled code created |
| 164 | using the all-Unicode method doesn't interfere with |
| 165 | code created in normal operation mode. */ |
| 166 | pyc_magic = MAGIC + 1; |
| 167 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 170 | void |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 171 | _PyImportHooks_Init(void) |
| 172 | { |
| 173 | PyObject *v, *path_hooks = NULL, *zimpimport; |
| 174 | int err = 0; |
| 175 | |
| 176 | /* adding sys.path_hooks and sys.path_importer_cache, setting up |
| 177 | zipimport */ |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 178 | if (PyType_Ready(&PyNullImporter_Type) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 179 | goto error; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 180 | |
| 181 | if (Py_VerboseFlag) |
| 182 | PySys_WriteStderr("# installing zipimport hook\n"); |
| 183 | |
| 184 | v = PyList_New(0); |
| 185 | if (v == NULL) |
| 186 | goto error; |
| 187 | err = PySys_SetObject("meta_path", v); |
| 188 | Py_DECREF(v); |
| 189 | if (err) |
| 190 | goto error; |
| 191 | v = PyDict_New(); |
| 192 | if (v == NULL) |
| 193 | goto error; |
| 194 | err = PySys_SetObject("path_importer_cache", v); |
| 195 | Py_DECREF(v); |
| 196 | if (err) |
| 197 | goto error; |
| 198 | path_hooks = PyList_New(0); |
| 199 | if (path_hooks == NULL) |
| 200 | goto error; |
| 201 | err = PySys_SetObject("path_hooks", path_hooks); |
| 202 | if (err) { |
| 203 | error: |
| 204 | PyErr_Print(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 205 | Py_FatalError("initializing sys.meta_path, sys.path_hooks, " |
| 206 | "path_importer_cache, or NullImporter failed" |
| 207 | ); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 208 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 209 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 210 | zimpimport = PyImport_ImportModule("zipimport"); |
| 211 | if (zimpimport == NULL) { |
| 212 | PyErr_Clear(); /* No zip import module -- okay */ |
| 213 | if (Py_VerboseFlag) |
| 214 | PySys_WriteStderr("# can't import zipimport\n"); |
| 215 | } |
| 216 | else { |
| 217 | PyObject *zipimporter = PyObject_GetAttrString(zimpimport, |
| 218 | "zipimporter"); |
| 219 | Py_DECREF(zimpimport); |
| 220 | if (zipimporter == NULL) { |
| 221 | PyErr_Clear(); /* No zipimporter object -- okay */ |
| 222 | if (Py_VerboseFlag) |
| 223 | PySys_WriteStderr( |
Fred Drake | 1e5fc55 | 2003-07-11 15:01:02 +0000 | [diff] [blame] | 224 | "# can't import zipimport.zipimporter\n"); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 225 | } |
| 226 | else { |
| 227 | /* sys.path_hooks.append(zipimporter) */ |
| 228 | err = PyList_Append(path_hooks, zipimporter); |
| 229 | Py_DECREF(zipimporter); |
| 230 | if (err) |
| 231 | goto error; |
| 232 | if (Py_VerboseFlag) |
| 233 | PySys_WriteStderr( |
| 234 | "# installed zipimport hook\n"); |
| 235 | } |
| 236 | } |
| 237 | Py_DECREF(path_hooks); |
| 238 | } |
| 239 | |
| 240 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 241 | _PyImport_Fini(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 242 | { |
| 243 | Py_XDECREF(extensions); |
| 244 | extensions = NULL; |
Barry Warsaw | 8429448 | 2000-10-03 16:02:05 +0000 | [diff] [blame] | 245 | PyMem_DEL(_PyImport_Filetab); |
| 246 | _PyImport_Filetab = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 250 | /* Locking primitives to prevent parallel imports of the same module |
| 251 | in different threads to return with a partially loaded module. |
| 252 | These calls are serialized by the global interpreter lock. */ |
| 253 | |
| 254 | #ifdef WITH_THREAD |
| 255 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 256 | #include "pythread.h" |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 258 | static PyThread_type_lock import_lock = 0; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 259 | static long import_lock_thread = -1; |
| 260 | static int import_lock_level = 0; |
| 261 | |
| 262 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 263 | lock_import(void) |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 264 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 265 | long me = PyThread_get_thread_ident(); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 266 | if (me == -1) |
| 267 | return; /* Too bad */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 268 | if (import_lock == NULL) { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 269 | import_lock = PyThread_allocate_lock(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 270 | if (import_lock == NULL) |
| 271 | return; /* Nothing much we can do. */ |
| 272 | } |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 273 | if (import_lock_thread == me) { |
| 274 | import_lock_level++; |
| 275 | return; |
| 276 | } |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 277 | if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) |
| 278 | { |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 279 | PyThreadState *tstate = PyEval_SaveThread(); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 280 | PyThread_acquire_lock(import_lock, 1); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 281 | PyEval_RestoreThread(tstate); |
| 282 | } |
| 283 | import_lock_thread = me; |
| 284 | import_lock_level = 1; |
| 285 | } |
| 286 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 287 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 288 | unlock_import(void) |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 289 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 290 | long me = PyThread_get_thread_ident(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 291 | if (me == -1 || import_lock == NULL) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 292 | return 0; /* Too bad */ |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 293 | if (import_lock_thread != me) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 294 | return -1; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 295 | import_lock_level--; |
| 296 | if (import_lock_level == 0) { |
| 297 | import_lock_thread = -1; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 298 | PyThread_release_lock(import_lock); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 299 | } |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 300 | return 1; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 303 | /* This function is called from PyOS_AfterFork to ensure that newly |
| 304 | created child processes do not share locks with the parent. */ |
| 305 | |
| 306 | void |
| 307 | _PyImport_ReInitLock(void) |
| 308 | { |
| 309 | #ifdef _AIX |
| 310 | if (import_lock != NULL) |
| 311 | import_lock = PyThread_allocate_lock(); |
| 312 | #endif |
| 313 | } |
| 314 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 315 | #else |
| 316 | |
| 317 | #define lock_import() |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 318 | #define unlock_import() 0 |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 319 | |
| 320 | #endif |
| 321 | |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 322 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 323 | imp_lock_held(PyObject *self, PyObject *noargs) |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 324 | { |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 325 | #ifdef WITH_THREAD |
Guido van Rossum | b8bff3f | 2002-04-07 06:34:38 +0000 | [diff] [blame] | 326 | return PyBool_FromLong(import_lock_thread != -1); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 327 | #else |
Guido van Rossum | b8bff3f | 2002-04-07 06:34:38 +0000 | [diff] [blame] | 328 | return PyBool_FromLong(0); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 329 | #endif |
| 330 | } |
| 331 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 332 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 333 | imp_acquire_lock(PyObject *self, PyObject *noargs) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 334 | { |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 335 | #ifdef WITH_THREAD |
| 336 | lock_import(); |
| 337 | #endif |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 338 | Py_INCREF(Py_None); |
| 339 | return Py_None; |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 343 | imp_release_lock(PyObject *self, PyObject *noargs) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 344 | { |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 345 | #ifdef WITH_THREAD |
| 346 | if (unlock_import() < 0) { |
| 347 | PyErr_SetString(PyExc_RuntimeError, |
| 348 | "not holding the import lock"); |
| 349 | return NULL; |
| 350 | } |
| 351 | #endif |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 352 | Py_INCREF(Py_None); |
| 353 | return Py_None; |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 356 | static void |
| 357 | imp_modules_reloading_clear(void) |
| 358 | { |
| 359 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 360 | if (interp->modules_reloading != NULL) |
| 361 | PyDict_Clear(interp->modules_reloading); |
| 362 | } |
| 363 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 364 | /* Helper for sys */ |
| 365 | |
| 366 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 367 | PyImport_GetModuleDict(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 368 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 369 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 370 | if (interp->modules == NULL) |
| 371 | Py_FatalError("PyImport_GetModuleDict: no module dictionary!"); |
| 372 | return interp->modules; |
| 373 | } |
| 374 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 375 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 376 | /* List of names to clear in sys */ |
| 377 | static char* sys_deletes[] = { |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 378 | "path", "argv", "ps1", "ps2", |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 379 | "last_type", "last_value", "last_traceback", |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 380 | "path_hooks", "path_importer_cache", "meta_path", |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 381 | /* misc stuff */ |
| 382 | "flags", "float_info", |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 383 | NULL |
| 384 | }; |
| 385 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 386 | static char* sys_files[] = { |
| 387 | "stdin", "__stdin__", |
| 388 | "stdout", "__stdout__", |
| 389 | "stderr", "__stderr__", |
| 390 | NULL |
| 391 | }; |
| 392 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 394 | /* Un-initialize things, as good as we can */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 395 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 396 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 397 | PyImport_Cleanup(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 398 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 399 | Py_ssize_t pos, ndone; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 400 | char *name; |
| 401 | PyObject *key, *value, *dict; |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 402 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 403 | PyObject *modules = interp->modules; |
| 404 | |
| 405 | if (modules == NULL) |
| 406 | return; /* Already done */ |
| 407 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 408 | /* Delete some special variables first. These are common |
| 409 | places where user values hide and people complain when their |
| 410 | destructors fail. Since the modules containing them are |
| 411 | deleted *last* of all, they would come too late in the normal |
| 412 | destruction order. Sigh. */ |
| 413 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 414 | value = PyDict_GetItemString(modules, "builtins"); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 415 | if (value != NULL && PyModule_Check(value)) { |
| 416 | dict = PyModule_GetDict(value); |
| 417 | if (Py_VerboseFlag) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 418 | PySys_WriteStderr("# clear builtins._\n"); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 419 | PyDict_SetItemString(dict, "_", Py_None); |
| 420 | } |
| 421 | value = PyDict_GetItemString(modules, "sys"); |
| 422 | if (value != NULL && PyModule_Check(value)) { |
| 423 | char **p; |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 424 | PyObject *v; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 425 | dict = PyModule_GetDict(value); |
| 426 | for (p = sys_deletes; *p != NULL; p++) { |
| 427 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 428 | PySys_WriteStderr("# clear sys.%s\n", *p); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 429 | PyDict_SetItemString(dict, *p, Py_None); |
| 430 | } |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 431 | for (p = sys_files; *p != NULL; p+=2) { |
| 432 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 433 | PySys_WriteStderr("# restore sys.%s\n", *p); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 434 | v = PyDict_GetItemString(dict, *(p+1)); |
| 435 | if (v == NULL) |
| 436 | v = Py_None; |
| 437 | PyDict_SetItemString(dict, *p, v); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /* First, delete __main__ */ |
| 442 | value = PyDict_GetItemString(modules, "__main__"); |
| 443 | if (value != NULL && PyModule_Check(value)) { |
| 444 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 445 | PySys_WriteStderr("# cleanup __main__\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 446 | _PyModule_Clear(value); |
| 447 | PyDict_SetItemString(modules, "__main__", Py_None); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 450 | /* The special treatment of "builtins" here is because even |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 451 | when it's not referenced as a module, its dictionary is |
| 452 | referenced by almost every module's __builtins__. Since |
| 453 | deleting a module clears its dictionary (even if there are |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 454 | references left to it), we need to delete the "builtins" |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 455 | module last. Likewise, we don't delete sys until the very |
| 456 | end because it is implicitly referenced (e.g. by print). |
| 457 | |
| 458 | Also note that we 'delete' modules by replacing their entry |
| 459 | in the modules dict with None, rather than really deleting |
| 460 | them; this avoids a rehash of the modules dictionary and |
| 461 | also marks them as "non existent" so they won't be |
| 462 | re-imported. */ |
| 463 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 464 | /* Next, repeatedly delete modules with a reference count of |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 465 | one (skipping builtins and sys) and delete them */ |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 466 | do { |
| 467 | ndone = 0; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 468 | pos = 0; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 469 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 470 | if (value->ob_refcnt != 1) |
| 471 | continue; |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 472 | if (PyUnicode_Check(key) && PyModule_Check(value)) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 473 | name = _PyUnicode_AsString(key); |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 474 | if (strcmp(name, "builtins") == 0) |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 475 | continue; |
| 476 | if (strcmp(name, "sys") == 0) |
| 477 | continue; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 478 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 479 | PySys_WriteStderr( |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 480 | "# cleanup[1] %s\n", name); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 481 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 482 | PyDict_SetItem(modules, key, Py_None); |
| 483 | ndone++; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 486 | } while (ndone > 0); |
| 487 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 488 | /* Next, delete all modules (still skipping builtins and sys) */ |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 489 | pos = 0; |
| 490 | while (PyDict_Next(modules, &pos, &key, &value)) { |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 491 | if (PyUnicode_Check(key) && PyModule_Check(value)) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 492 | name = _PyUnicode_AsString(key); |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 493 | if (strcmp(name, "builtins") == 0) |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 494 | continue; |
| 495 | if (strcmp(name, "sys") == 0) |
| 496 | continue; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 497 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 498 | PySys_WriteStderr("# cleanup[2] %s\n", name); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 499 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 500 | PyDict_SetItem(modules, key, Py_None); |
| 501 | } |
| 502 | } |
| 503 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 504 | /* Next, delete sys and builtins (in that order) */ |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 505 | value = PyDict_GetItemString(modules, "sys"); |
| 506 | if (value != NULL && PyModule_Check(value)) { |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 507 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 508 | PySys_WriteStderr("# cleanup sys\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 509 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 510 | PyDict_SetItemString(modules, "sys", Py_None); |
| 511 | } |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 512 | value = PyDict_GetItemString(modules, "builtins"); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 513 | if (value != NULL && PyModule_Check(value)) { |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 514 | if (Py_VerboseFlag) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 515 | PySys_WriteStderr("# cleanup builtins\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 516 | _PyModule_Clear(value); |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 517 | PyDict_SetItemString(modules, "builtins", Py_None); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* Finally, clear and delete the modules directory */ |
| 521 | PyDict_Clear(modules); |
| 522 | interp->modules = NULL; |
| 523 | Py_DECREF(modules); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 524 | Py_CLEAR(interp->modules_reloading); |
Guido van Rossum | 8d15b5d | 1990-10-26 14:58:58 +0000 | [diff] [blame] | 525 | } |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 526 | |
| 527 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 528 | /* Helper for pythonrun.c -- return magic number */ |
| 529 | |
| 530 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 531 | PyImport_GetMagicNumber(void) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 532 | { |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 533 | return pyc_magic; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 537 | /* Magic for extension modules (built-in as well as dynamically |
| 538 | loaded). To prevent initializing an extension module more than |
| 539 | once, we keep a static dictionary 'extensions' keyed by module name |
| 540 | (for built-in modules) or by filename (for dynamically loaded |
Barry Warsaw | 9288338 | 2001-08-13 23:05:44 +0000 | [diff] [blame] | 541 | modules), containing these modules. A copy of the module's |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 542 | dictionary is stored by calling _PyImport_FixupExtension() |
| 543 | immediately after the module initialization function succeeds. A |
| 544 | copy can be retrieved from there by calling |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 545 | _PyImport_FindExtension(). |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 546 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 547 | Modules which do support multiple multiple initialization set |
| 548 | their m_size field to a non-negative number (indicating the size |
| 549 | of the module-specific state). They are still recorded in the |
| 550 | extensions dictionary, to avoid loading shared libraries twice. |
| 551 | */ |
| 552 | |
| 553 | int |
| 554 | _PyImport_FixupExtension(PyObject *mod, char *name, char *filename) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 555 | { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 556 | PyObject *modules, *dict; |
| 557 | struct PyModuleDef *def; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 558 | if (extensions == NULL) { |
| 559 | extensions = PyDict_New(); |
| 560 | if (extensions == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 561 | return -1; |
| 562 | } |
| 563 | if (mod == NULL || !PyModule_Check(mod)) { |
| 564 | PyErr_BadInternalCall(); |
| 565 | return -1; |
| 566 | } |
| 567 | def = PyModule_GetDef(mod); |
| 568 | if (!def) { |
| 569 | PyErr_BadInternalCall(); |
| 570 | return -1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 571 | } |
| 572 | modules = PyImport_GetModuleDict(); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 573 | if (PyDict_SetItemString(modules, name, mod) < 0) |
| 574 | return -1; |
| 575 | if (_PyState_AddModule(mod, def) < 0) { |
| 576 | PyDict_DelItemString(modules, name); |
| 577 | return -1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 578 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 579 | if (def->m_size == -1) { |
| 580 | if (def->m_base.m_copy) { |
| 581 | /* Somebody already imported the module, |
| 582 | likely under a different name. |
| 583 | XXX this should really not happen. */ |
| 584 | Py_DECREF(def->m_base.m_copy); |
| 585 | def->m_base.m_copy = NULL; |
| 586 | } |
| 587 | dict = PyModule_GetDict(mod); |
| 588 | if (dict == NULL) |
| 589 | return -1; |
| 590 | def->m_base.m_copy = PyDict_Copy(dict); |
| 591 | if (def->m_base.m_copy == NULL) |
| 592 | return -1; |
| 593 | } |
| 594 | PyDict_SetItemString(extensions, filename, (PyObject*)def); |
| 595 | return 0; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 599 | _PyImport_FindExtension(char *name, char *filename) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 600 | { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 601 | PyObject *mod, *mdict; |
| 602 | PyModuleDef* def; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 603 | if (extensions == NULL) |
| 604 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 605 | def = (PyModuleDef*)PyDict_GetItemString(extensions, filename); |
| 606 | if (def == NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 607 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 608 | if (def->m_size == -1) { |
| 609 | /* Module does not support repeated initialization */ |
| 610 | if (def->m_base.m_copy == NULL) |
| 611 | return NULL; |
| 612 | mod = PyImport_AddModule(name); |
| 613 | if (mod == NULL) |
| 614 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 615 | mdict = PyModule_GetDict(mod); |
| 616 | if (mdict == NULL) |
| 617 | return NULL; |
| 618 | if (PyDict_Update(mdict, def->m_base.m_copy)) |
| 619 | return NULL; |
| 620 | } |
| 621 | else { |
| 622 | if (def->m_base.m_init == NULL) |
| 623 | return NULL; |
| 624 | mod = def->m_base.m_init(); |
| 625 | if (mod == NULL) |
| 626 | return NULL; |
| 627 | PyDict_SetItemString(PyImport_GetModuleDict(), name, mod); |
Benjamin Peterson | ad95653 | 2008-09-04 02:28:15 +0000 | [diff] [blame] | 628 | Py_DECREF(mod); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 629 | } |
| 630 | if (_PyState_AddModule(mod, def) < 0) { |
| 631 | PyDict_DelItemString(PyImport_GetModuleDict(), name); |
| 632 | Py_DECREF(mod); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 633 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 634 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 635 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 636 | PySys_WriteStderr("import %s # previously loaded (%s)\n", |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 637 | name, filename); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 638 | return mod; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 639 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | |
| 643 | /* Get the module object corresponding to a module name. |
| 644 | First check the modules dictionary if there's one there, |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 645 | if not, create a new one and insert it in the modules dictionary. |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 646 | Because the former action is most common, THIS DOES NOT RETURN A |
| 647 | 'NEW' REFERENCE! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 648 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 649 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 650 | PyImport_AddModule(const char *name) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 651 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 652 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 653 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 654 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 655 | if ((m = PyDict_GetItemString(modules, name)) != NULL && |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 656 | PyModule_Check(m)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 657 | return m; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 658 | m = PyModule_New(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 659 | if (m == NULL) |
| 660 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 661 | if (PyDict_SetItemString(modules, name, m) != 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 662 | Py_DECREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 663 | return NULL; |
| 664 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 665 | Py_DECREF(m); /* Yes, it still exists, in modules! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 666 | |
| 667 | return m; |
| 668 | } |
| 669 | |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 670 | /* Remove name from sys.modules, if it's there. */ |
| 671 | static void |
| 672 | _RemoveModule(const char *name) |
| 673 | { |
| 674 | PyObject *modules = PyImport_GetModuleDict(); |
| 675 | if (PyDict_GetItemString(modules, name) == NULL) |
| 676 | return; |
| 677 | if (PyDict_DelItemString(modules, name) < 0) |
| 678 | Py_FatalError("import: deleting existing key in" |
| 679 | "sys.modules failed"); |
| 680 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 681 | |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 682 | static PyObject * get_sourcefile(const char *file); |
| 683 | |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 684 | /* Execute a code object in a module and return the module object |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 685 | * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is |
| 686 | * removed from sys.modules, to avoid leaving damaged module objects |
| 687 | * in sys.modules. The caller may wish to restore the original |
| 688 | * module object (if any) in this case; PyImport_ReloadModule is an |
| 689 | * example. |
| 690 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 691 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 692 | PyImport_ExecCodeModule(char *name, PyObject *co) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 693 | { |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 694 | return PyImport_ExecCodeModuleEx(name, co, (char *)NULL); |
| 695 | } |
| 696 | |
| 697 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 698 | PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 699 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 700 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 701 | PyObject *m, *d, *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 702 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 703 | m = PyImport_AddModule(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 704 | if (m == NULL) |
| 705 | return NULL; |
Jeremy Hylton | ecd9129 | 2004-01-02 23:25:32 +0000 | [diff] [blame] | 706 | /* If the module is being reloaded, we get the old module back |
| 707 | and re-use its dict to exec the new code. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 708 | d = PyModule_GetDict(m); |
| 709 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 710 | if (PyDict_SetItemString(d, "__builtins__", |
| 711 | PyEval_GetBuiltins()) != 0) |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 712 | goto error; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 713 | } |
Guido van Rossum | 9c9a07c | 1996-05-16 20:43:40 +0000 | [diff] [blame] | 714 | /* Remember the filename as the __file__ attribute */ |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 715 | v = NULL; |
| 716 | if (pathname != NULL) { |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 717 | v = get_sourcefile(pathname); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 718 | if (v == NULL) |
| 719 | PyErr_Clear(); |
| 720 | } |
| 721 | if (v == NULL) { |
| 722 | v = ((PyCodeObject *)co)->co_filename; |
| 723 | Py_INCREF(v); |
| 724 | } |
| 725 | if (PyDict_SetItemString(d, "__file__", v) != 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 726 | PyErr_Clear(); /* Not important enough to report */ |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 727 | Py_DECREF(v); |
| 728 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 729 | v = PyEval_EvalCode((PyCodeObject *)co, d, d); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 730 | if (v == NULL) |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 731 | goto error; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 732 | Py_DECREF(v); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 733 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 734 | if ((m = PyDict_GetItemString(modules, name)) == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 735 | PyErr_Format(PyExc_ImportError, |
| 736 | "Loaded module %.200s not found in sys.modules", |
| 737 | name); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 738 | return NULL; |
| 739 | } |
| 740 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 741 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 742 | |
| 743 | return m; |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 744 | |
| 745 | error: |
| 746 | _RemoveModule(name); |
| 747 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | |
| 751 | /* Given a pathname for a Python source file, fill a buffer with the |
| 752 | pathname for the corresponding compiled file. Return the pathname |
| 753 | for the compiled file, or NULL if there's no space in the buffer. |
| 754 | Doesn't set an exception. */ |
| 755 | |
| 756 | static char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 757 | make_compiled_pathname(char *pathname, char *buf, size_t buflen) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 758 | { |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 759 | size_t len = strlen(pathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 760 | if (len+2 > buflen) |
| 761 | return NULL; |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 762 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 763 | #ifdef MS_WINDOWS |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 764 | /* Treat .pyw as if it were .py. The case of ".pyw" must match |
| 765 | that used in _PyImport_StandardFiletab. */ |
| 766 | if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0) |
| 767 | --len; /* pretend 'w' isn't there */ |
| 768 | #endif |
| 769 | memcpy(buf, pathname, len); |
| 770 | buf[len] = Py_OptimizeFlag ? 'o' : 'c'; |
| 771 | buf[len+1] = '\0'; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 772 | |
| 773 | return buf; |
| 774 | } |
| 775 | |
| 776 | |
| 777 | /* Given a pathname for a Python source file, its time of last |
| 778 | modification, and a pathname for a compiled file, check whether the |
| 779 | compiled file represents the same version of the source. If so, |
| 780 | return a FILE pointer for the compiled file, positioned just after |
| 781 | the header; if not, return NULL. |
| 782 | Doesn't set an exception. */ |
| 783 | |
| 784 | static FILE * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 785 | check_compiled_module(char *pathname, time_t mtime, char *cpathname) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 786 | { |
| 787 | FILE *fp; |
| 788 | long magic; |
| 789 | long pyc_mtime; |
| 790 | |
| 791 | fp = fopen(cpathname, "rb"); |
| 792 | if (fp == NULL) |
| 793 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 794 | magic = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 795 | if (magic != pyc_magic) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 796 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 797 | PySys_WriteStderr("# %s has bad magic\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 798 | fclose(fp); |
| 799 | return NULL; |
| 800 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 801 | pyc_mtime = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 802 | if (pyc_mtime != mtime) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 803 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 804 | PySys_WriteStderr("# %s has bad mtime\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 805 | fclose(fp); |
| 806 | return NULL; |
| 807 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 808 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 809 | PySys_WriteStderr("# %s matches %s\n", cpathname, pathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 810 | return fp; |
| 811 | } |
| 812 | |
| 813 | |
| 814 | /* Read a code object from a file and check it for validity */ |
| 815 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 816 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 817 | read_compiled_module(char *cpathname, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 818 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 819 | PyObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 820 | |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 821 | co = PyMarshal_ReadLastObjectFromFile(fp); |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 822 | if (co == NULL) |
| 823 | return NULL; |
| 824 | if (!PyCode_Check(co)) { |
| 825 | PyErr_Format(PyExc_ImportError, |
| 826 | "Non-code object in %.200s", cpathname); |
| 827 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 828 | return NULL; |
| 829 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 830 | return (PyCodeObject *)co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | |
| 834 | /* 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] | 835 | module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 836 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 837 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 838 | load_compiled_module(char *name, char *cpathname, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 839 | { |
| 840 | long magic; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 841 | PyCodeObject *co; |
| 842 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 843 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 844 | magic = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 845 | if (magic != pyc_magic) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 846 | PyErr_Format(PyExc_ImportError, |
| 847 | "Bad magic number in %.200s", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 848 | return NULL; |
| 849 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 850 | (void) PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 851 | co = read_compiled_module(cpathname, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 852 | if (co == NULL) |
| 853 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 854 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 855 | PySys_WriteStderr("import %s # precompiled from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 856 | name, cpathname); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 857 | m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 858 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 859 | |
| 860 | return m; |
| 861 | } |
| 862 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 863 | /* Parse a source file and return the corresponding code object */ |
| 864 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 865 | static PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 866 | parse_source_module(const char *pathname, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 867 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 868 | PyCodeObject *co = NULL; |
| 869 | mod_ty mod; |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 870 | PyCompilerFlags flags; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 871 | PyArena *arena = PyArena_New(); |
| 872 | if (arena == NULL) |
| 873 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 874 | |
Christian Heimes | b1b3efc | 2008-03-26 23:24:27 +0000 | [diff] [blame] | 875 | flags.cf_flags = 0; |
Martin v. Löwis | 85bcc66 | 2007-09-04 09:18:06 +0000 | [diff] [blame] | 876 | mod = PyParser_ASTFromFile(fp, pathname, NULL, |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 877 | Py_file_input, 0, 0, &flags, |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 878 | NULL, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 879 | if (mod) { |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 880 | co = PyAST_Compile(mod, pathname, NULL, arena); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 881 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 882 | PyArena_Free(arena); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 883 | return co; |
| 884 | } |
| 885 | |
| 886 | |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 887 | /* Helper to open a bytecode file for writing in exclusive mode */ |
| 888 | |
| 889 | static FILE * |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 890 | open_exclusive(char *filename, mode_t mode) |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 891 | { |
| 892 | #if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC) |
| 893 | /* Use O_EXCL to avoid a race condition when another process tries to |
| 894 | write the same file. When that happens, our open() call fails, |
| 895 | which is just fine (since it's only a cache). |
| 896 | XXX If the file exists and is writable but the directory is not |
| 897 | writable, the file will never be written. Oh well. |
| 898 | */ |
| 899 | int fd; |
| 900 | (void) unlink(filename); |
Tim Peters | 42c83af | 2000-09-29 04:03:10 +0000 | [diff] [blame] | 901 | fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC |
| 902 | #ifdef O_BINARY |
| 903 | |O_BINARY /* necessary for Windows */ |
| 904 | #endif |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 905 | #ifdef __VMS |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 906 | , mode, "ctxt=bin", "shr=nil" |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 907 | #else |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 908 | , mode |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 909 | #endif |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 910 | ); |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 911 | if (fd < 0) |
| 912 | return NULL; |
| 913 | return fdopen(fd, "wb"); |
| 914 | #else |
| 915 | /* Best we can do -- on Windows this can't happen anyway */ |
| 916 | return fopen(filename, "wb"); |
| 917 | #endif |
| 918 | } |
| 919 | |
| 920 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 921 | /* Write a compiled module to a file, placing the time of last |
| 922 | modification of its source into the header. |
| 923 | Errors are ignored, if a write error occurs an attempt is made to |
| 924 | remove the file. */ |
| 925 | |
| 926 | static void |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 927 | write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 928 | { |
| 929 | FILE *fp; |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 930 | time_t mtime = srcstat->st_mtime; |
| 931 | mode_t mode = srcstat->st_mode; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 932 | |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 933 | fp = open_exclusive(cpathname, mode); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 934 | if (fp == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 935 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 936 | PySys_WriteStderr( |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 937 | "# can't create %s\n", cpathname); |
| 938 | return; |
| 939 | } |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 940 | PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 941 | /* First write a 0 for mtime */ |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 942 | PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION); |
| 943 | PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION); |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 944 | if (fflush(fp) != 0 || ferror(fp)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 945 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 946 | PySys_WriteStderr("# can't write %s\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 947 | /* Don't keep partial file */ |
| 948 | fclose(fp); |
| 949 | (void) unlink(cpathname); |
| 950 | return; |
| 951 | } |
| 952 | /* Now write the true mtime */ |
| 953 | fseek(fp, 4L, 0); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 954 | assert(mtime < LONG_MAX); |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 955 | PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 956 | fflush(fp); |
| 957 | fclose(fp); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 958 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 959 | PySys_WriteStderr("# wrote %s\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | |
| 963 | /* 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] | 964 | object WITH INCREMENTED REFERENCE COUNT. If there's a matching |
| 965 | byte-compiled file, use that instead. */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 966 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 967 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 968 | load_source_module(char *name, char *pathname, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 969 | { |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 970 | struct stat st; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 971 | FILE *fpc; |
| 972 | char buf[MAXPATHLEN+1]; |
| 973 | char *cpathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 974 | PyCodeObject *co; |
| 975 | PyObject *m; |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 976 | |
| 977 | if (fstat(fileno(fp), &st) != 0) { |
Neal Norwitz | 708e51a | 2005-10-03 04:48:15 +0000 | [diff] [blame] | 978 | PyErr_Format(PyExc_RuntimeError, |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 979 | "unable to get file status from '%s'", |
Neal Norwitz | 708e51a | 2005-10-03 04:48:15 +0000 | [diff] [blame] | 980 | pathname); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 981 | return NULL; |
Neal Norwitz | 708e51a | 2005-10-03 04:48:15 +0000 | [diff] [blame] | 982 | } |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 983 | #if SIZEOF_TIME_T > 4 |
| 984 | /* Python's .pyc timestamp handling presumes that the timestamp fits |
| 985 | in 4 bytes. This will be fine until sometime in the year 2038, |
| 986 | when a 4-byte signed time_t will overflow. |
| 987 | */ |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 988 | if (st.st_mtime >> 32) { |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 989 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | c63d3e9 | 2001-03-01 06:33:32 +0000 | [diff] [blame] | 990 | "modification time overflows a 4 byte field"); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 991 | return NULL; |
| 992 | } |
| 993 | #endif |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 994 | cpathname = make_compiled_pathname(pathname, buf, |
Jeremy Hylton | 37832f0 | 2001-04-13 17:50:20 +0000 | [diff] [blame] | 995 | (size_t)MAXPATHLEN + 1); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 996 | if (cpathname != NULL && |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 997 | (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 998 | co = read_compiled_module(cpathname, fpc); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 999 | fclose(fpc); |
| 1000 | if (co == NULL) |
| 1001 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1002 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1003 | PySys_WriteStderr("import %s # precompiled from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1004 | name, cpathname); |
Guido van Rossum | afd3dae | 1998-08-25 18:44:34 +0000 | [diff] [blame] | 1005 | pathname = cpathname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1006 | } |
| 1007 | else { |
| 1008 | co = parse_source_module(pathname, fp); |
| 1009 | if (co == NULL) |
| 1010 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1011 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1012 | PySys_WriteStderr("import %s # from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1013 | name, pathname); |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1014 | if (cpathname) { |
| 1015 | PyObject *ro = PySys_GetObject("dont_write_bytecode"); |
| 1016 | if (ro == NULL || !PyObject_IsTrue(ro)) |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 1017 | write_compiled_module(co, cpathname, &st); |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1018 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1019 | } |
Guido van Rossum | afd3dae | 1998-08-25 18:44:34 +0000 | [diff] [blame] | 1020 | m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1021 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1022 | |
| 1023 | return m; |
| 1024 | } |
| 1025 | |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1026 | /* Get source file -> unicode or None |
| 1027 | * Returns the path to the py file if available, else the given path |
| 1028 | */ |
| 1029 | static PyObject * |
| 1030 | get_sourcefile(const char *file) |
| 1031 | { |
| 1032 | char py[MAXPATHLEN + 1]; |
| 1033 | Py_ssize_t len; |
| 1034 | PyObject *u; |
| 1035 | struct stat statbuf; |
| 1036 | |
| 1037 | if (!file || !*file) { |
| 1038 | Py_RETURN_NONE; |
| 1039 | } |
| 1040 | |
| 1041 | len = strlen(file); |
Christian Heimes | 3540b50 | 2008-01-11 07:03:05 +0000 | [diff] [blame] | 1042 | /* match '*.py?' */ |
| 1043 | if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) { |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1044 | return PyUnicode_DecodeFSDefault(file); |
| 1045 | } |
| 1046 | |
| 1047 | strncpy(py, file, len-1); |
Martin v. Löwis | 5021ebc | 2008-03-22 22:07:13 +0000 | [diff] [blame] | 1048 | py[len-1] = '\0'; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1049 | if (stat(py, &statbuf) == 0 && |
| 1050 | S_ISREG(statbuf.st_mode)) { |
| 1051 | u = PyUnicode_DecodeFSDefault(py); |
| 1052 | } |
| 1053 | else { |
| 1054 | u = PyUnicode_DecodeFSDefault(file); |
| 1055 | } |
| 1056 | return u; |
| 1057 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1058 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1059 | /* Forward */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1060 | static PyObject *load_module(char *, FILE *, char *, int, PyObject *); |
| 1061 | static struct filedescr *find_module(char *, char *, PyObject *, |
| 1062 | char *, size_t, FILE **, PyObject **); |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1063 | static struct _frozen * find_frozen(char *); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1064 | |
| 1065 | /* Load a package and return its module object WITH INCREMENTED |
| 1066 | REFERENCE COUNT */ |
| 1067 | |
| 1068 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1069 | load_package(char *name, char *pathname) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1070 | { |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 1071 | PyObject *m, *d; |
| 1072 | PyObject *file = NULL; |
| 1073 | PyObject *path = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1074 | int err; |
| 1075 | char buf[MAXPATHLEN+1]; |
| 1076 | FILE *fp = NULL; |
| 1077 | struct filedescr *fdp; |
| 1078 | |
| 1079 | m = PyImport_AddModule(name); |
| 1080 | if (m == NULL) |
| 1081 | return NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1082 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1083 | PySys_WriteStderr("import %s # directory %s\n", |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1084 | name, pathname); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1085 | d = PyModule_GetDict(m); |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1086 | file = get_sourcefile(pathname); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1087 | if (file == NULL) |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 1088 | goto error; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1089 | path = Py_BuildValue("[O]", file); |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 1090 | if (path == NULL) |
| 1091 | goto error; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1092 | err = PyDict_SetItemString(d, "__file__", file); |
| 1093 | if (err == 0) |
| 1094 | err = PyDict_SetItemString(d, "__path__", path); |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 1095 | if (err != 0) |
| 1096 | goto error; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1097 | buf[0] = '\0'; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1098 | fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1099 | if (fdp == NULL) { |
| 1100 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 1101 | PyErr_Clear(); |
Thomas Heller | 2565324 | 2004-06-07 15:04:10 +0000 | [diff] [blame] | 1102 | Py_INCREF(m); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1103 | } |
| 1104 | else |
| 1105 | m = NULL; |
| 1106 | goto cleanup; |
| 1107 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1108 | m = load_module(name, fp, buf, fdp->type, NULL); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1109 | if (fp != NULL) |
| 1110 | fclose(fp); |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 1111 | goto cleanup; |
| 1112 | |
| 1113 | error: |
| 1114 | m = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1115 | cleanup: |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1116 | Py_XDECREF(path); |
| 1117 | Py_XDECREF(file); |
| 1118 | return m; |
| 1119 | } |
| 1120 | |
| 1121 | |
| 1122 | /* Helper to test for built-in module */ |
| 1123 | |
| 1124 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1125 | is_builtin(char *name) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1126 | { |
| 1127 | int i; |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 1128 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
| 1129 | if (strcmp(name, PyImport_Inittab[i].name) == 0) { |
| 1130 | if (PyImport_Inittab[i].initfunc == NULL) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1131 | return -1; |
| 1132 | else |
| 1133 | return 1; |
| 1134 | } |
| 1135 | } |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1139 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1140 | /* Return an importer object for a sys.path/pkg.__path__ item 'p', |
| 1141 | possibly by fetching it from the path_importer_cache dict. If it |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1142 | wasn't yet cached, traverse path_hooks until a hook is found |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1143 | that can handle the path item. Return None if no hook could; |
| 1144 | this tells our caller it should fall back to the builtin |
| 1145 | import mechanism. Cache the result in path_importer_cache. |
| 1146 | Returns a borrowed reference. */ |
| 1147 | |
| 1148 | static PyObject * |
| 1149 | get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks, |
| 1150 | PyObject *p) |
| 1151 | { |
| 1152 | PyObject *importer; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 1153 | Py_ssize_t j, nhooks; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1154 | |
| 1155 | /* These conditions are the caller's responsibility: */ |
| 1156 | assert(PyList_Check(path_hooks)); |
| 1157 | assert(PyDict_Check(path_importer_cache)); |
| 1158 | |
| 1159 | nhooks = PyList_Size(path_hooks); |
| 1160 | if (nhooks < 0) |
| 1161 | return NULL; /* Shouldn't happen */ |
| 1162 | |
| 1163 | importer = PyDict_GetItem(path_importer_cache, p); |
| 1164 | if (importer != NULL) |
| 1165 | return importer; |
| 1166 | |
| 1167 | /* set path_importer_cache[p] to None to avoid recursion */ |
| 1168 | if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) |
| 1169 | return NULL; |
| 1170 | |
| 1171 | for (j = 0; j < nhooks; j++) { |
| 1172 | PyObject *hook = PyList_GetItem(path_hooks, j); |
| 1173 | if (hook == NULL) |
| 1174 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1175 | importer = PyObject_CallFunctionObjArgs(hook, p, NULL); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1176 | if (importer != NULL) |
| 1177 | break; |
| 1178 | |
| 1179 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 1180 | return NULL; |
| 1181 | } |
| 1182 | PyErr_Clear(); |
| 1183 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1184 | if (importer == NULL) { |
| 1185 | importer = PyObject_CallFunctionObjArgs( |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1186 | (PyObject *)&PyNullImporter_Type, p, NULL |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1187 | ); |
| 1188 | if (importer == NULL) { |
| 1189 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 1190 | PyErr_Clear(); |
| 1191 | return Py_None; |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | if (importer != NULL) { |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1196 | int err = PyDict_SetItem(path_importer_cache, p, importer); |
| 1197 | Py_DECREF(importer); |
| 1198 | if (err != 0) |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | return importer; |
| 1202 | } |
| 1203 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1204 | PyAPI_FUNC(PyObject *) |
| 1205 | PyImport_GetImporter(PyObject *path) { |
| 1206 | PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL; |
| 1207 | |
| 1208 | if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) { |
| 1209 | if ((path_hooks = PySys_GetObject("path_hooks"))) { |
| 1210 | importer = get_path_importer(path_importer_cache, |
| 1211 | path_hooks, path); |
| 1212 | } |
| 1213 | } |
| 1214 | Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */ |
| 1215 | return importer; |
| 1216 | } |
| 1217 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1218 | /* Search the path (default sys.path) for a module. Return the |
| 1219 | corresponding filedescr struct, and (via return arguments) the |
| 1220 | pathname and an open file. Return NULL if the module is not found. */ |
| 1221 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1222 | #ifdef MS_COREDLL |
Thomas Wouters | b4bd21c | 2000-07-22 23:38:01 +0000 | [diff] [blame] | 1223 | extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1224 | char *, Py_ssize_t); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1225 | #endif |
| 1226 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1227 | static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1228 | static int find_init_module(char *); /* Forward */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1229 | static struct filedescr importhookdescr = {"", "", IMP_HOOK}; |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1230 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1231 | static struct filedescr * |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1232 | find_module(char *fullname, char *subname, PyObject *path, char *buf, |
| 1233 | size_t buflen, FILE **p_fp, PyObject **p_loader) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1234 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 1235 | Py_ssize_t i, npath; |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 1236 | size_t len, namelen; |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1237 | struct filedescr *fdp = NULL; |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 1238 | char *filemode; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1239 | FILE *fp = NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1240 | PyObject *path_hooks, *path_importer_cache; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1241 | struct stat statbuf; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1242 | static struct filedescr fd_frozen = {"", "", PY_FROZEN}; |
| 1243 | static struct filedescr fd_builtin = {"", "", C_BUILTIN}; |
| 1244 | static struct filedescr fd_package = {"", "", PKG_DIRECTORY}; |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 1245 | char name[MAXPATHLEN+1]; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1246 | #if defined(PYOS_OS2) |
| 1247 | size_t saved_len; |
| 1248 | size_t saved_namelen; |
| 1249 | char *saved_buf = NULL; |
| 1250 | #endif |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1251 | if (p_loader != NULL) |
| 1252 | *p_loader = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1253 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1254 | if (strlen(subname) > MAXPATHLEN) { |
Guido van Rossum | 8f4d331 | 2001-10-18 18:54:11 +0000 | [diff] [blame] | 1255 | PyErr_SetString(PyExc_OverflowError, |
| 1256 | "module name is too long"); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 1257 | return NULL; |
| 1258 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1259 | strcpy(name, subname); |
| 1260 | |
| 1261 | /* sys.meta_path import hook */ |
| 1262 | if (p_loader != NULL) { |
| 1263 | PyObject *meta_path; |
| 1264 | |
| 1265 | meta_path = PySys_GetObject("meta_path"); |
| 1266 | if (meta_path == NULL || !PyList_Check(meta_path)) { |
| 1267 | PyErr_SetString(PyExc_ImportError, |
| 1268 | "sys.meta_path must be a list of " |
| 1269 | "import hooks"); |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | Py_INCREF(meta_path); /* zap guard */ |
| 1273 | npath = PyList_Size(meta_path); |
| 1274 | for (i = 0; i < npath; i++) { |
| 1275 | PyObject *loader; |
| 1276 | PyObject *hook = PyList_GetItem(meta_path, i); |
| 1277 | loader = PyObject_CallMethod(hook, "find_module", |
| 1278 | "sO", fullname, |
| 1279 | path != NULL ? |
| 1280 | path : Py_None); |
| 1281 | if (loader == NULL) { |
| 1282 | Py_DECREF(meta_path); |
| 1283 | return NULL; /* true error */ |
| 1284 | } |
| 1285 | if (loader != Py_None) { |
| 1286 | /* a loader was found */ |
| 1287 | *p_loader = loader; |
| 1288 | Py_DECREF(meta_path); |
| 1289 | return &importhookdescr; |
| 1290 | } |
| 1291 | Py_DECREF(loader); |
| 1292 | } |
| 1293 | Py_DECREF(meta_path); |
| 1294 | } |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 1295 | |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 1296 | if (find_frozen(fullname) != NULL) { |
| 1297 | strcpy(buf, fullname); |
| 1298 | return &fd_frozen; |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 1299 | } |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 1300 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1301 | if (path == NULL) { |
| 1302 | if (is_builtin(name)) { |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1303 | strcpy(buf, name); |
| 1304 | return &fd_builtin; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1305 | } |
Guido van Rossum | ac27910 | 1996-08-22 23:10:58 +0000 | [diff] [blame] | 1306 | #ifdef MS_COREDLL |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1307 | fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen); |
| 1308 | if (fp != NULL) { |
| 1309 | *p_fp = fp; |
| 1310 | return fdp; |
| 1311 | } |
Guido van Rossum | a5a3db7 | 1996-04-09 02:39:59 +0000 | [diff] [blame] | 1312 | #endif |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1313 | path = PySys_GetObject("path"); |
| 1314 | } |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 1315 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1316 | if (path == NULL || !PyList_Check(path)) { |
| 1317 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1318 | "sys.path must be a list of directory names"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1319 | return NULL; |
| 1320 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1321 | |
| 1322 | path_hooks = PySys_GetObject("path_hooks"); |
| 1323 | if (path_hooks == NULL || !PyList_Check(path_hooks)) { |
| 1324 | PyErr_SetString(PyExc_ImportError, |
| 1325 | "sys.path_hooks must be a list of " |
| 1326 | "import hooks"); |
| 1327 | return NULL; |
| 1328 | } |
| 1329 | path_importer_cache = PySys_GetObject("path_importer_cache"); |
| 1330 | if (path_importer_cache == NULL || |
| 1331 | !PyDict_Check(path_importer_cache)) { |
| 1332 | PyErr_SetString(PyExc_ImportError, |
| 1333 | "sys.path_importer_cache must be a dict"); |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1337 | npath = PyList_Size(path); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1338 | namelen = strlen(name); |
| 1339 | for (i = 0; i < npath; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1340 | PyObject *v = PyList_GetItem(path, i); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1341 | PyObject *origv = v; |
Guido van Rossum | 6262cc7 | 2007-05-09 23:29:27 +0000 | [diff] [blame] | 1342 | const char *base; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1343 | Py_ssize_t size; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1344 | if (!v) |
| 1345 | return NULL; |
Guido van Rossum | a4b8d1d | 2007-08-27 15:02:28 +0000 | [diff] [blame] | 1346 | if (PyUnicode_Check(v)) { |
Amaury Forgeot d'Arc | f1ca0b1 | 2008-06-11 17:40:47 +0000 | [diff] [blame] | 1347 | v = PyUnicode_AsEncodedString(v, |
| 1348 | Py_FileSystemDefaultEncoding, NULL); |
Guido van Rossum | a4b8d1d | 2007-08-27 15:02:28 +0000 | [diff] [blame] | 1349 | if (v == NULL) |
| 1350 | return NULL; |
| 1351 | } |
Amaury Forgeot d'Arc | f1ca0b1 | 2008-06-11 17:40:47 +0000 | [diff] [blame] | 1352 | else if (!PyBytes_Check(v)) |
Guido van Rossum | a4b8d1d | 2007-08-27 15:02:28 +0000 | [diff] [blame] | 1353 | continue; |
Amaury Forgeot d'Arc | f1ca0b1 | 2008-06-11 17:40:47 +0000 | [diff] [blame] | 1354 | else |
| 1355 | Py_INCREF(v); |
| 1356 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1357 | base = PyBytes_AS_STRING(v); |
| 1358 | size = PyBytes_GET_SIZE(v); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1359 | len = size; |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1360 | if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) { |
Amaury Forgeot d'Arc | f1ca0b1 | 2008-06-11 17:40:47 +0000 | [diff] [blame] | 1361 | Py_DECREF(v); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1362 | continue; /* Too long */ |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1363 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1364 | strcpy(buf, base); |
Amaury Forgeot d'Arc | f1ca0b1 | 2008-06-11 17:40:47 +0000 | [diff] [blame] | 1365 | Py_DECREF(v); |
| 1366 | |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1367 | if (strlen(buf) != len) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1368 | continue; /* v contains '\0' */ |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1369 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1370 | |
| 1371 | /* sys.path_hooks import hook */ |
| 1372 | if (p_loader != NULL) { |
| 1373 | PyObject *importer; |
| 1374 | |
| 1375 | importer = get_path_importer(path_importer_cache, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1376 | path_hooks, origv); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1377 | if (importer == NULL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1378 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1379 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1380 | /* Note: importer is a borrowed reference */ |
| 1381 | if (importer != Py_None) { |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1382 | PyObject *loader; |
| 1383 | loader = PyObject_CallMethod(importer, |
| 1384 | "find_module", |
| 1385 | "s", fullname); |
| 1386 | if (loader == NULL) |
| 1387 | return NULL; /* error */ |
| 1388 | if (loader != Py_None) { |
| 1389 | /* a loader was found */ |
| 1390 | *p_loader = loader; |
| 1391 | return &importhookdescr; |
| 1392 | } |
| 1393 | Py_DECREF(loader); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1394 | continue; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1395 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1396 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1397 | /* no hook was found, use builtin import */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1398 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1399 | if (len > 0 && buf[len-1] != SEP |
| 1400 | #ifdef ALTSEP |
| 1401 | && buf[len-1] != ALTSEP |
| 1402 | #endif |
| 1403 | ) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1404 | buf[len++] = SEP; |
Guido van Rossum | 215c340 | 2000-11-13 17:26:32 +0000 | [diff] [blame] | 1405 | strcpy(buf+len, name); |
| 1406 | len += namelen; |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1407 | |
| 1408 | /* Check for package import (buf holds a directory name, |
| 1409 | and there's an __init__ module in that directory */ |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1410 | #ifdef HAVE_STAT |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1411 | if (stat(buf, &statbuf) == 0 && /* it exists */ |
| 1412 | S_ISDIR(statbuf.st_mode) && /* it's a directory */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1413 | case_ok(buf, len, namelen, name)) { /* case matches */ |
| 1414 | if (find_init_module(buf)) { /* and has __init__.py */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1415 | return &fd_package; |
| 1416 | } |
| 1417 | else { |
| 1418 | char warnstr[MAXPATHLEN+80]; |
| 1419 | sprintf(warnstr, "Not importing directory " |
| 1420 | "'%.*s': missing __init__.py", |
| 1421 | MAXPATHLEN, buf); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 1422 | if (PyErr_WarnEx(PyExc_ImportWarning, |
| 1423 | warnstr, 1)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1424 | return NULL; |
| 1425 | } |
| 1426 | } |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1427 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1428 | #endif |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1429 | #if defined(PYOS_OS2) |
| 1430 | /* take a snapshot of the module spec for restoration |
| 1431 | * after the 8 character DLL hackery |
| 1432 | */ |
| 1433 | saved_buf = strdup(buf); |
| 1434 | saved_len = len; |
| 1435 | saved_namelen = namelen; |
| 1436 | #endif /* PYOS_OS2 */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1437 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 1438 | #if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING) |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 1439 | /* OS/2 limits DLLs to 8 character names (w/o |
| 1440 | extension) |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1441 | * so if the name is longer than that and its a |
| 1442 | * dynamically loaded module we're going to try, |
| 1443 | * truncate the name before trying |
| 1444 | */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1445 | if (strlen(subname) > 8) { |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1446 | /* is this an attempt to load a C extension? */ |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 1447 | const struct filedescr *scan; |
| 1448 | scan = _PyImport_DynLoadFiletab; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1449 | while (scan->suffix != NULL) { |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 1450 | if (!strcmp(scan->suffix, fdp->suffix)) |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1451 | break; |
| 1452 | else |
| 1453 | scan++; |
| 1454 | } |
| 1455 | if (scan->suffix != NULL) { |
| 1456 | /* yes, so truncate the name */ |
| 1457 | namelen = 8; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1458 | len -= strlen(subname) - namelen; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1459 | buf[len] = '\0'; |
| 1460 | } |
| 1461 | } |
| 1462 | #endif /* PYOS_OS2 */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1463 | strcpy(buf+len, fdp->suffix); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1464 | if (Py_VerboseFlag > 1) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1465 | PySys_WriteStderr("# trying %s\n", buf); |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 1466 | filemode = fdp->mode; |
Tim Peters | 86c7d2f | 2004-08-01 23:24:21 +0000 | [diff] [blame] | 1467 | if (filemode[0] == 'U') |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 1468 | filemode = "r" PY_STDIOTEXTMODE; |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 1469 | fp = fopen(buf, filemode); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1470 | if (fp != NULL) { |
| 1471 | if (case_ok(buf, len, namelen, name)) |
| 1472 | break; |
| 1473 | else { /* continue search */ |
| 1474 | fclose(fp); |
| 1475 | fp = NULL; |
Barry Warsaw | 914a0b1 | 2001-02-02 19:12:16 +0000 | [diff] [blame] | 1476 | } |
Barry Warsaw | 914a0b1 | 2001-02-02 19:12:16 +0000 | [diff] [blame] | 1477 | } |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1478 | #if defined(PYOS_OS2) |
| 1479 | /* restore the saved snapshot */ |
| 1480 | strcpy(buf, saved_buf); |
| 1481 | len = saved_len; |
| 1482 | namelen = saved_namelen; |
| 1483 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1484 | } |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1485 | #if defined(PYOS_OS2) |
| 1486 | /* don't need/want the module name snapshot anymore */ |
| 1487 | if (saved_buf) |
| 1488 | { |
| 1489 | free(saved_buf); |
| 1490 | saved_buf = NULL; |
| 1491 | } |
| 1492 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1493 | if (fp != NULL) |
| 1494 | break; |
| 1495 | } |
| 1496 | if (fp == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1497 | PyErr_Format(PyExc_ImportError, |
| 1498 | "No module named %.200s", name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1499 | return NULL; |
| 1500 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1501 | *p_fp = fp; |
| 1502 | return fdp; |
| 1503 | } |
| 1504 | |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 1505 | /* Helpers for main.c |
| 1506 | * Find the source file corresponding to a named module |
| 1507 | */ |
| 1508 | struct filedescr * |
| 1509 | _PyImport_FindModule(const char *name, PyObject *path, char *buf, |
| 1510 | size_t buflen, FILE **p_fp, PyObject **p_loader) |
| 1511 | { |
| 1512 | return find_module((char *) name, (char *) name, path, |
| 1513 | buf, buflen, p_fp, p_loader); |
| 1514 | } |
| 1515 | |
| 1516 | PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd) |
| 1517 | { |
| 1518 | return fd->type == PY_SOURCE || fd->type == PY_COMPILED; |
| 1519 | } |
| 1520 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1521 | /* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name) |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1522 | * The arguments here are tricky, best shown by example: |
| 1523 | * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0 |
| 1524 | * ^ ^ ^ ^ |
| 1525 | * |--------------------- buf ---------------------| |
| 1526 | * |------------------- len ------------------| |
| 1527 | * |------ name -------| |
| 1528 | * |----- namelen -----| |
| 1529 | * buf is the full path, but len only counts up to (& exclusive of) the |
| 1530 | * extension. name is the module name, also exclusive of extension. |
| 1531 | * |
| 1532 | * We've already done a successful stat() or fopen() on buf, so know that |
| 1533 | * there's some match, possibly case-insensitive. |
| 1534 | * |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1535 | * case_ok() is to return 1 if there's a case-sensitive match for |
| 1536 | * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK |
| 1537 | * exists. |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1538 | * |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1539 | * case_ok() is used to implement case-sensitive import semantics even |
| 1540 | * on platforms with case-insensitive filesystems. It's trivial to implement |
| 1541 | * for case-sensitive filesystems. It's pretty much a cross-platform |
| 1542 | * nightmare for systems with case-insensitive filesystems. |
| 1543 | */ |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1544 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1545 | /* First we may need a pile of platform-specific header files; the sequence |
| 1546 | * of #if's here should match the sequence in the body of case_ok(). |
| 1547 | */ |
Jason Tishler | 7961aa6 | 2005-05-20 00:56:54 +0000 | [diff] [blame] | 1548 | #if defined(MS_WINDOWS) |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1549 | #include <windows.h> |
Guido van Rossum | 4c3f57c | 2001-01-10 20:40:46 +0000 | [diff] [blame] | 1550 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1551 | #elif defined(DJGPP) |
| 1552 | #include <dir.h> |
| 1553 | |
Jason Tishler | 7961aa6 | 2005-05-20 00:56:54 +0000 | [diff] [blame] | 1554 | #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H) |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1555 | #include <sys/types.h> |
| 1556 | #include <dirent.h> |
| 1557 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1558 | #elif defined(PYOS_OS2) |
| 1559 | #define INCL_DOS |
| 1560 | #define INCL_DOSERRORS |
| 1561 | #define INCL_NOPMAPI |
| 1562 | #include <os2.h> |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1563 | #endif |
| 1564 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1565 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1566 | case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name) |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1567 | { |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1568 | /* Pick a platform-specific implementation; the sequence of #if's here should |
| 1569 | * match the sequence just above. |
| 1570 | */ |
| 1571 | |
Jason Tishler | 7961aa6 | 2005-05-20 00:56:54 +0000 | [diff] [blame] | 1572 | /* MS_WINDOWS */ |
| 1573 | #if defined(MS_WINDOWS) |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1574 | WIN32_FIND_DATA data; |
| 1575 | HANDLE h; |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1576 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 1577 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1578 | return 1; |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1579 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1580 | h = FindFirstFile(buf, &data); |
| 1581 | if (h == INVALID_HANDLE_VALUE) { |
| 1582 | PyErr_Format(PyExc_NameError, |
| 1583 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1584 | name, buf); |
| 1585 | return 0; |
| 1586 | } |
| 1587 | FindClose(h); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1588 | return strncmp(data.cFileName, name, namelen) == 0; |
| 1589 | |
| 1590 | /* DJGPP */ |
| 1591 | #elif defined(DJGPP) |
| 1592 | struct ffblk ffblk; |
| 1593 | int done; |
| 1594 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 1595 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 1596 | return 1; |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1597 | |
| 1598 | done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC); |
| 1599 | if (done) { |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1600 | PyErr_Format(PyExc_NameError, |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1601 | "Can't find file for module %.100s\n(filename %.300s)", |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1602 | name, buf); |
| 1603 | return 0; |
| 1604 | } |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1605 | return strncmp(ffblk.ff_name, name, namelen) == 0; |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1606 | |
Jason Tishler | 7961aa6 | 2005-05-20 00:56:54 +0000 | [diff] [blame] | 1607 | /* new-fangled macintosh (macosx) or Cygwin */ |
| 1608 | #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H) |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1609 | DIR *dirp; |
| 1610 | struct dirent *dp; |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1611 | char dirname[MAXPATHLEN + 1]; |
| 1612 | const int dirlen = len - namelen - 1; /* don't want trailing SEP */ |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1613 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 1614 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1615 | return 1; |
| 1616 | |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1617 | /* Copy the dir component into dirname; substitute "." if empty */ |
| 1618 | if (dirlen <= 0) { |
| 1619 | dirname[0] = '.'; |
| 1620 | dirname[1] = '\0'; |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1621 | } |
| 1622 | else { |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1623 | assert(dirlen <= MAXPATHLEN); |
| 1624 | memcpy(dirname, buf, dirlen); |
| 1625 | dirname[dirlen] = '\0'; |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1626 | } |
| 1627 | /* Open the directory and search the entries for an exact match. */ |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1628 | dirp = opendir(dirname); |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1629 | if (dirp) { |
Tim Peters | 677898a | 2001-03-02 03:28:03 +0000 | [diff] [blame] | 1630 | char *nameWithExt = buf + len - namelen; |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1631 | while ((dp = readdir(dirp)) != NULL) { |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1632 | const int thislen = |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1633 | #ifdef _DIRENT_HAVE_D_NAMELEN |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1634 | dp->d_namlen; |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1635 | #else |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1636 | strlen(dp->d_name); |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1637 | #endif |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1638 | if (thislen >= namelen && |
Tim Peters | 677898a | 2001-03-02 03:28:03 +0000 | [diff] [blame] | 1639 | strcmp(dp->d_name, nameWithExt) == 0) { |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1640 | (void)closedir(dirp); |
| 1641 | return 1; /* Found */ |
| 1642 | } |
| 1643 | } |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1644 | (void)closedir(dirp); |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1645 | } |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1646 | return 0 ; /* Not found */ |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1647 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1648 | /* OS/2 */ |
| 1649 | #elif defined(PYOS_OS2) |
| 1650 | HDIR hdir = 1; |
| 1651 | ULONG srchcnt = 1; |
| 1652 | FILEFINDBUF3 ffbuf; |
| 1653 | APIRET rc; |
| 1654 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 1655 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1656 | return 1; |
| 1657 | |
| 1658 | rc = DosFindFirst(buf, |
| 1659 | &hdir, |
| 1660 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
| 1661 | &ffbuf, sizeof(ffbuf), |
| 1662 | &srchcnt, |
| 1663 | FIL_STANDARD); |
| 1664 | if (rc != NO_ERROR) |
| 1665 | return 0; |
| 1666 | return strncmp(ffbuf.achName, name, namelen) == 0; |
| 1667 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1668 | /* assuming it's a case-sensitive filesystem, so there's nothing to do! */ |
| 1669 | #else |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1670 | return 1; |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1671 | |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1672 | #endif |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1673 | } |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1674 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1675 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1676 | #ifdef HAVE_STAT |
| 1677 | /* Helper to look for __init__.py or __init__.py[co] in potential package */ |
| 1678 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1679 | find_init_module(char *buf) |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1680 | { |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1681 | const size_t save_len = strlen(buf); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 1682 | size_t i = save_len; |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1683 | char *pname; /* pointer to start of __init__ */ |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1684 | struct stat statbuf; |
| 1685 | |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1686 | /* For calling case_ok(buf, len, namelen, name): |
| 1687 | * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0 |
| 1688 | * ^ ^ ^ ^ |
| 1689 | * |--------------------- buf ---------------------| |
| 1690 | * |------------------- len ------------------| |
| 1691 | * |------ name -------| |
| 1692 | * |----- namelen -----| |
| 1693 | */ |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1694 | if (save_len + 13 >= MAXPATHLEN) |
| 1695 | return 0; |
| 1696 | buf[i++] = SEP; |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1697 | pname = buf + i; |
| 1698 | strcpy(pname, "__init__.py"); |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1699 | if (stat(buf, &statbuf) == 0) { |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1700 | if (case_ok(buf, |
| 1701 | save_len + 9, /* len("/__init__") */ |
| 1702 | 8, /* len("__init__") */ |
| 1703 | pname)) { |
| 1704 | buf[save_len] = '\0'; |
| 1705 | return 1; |
| 1706 | } |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1707 | } |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1708 | i += strlen(pname); |
| 1709 | strcpy(buf+i, Py_OptimizeFlag ? "o" : "c"); |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1710 | if (stat(buf, &statbuf) == 0) { |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1711 | if (case_ok(buf, |
| 1712 | save_len + 9, /* len("/__init__") */ |
| 1713 | 8, /* len("__init__") */ |
| 1714 | pname)) { |
| 1715 | buf[save_len] = '\0'; |
| 1716 | return 1; |
| 1717 | } |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1718 | } |
| 1719 | buf[save_len] = '\0'; |
| 1720 | return 0; |
| 1721 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1722 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1723 | #endif /* HAVE_STAT */ |
| 1724 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1725 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1726 | static int init_builtin(char *); /* Forward */ |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1727 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1728 | /* Load an external module using the default search path and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1729 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1730 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1731 | static PyObject * |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1732 | load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1733 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1734 | PyObject *modules; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1735 | PyObject *m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1736 | int err; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1737 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1738 | /* First check that there's an open file (if we need one) */ |
| 1739 | switch (type) { |
| 1740 | case PY_SOURCE: |
| 1741 | case PY_COMPILED: |
| 1742 | if (fp == NULL) { |
| 1743 | PyErr_Format(PyExc_ValueError, |
| 1744 | "file object required for import (type code %d)", |
| 1745 | type); |
| 1746 | return NULL; |
| 1747 | } |
| 1748 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1749 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1750 | switch (type) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1751 | |
| 1752 | case PY_SOURCE: |
| 1753 | m = load_source_module(name, buf, fp); |
| 1754 | break; |
| 1755 | |
| 1756 | case PY_COMPILED: |
| 1757 | m = load_compiled_module(name, buf, fp); |
| 1758 | break; |
| 1759 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 1760 | #ifdef HAVE_DYNAMIC_LOADING |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1761 | case C_EXTENSION: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1762 | m = _PyImport_LoadDynamicModule(name, buf, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1763 | break; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 1764 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1765 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1766 | case PKG_DIRECTORY: |
| 1767 | m = load_package(name, buf); |
| 1768 | break; |
| 1769 | |
| 1770 | case C_BUILTIN: |
| 1771 | case PY_FROZEN: |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1772 | if (buf != NULL && buf[0] != '\0') |
| 1773 | name = buf; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1774 | if (type == C_BUILTIN) |
| 1775 | err = init_builtin(name); |
| 1776 | else |
| 1777 | err = PyImport_ImportFrozenModule(name); |
| 1778 | if (err < 0) |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1779 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1780 | if (err == 0) { |
| 1781 | PyErr_Format(PyExc_ImportError, |
| 1782 | "Purported %s module %.200s not found", |
| 1783 | type == C_BUILTIN ? |
| 1784 | "builtin" : "frozen", |
| 1785 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1786 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1787 | } |
| 1788 | modules = PyImport_GetModuleDict(); |
| 1789 | m = PyDict_GetItemString(modules, name); |
| 1790 | if (m == NULL) { |
| 1791 | PyErr_Format( |
| 1792 | PyExc_ImportError, |
| 1793 | "%s module %.200s not properly initialized", |
| 1794 | type == C_BUILTIN ? |
| 1795 | "builtin" : "frozen", |
| 1796 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1797 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1798 | } |
| 1799 | Py_INCREF(m); |
| 1800 | break; |
| 1801 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1802 | case IMP_HOOK: { |
| 1803 | if (loader == NULL) { |
| 1804 | PyErr_SetString(PyExc_ImportError, |
| 1805 | "import hook without loader"); |
| 1806 | return NULL; |
| 1807 | } |
| 1808 | m = PyObject_CallMethod(loader, "load_module", "s", name); |
| 1809 | break; |
| 1810 | } |
| 1811 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1812 | default: |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1813 | PyErr_Format(PyExc_ImportError, |
| 1814 | "Don't know how to import %.200s (type code %d)", |
| 1815 | name, type); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1816 | m = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1817 | |
| 1818 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1819 | |
| 1820 | return m; |
| 1821 | } |
| 1822 | |
| 1823 | |
| 1824 | /* Initialize a built-in module. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1825 | Return 1 for success, 0 if the module is not found, and -1 with |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1826 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1827 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1828 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1829 | init_builtin(char *name) |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1830 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1831 | struct _inittab *p; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1832 | |
Greg Ward | 201baee | 2001-10-04 14:52:06 +0000 | [diff] [blame] | 1833 | if (_PyImport_FindExtension(name, name) != NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1834 | return 1; |
| 1835 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 1836 | for (p = PyImport_Inittab; p->name != NULL; p++) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1837 | PyObject *mod; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1838 | if (strcmp(name, p->name) == 0) { |
| 1839 | if (p->initfunc == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1840 | PyErr_Format(PyExc_ImportError, |
| 1841 | "Cannot re-init internal module %.200s", |
| 1842 | name); |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1843 | return -1; |
| 1844 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1845 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1846 | PySys_WriteStderr("import %s # builtin\n", name); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1847 | mod = (*p->initfunc)(); |
| 1848 | if (mod == 0) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1849 | return -1; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1850 | if (_PyImport_FixupExtension(mod, name, name) < 0) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1851 | return -1; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1852 | /* FixupExtension has put the module into sys.modules, |
| 1853 | so we can release our own reference. */ |
| 1854 | Py_DECREF(mod); |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1855 | return 1; |
| 1856 | } |
| 1857 | } |
| 1858 | return 0; |
| 1859 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1860 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1861 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1862 | /* Frozen modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1863 | |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1864 | static struct _frozen * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1865 | find_frozen(char *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1866 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1867 | struct _frozen *p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1868 | |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 1869 | if (!name) |
| 1870 | return NULL; |
| 1871 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1872 | for (p = PyImport_FrozenModules; ; p++) { |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1873 | if (p->name == NULL) |
| 1874 | return NULL; |
| 1875 | if (strcmp(p->name, name) == 0) |
| 1876 | break; |
| 1877 | } |
| 1878 | return p; |
| 1879 | } |
| 1880 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1881 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1882 | get_frozen_object(char *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1883 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1884 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1885 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1886 | |
| 1887 | if (p == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1888 | PyErr_Format(PyExc_ImportError, |
| 1889 | "No such frozen object named %.200s", |
| 1890 | name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1891 | return NULL; |
| 1892 | } |
Guido van Rossum | 8f4d331 | 2001-10-18 18:54:11 +0000 | [diff] [blame] | 1893 | if (p->code == NULL) { |
| 1894 | PyErr_Format(PyExc_ImportError, |
| 1895 | "Excluded frozen object named %.200s", |
| 1896 | name); |
| 1897 | return NULL; |
| 1898 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1899 | size = p->size; |
| 1900 | if (size < 0) |
| 1901 | size = -size; |
| 1902 | return PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | /* Initialize a frozen module. |
| 1906 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 1907 | an exception set if the initialization failed. |
| 1908 | This function is also used from frozenmain.c */ |
Guido van Rossum | 0b34490 | 1995-02-07 15:35:27 +0000 | [diff] [blame] | 1909 | |
| 1910 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1911 | PyImport_ImportFrozenModule(char *name) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1912 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1913 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1914 | PyObject *co; |
| 1915 | PyObject *m; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1916 | int ispackage; |
| 1917 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1918 | |
| 1919 | if (p == NULL) |
| 1920 | return 0; |
Guido van Rossum | 8f4d331 | 2001-10-18 18:54:11 +0000 | [diff] [blame] | 1921 | if (p->code == NULL) { |
| 1922 | PyErr_Format(PyExc_ImportError, |
| 1923 | "Excluded frozen object named %.200s", |
| 1924 | name); |
| 1925 | return -1; |
| 1926 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1927 | size = p->size; |
| 1928 | ispackage = (size < 0); |
| 1929 | if (ispackage) |
| 1930 | size = -size; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1931 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1932 | PySys_WriteStderr("import %s # frozen%s\n", |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1933 | name, ispackage ? " package" : ""); |
| 1934 | co = PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1935 | if (co == NULL) |
| 1936 | return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1937 | if (!PyCode_Check(co)) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1938 | PyErr_Format(PyExc_TypeError, |
| 1939 | "frozen object %.200s is not a code object", |
| 1940 | name); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1941 | goto err_return; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1942 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1943 | if (ispackage) { |
| 1944 | /* Set __path__ to the package name */ |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 1945 | PyObject *d, *s, *l; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1946 | int err; |
| 1947 | m = PyImport_AddModule(name); |
| 1948 | if (m == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1949 | goto err_return; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1950 | d = PyModule_GetDict(m); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1951 | s = PyUnicode_InternFromString(name); |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1952 | if (s == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1953 | goto err_return; |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 1954 | l = PyList_New(1); |
| 1955 | if (l == NULL) { |
| 1956 | Py_DECREF(s); |
| 1957 | goto err_return; |
| 1958 | } |
| 1959 | PyList_SET_ITEM(l, 0, s); |
| 1960 | err = PyDict_SetItemString(d, "__path__", l); |
| 1961 | Py_DECREF(l); |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1962 | if (err != 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1963 | goto err_return; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1964 | } |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 1965 | m = PyImport_ExecCodeModuleEx(name, co, "<frozen>"); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1966 | if (m == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1967 | goto err_return; |
| 1968 | Py_DECREF(co); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1969 | Py_DECREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1970 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1971 | err_return: |
| 1972 | Py_DECREF(co); |
| 1973 | return -1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1974 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1975 | |
| 1976 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1977 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1978 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1979 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1980 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1981 | PyImport_ImportModule(const char *name) |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1982 | { |
Marc-André Lemburg | 3c61c35 | 2001-02-09 19:40:15 +0000 | [diff] [blame] | 1983 | PyObject *pname; |
| 1984 | PyObject *result; |
| 1985 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1986 | pname = PyUnicode_FromString(name); |
Neal Norwitz | a11e4c1 | 2003-03-23 14:31:01 +0000 | [diff] [blame] | 1987 | if (pname == NULL) |
| 1988 | return NULL; |
Marc-André Lemburg | 3c61c35 | 2001-02-09 19:40:15 +0000 | [diff] [blame] | 1989 | result = PyImport_Import(pname); |
| 1990 | Py_DECREF(pname); |
| 1991 | return result; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1994 | /* Import a module without blocking |
| 1995 | * |
| 1996 | * At first it tries to fetch the module from sys.modules. If the module was |
| 1997 | * never loaded before it loads it with PyImport_ImportModule() unless another |
| 1998 | * thread holds the import lock. In the latter case the function raises an |
| 1999 | * ImportError instead of blocking. |
| 2000 | * |
| 2001 | * Returns the module object with incremented ref count. |
| 2002 | */ |
| 2003 | PyObject * |
| 2004 | PyImport_ImportModuleNoBlock(const char *name) |
| 2005 | { |
| 2006 | PyObject *result; |
| 2007 | PyObject *modules; |
| 2008 | long me; |
| 2009 | |
| 2010 | /* Try to get the module from sys.modules[name] */ |
| 2011 | modules = PyImport_GetModuleDict(); |
| 2012 | if (modules == NULL) |
| 2013 | return NULL; |
| 2014 | |
| 2015 | result = PyDict_GetItemString(modules, name); |
| 2016 | if (result != NULL) { |
| 2017 | Py_INCREF(result); |
| 2018 | return result; |
| 2019 | } |
| 2020 | else { |
| 2021 | PyErr_Clear(); |
| 2022 | } |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 2023 | #ifdef WITH_THREAD |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 2024 | /* check the import lock |
| 2025 | * me might be -1 but I ignore the error here, the lock function |
| 2026 | * takes care of the problem */ |
| 2027 | me = PyThread_get_thread_ident(); |
| 2028 | if (import_lock_thread == -1 || import_lock_thread == me) { |
| 2029 | /* no thread or me is holding the lock */ |
| 2030 | return PyImport_ImportModule(name); |
| 2031 | } |
| 2032 | else { |
| 2033 | PyErr_Format(PyExc_ImportError, |
| 2034 | "Failed to import %.200s because the import lock" |
| 2035 | "is held by another thread.", |
| 2036 | name); |
| 2037 | return NULL; |
| 2038 | } |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 2039 | #else |
| 2040 | return PyImport_ImportModule(name); |
| 2041 | #endif |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2044 | /* Forward declarations for helper routines */ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2045 | static PyObject *get_parent(PyObject *globals, char *buf, |
| 2046 | Py_ssize_t *p_buflen, int level); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 2047 | static PyObject *load_next(PyObject *mod, PyObject *altmod, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2048 | char **p_name, char *buf, Py_ssize_t *p_buflen); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 2049 | static int mark_miss(char *name); |
| 2050 | static int ensure_fromlist(PyObject *mod, PyObject *fromlist, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2051 | char *buf, Py_ssize_t buflen, int recursive); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 2052 | static PyObject * import_submodule(PyObject *mod, char *name, char *fullname); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2053 | |
| 2054 | /* The Magnum Opus of dotted-name import :-) */ |
| 2055 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 2056 | static PyObject * |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2057 | import_module_level(char *name, PyObject *globals, PyObject *locals, |
| 2058 | PyObject *fromlist, int level) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2059 | { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2060 | char buf[MAXPATHLEN+1]; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2061 | Py_ssize_t buflen = 0; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2062 | PyObject *parent, *head, *next, *tail; |
| 2063 | |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 2064 | if (strchr(name, '/') != NULL |
| 2065 | #ifdef MS_WINDOWS |
| 2066 | || strchr(name, '\\') != NULL |
| 2067 | #endif |
| 2068 | ) { |
| 2069 | PyErr_SetString(PyExc_ImportError, |
| 2070 | "Import by filename is not supported."); |
| 2071 | return NULL; |
| 2072 | } |
| 2073 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2074 | parent = get_parent(globals, buf, &buflen, level); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2075 | if (parent == NULL) |
| 2076 | return NULL; |
| 2077 | |
| 2078 | head = load_next(parent, Py_None, &name, buf, &buflen); |
| 2079 | if (head == NULL) |
| 2080 | return NULL; |
| 2081 | |
| 2082 | tail = head; |
| 2083 | Py_INCREF(tail); |
| 2084 | while (name) { |
| 2085 | next = load_next(tail, tail, &name, buf, &buflen); |
| 2086 | Py_DECREF(tail); |
| 2087 | if (next == NULL) { |
| 2088 | Py_DECREF(head); |
| 2089 | return NULL; |
| 2090 | } |
| 2091 | tail = next; |
| 2092 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2093 | if (tail == Py_None) { |
| 2094 | /* If tail is Py_None, both get_parent and load_next found |
| 2095 | an empty module name: someone called __import__("") or |
| 2096 | doctored faulty bytecode */ |
| 2097 | Py_DECREF(tail); |
| 2098 | Py_DECREF(head); |
| 2099 | PyErr_SetString(PyExc_ValueError, |
| 2100 | "Empty module name"); |
| 2101 | return NULL; |
| 2102 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2103 | |
| 2104 | if (fromlist != NULL) { |
| 2105 | if (fromlist == Py_None || !PyObject_IsTrue(fromlist)) |
| 2106 | fromlist = NULL; |
| 2107 | } |
| 2108 | |
| 2109 | if (fromlist == NULL) { |
| 2110 | Py_DECREF(tail); |
| 2111 | return head; |
| 2112 | } |
| 2113 | |
| 2114 | Py_DECREF(head); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 2115 | if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2116 | Py_DECREF(tail); |
| 2117 | return NULL; |
| 2118 | } |
| 2119 | |
| 2120 | return tail; |
| 2121 | } |
| 2122 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2123 | PyObject * |
| 2124 | PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, |
| 2125 | PyObject *fromlist, int level) |
| 2126 | { |
| 2127 | PyObject *result; |
| 2128 | lock_import(); |
| 2129 | result = import_module_level(name, globals, locals, fromlist, level); |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 2130 | if (unlock_import() < 0) { |
| 2131 | Py_XDECREF(result); |
| 2132 | PyErr_SetString(PyExc_RuntimeError, |
| 2133 | "not holding the import lock"); |
| 2134 | return NULL; |
| 2135 | } |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 2136 | return result; |
| 2137 | } |
| 2138 | |
Fred Drake | 8759090 | 2004-05-28 20:21:36 +0000 | [diff] [blame] | 2139 | /* Return the package that an import is being performed in. If globals comes |
| 2140 | from the module foo.bar.bat (not itself a package), this returns the |
| 2141 | sys.modules entry for foo.bar. If globals is from a package's __init__.py, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2142 | the package's entry in sys.modules is returned, as a borrowed reference. |
Fred Drake | 8759090 | 2004-05-28 20:21:36 +0000 | [diff] [blame] | 2143 | |
| 2144 | The *name* of the returned package is returned in buf, with the length of |
| 2145 | the name in *p_buflen. |
| 2146 | |
| 2147 | If globals doesn't come from a package or a module in a package, or a |
| 2148 | corresponding entry is not found in sys.modules, Py_None is returned. |
| 2149 | */ |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2150 | static PyObject * |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2151 | get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2152 | { |
| 2153 | static PyObject *namestr = NULL; |
| 2154 | static PyObject *pathstr = NULL; |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2155 | static PyObject *pkgstr = NULL; |
| 2156 | PyObject *pkgname, *modname, *modpath, *modules, *parent; |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 2157 | int orig_level = level; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2158 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2159 | if (globals == NULL || !PyDict_Check(globals) || !level) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2160 | return Py_None; |
| 2161 | |
| 2162 | if (namestr == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2163 | namestr = PyUnicode_InternFromString("__name__"); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2164 | if (namestr == NULL) |
| 2165 | return NULL; |
| 2166 | } |
| 2167 | if (pathstr == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2168 | pathstr = PyUnicode_InternFromString("__path__"); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2169 | if (pathstr == NULL) |
| 2170 | return NULL; |
| 2171 | } |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2172 | if (pkgstr == NULL) { |
| 2173 | pkgstr = PyUnicode_InternFromString("__package__"); |
| 2174 | if (pkgstr == NULL) |
| 2175 | return NULL; |
| 2176 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2177 | |
| 2178 | *buf = '\0'; |
| 2179 | *p_buflen = 0; |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2180 | pkgname = PyDict_GetItem(globals, pkgstr); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2181 | |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2182 | if ((pkgname != NULL) && (pkgname != Py_None)) { |
| 2183 | /* __package__ is set, so use it */ |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2184 | char *pkgname_str; |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2185 | Py_ssize_t len; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2186 | |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2187 | if (!PyUnicode_Check(pkgname)) { |
| 2188 | PyErr_SetString(PyExc_ValueError, |
| 2189 | "__package__ set to non-string"); |
| 2190 | return NULL; |
| 2191 | } |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 2192 | pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len); |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2193 | if (len == 0) { |
| 2194 | if (level > 0) { |
| 2195 | PyErr_SetString(PyExc_ValueError, |
| 2196 | "Attempted relative import in non-package"); |
| 2197 | return NULL; |
| 2198 | } |
| 2199 | return Py_None; |
| 2200 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2201 | if (len > MAXPATHLEN) { |
| 2202 | PyErr_SetString(PyExc_ValueError, |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2203 | "Package name too long"); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2204 | return NULL; |
| 2205 | } |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2206 | strcpy(buf, pkgname_str); |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2207 | } else { |
| 2208 | /* __package__ not set, so figure it out and set it */ |
| 2209 | modname = PyDict_GetItem(globals, namestr); |
| 2210 | if (modname == NULL || !PyUnicode_Check(modname)) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2211 | return Py_None; |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2212 | |
| 2213 | modpath = PyDict_GetItem(globals, pathstr); |
| 2214 | if (modpath != NULL) { |
| 2215 | /* __path__ is set, so modname is already the package name */ |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2216 | char *modname_str; |
| 2217 | Py_ssize_t len; |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2218 | int error; |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2219 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 2220 | modname_str = _PyUnicode_AsStringAndSize(modname, &len); |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2221 | if (len > MAXPATHLEN) { |
| 2222 | PyErr_SetString(PyExc_ValueError, |
| 2223 | "Module name too long"); |
| 2224 | return NULL; |
| 2225 | } |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 2226 | strcpy(buf, modname_str); |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2227 | error = PyDict_SetItem(globals, pkgstr, modname); |
| 2228 | if (error) { |
| 2229 | PyErr_SetString(PyExc_ValueError, |
| 2230 | "Could not set __package__"); |
| 2231 | return NULL; |
| 2232 | } |
| 2233 | } else { |
| 2234 | /* Normal module, so work out the package name if any */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 2235 | char *start = _PyUnicode_AsString(modname); |
Nick Coghlan | de10c85 | 2007-12-04 12:22:52 +0000 | [diff] [blame] | 2236 | char *lastdot = strrchr(start, '.'); |
| 2237 | size_t len; |
| 2238 | int error; |
| 2239 | if (lastdot == NULL && level > 0) { |
| 2240 | PyErr_SetString(PyExc_ValueError, |
| 2241 | "Attempted relative import in non-package"); |
| 2242 | return NULL; |
| 2243 | } |
| 2244 | if (lastdot == NULL) { |
| 2245 | error = PyDict_SetItem(globals, pkgstr, Py_None); |
| 2246 | if (error) { |
| 2247 | PyErr_SetString(PyExc_ValueError, |
| 2248 | "Could not set __package__"); |
| 2249 | return NULL; |
| 2250 | } |
| 2251 | return Py_None; |
| 2252 | } |
| 2253 | len = lastdot - start; |
| 2254 | if (len >= MAXPATHLEN) { |
| 2255 | PyErr_SetString(PyExc_ValueError, |
| 2256 | "Module name too long"); |
| 2257 | return NULL; |
| 2258 | } |
| 2259 | strncpy(buf, start, len); |
| 2260 | buf[len] = '\0'; |
| 2261 | pkgname = PyUnicode_FromString(buf); |
| 2262 | if (pkgname == NULL) { |
| 2263 | return NULL; |
| 2264 | } |
| 2265 | error = PyDict_SetItem(globals, pkgstr, pkgname); |
| 2266 | Py_DECREF(pkgname); |
| 2267 | if (error) { |
| 2268 | PyErr_SetString(PyExc_ValueError, |
| 2269 | "Could not set __package__"); |
| 2270 | return NULL; |
| 2271 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2272 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2273 | } |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2274 | while (--level > 0) { |
| 2275 | char *dot = strrchr(buf, '.'); |
| 2276 | if (dot == NULL) { |
| 2277 | PyErr_SetString(PyExc_ValueError, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2278 | "Attempted relative import beyond " |
| 2279 | "toplevel package"); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2280 | return NULL; |
| 2281 | } |
| 2282 | *dot = '\0'; |
| 2283 | } |
| 2284 | *p_buflen = strlen(buf); |
| 2285 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2286 | modules = PyImport_GetModuleDict(); |
| 2287 | parent = PyDict_GetItemString(modules, buf); |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 2288 | if (parent == NULL) { |
| 2289 | if (orig_level < 1) { |
| 2290 | PyObject *err_msg = PyBytes_FromFormat( |
| 2291 | "Parent module '%.200s' not found " |
| 2292 | "while handling absolute import", buf); |
| 2293 | if (err_msg == NULL) { |
| 2294 | return NULL; |
| 2295 | } |
| 2296 | if (!PyErr_WarnEx(PyExc_RuntimeWarning, |
| 2297 | PyBytes_AsString(err_msg), 1)) { |
| 2298 | *buf = '\0'; |
| 2299 | *p_buflen = 0; |
| 2300 | parent = Py_None; |
| 2301 | } |
| 2302 | Py_DECREF(err_msg); |
| 2303 | } else { |
| 2304 | PyErr_Format(PyExc_SystemError, |
| 2305 | "Parent module '%.200s' not loaded, " |
| 2306 | "cannot perform relative import", buf); |
| 2307 | } |
| 2308 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2309 | return parent; |
| 2310 | /* We expect, but can't guarantee, if parent != None, that: |
| 2311 | - parent.__name__ == buf |
| 2312 | - parent.__dict__ is globals |
| 2313 | If this is violated... Who cares? */ |
| 2314 | } |
| 2315 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2316 | /* altmod is either None or same as mod */ |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2317 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2318 | load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2319 | Py_ssize_t *p_buflen) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2320 | { |
| 2321 | char *name = *p_name; |
| 2322 | char *dot = strchr(name, '.'); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 2323 | size_t len; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2324 | char *p; |
| 2325 | PyObject *result; |
| 2326 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2327 | if (strlen(name) == 0) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2328 | /* completely empty module name should only happen in |
| 2329 | 'from . import' (or '__import__("")')*/ |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2330 | Py_INCREF(mod); |
| 2331 | *p_name = NULL; |
| 2332 | return mod; |
| 2333 | } |
| 2334 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2335 | if (dot == NULL) { |
| 2336 | *p_name = NULL; |
| 2337 | len = strlen(name); |
| 2338 | } |
| 2339 | else { |
| 2340 | *p_name = dot+1; |
| 2341 | len = dot-name; |
| 2342 | } |
Guido van Rossum | 111c20b | 1998-04-11 17:38:22 +0000 | [diff] [blame] | 2343 | if (len == 0) { |
| 2344 | PyErr_SetString(PyExc_ValueError, |
| 2345 | "Empty module name"); |
| 2346 | return NULL; |
| 2347 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2348 | |
| 2349 | p = buf + *p_buflen; |
| 2350 | if (p != buf) |
| 2351 | *p++ = '.'; |
| 2352 | if (p+len-buf >= MAXPATHLEN) { |
| 2353 | PyErr_SetString(PyExc_ValueError, |
| 2354 | "Module name too long"); |
| 2355 | return NULL; |
| 2356 | } |
| 2357 | strncpy(p, name, len); |
| 2358 | p[len] = '\0'; |
| 2359 | *p_buflen = p+len-buf; |
| 2360 | |
| 2361 | result = import_submodule(mod, p, buf); |
| 2362 | if (result == Py_None && altmod != mod) { |
| 2363 | Py_DECREF(result); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 2364 | /* Here, altmod must be None and mod must not be None */ |
Guido van Rossum | 0c81945 | 1997-09-07 06:16:57 +0000 | [diff] [blame] | 2365 | result = import_submodule(altmod, p, p); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 2366 | if (result != NULL && result != Py_None) { |
| 2367 | if (mark_miss(buf) != 0) { |
| 2368 | Py_DECREF(result); |
| 2369 | return NULL; |
| 2370 | } |
| 2371 | strncpy(buf, name, len); |
| 2372 | buf[len] = '\0'; |
| 2373 | *p_buflen = len; |
| 2374 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2375 | } |
| 2376 | if (result == NULL) |
| 2377 | return NULL; |
| 2378 | |
| 2379 | if (result == Py_None) { |
| 2380 | Py_DECREF(result); |
| 2381 | PyErr_Format(PyExc_ImportError, |
| 2382 | "No module named %.200s", name); |
| 2383 | return NULL; |
| 2384 | } |
| 2385 | |
| 2386 | return result; |
| 2387 | } |
| 2388 | |
| 2389 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2390 | mark_miss(char *name) |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 2391 | { |
| 2392 | PyObject *modules = PyImport_GetModuleDict(); |
| 2393 | return PyDict_SetItemString(modules, name, Py_None); |
| 2394 | } |
| 2395 | |
| 2396 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2397 | ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen, |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2398 | int recursive) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2399 | { |
| 2400 | int i; |
| 2401 | |
| 2402 | if (!PyObject_HasAttrString(mod, "__path__")) |
| 2403 | return 1; |
| 2404 | |
| 2405 | for (i = 0; ; i++) { |
| 2406 | PyObject *item = PySequence_GetItem(fromlist, i); |
| 2407 | int hasit; |
| 2408 | if (item == NULL) { |
| 2409 | if (PyErr_ExceptionMatches(PyExc_IndexError)) { |
| 2410 | PyErr_Clear(); |
| 2411 | return 1; |
| 2412 | } |
| 2413 | return 0; |
| 2414 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2415 | if (!PyUnicode_Check(item)) { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2416 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 2417 | "Item in ``from list'' not a string"); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2418 | Py_DECREF(item); |
| 2419 | return 0; |
| 2420 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2421 | if (PyUnicode_AS_UNICODE(item)[0] == '*') { |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 2422 | PyObject *all; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2423 | Py_DECREF(item); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 2424 | /* See if the package defines __all__ */ |
| 2425 | if (recursive) |
| 2426 | continue; /* Avoid endless recursion */ |
| 2427 | all = PyObject_GetAttrString(mod, "__all__"); |
| 2428 | if (all == NULL) |
| 2429 | PyErr_Clear(); |
| 2430 | else { |
Martin v. Löwis | 83969ee | 2004-03-23 16:28:13 +0000 | [diff] [blame] | 2431 | int ret = ensure_fromlist(mod, all, buf, buflen, 1); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 2432 | Py_DECREF(all); |
Martin v. Löwis | 83969ee | 2004-03-23 16:28:13 +0000 | [diff] [blame] | 2433 | if (!ret) |
| 2434 | return 0; |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 2435 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2436 | continue; |
| 2437 | } |
| 2438 | hasit = PyObject_HasAttr(mod, item); |
| 2439 | if (!hasit) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2440 | PyObject *item8; |
| 2441 | char *subname; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2442 | PyObject *submod; |
| 2443 | char *p; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2444 | if (!Py_FileSystemDefaultEncoding) { |
| 2445 | item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item), |
| 2446 | PyUnicode_GetSize(item), |
Martin v. Löwis | 427dbff | 2007-06-12 05:53:00 +0000 | [diff] [blame] | 2447 | NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2448 | } else { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2449 | item8 = PyUnicode_AsEncodedString(item, |
| 2450 | Py_FileSystemDefaultEncoding, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2451 | } |
| 2452 | if (!item8) { |
| 2453 | PyErr_SetString(PyExc_ValueError, "Cannot encode path item"); |
| 2454 | return 0; |
| 2455 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2456 | subname = PyBytes_AS_STRING(item8); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2457 | if (buflen + strlen(subname) >= MAXPATHLEN) { |
| 2458 | PyErr_SetString(PyExc_ValueError, |
| 2459 | "Module name too long"); |
| 2460 | Py_DECREF(item); |
| 2461 | return 0; |
| 2462 | } |
| 2463 | p = buf + buflen; |
| 2464 | *p++ = '.'; |
| 2465 | strcpy(p, subname); |
| 2466 | submod = import_submodule(mod, subname, buf); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2467 | Py_DECREF(item8); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2468 | Py_XDECREF(submod); |
| 2469 | if (submod == NULL) { |
| 2470 | Py_DECREF(item); |
| 2471 | return 0; |
| 2472 | } |
| 2473 | } |
| 2474 | Py_DECREF(item); |
| 2475 | } |
| 2476 | |
Guido van Rossum | a7f2e81 | 1997-10-03 15:33:32 +0000 | [diff] [blame] | 2477 | /* NOTREACHED */ |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Neil Schemenauer | 00b0966 | 2003-06-16 21:03:07 +0000 | [diff] [blame] | 2480 | static int |
| 2481 | add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname, |
| 2482 | PyObject *modules) |
| 2483 | { |
| 2484 | if (mod == Py_None) |
| 2485 | return 1; |
| 2486 | /* Irrespective of the success of this load, make a |
| 2487 | reference to it in the parent package module. A copy gets |
| 2488 | saved in the modules dictionary under the full name, so get a |
| 2489 | reference from there, if need be. (The exception is when the |
| 2490 | load failed with a SyntaxError -- then there's no trace in |
| 2491 | sys.modules. In that case, of course, do nothing extra.) */ |
| 2492 | if (submod == NULL) { |
| 2493 | submod = PyDict_GetItemString(modules, fullname); |
| 2494 | if (submod == NULL) |
| 2495 | return 1; |
| 2496 | } |
| 2497 | if (PyModule_Check(mod)) { |
| 2498 | /* We can't use setattr here since it can give a |
| 2499 | * spurious warning if the submodule name shadows a |
| 2500 | * builtin name */ |
| 2501 | PyObject *dict = PyModule_GetDict(mod); |
| 2502 | if (!dict) |
| 2503 | return 0; |
| 2504 | if (PyDict_SetItemString(dict, subname, submod) < 0) |
| 2505 | return 0; |
| 2506 | } |
| 2507 | else { |
| 2508 | if (PyObject_SetAttrString(mod, subname, submod) < 0) |
| 2509 | return 0; |
| 2510 | } |
| 2511 | return 1; |
| 2512 | } |
| 2513 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2514 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2515 | import_submodule(PyObject *mod, char *subname, char *fullname) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2516 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2517 | PyObject *modules = PyImport_GetModuleDict(); |
Neil Schemenauer | 00b0966 | 2003-06-16 21:03:07 +0000 | [diff] [blame] | 2518 | PyObject *m = NULL; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2519 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2520 | /* Require: |
| 2521 | if mod == None: subname == fullname |
| 2522 | else: mod.__name__ + "." + subname == fullname |
| 2523 | */ |
| 2524 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 2525 | if ((m = PyDict_GetItemString(modules, fullname)) != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2526 | Py_INCREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 2527 | } |
| 2528 | else { |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2529 | PyObject *path, *loader = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2530 | char buf[MAXPATHLEN+1]; |
| 2531 | struct filedescr *fdp; |
| 2532 | FILE *fp = NULL; |
| 2533 | |
Guido van Rossum | 9c0afe5 | 1998-05-19 15:09:05 +0000 | [diff] [blame] | 2534 | if (mod == Py_None) |
| 2535 | path = NULL; |
| 2536 | else { |
| 2537 | path = PyObject_GetAttrString(mod, "__path__"); |
| 2538 | if (path == NULL) { |
| 2539 | PyErr_Clear(); |
| 2540 | Py_INCREF(Py_None); |
| 2541 | return Py_None; |
| 2542 | } |
| 2543 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2544 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2545 | buf[0] = '\0'; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2546 | fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1, |
| 2547 | &fp, &loader); |
Guido van Rossum | b68cd42 | 1998-07-01 17:36:26 +0000 | [diff] [blame] | 2548 | Py_XDECREF(path); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2549 | if (fdp == NULL) { |
| 2550 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) |
| 2551 | return NULL; |
| 2552 | PyErr_Clear(); |
| 2553 | Py_INCREF(Py_None); |
| 2554 | return Py_None; |
| 2555 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2556 | m = load_module(fullname, fp, buf, fdp->type, loader); |
| 2557 | Py_XDECREF(loader); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2558 | if (fp) |
| 2559 | fclose(fp); |
Neil Schemenauer | 00b0966 | 2003-06-16 21:03:07 +0000 | [diff] [blame] | 2560 | if (!add_submodule(mod, m, fullname, subname, modules)) { |
| 2561 | Py_XDECREF(m); |
| 2562 | m = NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2563 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2564 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2565 | |
| 2566 | return m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2569 | |
| 2570 | /* Re-import a module of any kind and return its module object, WITH |
| 2571 | INCREMENTED REFERENCE COUNT */ |
| 2572 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2573 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2574 | PyImport_ReloadModule(PyObject *m) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2575 | { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2576 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 2577 | PyObject *modules_reloading = interp->modules_reloading; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2578 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2579 | PyObject *path = NULL, *loader = NULL, *existing_m = NULL; |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2580 | char *name, *subname; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2581 | char buf[MAXPATHLEN+1]; |
| 2582 | struct filedescr *fdp; |
| 2583 | FILE *fp = NULL; |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 2584 | PyObject *newm; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2585 | |
| 2586 | if (modules_reloading == NULL) { |
| 2587 | Py_FatalError("PyImport_ReloadModule: " |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2588 | "no modules_reloading dictionary!"); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2589 | return NULL; |
| 2590 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2591 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2592 | if (m == NULL || !PyModule_Check(m)) { |
| 2593 | PyErr_SetString(PyExc_TypeError, |
| 2594 | "reload() argument must be module"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2595 | return NULL; |
| 2596 | } |
Neal Norwitz | 5616c6d | 2007-08-26 05:32:41 +0000 | [diff] [blame] | 2597 | name = (char*)PyModule_GetName(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2598 | if (name == NULL) |
| 2599 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2600 | if (m != PyDict_GetItemString(modules, name)) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2601 | PyErr_Format(PyExc_ImportError, |
| 2602 | "reload(): module %.200s not in sys.modules", |
| 2603 | name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2604 | return NULL; |
| 2605 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2606 | existing_m = PyDict_GetItemString(modules_reloading, name); |
| 2607 | if (existing_m != NULL) { |
| 2608 | /* Due to a recursive reload, this module is already |
| 2609 | being reloaded. */ |
| 2610 | Py_INCREF(existing_m); |
| 2611 | return existing_m; |
| 2612 | } |
| 2613 | if (PyDict_SetItemString(modules_reloading, name, m) < 0) |
| 2614 | return NULL; |
| 2615 | |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2616 | subname = strrchr(name, '.'); |
| 2617 | if (subname == NULL) |
| 2618 | subname = name; |
| 2619 | else { |
| 2620 | PyObject *parentname, *parent; |
Christian Heimes | c4cb3b8 | 2007-11-04 12:10:01 +0000 | [diff] [blame] | 2621 | parentname = PyUnicode_FromStringAndSize(name, (subname-name)); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2622 | if (parentname == NULL) { |
| 2623 | imp_modules_reloading_clear(); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2624 | return NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2625 | } |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2626 | parent = PyDict_GetItem(modules, parentname); |
| 2627 | if (parent == NULL) { |
| 2628 | PyErr_Format(PyExc_ImportError, |
| 2629 | "reload(): parent %.200s not in sys.modules", |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 2630 | _PyUnicode_AsString(parentname)); |
Georg Brandl | 0c55f29 | 2005-09-14 06:56:20 +0000 | [diff] [blame] | 2631 | Py_DECREF(parentname); |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2632 | imp_modules_reloading_clear(); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2633 | return NULL; |
| 2634 | } |
Georg Brandl | 0c55f29 | 2005-09-14 06:56:20 +0000 | [diff] [blame] | 2635 | Py_DECREF(parentname); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2636 | subname++; |
| 2637 | path = PyObject_GetAttrString(parent, "__path__"); |
| 2638 | if (path == NULL) |
| 2639 | PyErr_Clear(); |
| 2640 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2641 | buf[0] = '\0'; |
Phillip J. Eby | 7ec642a | 2004-09-23 04:37:36 +0000 | [diff] [blame] | 2642 | fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2643 | Py_XDECREF(path); |
Phillip J. Eby | 7ec642a | 2004-09-23 04:37:36 +0000 | [diff] [blame] | 2644 | |
| 2645 | if (fdp == NULL) { |
| 2646 | Py_XDECREF(loader); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2647 | imp_modules_reloading_clear(); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2648 | return NULL; |
Phillip J. Eby | 7ec642a | 2004-09-23 04:37:36 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | newm = load_module(name, fp, buf, fdp->type, loader); |
| 2652 | Py_XDECREF(loader); |
| 2653 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2654 | if (fp) |
| 2655 | fclose(fp); |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 2656 | if (newm == NULL) { |
| 2657 | /* load_module probably removed name from modules because of |
| 2658 | * the error. Put back the original module object. We're |
| 2659 | * going to return NULL in this case regardless of whether |
| 2660 | * replacing name succeeds, so the return value is ignored. |
| 2661 | */ |
| 2662 | PyDict_SetItemString(modules, name, m); |
| 2663 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2664 | imp_modules_reloading_clear(); |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 2665 | return newm; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2669 | /* Higher-level import emulator which emulates the "import" statement |
| 2670 | more accurately -- it invokes the __import__() function from the |
| 2671 | builtins of the current globals. This means that the import is |
| 2672 | done using whatever import hooks are installed in the current |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 2673 | environment, e.g. by "rexec". |
| 2674 | A dummy list ["__doc__"] is passed as the 4th argument so that |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 2675 | e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 2676 | will return <module "gencache"> instead of <module "win32com">. */ |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2677 | |
| 2678 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2679 | PyImport_Import(PyObject *module_name) |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2680 | { |
| 2681 | static PyObject *silly_list = NULL; |
| 2682 | static PyObject *builtins_str = NULL; |
| 2683 | static PyObject *import_str = NULL; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2684 | PyObject *globals = NULL; |
| 2685 | PyObject *import = NULL; |
| 2686 | PyObject *builtins = NULL; |
| 2687 | PyObject *r = NULL; |
| 2688 | |
| 2689 | /* Initialize constant string objects */ |
| 2690 | if (silly_list == NULL) { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2691 | import_str = PyUnicode_InternFromString("__import__"); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2692 | if (import_str == NULL) |
| 2693 | return NULL; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2694 | builtins_str = PyUnicode_InternFromString("__builtins__"); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2695 | if (builtins_str == NULL) |
| 2696 | return NULL; |
| 2697 | silly_list = Py_BuildValue("[s]", "__doc__"); |
| 2698 | if (silly_list == NULL) |
| 2699 | return NULL; |
| 2700 | } |
| 2701 | |
| 2702 | /* Get the builtins from current globals */ |
| 2703 | globals = PyEval_GetGlobals(); |
Guido van Rossum | 85cd1d6 | 2001-02-20 21:43:24 +0000 | [diff] [blame] | 2704 | if (globals != NULL) { |
Guido van Rossum | 6646856 | 1998-10-22 15:46:50 +0000 | [diff] [blame] | 2705 | Py_INCREF(globals); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2706 | builtins = PyObject_GetItem(globals, builtins_str); |
| 2707 | if (builtins == NULL) |
| 2708 | goto err; |
| 2709 | } |
| 2710 | else { |
| 2711 | /* No globals -- use standard builtins, and fake globals */ |
| 2712 | PyErr_Clear(); |
| 2713 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 2714 | builtins = PyImport_ImportModuleLevel("builtins", |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2715 | NULL, NULL, NULL, 0); |
Guido van Rossum | 85cd1d6 | 2001-02-20 21:43:24 +0000 | [diff] [blame] | 2716 | if (builtins == NULL) |
| 2717 | return NULL; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2718 | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
| 2719 | if (globals == NULL) |
| 2720 | goto err; |
| 2721 | } |
| 2722 | |
| 2723 | /* Get the __import__ function from the builtins */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2724 | if (PyDict_Check(builtins)) { |
Fred Drake | a76ba6e | 2001-03-06 06:31:15 +0000 | [diff] [blame] | 2725 | import = PyObject_GetItem(builtins, import_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2726 | if (import == NULL) |
| 2727 | PyErr_SetObject(PyExc_KeyError, import_str); |
| 2728 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2729 | else |
Fred Drake | a76ba6e | 2001-03-06 06:31:15 +0000 | [diff] [blame] | 2730 | import = PyObject_GetAttr(builtins, import_str); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2731 | if (import == NULL) |
| 2732 | goto err; |
| 2733 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 2734 | /* Call the __import__ function with the proper argument list |
| 2735 | * Always use absolute import here. */ |
| 2736 | r = PyObject_CallFunction(import, "OOOOi", module_name, globals, |
| 2737 | globals, silly_list, 0, NULL); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2738 | |
| 2739 | err: |
| 2740 | Py_XDECREF(globals); |
| 2741 | Py_XDECREF(builtins); |
| 2742 | Py_XDECREF(import); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 2743 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2744 | return r; |
| 2745 | } |
| 2746 | |
| 2747 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2748 | /* Module 'imp' provides Python access to the primitives used for |
| 2749 | importing modules. |
| 2750 | */ |
| 2751 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2752 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 2753 | imp_get_magic(PyObject *self, PyObject *noargs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2754 | { |
| 2755 | char buf[4]; |
| 2756 | |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 2757 | buf[0] = (char) ((pyc_magic >> 0) & 0xff); |
| 2758 | buf[1] = (char) ((pyc_magic >> 8) & 0xff); |
| 2759 | buf[2] = (char) ((pyc_magic >> 16) & 0xff); |
| 2760 | buf[3] = (char) ((pyc_magic >> 24) & 0xff); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2761 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2762 | return PyBytes_FromStringAndSize(buf, 4); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2765 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 2766 | imp_get_suffixes(PyObject *self, PyObject *noargs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2767 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2768 | PyObject *list; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2769 | struct filedescr *fdp; |
| 2770 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2771 | list = PyList_New(0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2772 | if (list == NULL) |
| 2773 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2774 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
| 2775 | PyObject *item = Py_BuildValue("ssi", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2776 | fdp->suffix, fdp->mode, fdp->type); |
| 2777 | if (item == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2778 | Py_DECREF(list); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2779 | return NULL; |
| 2780 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2781 | if (PyList_Append(list, item) < 0) { |
| 2782 | Py_DECREF(list); |
| 2783 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2784 | return NULL; |
| 2785 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2786 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2787 | } |
| 2788 | return list; |
| 2789 | } |
| 2790 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2791 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2792 | call_find_module(char *name, PyObject *path) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2793 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 2794 | extern int fclose(FILE *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2795 | PyObject *fob, *ret; |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 2796 | PyObject *pathobj; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2797 | struct filedescr *fdp; |
| 2798 | char pathname[MAXPATHLEN+1]; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2799 | FILE *fp = NULL; |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 2800 | int fd = -1; |
Brett Cannon | 3bb42d9 | 2007-10-20 03:43:15 +0000 | [diff] [blame] | 2801 | char *found_encoding = NULL; |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 2802 | char *encoding = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2803 | |
| 2804 | pathname[0] = '\0'; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2805 | if (path == Py_None) |
| 2806 | path = NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2807 | fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2808 | if (fdp == NULL) |
| 2809 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2810 | if (fp != NULL) { |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 2811 | fd = fileno(fp); |
| 2812 | if (fd != -1) |
| 2813 | fd = dup(fd); |
| 2814 | fclose(fp); |
| 2815 | fp = NULL; |
| 2816 | } |
| 2817 | if (fd != -1) { |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 2818 | if (strchr(fdp->mode, 'b') == NULL) { |
Brett Cannon | 3bb42d9 | 2007-10-20 03:43:15 +0000 | [diff] [blame] | 2819 | /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed |
| 2820 | memory. */ |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 2821 | found_encoding = PyTokenizer_FindEncoding(fd); |
| 2822 | lseek(fd, 0, 0); /* Reset position */ |
Amaury Forgeot d'Arc | 1b933ed | 2008-09-04 22:34:09 +0000 | [diff] [blame] | 2823 | if (found_encoding == NULL && PyErr_Occurred()) |
| 2824 | return NULL; |
Brett Cannon | 3bb42d9 | 2007-10-20 03:43:15 +0000 | [diff] [blame] | 2825 | encoding = (found_encoding != NULL) ? found_encoding : |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 2826 | (char*)PyUnicode_GetDefaultEncoding(); |
| 2827 | } |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 2828 | fob = PyFile_FromFd(fd, pathname, fdp->mode, -1, |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 2829 | (char*)encoding, NULL, NULL, 1); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2830 | if (fob == NULL) { |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 2831 | close(fd); |
Brett Cannon | 3bb42d9 | 2007-10-20 03:43:15 +0000 | [diff] [blame] | 2832 | PyMem_FREE(found_encoding); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2833 | return NULL; |
| 2834 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2835 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2836 | else { |
| 2837 | fob = Py_None; |
| 2838 | Py_INCREF(fob); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 2839 | } |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 2840 | pathobj = PyUnicode_DecodeFSDefault(pathname); |
| 2841 | ret = Py_BuildValue("NN(ssi)", |
| 2842 | fob, pathobj, fdp->suffix, fdp->mode, fdp->type); |
Brett Cannon | 3bb42d9 | 2007-10-20 03:43:15 +0000 | [diff] [blame] | 2843 | PyMem_FREE(found_encoding); |
| 2844 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2845 | return ret; |
| 2846 | } |
| 2847 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2848 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2849 | imp_find_module(PyObject *self, PyObject *args) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2850 | { |
| 2851 | char *name; |
| 2852 | PyObject *path = NULL; |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 2853 | if (!PyArg_ParseTuple(args, "es|O:find_module", |
| 2854 | Py_FileSystemDefaultEncoding, &name, |
| 2855 | &path)) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2856 | return NULL; |
| 2857 | return call_find_module(name, path); |
| 2858 | } |
| 2859 | |
| 2860 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2861 | imp_init_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2862 | { |
| 2863 | char *name; |
| 2864 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2865 | PyObject *m; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2866 | if (!PyArg_ParseTuple(args, "s:init_builtin", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2867 | return NULL; |
| 2868 | ret = init_builtin(name); |
| 2869 | if (ret < 0) |
| 2870 | return NULL; |
| 2871 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2872 | Py_INCREF(Py_None); |
| 2873 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2874 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2875 | m = PyImport_AddModule(name); |
| 2876 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2877 | return m; |
| 2878 | } |
| 2879 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2880 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2881 | imp_init_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2882 | { |
| 2883 | char *name; |
| 2884 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2885 | PyObject *m; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2886 | if (!PyArg_ParseTuple(args, "s:init_frozen", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2887 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2888 | ret = PyImport_ImportFrozenModule(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2889 | if (ret < 0) |
| 2890 | return NULL; |
| 2891 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2892 | Py_INCREF(Py_None); |
| 2893 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2894 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2895 | m = PyImport_AddModule(name); |
| 2896 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2897 | return m; |
| 2898 | } |
| 2899 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2900 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2901 | imp_get_frozen_object(PyObject *self, PyObject *args) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2902 | { |
| 2903 | char *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 2904 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2905 | if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name)) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2906 | return NULL; |
| 2907 | return get_frozen_object(name); |
| 2908 | } |
| 2909 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2910 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2911 | imp_is_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2912 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2913 | char *name; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2914 | if (!PyArg_ParseTuple(args, "s:is_builtin", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2915 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2916 | return PyLong_FromLong(is_builtin(name)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2919 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2920 | imp_is_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2921 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2922 | char *name; |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 2923 | struct _frozen *p; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2924 | if (!PyArg_ParseTuple(args, "s:is_frozen", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2925 | return NULL; |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 2926 | p = find_frozen(name); |
Guido van Rossum | b8bff3f | 2002-04-07 06:34:38 +0000 | [diff] [blame] | 2927 | return PyBool_FromLong((long) (p == NULL ? 0 : p->size)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | static FILE * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2931 | get_file(char *pathname, PyObject *fob, char *mode) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2932 | { |
| 2933 | FILE *fp; |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 2934 | if (mode[0] == 'U') |
| 2935 | mode = "r" PY_STDIOTEXTMODE; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2936 | if (fob == NULL) { |
| 2937 | fp = fopen(pathname, mode); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2938 | } |
| 2939 | else { |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 2940 | int fd = PyObject_AsFileDescriptor(fob); |
| 2941 | if (fd == -1) |
| 2942 | return NULL; |
| 2943 | /* XXX This will leak a FILE struct. Fix this!!!! |
| 2944 | (But it doesn't leak a file descrioptor!) */ |
| 2945 | fp = fdopen(fd, mode); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2946 | } |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 2947 | if (fp == NULL) |
| 2948 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2949 | return fp; |
| 2950 | } |
| 2951 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2952 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2953 | imp_load_compiled(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2954 | { |
| 2955 | char *name; |
| 2956 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2957 | PyObject *fob = NULL; |
| 2958 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2959 | FILE *fp; |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 2960 | if (!PyArg_ParseTuple(args, "ss|O:load_compiled", |
| 2961 | &name, &pathname, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2962 | return NULL; |
| 2963 | fp = get_file(pathname, fob, "rb"); |
| 2964 | if (fp == NULL) |
| 2965 | return NULL; |
| 2966 | m = load_compiled_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2967 | if (fob == NULL) |
| 2968 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2969 | return m; |
| 2970 | } |
| 2971 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2972 | #ifdef HAVE_DYNAMIC_LOADING |
| 2973 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2974 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2975 | imp_load_dynamic(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2976 | { |
| 2977 | char *name; |
| 2978 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2979 | PyObject *fob = NULL; |
| 2980 | PyObject *m; |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2981 | FILE *fp = NULL; |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 2982 | if (!PyArg_ParseTuple(args, "ss|O:load_dynamic", |
| 2983 | &name, &pathname, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2984 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2985 | if (fob) { |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2986 | fp = get_file(pathname, fob, "r"); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2987 | if (fp == NULL) |
| 2988 | return NULL; |
| 2989 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2990 | m = _PyImport_LoadDynamicModule(name, pathname, fp); |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2991 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2994 | #endif /* HAVE_DYNAMIC_LOADING */ |
| 2995 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2996 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2997 | imp_load_source(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2998 | { |
| 2999 | char *name; |
| 3000 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3001 | PyObject *fob = NULL; |
| 3002 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3003 | FILE *fp; |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 3004 | if (!PyArg_ParseTuple(args, "ss|O:load_source", |
| 3005 | &name, &pathname, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3006 | return NULL; |
| 3007 | fp = get_file(pathname, fob, "r"); |
| 3008 | if (fp == NULL) |
| 3009 | return NULL; |
| 3010 | m = load_source_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3011 | if (fob == NULL) |
| 3012 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3013 | return m; |
| 3014 | } |
| 3015 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3016 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3017 | imp_load_module(PyObject *self, PyObject *args) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3018 | { |
| 3019 | char *name; |
| 3020 | PyObject *fob; |
| 3021 | char *pathname; |
| 3022 | char *suffix; /* Unused */ |
| 3023 | char *mode; |
| 3024 | int type; |
| 3025 | FILE *fp; |
| 3026 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 3027 | if (!PyArg_ParseTuple(args, "sOs(ssi):load_module", |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3028 | &name, &fob, &pathname, |
| 3029 | &suffix, &mode, &type)) |
| 3030 | return NULL; |
Guido van Rossum | 5e2c5fa | 2002-05-30 17:33:07 +0000 | [diff] [blame] | 3031 | if (*mode) { |
| 3032 | /* Mode must start with 'r' or 'U' and must not contain '+'. |
| 3033 | Implicit in this test is the assumption that the mode |
| 3034 | may contain other modifiers like 'b' or 't'. */ |
| 3035 | |
| 3036 | if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) { |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 3037 | PyErr_Format(PyExc_ValueError, |
| 3038 | "invalid file open mode %.200s", mode); |
| 3039 | return NULL; |
Guido van Rossum | 5e2c5fa | 2002-05-30 17:33:07 +0000 | [diff] [blame] | 3040 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3041 | } |
| 3042 | if (fob == Py_None) |
| 3043 | fp = NULL; |
| 3044 | else { |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 3045 | fp = get_file(NULL, fob, mode); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3046 | if (fp == NULL) |
| 3047 | return NULL; |
| 3048 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 3049 | return load_module(name, fp, pathname, type, NULL); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3053 | imp_load_package(PyObject *self, PyObject *args) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3054 | { |
| 3055 | char *name; |
| 3056 | char *pathname; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 3057 | if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname)) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3058 | return NULL; |
| 3059 | return load_package(name, pathname); |
| 3060 | } |
| 3061 | |
| 3062 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3063 | imp_new_module(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3064 | { |
| 3065 | char *name; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 3066 | if (!PyArg_ParseTuple(args, "s:new_module", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3067 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3068 | return PyModule_New(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 3071 | static PyObject * |
| 3072 | imp_reload(PyObject *self, PyObject *v) |
| 3073 | { |
| 3074 | return PyImport_ReloadModule(v); |
| 3075 | } |
| 3076 | |
| 3077 | PyDoc_STRVAR(doc_reload, |
| 3078 | "reload(module) -> module\n\ |
| 3079 | \n\ |
| 3080 | Reload the module. The module must have been successfully imported before."); |
| 3081 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3082 | /* Doc strings */ |
| 3083 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3084 | PyDoc_STRVAR(doc_imp, |
| 3085 | "This module provides the components needed to build your own\n\ |
| 3086 | __import__ function. Undocumented functions are obsolete."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3087 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3088 | PyDoc_STRVAR(doc_find_module, |
| 3089 | "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3090 | Search for a module. If path is omitted or None, search for a\n\ |
| 3091 | built-in, frozen or special module and continue search in sys.path.\n\ |
| 3092 | The module name cannot contain '.'; to search for a submodule of a\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3093 | package, pass the submodule name and the package's __path__."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3094 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3095 | PyDoc_STRVAR(doc_load_module, |
| 3096 | "load_module(name, file, filename, (suffix, mode, type)) -> module\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3097 | Load a module, given information returned by find_module().\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3098 | The module name must include the full package name, if any."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3099 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3100 | PyDoc_STRVAR(doc_get_magic, |
| 3101 | "get_magic() -> string\n\ |
| 3102 | Return the magic number for .pyc or .pyo files."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3103 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3104 | PyDoc_STRVAR(doc_get_suffixes, |
| 3105 | "get_suffixes() -> [(suffix, mode, type), ...]\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3106 | Return a list of (suffix, mode, type) tuples describing the files\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3107 | that find_module() looks for."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3108 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3109 | PyDoc_STRVAR(doc_new_module, |
| 3110 | "new_module(name) -> module\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3111 | Create a new module. Do not enter it in sys.modules.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3112 | The module name must include the full package name, if any."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3113 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3114 | PyDoc_STRVAR(doc_lock_held, |
Tim Peters | a7c6509 | 2004-08-01 23:26:05 +0000 | [diff] [blame] | 3115 | "lock_held() -> boolean\n\ |
| 3116 | Return True if the import lock is currently held, else False.\n\ |
| 3117 | On platforms without threads, return False."); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 3118 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 3119 | PyDoc_STRVAR(doc_acquire_lock, |
| 3120 | "acquire_lock() -> None\n\ |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 3121 | Acquires the interpreter's import lock for the current thread.\n\ |
| 3122 | This lock should be used by import hooks to ensure thread-safety\n\ |
| 3123 | when importing modules.\n\ |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 3124 | On platforms without threads, this function does nothing."); |
| 3125 | |
| 3126 | PyDoc_STRVAR(doc_release_lock, |
| 3127 | "release_lock() -> None\n\ |
| 3128 | Release the interpreter's import lock.\n\ |
| 3129 | On platforms without threads, this function does nothing."); |
| 3130 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3131 | static PyMethodDef imp_methods[] = { |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 3132 | {"find_module", imp_find_module, METH_VARARGS, doc_find_module}, |
| 3133 | {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic}, |
| 3134 | {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes}, |
| 3135 | {"load_module", imp_load_module, METH_VARARGS, doc_load_module}, |
| 3136 | {"new_module", imp_new_module, METH_VARARGS, doc_new_module}, |
| 3137 | {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, |
| 3138 | {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock}, |
| 3139 | {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock}, |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 3140 | {"reload", imp_reload, METH_O, doc_reload}, |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3141 | /* The rest are obsolete */ |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 3142 | {"get_frozen_object", imp_get_frozen_object, METH_VARARGS}, |
| 3143 | {"init_builtin", imp_init_builtin, METH_VARARGS}, |
| 3144 | {"init_frozen", imp_init_frozen, METH_VARARGS}, |
| 3145 | {"is_builtin", imp_is_builtin, METH_VARARGS}, |
| 3146 | {"is_frozen", imp_is_frozen, METH_VARARGS}, |
| 3147 | {"load_compiled", imp_load_compiled, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 3148 | #ifdef HAVE_DYNAMIC_LOADING |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 3149 | {"load_dynamic", imp_load_dynamic, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 3150 | #endif |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 3151 | {"load_package", imp_load_package, METH_VARARGS}, |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 3152 | {"load_source", imp_load_source, METH_VARARGS}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3153 | {NULL, NULL} /* sentinel */ |
| 3154 | }; |
| 3155 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 3156 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3157 | setint(PyObject *d, char *name, int value) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3158 | { |
| 3159 | PyObject *v; |
| 3160 | int err; |
| 3161 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3162 | v = PyLong_FromLong((long)value); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3163 | err = PyDict_SetItemString(d, name, v); |
| 3164 | Py_XDECREF(v); |
| 3165 | return err; |
| 3166 | } |
| 3167 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3168 | typedef struct { |
| 3169 | PyObject_HEAD |
| 3170 | } NullImporter; |
| 3171 | |
| 3172 | static int |
| 3173 | NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds) |
| 3174 | { |
| 3175 | char *path; |
Christian Heimes | 7d3bc0a | 2007-11-07 17:26:16 +0000 | [diff] [blame] | 3176 | Py_ssize_t pathlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3177 | |
| 3178 | if (!_PyArg_NoKeywords("NullImporter()", kwds)) |
| 3179 | return -1; |
| 3180 | |
Amaury Forgeot d'Arc | f1ca0b1 | 2008-06-11 17:40:47 +0000 | [diff] [blame] | 3181 | if (!PyArg_ParseTuple(args, "es:NullImporter", |
| 3182 | Py_FileSystemDefaultEncoding, &path)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3183 | return -1; |
| 3184 | |
Christian Heimes | 7d3bc0a | 2007-11-07 17:26:16 +0000 | [diff] [blame] | 3185 | pathlen = strlen(path); |
| 3186 | if (pathlen == 0) { |
Georg Brandl | 8494d57 | 2008-07-19 10:13:15 +0000 | [diff] [blame] | 3187 | PyMem_Free(path); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3188 | PyErr_SetString(PyExc_ImportError, "empty pathname"); |
| 3189 | return -1; |
| 3190 | } else { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3191 | struct stat statbuf; |
| 3192 | int rv; |
| 3193 | |
| 3194 | rv = stat(path, &statbuf); |
Guido van Rossum | c1bdbc3 | 2007-11-07 17:46:34 +0000 | [diff] [blame] | 3195 | #ifdef MS_WINDOWS |
| 3196 | /* MS Windows stat() chokes on paths like C:\path\. Try to |
| 3197 | * recover *one* time by stripping off a trailing slash or |
| 3198 | * backslash. http://bugs.python.org/issue1293 |
| 3199 | */ |
Christian Heimes | 7d3bc0a | 2007-11-07 17:26:16 +0000 | [diff] [blame] | 3200 | if (rv != 0 && pathlen <= MAXPATHLEN && |
| 3201 | (path[pathlen-1] == '/' || path[pathlen-1] == '\\')) { |
| 3202 | char mangled[MAXPATHLEN+1]; |
| 3203 | |
| 3204 | strcpy(mangled, path); |
| 3205 | mangled[pathlen-1] = '\0'; |
| 3206 | rv = stat(mangled, &statbuf); |
| 3207 | } |
Christian Heimes | 7d3bc0a | 2007-11-07 17:26:16 +0000 | [diff] [blame] | 3208 | #endif |
Georg Brandl | 8494d57 | 2008-07-19 10:13:15 +0000 | [diff] [blame] | 3209 | PyMem_Free(path); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3210 | if (rv == 0) { |
| 3211 | /* it exists */ |
| 3212 | if (S_ISDIR(statbuf.st_mode)) { |
| 3213 | /* it's a directory */ |
| 3214 | PyErr_SetString(PyExc_ImportError, |
| 3215 | "existing directory"); |
| 3216 | return -1; |
| 3217 | } |
| 3218 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3219 | } |
| 3220 | return 0; |
| 3221 | } |
| 3222 | |
| 3223 | static PyObject * |
| 3224 | NullImporter_find_module(NullImporter *self, PyObject *args) |
| 3225 | { |
| 3226 | Py_RETURN_NONE; |
| 3227 | } |
| 3228 | |
| 3229 | static PyMethodDef NullImporter_methods[] = { |
| 3230 | {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS, |
| 3231 | "Always return None" |
| 3232 | }, |
| 3233 | {NULL} /* Sentinel */ |
| 3234 | }; |
| 3235 | |
| 3236 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 3237 | PyTypeObject PyNullImporter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3238 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3239 | "imp.NullImporter", /*tp_name*/ |
| 3240 | sizeof(NullImporter), /*tp_basicsize*/ |
| 3241 | 0, /*tp_itemsize*/ |
| 3242 | 0, /*tp_dealloc*/ |
| 3243 | 0, /*tp_print*/ |
| 3244 | 0, /*tp_getattr*/ |
| 3245 | 0, /*tp_setattr*/ |
| 3246 | 0, /*tp_compare*/ |
| 3247 | 0, /*tp_repr*/ |
| 3248 | 0, /*tp_as_number*/ |
| 3249 | 0, /*tp_as_sequence*/ |
| 3250 | 0, /*tp_as_mapping*/ |
| 3251 | 0, /*tp_hash */ |
| 3252 | 0, /*tp_call*/ |
| 3253 | 0, /*tp_str*/ |
| 3254 | 0, /*tp_getattro*/ |
| 3255 | 0, /*tp_setattro*/ |
| 3256 | 0, /*tp_as_buffer*/ |
| 3257 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3258 | "Null importer object", /* tp_doc */ |
| 3259 | 0, /* tp_traverse */ |
| 3260 | 0, /* tp_clear */ |
| 3261 | 0, /* tp_richcompare */ |
| 3262 | 0, /* tp_weaklistoffset */ |
| 3263 | 0, /* tp_iter */ |
| 3264 | 0, /* tp_iternext */ |
| 3265 | NullImporter_methods, /* tp_methods */ |
| 3266 | 0, /* tp_members */ |
| 3267 | 0, /* tp_getset */ |
| 3268 | 0, /* tp_base */ |
| 3269 | 0, /* tp_dict */ |
| 3270 | 0, /* tp_descr_get */ |
| 3271 | 0, /* tp_descr_set */ |
| 3272 | 0, /* tp_dictoffset */ |
| 3273 | (initproc)NullImporter_init, /* tp_init */ |
| 3274 | 0, /* tp_alloc */ |
| 3275 | PyType_GenericNew /* tp_new */ |
| 3276 | }; |
| 3277 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3278 | static struct PyModuleDef impmodule = { |
| 3279 | PyModuleDef_HEAD_INIT, |
| 3280 | "imp", |
| 3281 | doc_imp, |
| 3282 | 0, |
| 3283 | imp_methods, |
| 3284 | NULL, |
| 3285 | NULL, |
| 3286 | NULL, |
| 3287 | NULL |
| 3288 | }; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3289 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 3290 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3291 | PyInit_imp(void) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3292 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3293 | PyObject *m, *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3294 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 3295 | if (PyType_Ready(&PyNullImporter_Type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3296 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3297 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3298 | m = PyModule_Create(&impmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 3299 | if (m == NULL) |
| 3300 | goto failure; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3301 | d = PyModule_GetDict(m); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3302 | if (d == NULL) |
| 3303 | goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3304 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3305 | if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; |
| 3306 | if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; |
| 3307 | if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure; |
| 3308 | if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure; |
| 3309 | if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure; |
| 3310 | if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure; |
| 3311 | if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure; |
| 3312 | if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure; |
Guido van Rossum | 0f84a34 | 1998-08-06 13:36:01 +0000 | [diff] [blame] | 3313 | if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 3314 | if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3315 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 3316 | Py_INCREF(&PyNullImporter_Type); |
| 3317 | PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3318 | return m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3319 | failure: |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3320 | Py_XDECREF(m); |
| 3321 | return NULL; |
| 3322 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3323 | } |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3324 | |
| 3325 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 3326 | /* API for embedding applications that want to add their own entries |
| 3327 | to the table of built-in modules. This should normally be called |
| 3328 | *before* Py_Initialize(). When the table resize fails, -1 is |
| 3329 | returned and the existing table is unchanged. |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3330 | |
| 3331 | After a similar function by Just van Rossum. */ |
| 3332 | |
| 3333 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3334 | PyImport_ExtendInittab(struct _inittab *newtab) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3335 | { |
| 3336 | static struct _inittab *our_copy = NULL; |
| 3337 | struct _inittab *p; |
| 3338 | int i, n; |
| 3339 | |
| 3340 | /* Count the number of entries in both tables */ |
| 3341 | for (n = 0; newtab[n].name != NULL; n++) |
| 3342 | ; |
| 3343 | if (n == 0) |
| 3344 | return 0; /* Nothing to do */ |
| 3345 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) |
| 3346 | ; |
| 3347 | |
| 3348 | /* Allocate new memory for the combined table */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 3349 | p = our_copy; |
| 3350 | PyMem_RESIZE(p, struct _inittab, i+n+1); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3351 | if (p == NULL) |
| 3352 | return -1; |
| 3353 | |
| 3354 | /* Copy the tables into the new memory */ |
| 3355 | if (our_copy != PyImport_Inittab) |
| 3356 | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
| 3357 | PyImport_Inittab = our_copy = p; |
| 3358 | memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); |
| 3359 | |
| 3360 | return 0; |
| 3361 | } |
| 3362 | |
| 3363 | /* Shorthand to add a single entry given a name and a function */ |
| 3364 | |
| 3365 | int |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3366 | PyImport_AppendInittab(char *name, PyObject* (*initfunc)(void)) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3367 | { |
| 3368 | struct _inittab newtab[2]; |
| 3369 | |
| 3370 | memset(newtab, '\0', sizeof newtab); |
| 3371 | |
| 3372 | newtab[0].name = name; |
| 3373 | newtab[0].initfunc = initfunc; |
| 3374 | |
| 3375 | return PyImport_ExtendInittab(newtab); |
| 3376 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3377 | |
| 3378 | #ifdef __cplusplus |
| 3379 | } |
| 3380 | #endif |