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