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