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