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) |
Benjamin Peterson | abdb552 | 2012-03-15 15:40:37 -0500 | [diff] [blame] | 107 | 3210 (added size modulo 2**32 to the pyc header) |
| 108 | Python 3.3a1 3220 (changed PEP 380 implementation) |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 109 | */ |
Guido van Rossum | 3ddee71 | 1991-12-16 13:06:34 +0000 | [diff] [blame] | 110 | |
Nick Coghlan | cd419ab | 2010-09-11 00:39:25 +0000 | [diff] [blame] | 111 | /* MAGIC must change whenever the bytecode emitted by the compiler may no |
| 112 | longer be understood by older implementations of the eval loop (usually |
| 113 | due to the addition of new opcodes) |
Martin v. Löwis | fadcd31 | 2011-10-23 18:08:20 +0200 | [diff] [blame] | 114 | TAG must change for each major Python release. The magic number will take |
| 115 | care of any bytecode changes that occur during development. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 116 | */ |
Benjamin Peterson | 48deae1 | 2011-06-03 17:50:16 -0500 | [diff] [blame] | 117 | #define QUOTE(arg) #arg |
| 118 | #define STRIFY(name) QUOTE(name) |
| 119 | #define MAJOR STRIFY(PY_MAJOR_VERSION) |
| 120 | #define MINOR STRIFY(PY_MINOR_VERSION) |
Benjamin Peterson | f53d20f | 2012-03-16 09:39:12 -0500 | [diff] [blame] | 121 | #define MAGIC (3220 | ((long)'\r'<<16) | ((long)'\n'<<24)) |
Benjamin Peterson | 48deae1 | 2011-06-03 17:50:16 -0500 | [diff] [blame] | 122 | #define TAG "cpython-" MAJOR MINOR; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 123 | #define CACHEDIR "__pycache__" |
| 124 | /* Current magic word and string tag as globals. */ |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 125 | static long pyc_magic = MAGIC; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 126 | static const char *pyc_tag = TAG; |
Benjamin Peterson | 48deae1 | 2011-06-03 17:50:16 -0500 | [diff] [blame] | 127 | #undef QUOTE |
| 128 | #undef STRIFY |
| 129 | #undef MAJOR |
| 130 | #undef MINOR |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 131 | |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 132 | /* See _PyImport_FixupExtensionObject() below */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 133 | static PyObject *extensions = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 134 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 135 | /* Function from Parser/tokenizer.c */ |
| 136 | extern char * PyTokenizer_FindEncodingFilename(int, PyObject *); |
| 137 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 138 | /* This table is defined in config.c: */ |
| 139 | extern struct _inittab _PyImport_Inittab[]; |
| 140 | |
| 141 | struct _inittab *PyImport_Inittab = _PyImport_Inittab; |
Guido van Rossum | 66f1fa8 | 1991-04-03 19:03:52 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 143 | /* these tables define the module suffixes that Python recognizes */ |
| 144 | struct filedescr * _PyImport_Filetab = NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 146 | static const struct filedescr _PyImport_StandardFiletab[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | {".py", "U", PY_SOURCE}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 148 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | {".pyw", "U", PY_SOURCE}, |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 150 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | {".pyc", "rb", PY_COMPILED}, |
| 152 | {0, 0} |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 155 | static PyObject *initstr = NULL; |
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 | { |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 205 | PyObject *v, *path_hooks = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | if (PyType_Ready(&PyNullImporter_Type) < 0) |
| 209 | goto error; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 210 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 211 | /* adding sys.path_hooks and sys.path_importer_cache */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | v = PyList_New(0); |
| 213 | if (v == NULL) |
| 214 | goto error; |
| 215 | err = PySys_SetObject("meta_path", v); |
| 216 | Py_DECREF(v); |
| 217 | if (err) |
| 218 | goto error; |
| 219 | v = PyDict_New(); |
| 220 | if (v == NULL) |
| 221 | goto error; |
| 222 | err = PySys_SetObject("path_importer_cache", v); |
| 223 | Py_DECREF(v); |
| 224 | if (err) |
| 225 | goto error; |
| 226 | path_hooks = PyList_New(0); |
| 227 | if (path_hooks == NULL) |
| 228 | goto error; |
| 229 | err = PySys_SetObject("path_hooks", path_hooks); |
| 230 | if (err) { |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 231 | error: |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 232 | PyErr_Print(); |
| 233 | Py_FatalError("initializing sys.meta_path, sys.path_hooks, " |
| 234 | "path_importer_cache, or NullImporter failed" |
| 235 | ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 237 | Py_DECREF(path_hooks); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | _PyImportZip_Init(void) |
| 242 | { |
| 243 | PyObject *path_hooks, *zimpimport; |
| 244 | int err = 0; |
| 245 | |
| 246 | path_hooks = PySys_GetObject("path_hooks"); |
| 247 | if (path_hooks == NULL) |
| 248 | goto error; |
| 249 | |
| 250 | if (Py_VerboseFlag) |
| 251 | PySys_WriteStderr("# installing zipimport hook\n"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | zimpimport = PyImport_ImportModule("zipimport"); |
| 254 | if (zimpimport == NULL) { |
| 255 | PyErr_Clear(); /* No zip import module -- okay */ |
| 256 | if (Py_VerboseFlag) |
| 257 | PySys_WriteStderr("# can't import zipimport\n"); |
| 258 | } |
| 259 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 260 | _Py_IDENTIFIER(zipimporter); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 261 | PyObject *zipimporter = _PyObject_GetAttrId(zimpimport, |
| 262 | &PyId_zipimporter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | Py_DECREF(zimpimport); |
| 264 | if (zipimporter == NULL) { |
| 265 | PyErr_Clear(); /* No zipimporter object -- okay */ |
| 266 | if (Py_VerboseFlag) |
| 267 | PySys_WriteStderr( |
| 268 | "# can't import zipimport.zipimporter\n"); |
| 269 | } |
| 270 | else { |
| 271 | /* sys.path_hooks.append(zipimporter) */ |
| 272 | err = PyList_Append(path_hooks, zipimporter); |
| 273 | Py_DECREF(zipimporter); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 274 | if (err < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | goto error; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 276 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | if (Py_VerboseFlag) |
| 278 | PySys_WriteStderr( |
| 279 | "# installed zipimport hook\n"); |
| 280 | } |
| 281 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 282 | |
| 283 | return; |
| 284 | |
| 285 | error: |
| 286 | PyErr_Print(); |
| 287 | Py_FatalError("initializing zipimport or NullImporter failed"); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 290 | /* Locking primitives to prevent parallel imports of the same module |
| 291 | in different threads to return with a partially loaded module. |
| 292 | These calls are serialized by the global interpreter lock. */ |
| 293 | |
| 294 | #ifdef WITH_THREAD |
| 295 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 296 | #include "pythread.h" |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 298 | static PyThread_type_lock import_lock = 0; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 299 | static long import_lock_thread = -1; |
| 300 | static int import_lock_level = 0; |
| 301 | |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 302 | void |
| 303 | _PyImport_AcquireLock(void) |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | long me = PyThread_get_thread_ident(); |
| 306 | if (me == -1) |
| 307 | return; /* Too bad */ |
| 308 | if (import_lock == NULL) { |
| 309 | import_lock = PyThread_allocate_lock(); |
| 310 | if (import_lock == NULL) |
| 311 | return; /* Nothing much we can do. */ |
| 312 | } |
| 313 | if (import_lock_thread == me) { |
| 314 | import_lock_level++; |
| 315 | return; |
| 316 | } |
| 317 | if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) |
| 318 | { |
| 319 | PyThreadState *tstate = PyEval_SaveThread(); |
| 320 | PyThread_acquire_lock(import_lock, 1); |
| 321 | PyEval_RestoreThread(tstate); |
| 322 | } |
| 323 | import_lock_thread = me; |
| 324 | import_lock_level = 1; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 327 | int |
| 328 | _PyImport_ReleaseLock(void) |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | long me = PyThread_get_thread_ident(); |
| 331 | if (me == -1 || import_lock == NULL) |
| 332 | return 0; /* Too bad */ |
| 333 | if (import_lock_thread != me) |
| 334 | return -1; |
| 335 | import_lock_level--; |
| 336 | if (import_lock_level == 0) { |
| 337 | import_lock_thread = -1; |
| 338 | PyThread_release_lock(import_lock); |
| 339 | } |
| 340 | return 1; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 343 | /* This function is called from PyOS_AfterFork to ensure that newly |
| 344 | created child processes do not share locks with the parent. |
| 345 | We now acquire the import lock around fork() calls but on some platforms |
| 346 | (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] | 347 | |
| 348 | void |
| 349 | _PyImport_ReInitLock(void) |
| 350 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | if (import_lock != NULL) |
| 352 | import_lock = PyThread_allocate_lock(); |
Nick Coghlan | b2ddf79 | 2010-12-02 04:11:46 +0000 | [diff] [blame] | 353 | if (import_lock_level > 1) { |
| 354 | /* Forked as a side effect of import */ |
| 355 | long me = PyThread_get_thread_ident(); |
| 356 | PyThread_acquire_lock(import_lock, 0); |
Victor Stinner | 942003c | 2011-03-07 16:57:48 +0100 | [diff] [blame] | 357 | /* XXX: can the previous line fail? */ |
Nick Coghlan | b2ddf79 | 2010-12-02 04:11:46 +0000 | [diff] [blame] | 358 | import_lock_thread = me; |
| 359 | import_lock_level--; |
| 360 | } else { |
| 361 | import_lock_thread = -1; |
| 362 | import_lock_level = 0; |
| 363 | } |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 366 | #endif |
| 367 | |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 368 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 369 | imp_lock_held(PyObject *self, PyObject *noargs) |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 370 | { |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 371 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | return PyBool_FromLong(import_lock_thread != -1); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 373 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | return PyBool_FromLong(0); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 375 | #endif |
| 376 | } |
| 377 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 378 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 379 | imp_acquire_lock(PyObject *self, PyObject *noargs) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 380 | { |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 381 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | _PyImport_AcquireLock(); |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 383 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | Py_INCREF(Py_None); |
| 385 | return Py_None; |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 389 | imp_release_lock(PyObject *self, PyObject *noargs) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 390 | { |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 391 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | if (_PyImport_ReleaseLock() < 0) { |
| 393 | PyErr_SetString(PyExc_RuntimeError, |
| 394 | "not holding the import lock"); |
| 395 | return NULL; |
| 396 | } |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 397 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | Py_INCREF(Py_None); |
| 399 | return Py_None; |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Antoine Pitrou | 8db076c | 2011-10-30 19:13:55 +0100 | [diff] [blame] | 402 | void |
| 403 | _PyImport_Fini(void) |
| 404 | { |
| 405 | Py_XDECREF(extensions); |
| 406 | extensions = NULL; |
| 407 | PyMem_DEL(_PyImport_Filetab); |
| 408 | _PyImport_Filetab = NULL; |
| 409 | #ifdef WITH_THREAD |
| 410 | if (import_lock != NULL) { |
| 411 | PyThread_free_lock(import_lock); |
| 412 | import_lock = NULL; |
| 413 | } |
| 414 | #endif |
| 415 | } |
| 416 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 417 | static void |
| 418 | imp_modules_reloading_clear(void) |
| 419 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 421 | if (interp->modules_reloading != NULL) |
| 422 | PyDict_Clear(interp->modules_reloading); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 425 | /* Helper for sys */ |
| 426 | |
| 427 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 428 | PyImport_GetModuleDict(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 431 | if (interp->modules == NULL) |
| 432 | Py_FatalError("PyImport_GetModuleDict: no module dictionary!"); |
| 433 | return interp->modules; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 436 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 437 | /* List of names to clear in sys */ |
| 438 | static char* sys_deletes[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | "path", "argv", "ps1", "ps2", |
| 440 | "last_type", "last_value", "last_traceback", |
| 441 | "path_hooks", "path_importer_cache", "meta_path", |
| 442 | /* misc stuff */ |
| 443 | "flags", "float_info", |
| 444 | NULL |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 445 | }; |
| 446 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 447 | static char* sys_files[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | "stdin", "__stdin__", |
| 449 | "stdout", "__stdout__", |
| 450 | "stderr", "__stderr__", |
| 451 | NULL |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 454 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 455 | /* Un-initialize things, as good as we can */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 456 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 457 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 458 | PyImport_Cleanup(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | Py_ssize_t pos, ndone; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | PyObject *key, *value, *dict; |
| 462 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 463 | PyObject *modules = interp->modules; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | if (modules == NULL) |
| 466 | return; /* Already done */ |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | /* Delete some special variables first. These are common |
| 469 | places where user values hide and people complain when their |
| 470 | destructors fail. Since the modules containing them are |
| 471 | deleted *last* of all, they would come too late in the normal |
| 472 | destruction order. Sigh. */ |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | value = PyDict_GetItemString(modules, "builtins"); |
| 475 | if (value != NULL && PyModule_Check(value)) { |
| 476 | dict = PyModule_GetDict(value); |
| 477 | if (Py_VerboseFlag) |
| 478 | PySys_WriteStderr("# clear builtins._\n"); |
| 479 | PyDict_SetItemString(dict, "_", Py_None); |
| 480 | } |
| 481 | value = PyDict_GetItemString(modules, "sys"); |
| 482 | if (value != NULL && PyModule_Check(value)) { |
| 483 | char **p; |
| 484 | PyObject *v; |
| 485 | dict = PyModule_GetDict(value); |
| 486 | for (p = sys_deletes; *p != NULL; p++) { |
| 487 | if (Py_VerboseFlag) |
| 488 | PySys_WriteStderr("# clear sys.%s\n", *p); |
| 489 | PyDict_SetItemString(dict, *p, Py_None); |
| 490 | } |
| 491 | for (p = sys_files; *p != NULL; p+=2) { |
| 492 | if (Py_VerboseFlag) |
| 493 | PySys_WriteStderr("# restore sys.%s\n", *p); |
| 494 | v = PyDict_GetItemString(dict, *(p+1)); |
| 495 | if (v == NULL) |
| 496 | v = Py_None; |
| 497 | PyDict_SetItemString(dict, *p, v); |
| 498 | } |
| 499 | } |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | /* First, delete __main__ */ |
| 502 | value = PyDict_GetItemString(modules, "__main__"); |
| 503 | if (value != NULL && PyModule_Check(value)) { |
| 504 | if (Py_VerboseFlag) |
| 505 | PySys_WriteStderr("# cleanup __main__\n"); |
| 506 | _PyModule_Clear(value); |
| 507 | PyDict_SetItemString(modules, "__main__", Py_None); |
| 508 | } |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | /* The special treatment of "builtins" here is because even |
| 511 | when it's not referenced as a module, its dictionary is |
| 512 | referenced by almost every module's __builtins__. Since |
| 513 | deleting a module clears its dictionary (even if there are |
| 514 | references left to it), we need to delete the "builtins" |
| 515 | module last. Likewise, we don't delete sys until the very |
| 516 | end because it is implicitly referenced (e.g. by print). |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | Also note that we 'delete' modules by replacing their entry |
| 519 | in the modules dict with None, rather than really deleting |
| 520 | them; this avoids a rehash of the modules dictionary and |
| 521 | also marks them as "non existent" so they won't be |
| 522 | re-imported. */ |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | /* Next, repeatedly delete modules with a reference count of |
| 525 | one (skipping builtins and sys) and delete them */ |
| 526 | do { |
| 527 | ndone = 0; |
| 528 | pos = 0; |
| 529 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 530 | if (value->ob_refcnt != 1) |
| 531 | continue; |
| 532 | if (PyUnicode_Check(key) && PyModule_Check(value)) { |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 533 | if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | continue; |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 535 | if (PyUnicode_CompareWithASCIIString(key, "sys") == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | continue; |
| 537 | if (Py_VerboseFlag) |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 538 | PySys_FormatStderr( |
| 539 | "# cleanup[1] %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 | ndone++; |
| 543 | } |
| 544 | } |
| 545 | } while (ndone > 0); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | /* Next, delete all modules (still skipping builtins and sys) */ |
| 548 | pos = 0; |
| 549 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 550 | if (PyUnicode_Check(key) && PyModule_Check(value)) { |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 551 | if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | continue; |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 553 | if (PyUnicode_CompareWithASCIIString(key, "sys") == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | continue; |
| 555 | if (Py_VerboseFlag) |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 556 | PySys_FormatStderr("# cleanup[2] %U\n", key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | _PyModule_Clear(value); |
| 558 | PyDict_SetItem(modules, key, Py_None); |
| 559 | } |
| 560 | } |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | /* Next, delete sys and builtins (in that order) */ |
| 563 | value = PyDict_GetItemString(modules, "sys"); |
| 564 | if (value != NULL && PyModule_Check(value)) { |
| 565 | if (Py_VerboseFlag) |
| 566 | PySys_WriteStderr("# cleanup sys\n"); |
| 567 | _PyModule_Clear(value); |
| 568 | PyDict_SetItemString(modules, "sys", Py_None); |
| 569 | } |
| 570 | value = PyDict_GetItemString(modules, "builtins"); |
| 571 | if (value != NULL && PyModule_Check(value)) { |
| 572 | if (Py_VerboseFlag) |
| 573 | PySys_WriteStderr("# cleanup builtins\n"); |
| 574 | _PyModule_Clear(value); |
| 575 | PyDict_SetItemString(modules, "builtins", Py_None); |
| 576 | } |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | /* Finally, clear and delete the modules directory */ |
| 579 | PyDict_Clear(modules); |
| 580 | interp->modules = NULL; |
| 581 | Py_DECREF(modules); |
| 582 | Py_CLEAR(interp->modules_reloading); |
Guido van Rossum | 8d15b5d | 1990-10-26 14:58:58 +0000 | [diff] [blame] | 583 | } |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 584 | |
| 585 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 586 | /* Helper for pythonrun.c -- return magic number and tag. */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 587 | |
| 588 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 589 | PyImport_GetMagicNumber(void) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | return pyc_magic; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 595 | const char * |
| 596 | PyImport_GetMagicTag(void) |
| 597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | return pyc_tag; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 601 | /* Magic for extension modules (built-in as well as dynamically |
| 602 | loaded). To prevent initializing an extension module more than |
| 603 | once, we keep a static dictionary 'extensions' keyed by module name |
| 604 | (for built-in modules) or by filename (for dynamically loaded |
Barry Warsaw | 9288338 | 2001-08-13 23:05:44 +0000 | [diff] [blame] | 605 | modules), containing these modules. A copy of the module's |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 606 | dictionary is stored by calling _PyImport_FixupExtensionObject() |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 607 | immediately after the module initialization function succeeds. A |
| 608 | copy can be retrieved from there by calling |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 609 | _PyImport_FindExtensionObject(). |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 610 | |
Barry Warsaw | 7c9627b | 2010-06-17 18:38:20 +0000 | [diff] [blame] | 611 | Modules which do support multiple initialization set their m_size |
| 612 | field to a non-negative number (indicating the size of the |
| 613 | module-specific state). They are still recorded in the extensions |
| 614 | dictionary, to avoid loading shared libraries twice. |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 615 | */ |
| 616 | |
| 617 | int |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 618 | _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, |
| 619 | PyObject *filename) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 620 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | PyObject *modules, *dict; |
| 622 | struct PyModuleDef *def; |
| 623 | if (extensions == NULL) { |
| 624 | extensions = PyDict_New(); |
| 625 | if (extensions == NULL) |
| 626 | return -1; |
| 627 | } |
| 628 | if (mod == NULL || !PyModule_Check(mod)) { |
| 629 | PyErr_BadInternalCall(); |
| 630 | return -1; |
| 631 | } |
| 632 | def = PyModule_GetDef(mod); |
| 633 | if (!def) { |
| 634 | PyErr_BadInternalCall(); |
| 635 | return -1; |
| 636 | } |
| 637 | modules = PyImport_GetModuleDict(); |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 638 | if (PyDict_SetItem(modules, name, mod) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | return -1; |
| 640 | if (_PyState_AddModule(mod, def) < 0) { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 641 | PyDict_DelItem(modules, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | return -1; |
| 643 | } |
| 644 | if (def->m_size == -1) { |
| 645 | if (def->m_base.m_copy) { |
| 646 | /* Somebody already imported the module, |
| 647 | likely under a different name. |
| 648 | XXX this should really not happen. */ |
| 649 | Py_DECREF(def->m_base.m_copy); |
| 650 | def->m_base.m_copy = NULL; |
| 651 | } |
| 652 | dict = PyModule_GetDict(mod); |
| 653 | if (dict == NULL) |
| 654 | return -1; |
| 655 | def->m_base.m_copy = PyDict_Copy(dict); |
| 656 | if (def->m_base.m_copy == NULL) |
| 657 | return -1; |
| 658 | } |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 659 | PyDict_SetItem(extensions, filename, (PyObject*)def); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | return 0; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 663 | int |
| 664 | _PyImport_FixupBuiltin(PyObject *mod, char *name) |
| 665 | { |
| 666 | int res; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 667 | PyObject *nameobj; |
Victor Stinner | 21fcd0c | 2011-03-07 18:28:15 +0100 | [diff] [blame] | 668 | nameobj = PyUnicode_InternFromString(name); |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 669 | if (nameobj == NULL) |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 670 | return -1; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 671 | res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj); |
| 672 | Py_DECREF(nameobj); |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 673 | return res; |
| 674 | } |
| 675 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 676 | PyObject * |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 677 | _PyImport_FindExtensionObject(PyObject *name, PyObject *filename) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 678 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | PyObject *mod, *mdict; |
| 680 | PyModuleDef* def; |
| 681 | if (extensions == NULL) |
| 682 | return NULL; |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 683 | def = (PyModuleDef*)PyDict_GetItem(extensions, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | if (def == NULL) |
| 685 | return NULL; |
| 686 | if (def->m_size == -1) { |
| 687 | /* Module does not support repeated initialization */ |
| 688 | if (def->m_base.m_copy == NULL) |
| 689 | return NULL; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 690 | mod = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | if (mod == NULL) |
| 692 | return NULL; |
| 693 | mdict = PyModule_GetDict(mod); |
| 694 | if (mdict == NULL) |
| 695 | return NULL; |
| 696 | if (PyDict_Update(mdict, def->m_base.m_copy)) |
| 697 | return NULL; |
| 698 | } |
| 699 | else { |
| 700 | if (def->m_base.m_init == NULL) |
| 701 | return NULL; |
| 702 | mod = def->m_base.m_init(); |
| 703 | if (mod == NULL) |
| 704 | return NULL; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 705 | PyDict_SetItem(PyImport_GetModuleDict(), name, mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | Py_DECREF(mod); |
| 707 | } |
| 708 | if (_PyState_AddModule(mod, def) < 0) { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 709 | PyDict_DelItem(PyImport_GetModuleDict(), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | Py_DECREF(mod); |
| 711 | return NULL; |
| 712 | } |
| 713 | if (Py_VerboseFlag) |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 714 | PySys_FormatStderr("import %U # previously loaded (%R)\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | name, filename); |
| 716 | return mod; |
Brett Cannon | 9a5b25a | 2010-03-01 02:09:17 +0000 | [diff] [blame] | 717 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 720 | PyObject * |
Victor Stinner | 501c09a | 2011-02-23 00:02:00 +0000 | [diff] [blame] | 721 | _PyImport_FindBuiltin(const char *name) |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 722 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 723 | PyObject *res, *nameobj; |
Victor Stinner | 21fcd0c | 2011-03-07 18:28:15 +0100 | [diff] [blame] | 724 | nameobj = PyUnicode_InternFromString(name); |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 725 | if (nameobj == NULL) |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 726 | return NULL; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 727 | res = _PyImport_FindExtensionObject(nameobj, nameobj); |
| 728 | Py_DECREF(nameobj); |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 729 | return res; |
| 730 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 731 | |
| 732 | /* Get the module object corresponding to a module name. |
| 733 | First check the modules dictionary if there's one there, |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 734 | 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] | 735 | Because the former action is most common, THIS DOES NOT RETURN A |
| 736 | 'NEW' REFERENCE! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 737 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 738 | PyObject * |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 739 | PyImport_AddModuleObject(PyObject *name) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | PyObject *modules = PyImport_GetModuleDict(); |
| 742 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 743 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 744 | if ((m = PyDict_GetItem(modules, name)) != NULL && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | PyModule_Check(m)) |
| 746 | return m; |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 747 | m = PyModule_NewObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | if (m == NULL) |
| 749 | return NULL; |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 750 | if (PyDict_SetItem(modules, name, m) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | Py_DECREF(m); |
| 752 | return NULL; |
| 753 | } |
| 754 | Py_DECREF(m); /* Yes, it still exists, in modules! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 759 | PyObject * |
| 760 | PyImport_AddModule(const char *name) |
| 761 | { |
| 762 | PyObject *nameobj, *module; |
| 763 | nameobj = PyUnicode_FromString(name); |
| 764 | if (nameobj == NULL) |
| 765 | return NULL; |
| 766 | module = PyImport_AddModuleObject(nameobj); |
| 767 | Py_DECREF(nameobj); |
| 768 | return module; |
| 769 | } |
| 770 | |
| 771 | |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 772 | /* Remove name from sys.modules, if it's there. */ |
| 773 | static void |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 774 | remove_module(PyObject *name) |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 775 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 | PyObject *modules = PyImport_GetModuleDict(); |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 777 | if (PyDict_GetItem(modules, name) == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | return; |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 779 | if (PyDict_DelItem(modules, name) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | Py_FatalError("import: deleting existing key in" |
| 781 | "sys.modules failed"); |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 782 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 783 | |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 784 | static PyObject * get_sourcefile(PyObject *filename); |
| 785 | static PyObject *make_source_pathname(PyObject *pathname); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 786 | static PyObject* make_compiled_pathname(PyObject *pathname, int debug); |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 787 | |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 788 | /* Execute a code object in a module and return the module object |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 789 | * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is |
| 790 | * removed from sys.modules, to avoid leaving damaged module objects |
| 791 | * in sys.modules. The caller may wish to restore the original |
| 792 | * module object (if any) in this case; PyImport_ReloadModule is an |
| 793 | * example. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 794 | * |
| 795 | * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer |
| 796 | * interface. The other two exist primarily for backward compatibility. |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 797 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 798 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 799 | PyImport_ExecCodeModule(char *name, PyObject *co) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 800 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | return PyImport_ExecCodeModuleWithPathnames( |
| 802 | name, co, (char *)NULL, (char *)NULL); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 806 | PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 807 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | return PyImport_ExecCodeModuleWithPathnames( |
| 809 | name, co, pathname, (char *)NULL); |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | PyObject * |
| 813 | PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | char *cpathname) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 815 | { |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 816 | PyObject *m = NULL; |
| 817 | PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL; |
| 818 | |
| 819 | nameobj = PyUnicode_FromString(name); |
| 820 | if (nameobj == NULL) |
| 821 | return NULL; |
| 822 | |
| 823 | if (pathname != NULL) { |
| 824 | pathobj = PyUnicode_DecodeFSDefault(pathname); |
| 825 | if (pathobj == NULL) |
| 826 | goto error; |
| 827 | } else |
| 828 | pathobj = NULL; |
| 829 | if (cpathname != NULL) { |
| 830 | cpathobj = PyUnicode_DecodeFSDefault(cpathname); |
| 831 | if (cpathobj == NULL) |
| 832 | goto error; |
| 833 | } else |
| 834 | cpathobj = NULL; |
| 835 | m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); |
| 836 | error: |
| 837 | Py_DECREF(nameobj); |
| 838 | Py_XDECREF(pathobj); |
| 839 | Py_XDECREF(cpathobj); |
| 840 | return m; |
| 841 | } |
| 842 | |
| 843 | PyObject* |
| 844 | PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, |
| 845 | PyObject *cpathname) |
| 846 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | PyObject *modules = PyImport_GetModuleDict(); |
| 848 | PyObject *m, *d, *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 849 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 850 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | if (m == NULL) |
| 852 | return NULL; |
| 853 | /* If the module is being reloaded, we get the old module back |
| 854 | and re-use its dict to exec the new code. */ |
| 855 | d = PyModule_GetDict(m); |
| 856 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 857 | if (PyDict_SetItemString(d, "__builtins__", |
| 858 | PyEval_GetBuiltins()) != 0) |
| 859 | goto error; |
| 860 | } |
| 861 | /* Remember the filename as the __file__ attribute */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | if (pathname != NULL) { |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 863 | v = get_sourcefile(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | if (v == NULL) |
| 865 | PyErr_Clear(); |
| 866 | } |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 867 | else |
| 868 | v = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | if (v == NULL) { |
| 870 | v = ((PyCodeObject *)co)->co_filename; |
| 871 | Py_INCREF(v); |
| 872 | } |
| 873 | if (PyDict_SetItemString(d, "__file__", v) != 0) |
| 874 | PyErr_Clear(); /* Not important enough to report */ |
| 875 | Py_DECREF(v); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | /* Remember the pyc path name as the __cached__ attribute. */ |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 878 | if (cpathname != NULL) |
| 879 | v = cpathname; |
| 880 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 881 | v = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | if (PyDict_SetItemString(d, "__cached__", v) != 0) |
| 883 | PyErr_Clear(); /* Not important enough to report */ |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 884 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 885 | v = PyEval_EvalCode(co, d, d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | if (v == NULL) |
| 887 | goto error; |
| 888 | Py_DECREF(v); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 889 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 890 | if ((m = PyDict_GetItem(modules, name)) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 892 | "Loaded module %R not found in sys.modules", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | name); |
| 894 | return NULL; |
| 895 | } |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | return m; |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 900 | |
| 901 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | remove_module(name); |
| 903 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 907 | /* Like rightmost_sep, but operate on unicode objects. */ |
| 908 | static Py_ssize_t |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 909 | 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] | 910 | { |
| 911 | Py_ssize_t found, i; |
| 912 | Py_UCS4 c; |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 913 | for (found = -1, i = start; i < end; i++) { |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 914 | c = PyUnicode_READ_CHAR(o, i); |
| 915 | if (c == SEP |
| 916 | #ifdef ALTSEP |
| 917 | || c == ALTSEP |
| 918 | #endif |
| 919 | ) |
| 920 | { |
| 921 | found = i; |
| 922 | } |
| 923 | } |
| 924 | return found; |
| 925 | } |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 926 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 927 | /* Given a pathname for a Python source file, fill a buffer with the |
| 928 | pathname for the corresponding compiled file. Return the pathname |
| 929 | 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] | 930 | Doesn't set an exception. |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 931 | |
Martin v. Löwis | 30260a7 | 2011-10-23 17:35:46 +0200 | [diff] [blame] | 932 | foo.py -> __pycache__/foo.<tag>.pyc |
| 933 | |
| 934 | pathstr is assumed to be "ready". |
| 935 | */ |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 936 | |
| 937 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 938 | make_compiled_pathname(PyObject *pathstr, int debug) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 939 | { |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 940 | PyObject *result; |
| 941 | Py_ssize_t fname, ext, len, i, pos, taglen; |
Martin v. Löwis | fadcd31 | 2011-10-23 18:08:20 +0200 | [diff] [blame] | 942 | Py_ssize_t pycache_len = sizeof(CACHEDIR) - 1; |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 943 | int kind; |
| 944 | void *data; |
Antoine Pitrou | b528fcf | 2011-10-25 00:21:02 +0200 | [diff] [blame] | 945 | Py_UCS4 lastsep; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 946 | |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 947 | /* Compute the output string size. */ |
| 948 | len = PyUnicode_GET_LENGTH(pathstr); |
| 949 | /* If there is no separator, this returns -1, so |
Antoine Pitrou | b528fcf | 2011-10-25 00:21:02 +0200 | [diff] [blame] | 950 | fname will be 0. */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 951 | fname = rightmost_sep_obj(pathstr, 0, len) + 1; |
Antoine Pitrou | b528fcf | 2011-10-25 00:21:02 +0200 | [diff] [blame] | 952 | /* Windows: re-use the last separator character (/ or \\) when |
| 953 | appending the __pycache__ path. */ |
| 954 | if (fname > 0) |
| 955 | lastsep = PyUnicode_READ_CHAR(pathstr, fname -1); |
| 956 | else |
| 957 | lastsep = SEP; |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 958 | ext = fname - 1; |
| 959 | for(i = fname; i < len; i++) |
| 960 | if (PyUnicode_READ_CHAR(pathstr, i) == '.') |
| 961 | ext = i + 1; |
| 962 | if (ext < fname) |
| 963 | /* No dot in filename; use entire filename */ |
| 964 | ext = len; |
| 965 | |
| 966 | /* result = pathstr[:fname] + "__pycache__" + SEP + |
| 967 | pathstr[fname:ext] + tag + ".py[co]" */ |
| 968 | taglen = strlen(pyc_tag); |
Martin v. Löwis | 2cc0cc5 | 2011-10-23 18:41:56 +0200 | [diff] [blame] | 969 | result = PyUnicode_New(ext + pycache_len + 1 + taglen + 4, |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 970 | PyUnicode_MAX_CHAR_VALUE(pathstr)); |
| 971 | if (!result) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 972 | return NULL; |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 973 | kind = PyUnicode_KIND(result); |
| 974 | data = PyUnicode_DATA(result); |
| 975 | PyUnicode_CopyCharacters(result, 0, pathstr, 0, fname); |
| 976 | pos = fname; |
Martin v. Löwis | 2cc0cc5 | 2011-10-23 18:41:56 +0200 | [diff] [blame] | 977 | for (i = 0; i < pycache_len; i++) |
Martin v. Löwis | fadcd31 | 2011-10-23 18:08:20 +0200 | [diff] [blame] | 978 | PyUnicode_WRITE(kind, data, pos++, CACHEDIR[i]); |
Antoine Pitrou | b528fcf | 2011-10-25 00:21:02 +0200 | [diff] [blame] | 979 | PyUnicode_WRITE(kind, data, pos++, lastsep); |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 980 | PyUnicode_CopyCharacters(result, pos, pathstr, |
| 981 | fname, ext - fname); |
| 982 | pos += ext - fname; |
| 983 | for (i = 0; pyc_tag[i]; i++) |
| 984 | PyUnicode_WRITE(kind, data, pos++, pyc_tag[i]); |
| 985 | PyUnicode_WRITE(kind, data, pos++, '.'); |
| 986 | PyUnicode_WRITE(kind, data, pos++, 'p'); |
| 987 | PyUnicode_WRITE(kind, data, pos++, 'y'); |
| 988 | PyUnicode_WRITE(kind, data, pos++, debug ? 'c' : 'o'); |
| 989 | return result; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 993 | /* Given a pathname to a Python byte compiled file, return the path to the |
| 994 | source file, if the path matches the PEP 3147 format. This does not check |
| 995 | for any file existence, however, if the pyc file name does not match PEP |
| 996 | 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] | 997 | the resulting path will always be shorter. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 999 | (...)/__pycache__/foo.<tag>.pyc -> (...)/foo.py */ |
| 1000 | |
| 1001 | static PyObject* |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 1002 | make_source_pathname(PyObject *path) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1003 | { |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 1004 | Py_ssize_t left, right, dot0, dot1, len; |
| 1005 | Py_ssize_t i, j; |
| 1006 | PyObject *result; |
| 1007 | int kind; |
| 1008 | void *data; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1009 | |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 1010 | len = PyUnicode_GET_LENGTH(path); |
| 1011 | if (len > MAXPATHLEN) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1012 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | /* Look back two slashes from the end. In between these two slashes |
| 1015 | must be the string __pycache__ or this is not a PEP 3147 style |
| 1016 | path. It's possible for there to be only one slash. |
| 1017 | */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 1018 | right = rightmost_sep_obj(path, 0, len); |
| 1019 | if (right == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | return NULL; |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 1021 | left = rightmost_sep_obj(path, 0, right); |
| 1022 | if (left == -1) |
| 1023 | left = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | else |
| 1025 | left++; |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 1026 | if (right-left != sizeof(CACHEDIR)-1) |
| 1027 | return NULL; |
| 1028 | for (i = 0; i < sizeof(CACHEDIR)-1; i++) |
| 1029 | if (PyUnicode_READ_CHAR(path, left+i) != CACHEDIR[i]) |
| 1030 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | /* Now verify that the path component to the right of the last slash |
| 1033 | has two dots in it. |
| 1034 | */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 1035 | dot0 = PyUnicode_FindChar(path, '.', right+1, len, 1); |
| 1036 | if (dot0 < 0) |
| 1037 | return NULL; |
| 1038 | dot1 = PyUnicode_FindChar(path, '.', dot0+1, len, 1); |
| 1039 | if (dot1 < 0) |
| 1040 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | /* Too many dots? */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 1042 | if (PyUnicode_FindChar(path, '.', dot1+1, len, 1) != -1) |
| 1043 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | /* This is a PEP 3147 path. Start by copying everything from the |
| 1046 | start of pathname up to and including the leftmost slash. Then |
| 1047 | copy the file's basename, removing the magic tag and adding a .py |
| 1048 | suffix. |
| 1049 | */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 1050 | result = PyUnicode_New(left + (dot0-right) + 2, |
| 1051 | PyUnicode_MAX_CHAR_VALUE(path)); |
| 1052 | if (!result) |
| 1053 | return NULL; |
| 1054 | kind = PyUnicode_KIND(result); |
| 1055 | data = PyUnicode_DATA(result); |
| 1056 | PyUnicode_CopyCharacters(result, 0, path, 0, (i = left)); |
| 1057 | PyUnicode_CopyCharacters(result, left, path, right+1, |
| 1058 | (j = dot0-right)); |
| 1059 | PyUnicode_WRITE(kind, data, i+j, 'p'); |
| 1060 | PyUnicode_WRITE(kind, data, i+j+1, 'y'); |
| 1061 | return result; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1064 | |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1065 | static void |
| 1066 | update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) |
| 1067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | PyObject *constants, *tmp; |
| 1069 | Py_ssize_t i, n; |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | if (PyUnicode_Compare(co->co_filename, oldname)) |
| 1072 | return; |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | tmp = co->co_filename; |
| 1075 | co->co_filename = newname; |
| 1076 | Py_INCREF(co->co_filename); |
| 1077 | Py_DECREF(tmp); |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | constants = co->co_consts; |
| 1080 | n = PyTuple_GET_SIZE(constants); |
| 1081 | for (i = 0; i < n; i++) { |
| 1082 | tmp = PyTuple_GET_ITEM(constants, i); |
| 1083 | if (PyCode_Check(tmp)) |
| 1084 | update_code_filenames((PyCodeObject *)tmp, |
| 1085 | oldname, newname); |
| 1086 | } |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 1089 | static void |
| 1090 | update_compiled_module(PyCodeObject *co, PyObject *newname) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1091 | { |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 1092 | PyObject *oldname; |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1093 | |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 1094 | if (PyUnicode_Compare(co->co_filename, newname) == 0) |
| 1095 | return; |
Hirokazu Yamamoto | 4f447fb | 2009-03-04 01:52:10 +0000 | [diff] [blame] | 1096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | oldname = co->co_filename; |
| 1098 | Py_INCREF(oldname); |
| 1099 | update_code_filenames(co, oldname, newname); |
| 1100 | Py_DECREF(oldname); |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1101 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1102 | |
Brett Cannon | 442c9b9 | 2011-03-23 16:14:42 -0700 | [diff] [blame] | 1103 | static PyObject * |
| 1104 | imp_fix_co_filename(PyObject *self, PyObject *args) |
| 1105 | { |
| 1106 | PyObject *co; |
| 1107 | PyObject *file_path; |
| 1108 | |
| 1109 | if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path)) |
| 1110 | return NULL; |
| 1111 | |
| 1112 | if (!PyCode_Check(co)) { |
| 1113 | PyErr_SetString(PyExc_TypeError, |
| 1114 | "first argument must be a code object"); |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | |
| 1118 | if (!PyUnicode_Check(file_path)) { |
| 1119 | PyErr_SetString(PyExc_TypeError, |
| 1120 | "second argument must be a string"); |
| 1121 | return NULL; |
| 1122 | } |
| 1123 | |
| 1124 | update_compiled_module((PyCodeObject*)co, file_path); |
| 1125 | |
| 1126 | Py_RETURN_NONE; |
| 1127 | } |
| 1128 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1129 | |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1130 | /* Get source file -> unicode or None |
| 1131 | * Returns the path to the py file if available, else the given path |
| 1132 | */ |
| 1133 | static PyObject * |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1134 | get_sourcefile(PyObject *filename) |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1137 | Py_UCS4 *fileuni; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1138 | PyObject *py; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | struct stat statbuf; |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1140 | int err; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1142 | len = PyUnicode_GET_LENGTH(filename); |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1143 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | Py_RETURN_NONE; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1145 | |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1146 | /* don't match *.pyc or *.pyo? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | fileuni = PyUnicode_AsUCS4Copy(filename); |
| 1148 | if (!fileuni) |
| 1149 | return NULL; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1150 | if (len < 5 |
| 1151 | || fileuni[len-4] != '.' |
| 1152 | || (fileuni[len-3] != 'p' && fileuni[len-3] != 'P') |
| 1153 | || (fileuni[len-2] != 'y' && fileuni[len-2] != 'Y')) |
| 1154 | goto unchanged; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | /* Start by trying to turn PEP 3147 path into source path. If that |
| 1157 | * fails, just chop off the trailing character, i.e. legacy pyc path |
| 1158 | * to py. |
| 1159 | */ |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1160 | py = make_source_pathname(filename); |
| 1161 | if (py == NULL) { |
| 1162 | PyErr_Clear(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1163 | py = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, fileuni, len - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | } |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1165 | if (py == NULL) |
| 1166 | goto error; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1167 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1168 | err = _Py_stat(py, &statbuf); |
| 1169 | if (err == -2) |
| 1170 | goto error; |
| 1171 | if (err == 0 && S_ISREG(statbuf.st_mode)) { |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 1172 | PyMem_Free(fileuni); |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1173 | return py; |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 1174 | } |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1175 | Py_DECREF(py); |
| 1176 | goto unchanged; |
| 1177 | |
| 1178 | error: |
| 1179 | PyErr_Clear(); |
| 1180 | unchanged: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1181 | PyMem_Free(fileuni); |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1182 | Py_INCREF(filename); |
| 1183 | return filename; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1184 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1185 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1186 | /* Forward */ |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1187 | static struct filedescr *find_module(PyObject *, PyObject *, PyObject *, |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1188 | PyObject **, FILE **, PyObject **); |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1189 | static struct _frozen * find_frozen(PyObject *); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1190 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1191 | |
| 1192 | /* Helper to test for built-in module */ |
| 1193 | |
| 1194 | static int |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1195 | is_builtin(PyObject *name) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1196 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1197 | int i, cmp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1199 | cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name); |
| 1200 | if (cmp == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | if (PyImport_Inittab[i].initfunc == NULL) |
| 1202 | return -1; |
| 1203 | else |
| 1204 | return 1; |
| 1205 | } |
| 1206 | } |
| 1207 | return 0; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1210 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1211 | /* Return an importer object for a sys.path/pkg.__path__ item 'p', |
| 1212 | possibly by fetching it from the path_importer_cache dict. If it |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1213 | 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] | 1214 | that can handle the path item. Return None if no hook could; |
| 1215 | this tells our caller it should fall back to the builtin |
| 1216 | import mechanism. Cache the result in path_importer_cache. |
| 1217 | Returns a borrowed reference. */ |
| 1218 | |
| 1219 | static PyObject * |
| 1220 | get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | PyObject *p) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | PyObject *importer; |
| 1224 | Py_ssize_t j, nhooks; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | /* These conditions are the caller's responsibility: */ |
| 1227 | assert(PyList_Check(path_hooks)); |
| 1228 | assert(PyDict_Check(path_importer_cache)); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | nhooks = PyList_Size(path_hooks); |
| 1231 | if (nhooks < 0) |
| 1232 | return NULL; /* Shouldn't happen */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | importer = PyDict_GetItem(path_importer_cache, p); |
| 1235 | if (importer != NULL) |
| 1236 | return importer; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | /* set path_importer_cache[p] to None to avoid recursion */ |
| 1239 | if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) |
| 1240 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | for (j = 0; j < nhooks; j++) { |
| 1243 | PyObject *hook = PyList_GetItem(path_hooks, j); |
| 1244 | if (hook == NULL) |
| 1245 | return NULL; |
| 1246 | importer = PyObject_CallFunctionObjArgs(hook, p, NULL); |
| 1247 | if (importer != NULL) |
| 1248 | break; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 1251 | return NULL; |
| 1252 | } |
| 1253 | PyErr_Clear(); |
| 1254 | } |
| 1255 | if (importer == NULL) { |
| 1256 | importer = PyObject_CallFunctionObjArgs( |
| 1257 | (PyObject *)&PyNullImporter_Type, p, NULL |
| 1258 | ); |
| 1259 | if (importer == NULL) { |
| 1260 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 1261 | PyErr_Clear(); |
| 1262 | return Py_None; |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | if (importer != NULL) { |
| 1267 | int err = PyDict_SetItem(path_importer_cache, p, importer); |
| 1268 | Py_DECREF(importer); |
| 1269 | if (err != 0) |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | return importer; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1275 | PyAPI_FUNC(PyObject *) |
| 1276 | PyImport_GetImporter(PyObject *path) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) { |
| 1280 | if ((path_hooks = PySys_GetObject("path_hooks"))) { |
| 1281 | importer = get_path_importer(path_importer_cache, |
| 1282 | path_hooks, path); |
| 1283 | } |
| 1284 | } |
| 1285 | Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */ |
| 1286 | return importer; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1289 | /* Search the path (default sys.path) for a module. Return the |
| 1290 | corresponding filedescr struct, and (via return arguments) the |
| 1291 | pathname and an open file. Return NULL if the module is not found. */ |
| 1292 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1293 | #ifdef MS_COREDLL |
Victor Stinner | 4d6c1c4 | 2011-03-08 23:49:04 +0100 | [diff] [blame] | 1294 | extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **, |
| 1295 | PyObject **p_path); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1296 | #endif |
| 1297 | |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1298 | /* Forward */ |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1299 | static int case_ok(PyObject *, Py_ssize_t, PyObject *); |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1300 | static int find_init_module(PyObject *); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1301 | static struct filedescr importhookdescr = {"", "", IMP_HOOK}; |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1302 | |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1303 | /* Get the path of a module: get its importer and call importer.find_module() |
| 1304 | hook, or check if the module if a package (if path/__init__.py exists). |
| 1305 | |
| 1306 | -1: error: a Python error occurred |
| 1307 | 0: ignore: an error occurred because of invalid data, but the error is not |
| 1308 | important enough to be reported. |
| 1309 | 1: get path: module not found, but *buf contains its path |
| 1310 | 2: found: *p_fd is the file descriptor (IMP_HOOK or PKG_DIRECTORY) |
| 1311 | and *buf is the path */ |
| 1312 | |
| 1313 | static int |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1314 | find_module_path(PyObject *fullname, PyObject *name, PyObject *path, |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1315 | PyObject *path_hooks, PyObject *path_importer_cache, |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1316 | PyObject **p_path, PyObject **p_loader, struct filedescr **p_fd) |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1317 | { |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1318 | PyObject *path_unicode, *filename = NULL; |
| 1319 | Py_ssize_t len, pos; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1320 | struct stat statbuf; |
| 1321 | static struct filedescr fd_package = {"", "", PKG_DIRECTORY}; |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1322 | int err, result, addsep; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1323 | |
| 1324 | if (PyUnicode_Check(path)) { |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1325 | Py_INCREF(path); |
| 1326 | path_unicode = path; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1327 | } |
| 1328 | else if (PyBytes_Check(path)) { |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1329 | path_unicode = PyUnicode_DecodeFSDefaultAndSize( |
| 1330 | PyBytes_AS_STRING(path), PyBytes_GET_SIZE(path)); |
| 1331 | if (path_unicode == NULL) |
| 1332 | return -1; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1333 | } |
| 1334 | else |
| 1335 | return 0; |
| 1336 | |
Victor Stinner | 46084ba | 2011-10-06 02:39:42 +0200 | [diff] [blame] | 1337 | if (PyUnicode_READY(path_unicode)) |
| 1338 | return -1; |
| 1339 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1340 | len = PyUnicode_GET_LENGTH(path_unicode); |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1341 | if (PyUnicode_FindChar(path_unicode, 0, 0, len, 1) != -1) { |
| 1342 | result = 0; |
| 1343 | goto out; /* path contains '\0' */ |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1344 | } |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1345 | |
| 1346 | /* sys.path_hooks import hook */ |
| 1347 | if (p_loader != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1348 | _Py_IDENTIFIER(find_module); |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1349 | PyObject *importer; |
| 1350 | |
| 1351 | importer = get_path_importer(path_importer_cache, |
| 1352 | path_hooks, path); |
| 1353 | if (importer == NULL) { |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1354 | result = -1; |
| 1355 | goto out; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1356 | } |
| 1357 | /* Note: importer is a borrowed reference */ |
| 1358 | if (importer != Py_None) { |
| 1359 | PyObject *loader; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1360 | loader = _PyObject_CallMethodId(importer, |
| 1361 | &PyId_find_module, "O", fullname); |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1362 | if (loader == NULL) { |
| 1363 | result = -1; /* error */ |
| 1364 | goto out; |
| 1365 | } |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1366 | if (loader != Py_None) { |
| 1367 | /* a loader was found */ |
| 1368 | *p_loader = loader; |
| 1369 | *p_fd = &importhookdescr; |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1370 | result = 2; |
| 1371 | goto out; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1372 | } |
| 1373 | Py_DECREF(loader); |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1374 | result = 0; |
| 1375 | goto out; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | /* no hook was found, use builtin import */ |
| 1379 | |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1380 | addsep = 0; |
| 1381 | if (len > 0 && PyUnicode_READ_CHAR(path_unicode, len-1) != SEP |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1382 | #ifdef ALTSEP |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1383 | && PyUnicode_READ_CHAR(path_unicode, len-1) != ALTSEP |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1384 | #endif |
| 1385 | ) |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1386 | addsep = 1; |
| 1387 | filename = PyUnicode_New(len + PyUnicode_GET_LENGTH(name) + addsep, |
| 1388 | Py_MAX(PyUnicode_MAX_CHAR_VALUE(path_unicode), |
| 1389 | PyUnicode_MAX_CHAR_VALUE(name))); |
| 1390 | if (filename == NULL) { |
| 1391 | result = -1; |
| 1392 | goto out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | } |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1394 | PyUnicode_CopyCharacters(filename, 0, path_unicode, 0, len); |
| 1395 | pos = len; |
| 1396 | if (addsep) |
Victor Stinner | 1f79517 | 2011-11-17 00:45:54 +0100 | [diff] [blame] | 1397 | PyUnicode_WRITE(PyUnicode_KIND(filename), |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1398 | PyUnicode_DATA(filename), |
| 1399 | pos++, SEP); |
Victor Stinner | 1f79517 | 2011-11-17 00:45:54 +0100 | [diff] [blame] | 1400 | PyUnicode_CopyCharacters(filename, pos, name, 0, |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1401 | PyUnicode_GET_LENGTH(name)); |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1402 | |
| 1403 | /* Check for package import (buf holds a directory name, |
| 1404 | and there's an __init__ module in that directory */ |
| 1405 | #ifdef HAVE_STAT |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1406 | err = _Py_stat(filename, &statbuf); |
| 1407 | if (err == -2) { |
| 1408 | result = -1; |
| 1409 | goto out; |
| 1410 | } |
| 1411 | if (err == 0 && /* it exists */ |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1412 | S_ISDIR(statbuf.st_mode)) /* it's a directory */ |
| 1413 | { |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1414 | int match; |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1415 | |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1416 | match = case_ok(filename, 0, name); |
| 1417 | if (match < 0) { |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1418 | result = -1; |
| 1419 | goto out; |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1420 | } |
| 1421 | if (match) { /* case matches */ |
| 1422 | if (find_init_module(filename)) { /* and has __init__.py */ |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1423 | *p_path = filename; |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1424 | filename = NULL; |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1425 | *p_fd = &fd_package; |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1426 | result = 2; |
| 1427 | goto out; |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1428 | } |
| 1429 | else { |
| 1430 | int err; |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1431 | err = PyErr_WarnFormat(PyExc_ImportWarning, 1, |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1432 | "Not importing directory %R: missing __init__.py", |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1433 | filename); |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1434 | if (err) { |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1435 | result = -1; |
| 1436 | goto out; |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1437 | } |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1438 | } |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | #endif |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1442 | *p_path = filename; |
Martin v. Löwis | 9af29d3 | 2011-10-23 18:45:16 +0200 | [diff] [blame] | 1443 | filename = NULL; |
| 1444 | result = 1; |
| 1445 | out: |
| 1446 | Py_DECREF(path_unicode); |
| 1447 | Py_XDECREF(filename); |
| 1448 | return result; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /* Find a module in search_path_list. For each path, try |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1452 | find_module_path() or try each _PyImport_Filetab suffix. |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1453 | |
| 1454 | If the module is found, return a file descriptor, write the path in |
| 1455 | *p_filename, write the pointer to the file object into *p_fp, and (if |
| 1456 | p_loader is not NULL) the loader into *p_loader. |
| 1457 | |
| 1458 | Otherwise, raise an exception and return NULL. */ |
| 1459 | |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1460 | static struct filedescr* |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1461 | find_module_path_list(PyObject *fullname, PyObject *name, |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1462 | PyObject *search_path_list, PyObject *path_hooks, |
| 1463 | PyObject *path_importer_cache, |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1464 | PyObject **p_path, FILE **p_fp, PyObject **p_loader) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | Py_ssize_t i, npath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | struct filedescr *fdp = NULL; |
| 1468 | char *filemode; |
| 1469 | FILE *fp = NULL; |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1470 | PyObject *prefix, *filename; |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1471 | int match; |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1472 | int err; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1473 | |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1474 | npath = PyList_Size(search_path_list); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | for (i = 0; i < npath; i++) { |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1476 | PyObject *path; |
| 1477 | int ok; |
| 1478 | |
| 1479 | path = PyList_GetItem(search_path_list, i); |
| 1480 | if (path == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | return NULL; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1482 | |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1483 | prefix = NULL; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1484 | ok = find_module_path(fullname, name, path, |
| 1485 | path_hooks, path_importer_cache, |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1486 | &prefix, p_loader, &fdp); |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1487 | if (ok < 0) |
| 1488 | return NULL; |
| 1489 | if (ok == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | continue; |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1491 | if (ok == 2) { |
| 1492 | *p_path = prefix; |
Victor Stinner | d68c2cf | 2011-03-12 16:02:28 -0500 | [diff] [blame] | 1493 | return fdp; |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1494 | } |
Amaury Forgeot d'Arc | f1ca0b1 | 2008-06-11 17:40:47 +0000 | [diff] [blame] | 1495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
Victor Stinner | a1fe1f8 | 2011-09-23 18:59:08 +0200 | [diff] [blame] | 1497 | struct stat statbuf; |
| 1498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | filemode = fdp->mode; |
| 1500 | if (filemode[0] == 'U') |
| 1501 | filemode = "r" PY_STDIOTEXTMODE; |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1502 | |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1503 | filename = PyUnicode_FromFormat("%U%s", prefix, fdp->suffix); |
| 1504 | if (filename == NULL) { |
| 1505 | Py_DECREF(prefix); |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1506 | return NULL; |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1507 | } |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1508 | |
| 1509 | if (Py_VerboseFlag > 1) |
| 1510 | PySys_FormatStderr("# trying %R\n", filename); |
| 1511 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1512 | err = _Py_stat(filename, &statbuf); |
| 1513 | if (err == -2) { |
| 1514 | Py_DECREF(prefix); |
| 1515 | Py_DECREF(filename); |
| 1516 | return NULL; |
| 1517 | } |
| 1518 | if (err != 0 || S_ISDIR(statbuf.st_mode)) { |
Charles-François Natali | 1659b83 | 2011-12-07 23:17:58 +0100 | [diff] [blame] | 1519 | /* it doesn't exist, or it's a directory */ |
Victor Stinner | a1fe1f8 | 2011-09-23 18:59:08 +0200 | [diff] [blame] | 1520 | Py_DECREF(filename); |
| 1521 | continue; |
| 1522 | } |
| 1523 | |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1524 | fp = _Py_fopen(filename, filemode); |
| 1525 | if (fp == NULL) { |
| 1526 | Py_DECREF(filename); |
Victor Stinner | 925ef39 | 2011-06-20 15:01:10 +0200 | [diff] [blame] | 1527 | if (PyErr_Occurred()) { |
| 1528 | Py_DECREF(prefix); |
| 1529 | return NULL; |
| 1530 | } |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1531 | continue; |
| 1532 | } |
| 1533 | match = case_ok(filename, -(Py_ssize_t)strlen(fdp->suffix), name); |
| 1534 | if (match < 0) { |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1535 | Py_DECREF(prefix); |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1536 | Py_DECREF(filename); |
| 1537 | return NULL; |
| 1538 | } |
| 1539 | if (match) { |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1540 | Py_DECREF(prefix); |
| 1541 | *p_path = filename; |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1542 | *p_fp = fp; |
| 1543 | return fdp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | } |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1545 | Py_DECREF(filename); |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1546 | |
| 1547 | fclose(fp); |
| 1548 | fp = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | } |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1550 | Py_DECREF(prefix); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | } |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1552 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 98dbba5 | 2011-03-14 15:15:47 -0400 | [diff] [blame] | 1553 | "No module named %R", name); |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 1554 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1557 | /* Find a module: |
| 1558 | |
| 1559 | - try find_module() of each sys.meta_path hook |
| 1560 | - try find_frozen() |
| 1561 | - try is_builtin() |
| 1562 | - try _PyWin_FindRegisteredModule() (Windows only) |
| 1563 | - otherwise, call find_module_path_list() with search_path_list (if not |
| 1564 | NULL) or sys.path |
| 1565 | |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1566 | fullname can be NULL, but only if p_loader is NULL. |
| 1567 | |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1568 | Return: |
| 1569 | |
| 1570 | - &fd_builtin (C_BUILTIN) if it is a builtin |
| 1571 | - &fd_frozen (PY_FROZEN) if it is frozen |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1572 | - &fd_package (PKG_DIRECTORY) and write the filename into *p_path |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1573 | if it is a package |
| 1574 | - &importhookdescr (IMP_HOOK) and write the loader into *p_loader if a |
| 1575 | importer loader was found |
| 1576 | - a file descriptor (PY_SOURCE, PY_COMPILED, C_EXTENSION, PY_RESOURCE or |
| 1577 | PY_CODERESOURCE: see _PyImport_Filetab), write the filename into |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1578 | *p_path and the pointer to the open file into *p_fp |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1579 | - NULL on error |
| 1580 | |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1581 | By default, *p_path, *p_fp and *p_loader (if set) are set to NULL. |
| 1582 | Eg. *p_path is set to NULL for a builtin package. |
| 1583 | */ |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1584 | |
| 1585 | static struct filedescr * |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1586 | find_module(PyObject *fullname, PyObject *name, PyObject *search_path_list, |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1587 | PyObject **p_path, FILE **p_fp, PyObject **p_loader) |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1588 | { |
| 1589 | Py_ssize_t i, npath; |
| 1590 | static struct filedescr fd_frozen = {"", "", PY_FROZEN}; |
| 1591 | static struct filedescr fd_builtin = {"", "", C_BUILTIN}; |
| 1592 | PyObject *path_hooks, *path_importer_cache; |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1593 | |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1594 | *p_path = NULL; |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1595 | *p_fp = NULL; |
| 1596 | if (p_loader != NULL) |
| 1597 | *p_loader = NULL; |
| 1598 | |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1599 | /* sys.meta_path import hook */ |
| 1600 | if (p_loader != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1601 | _Py_IDENTIFIER(find_module); |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1602 | PyObject *meta_path; |
| 1603 | |
| 1604 | meta_path = PySys_GetObject("meta_path"); |
| 1605 | if (meta_path == NULL || !PyList_Check(meta_path)) { |
Victor Stinner | 1619132 | 2011-09-15 19:28:05 +0200 | [diff] [blame] | 1606 | PyErr_SetString(PyExc_RuntimeError, |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1607 | "sys.meta_path must be a list of " |
| 1608 | "import hooks"); |
| 1609 | return NULL; |
| 1610 | } |
| 1611 | Py_INCREF(meta_path); /* zap guard */ |
| 1612 | npath = PyList_Size(meta_path); |
| 1613 | for (i = 0; i < npath; i++) { |
| 1614 | PyObject *loader; |
| 1615 | PyObject *hook = PyList_GetItem(meta_path, i); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1616 | loader = _PyObject_CallMethodId(hook, &PyId_find_module, |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1617 | "OO", fullname, |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1618 | search_path_list != NULL ? |
| 1619 | search_path_list : Py_None); |
| 1620 | if (loader == NULL) { |
| 1621 | Py_DECREF(meta_path); |
| 1622 | return NULL; /* true error */ |
| 1623 | } |
| 1624 | if (loader != Py_None) { |
| 1625 | /* a loader was found */ |
| 1626 | *p_loader = loader; |
| 1627 | Py_DECREF(meta_path); |
| 1628 | return &importhookdescr; |
| 1629 | } |
| 1630 | Py_DECREF(loader); |
| 1631 | } |
| 1632 | Py_DECREF(meta_path); |
| 1633 | } |
| 1634 | |
Victor Stinner | df75a02 | 2011-03-14 13:40:04 -0400 | [diff] [blame] | 1635 | if (find_frozen(fullname) != NULL) |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1636 | return &fd_frozen; |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1637 | |
| 1638 | if (search_path_list == NULL) { |
| 1639 | #ifdef MS_COREDLL |
| 1640 | FILE *fp; |
| 1641 | struct filedescr *fdp; |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1642 | #endif |
Victor Stinner | df75a02 | 2011-03-14 13:40:04 -0400 | [diff] [blame] | 1643 | if (is_builtin(name)) |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1644 | return &fd_builtin; |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1645 | #ifdef MS_COREDLL |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1646 | fp = _PyWin_FindRegisteredModule(name, &fdp, p_path); |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1647 | if (fp != NULL) { |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1648 | *p_fp = fp; |
| 1649 | return fdp; |
| 1650 | } |
| 1651 | else if (PyErr_Occurred()) |
| 1652 | return NULL; |
| 1653 | #endif |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1654 | search_path_list = PySys_GetObject("path"); |
| 1655 | } |
| 1656 | |
| 1657 | if (search_path_list == NULL || !PyList_Check(search_path_list)) { |
Victor Stinner | 1619132 | 2011-09-15 19:28:05 +0200 | [diff] [blame] | 1658 | PyErr_SetString(PyExc_RuntimeError, |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1659 | "sys.path must be a list of directory names"); |
| 1660 | return NULL; |
| 1661 | } |
| 1662 | |
| 1663 | path_hooks = PySys_GetObject("path_hooks"); |
| 1664 | if (path_hooks == NULL || !PyList_Check(path_hooks)) { |
Victor Stinner | 1619132 | 2011-09-15 19:28:05 +0200 | [diff] [blame] | 1665 | PyErr_SetString(PyExc_RuntimeError, |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1666 | "sys.path_hooks must be a list of " |
| 1667 | "import hooks"); |
| 1668 | return NULL; |
| 1669 | } |
| 1670 | path_importer_cache = PySys_GetObject("path_importer_cache"); |
| 1671 | if (path_importer_cache == NULL || |
| 1672 | !PyDict_Check(path_importer_cache)) { |
Victor Stinner | 1619132 | 2011-09-15 19:28:05 +0200 | [diff] [blame] | 1673 | PyErr_SetString(PyExc_RuntimeError, |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1674 | "sys.path_importer_cache must be a dict"); |
| 1675 | return NULL; |
| 1676 | } |
| 1677 | |
| 1678 | return find_module_path_list(fullname, name, |
| 1679 | search_path_list, path_hooks, |
| 1680 | path_importer_cache, |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 1681 | p_path, p_fp, p_loader); |
Victor Stinner | 3758028 | 2011-03-20 01:34:43 +0100 | [diff] [blame] | 1682 | } |
| 1683 | |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1684 | /* 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] | 1685 | * The arguments here are tricky, best shown by example: |
| 1686 | * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0 |
| 1687 | * ^ ^ ^ ^ |
| 1688 | * |--------------------- buf ---------------------| |
| 1689 | * |------------------- len ------------------| |
| 1690 | * |------ name -------| |
| 1691 | * |----- namelen -----| |
| 1692 | * buf is the full path, but len only counts up to (& exclusive of) the |
| 1693 | * extension. name is the module name, also exclusive of extension. |
| 1694 | * |
| 1695 | * We've already done a successful stat() or fopen() on buf, so know that |
| 1696 | * there's some match, possibly case-insensitive. |
| 1697 | * |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1698 | * case_bytes() is to return 1 if there's a case-sensitive match for |
| 1699 | * 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] | 1700 | * exists. |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1701 | * |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1702 | * case_bytes() is used to implement case-sensitive import semantics even |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1703 | * on platforms with case-insensitive filesystems. It's trivial to implement |
| 1704 | * for case-sensitive filesystems. It's pretty much a cross-platform |
| 1705 | * nightmare for systems with case-insensitive filesystems. |
| 1706 | */ |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1707 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1708 | /* 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] | 1709 | * 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] | 1710 | */ |
Jason Tishler | 7961aa6 | 2005-05-20 00:56:54 +0000 | [diff] [blame] | 1711 | #if defined(MS_WINDOWS) |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1712 | #include <windows.h> |
Guido van Rossum | 4c3f57c | 2001-01-10 20:40:46 +0000 | [diff] [blame] | 1713 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1714 | #elif defined(DJGPP) |
| 1715 | #include <dir.h> |
| 1716 | |
Jason Tishler | 7961aa6 | 2005-05-20 00:56:54 +0000 | [diff] [blame] | 1717 | #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H) |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1718 | #include <sys/types.h> |
| 1719 | #include <dirent.h> |
| 1720 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1721 | #elif defined(PYOS_OS2) |
| 1722 | #define INCL_DOS |
| 1723 | #define INCL_DOSERRORS |
| 1724 | #define INCL_NOPMAPI |
| 1725 | #include <os2.h> |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1726 | #endif |
| 1727 | |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1728 | #if defined(DJGPP) \ |
| 1729 | || ((defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) \ |
| 1730 | && defined(HAVE_DIRENT_H)) \ |
| 1731 | || defined(PYOS_OS2) |
| 1732 | # define USE_CASE_OK_BYTES |
| 1733 | #endif |
| 1734 | |
| 1735 | |
| 1736 | #ifdef USE_CASE_OK_BYTES |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1737 | static int |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1738 | 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] | 1739 | { |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1740 | /* Pick a platform-specific implementation; the sequence of #if's here should |
| 1741 | * match the sequence just above. |
| 1742 | */ |
| 1743 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1744 | /* DJGPP */ |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1745 | #if defined(DJGPP) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1746 | struct ffblk ffblk; |
| 1747 | int done; |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1749 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
| 1750 | return 1; |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC); |
| 1753 | if (done) { |
| 1754 | PyErr_Format(PyExc_NameError, |
| 1755 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1756 | name, buf); |
Victor Stinner | 9c61e24 | 2011-03-22 01:22:27 +0100 | [diff] [blame] | 1757 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1758 | } |
| 1759 | return strncmp(ffblk.ff_name, name, namelen) == 0; |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1760 | |
Jason Tishler | 7961aa6 | 2005-05-20 00:56:54 +0000 | [diff] [blame] | 1761 | /* new-fangled macintosh (macosx) or Cygwin */ |
| 1762 | #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | DIR *dirp; |
| 1764 | struct dirent *dp; |
| 1765 | char dirname[MAXPATHLEN + 1]; |
| 1766 | const int dirlen = len - namelen - 1; /* don't want trailing SEP */ |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
| 1769 | return 1; |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1771 | /* Copy the dir component into dirname; substitute "." if empty */ |
| 1772 | if (dirlen <= 0) { |
| 1773 | dirname[0] = '.'; |
| 1774 | dirname[1] = '\0'; |
| 1775 | } |
| 1776 | else { |
| 1777 | assert(dirlen <= MAXPATHLEN); |
| 1778 | memcpy(dirname, buf, dirlen); |
| 1779 | dirname[dirlen] = '\0'; |
| 1780 | } |
| 1781 | /* Open the directory and search the entries for an exact match. */ |
| 1782 | dirp = opendir(dirname); |
| 1783 | if (dirp) { |
| 1784 | char *nameWithExt = buf + len - namelen; |
| 1785 | while ((dp = readdir(dirp)) != NULL) { |
| 1786 | const int thislen = |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1787 | #ifdef _DIRENT_HAVE_D_NAMELEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | dp->d_namlen; |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1789 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | strlen(dp->d_name); |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1791 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | if (thislen >= namelen && |
| 1793 | strcmp(dp->d_name, nameWithExt) == 0) { |
| 1794 | (void)closedir(dirp); |
| 1795 | return 1; /* Found */ |
| 1796 | } |
| 1797 | } |
| 1798 | (void)closedir(dirp); |
| 1799 | } |
| 1800 | return 0 ; /* Not found */ |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1801 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1802 | /* OS/2 */ |
| 1803 | #elif defined(PYOS_OS2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1804 | HDIR hdir = 1; |
| 1805 | ULONG srchcnt = 1; |
| 1806 | FILEFINDBUF3 ffbuf; |
| 1807 | APIRET rc; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
| 1810 | return 1; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1812 | rc = DosFindFirst(buf, |
| 1813 | &hdir, |
| 1814 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
| 1815 | &ffbuf, sizeof(ffbuf), |
| 1816 | &srchcnt, |
| 1817 | FIL_STANDARD); |
| 1818 | if (rc != NO_ERROR) |
| 1819 | return 0; |
| 1820 | return strncmp(ffbuf.achName, name, namelen) == 0; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1821 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1822 | /* assuming it's a case-sensitive filesystem, so there's nothing to do! */ |
| 1823 | #else |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1824 | # error "USE_CASE_OK_BYTES is not correctly defined" |
| 1825 | #endif |
| 1826 | } |
| 1827 | #endif |
| 1828 | |
| 1829 | /* |
| 1830 | * Check if a filename case matchs the name case. We've already done a |
| 1831 | * successful stat() or fopen() on buf, so know that there's some match, |
| 1832 | * possibly case-insensitive. |
| 1833 | * |
| 1834 | * case_ok() is to return 1 if there's a case-sensitive match for name, 0 if it |
| 1835 | * the filename doesn't match, or -1 on error. case_ok() is also to return 1 |
| 1836 | * if envar PYTHONCASEOK exists. |
| 1837 | * |
| 1838 | * case_ok() is used to implement case-sensitive import semantics even |
| 1839 | * on platforms with case-insensitive filesystems. It's trivial to implement |
| 1840 | * for case-sensitive filesystems. It's pretty much a cross-platform |
| 1841 | * nightmare for systems with case-insensitive filesystems. |
| 1842 | */ |
| 1843 | |
| 1844 | static int |
| 1845 | case_ok(PyObject *filename, Py_ssize_t prefix_delta, PyObject *name) |
| 1846 | { |
| 1847 | #ifdef MS_WINDOWS |
| 1848 | WIN32_FIND_DATAW data; |
| 1849 | HANDLE h; |
| 1850 | int cmp; |
Victor Stinner | 1f79517 | 2011-11-17 00:45:54 +0100 | [diff] [blame] | 1851 | wchar_t *wfilename, *wname; |
Victor Stinner | 8c98189 | 2011-10-11 22:27:13 +0200 | [diff] [blame] | 1852 | Py_ssize_t wname_len; |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1853 | |
| 1854 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
| 1855 | return 1; |
| 1856 | |
Victor Stinner | 1f79517 | 2011-11-17 00:45:54 +0100 | [diff] [blame] | 1857 | wfilename = PyUnicode_AsUnicode(filename); |
| 1858 | if (wfilename == NULL) |
| 1859 | return -1; |
| 1860 | |
| 1861 | h = FindFirstFileW(wfilename, &data); |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1862 | if (h == INVALID_HANDLE_VALUE) { |
| 1863 | PyErr_Format(PyExc_NameError, |
| 1864 | "Can't find file for module %R\n(filename %R)", |
| 1865 | name, filename); |
Victor Stinner | 1f79517 | 2011-11-17 00:45:54 +0100 | [diff] [blame] | 1866 | return -1; |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1867 | } |
| 1868 | FindClose(h); |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 1869 | |
| 1870 | wname = PyUnicode_AsUnicodeAndSize(name, &wname_len); |
| 1871 | if (wname == NULL) |
| 1872 | return -1; |
| 1873 | |
| 1874 | cmp = wcsncmp(data.cFileName, wname, wname_len); |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1875 | return cmp == 0; |
| 1876 | #elif defined(USE_CASE_OK_BYTES) |
| 1877 | int match; |
| 1878 | PyObject *filebytes, *namebytes; |
| 1879 | filebytes = PyUnicode_EncodeFSDefault(filename); |
| 1880 | if (filebytes == NULL) |
| 1881 | return -1; |
| 1882 | namebytes = PyUnicode_EncodeFSDefault(name); |
| 1883 | if (namebytes == NULL) { |
| 1884 | Py_DECREF(filebytes); |
| 1885 | return -1; |
| 1886 | } |
| 1887 | match = case_bytes( |
| 1888 | PyBytes_AS_STRING(filebytes), |
| 1889 | PyBytes_GET_SIZE(filebytes) + prefix_delta, |
Victor Stinner | 1304f2d | 2011-03-20 04:28:55 +0100 | [diff] [blame] | 1890 | PyBytes_GET_SIZE(namebytes), |
| 1891 | PyBytes_AS_STRING(namebytes)); |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1892 | Py_DECREF(filebytes); |
| 1893 | Py_DECREF(namebytes); |
| 1894 | return match; |
| 1895 | #else |
| 1896 | /* 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] | 1897 | return 1; |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1898 | |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1899 | #endif |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1900 | } |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1901 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1902 | #ifdef HAVE_STAT |
Victor Stinner | 4f4402c | 2010-08-14 14:50:26 +0000 | [diff] [blame] | 1903 | |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1904 | /* Helper to look for __init__.py or __init__.py[co] in potential package. |
| 1905 | 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] | 1906 | static int |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1907 | find_init_module(PyObject *directory) |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | struct stat statbuf; |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1910 | PyObject *filename; |
| 1911 | int match; |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1912 | int err; |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1913 | |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1914 | filename = PyUnicode_FromFormat("%U%c__init__.py", directory, SEP); |
| 1915 | if (filename == NULL) |
| 1916 | return -1; |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1917 | err = _Py_stat(filename, &statbuf); |
| 1918 | if (err == -2) |
| 1919 | return -1; |
| 1920 | if (err == 0) { |
Victor Stinner | cc9564e | 2011-03-20 04:58:29 +0100 | [diff] [blame] | 1921 | /* 3=len(".py") */ |
| 1922 | match = case_ok(filename, -3, initstr); |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1923 | if (match < 0) { |
| 1924 | Py_DECREF(filename); |
| 1925 | return -1; |
| 1926 | } |
| 1927 | if (match) { |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1928 | Py_DECREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1929 | return 1; |
| 1930 | } |
| 1931 | } |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1932 | Py_DECREF(filename); |
| 1933 | |
| 1934 | filename = PyUnicode_FromFormat("%U%c__init__.py%c", |
| 1935 | directory, SEP, Py_OptimizeFlag ? 'o' : 'c'); |
| 1936 | if (filename == NULL) |
| 1937 | return -1; |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1938 | err = _Py_stat(filename, &statbuf); |
| 1939 | if (err == -2) { |
| 1940 | Py_DECREF(filename); |
| 1941 | return -1; |
| 1942 | } |
| 1943 | if (err == 0) { |
Victor Stinner | cc9564e | 2011-03-20 04:58:29 +0100 | [diff] [blame] | 1944 | /* 4=len(".pyc") */ |
| 1945 | match = case_ok(filename, -4, initstr); |
Victor Stinner | c9a271c | 2011-03-14 14:34:13 -0400 | [diff] [blame] | 1946 | if (match < 0) { |
| 1947 | Py_DECREF(filename); |
| 1948 | return -1; |
| 1949 | } |
| 1950 | if (match) { |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1951 | Py_DECREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | return 1; |
| 1953 | } |
| 1954 | } |
Victor Stinner | 547a2a6 | 2011-03-20 03:07:28 +0100 | [diff] [blame] | 1955 | Py_DECREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | return 0; |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1957 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1958 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1959 | #endif /* HAVE_STAT */ |
| 1960 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1961 | |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1962 | static int init_builtin(PyObject *); /* Forward */ |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1963 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1964 | /* Initialize a built-in module. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1965 | 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] | 1966 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1967 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1968 | static int |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1969 | init_builtin(PyObject *name) |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1970 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | struct _inittab *p; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1972 | |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1973 | if (_PyImport_FindExtensionObject(name, name) != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | return 1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | for (p = PyImport_Inittab; p->name != NULL; p++) { |
| 1977 | PyObject *mod; |
Antoine Pitrou | 6c40eb7 | 2012-01-18 20:16:09 +0100 | [diff] [blame] | 1978 | PyModuleDef *def; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1979 | if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | if (p->initfunc == NULL) { |
| 1981 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1982 | "Cannot re-init internal module %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | name); |
| 1984 | return -1; |
| 1985 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | mod = (*p->initfunc)(); |
| 1987 | if (mod == 0) |
| 1988 | return -1; |
Antoine Pitrou | 6c40eb7 | 2012-01-18 20:16:09 +0100 | [diff] [blame] | 1989 | /* Remember pointer to module init function. */ |
| 1990 | def = PyModule_GetDef(mod); |
| 1991 | def->m_base.m_init = p->initfunc; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1992 | if (_PyImport_FixupExtensionObject(mod, name, name) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | return -1; |
| 1994 | /* FixupExtension has put the module into sys.modules, |
| 1995 | so we can release our own reference. */ |
| 1996 | Py_DECREF(mod); |
| 1997 | return 1; |
| 1998 | } |
| 1999 | } |
| 2000 | return 0; |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 2001 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 2002 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2003 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2004 | /* Frozen modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2005 | |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 2006 | static struct _frozen * |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2007 | find_frozen(PyObject *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2008 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | struct _frozen *p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2010 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2011 | if (name == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | return NULL; |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 2013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2014 | for (p = PyImport_FrozenModules; ; p++) { |
| 2015 | if (p->name == NULL) |
| 2016 | return NULL; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2017 | if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2018 | break; |
| 2019 | } |
| 2020 | return p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2023 | static PyObject * |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2024 | get_frozen_object(PyObject *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2025 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | struct _frozen *p = find_frozen(name); |
| 2027 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | if (p == NULL) { |
| 2030 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2031 | "No such frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | name); |
| 2033 | return NULL; |
| 2034 | } |
| 2035 | if (p->code == NULL) { |
| 2036 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2037 | "Excluded frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2038 | name); |
| 2039 | return NULL; |
| 2040 | } |
| 2041 | size = p->size; |
| 2042 | if (size < 0) |
| 2043 | size = -size; |
| 2044 | return PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2047 | static PyObject * |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2048 | is_frozen_package(PyObject *name) |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2049 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | struct _frozen *p = find_frozen(name); |
| 2051 | int size; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2053 | if (p == NULL) { |
| 2054 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2055 | "No such frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | name); |
| 2057 | return NULL; |
| 2058 | } |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2060 | size = p->size; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | if (size < 0) |
| 2063 | Py_RETURN_TRUE; |
| 2064 | else |
| 2065 | Py_RETURN_FALSE; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2069 | /* Initialize a frozen module. |
Brett Cannon | 3c2ac44 | 2009-03-08 20:49:47 +0000 | [diff] [blame] | 2070 | 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] | 2071 | an exception set if the initialization failed. |
| 2072 | This function is also used from frozenmain.c */ |
Guido van Rossum | 0b34490 | 1995-02-07 15:35:27 +0000 | [diff] [blame] | 2073 | |
| 2074 | int |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2075 | PyImport_ImportFrozenModuleObject(PyObject *name) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 2076 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2077 | struct _frozen *p; |
| 2078 | PyObject *co, *m, *path; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2079 | int ispackage; |
| 2080 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2081 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2082 | p = find_frozen(name); |
| 2083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | if (p == NULL) |
| 2085 | return 0; |
| 2086 | if (p->code == NULL) { |
| 2087 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2088 | "Excluded frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2089 | name); |
| 2090 | return -1; |
| 2091 | } |
| 2092 | size = p->size; |
| 2093 | ispackage = (size < 0); |
| 2094 | if (ispackage) |
| 2095 | size = -size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | co = PyMarshal_ReadObjectFromString((char *)p->code, size); |
| 2097 | if (co == NULL) |
| 2098 | return -1; |
| 2099 | if (!PyCode_Check(co)) { |
| 2100 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2101 | "frozen object %R is not a code object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | name); |
| 2103 | goto err_return; |
| 2104 | } |
| 2105 | if (ispackage) { |
| 2106 | /* Set __path__ to the package name */ |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2107 | PyObject *d, *l; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2108 | int err; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2109 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2110 | if (m == NULL) |
| 2111 | goto err_return; |
| 2112 | d = PyModule_GetDict(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2113 | l = PyList_New(1); |
| 2114 | if (l == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2115 | goto err_return; |
| 2116 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2117 | Py_INCREF(name); |
| 2118 | PyList_SET_ITEM(l, 0, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | err = PyDict_SetItemString(d, "__path__", l); |
| 2120 | Py_DECREF(l); |
| 2121 | if (err != 0) |
| 2122 | goto err_return; |
| 2123 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2124 | path = PyUnicode_FromString("<frozen>"); |
| 2125 | if (path == NULL) |
| 2126 | goto err_return; |
| 2127 | m = PyImport_ExecCodeModuleObject(name, co, path, NULL); |
| 2128 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | if (m == NULL) |
| 2130 | goto err_return; |
| 2131 | Py_DECREF(co); |
| 2132 | Py_DECREF(m); |
| 2133 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2134 | err_return: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | Py_DECREF(co); |
| 2136 | return -1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 2137 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2138 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2139 | int |
| 2140 | PyImport_ImportFrozenModule(char *name) |
| 2141 | { |
| 2142 | PyObject *nameobj; |
| 2143 | int ret; |
| 2144 | nameobj = PyUnicode_InternFromString(name); |
| 2145 | if (nameobj == NULL) |
| 2146 | return -1; |
| 2147 | ret = PyImport_ImportFrozenModuleObject(nameobj); |
| 2148 | Py_DECREF(nameobj); |
| 2149 | return ret; |
| 2150 | } |
| 2151 | |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2152 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2153 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 2154 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2155 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2156 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2157 | PyImport_ImportModule(const char *name) |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | PyObject *pname; |
| 2160 | PyObject *result; |
Marc-André Lemburg | 3c61c35 | 2001-02-09 19:40:15 +0000 | [diff] [blame] | 2161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | pname = PyUnicode_FromString(name); |
| 2163 | if (pname == NULL) |
| 2164 | return NULL; |
| 2165 | result = PyImport_Import(pname); |
| 2166 | Py_DECREF(pname); |
| 2167 | return result; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 2170 | /* Import a module without blocking |
| 2171 | * |
| 2172 | * At first it tries to fetch the module from sys.modules. If the module was |
| 2173 | * never loaded before it loads it with PyImport_ImportModule() unless another |
| 2174 | * thread holds the import lock. In the latter case the function raises an |
| 2175 | * ImportError instead of blocking. |
| 2176 | * |
| 2177 | * Returns the module object with incremented ref count. |
| 2178 | */ |
| 2179 | PyObject * |
| 2180 | PyImport_ImportModuleNoBlock(const char *name) |
| 2181 | { |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 2182 | PyObject *nameobj, *modules, *result; |
Victor Stinner | 0af0306 | 2011-09-02 00:11:43 +0200 | [diff] [blame] | 2183 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | long me; |
Victor Stinner | 0af0306 | 2011-09-02 00:11:43 +0200 | [diff] [blame] | 2185 | #endif |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | /* Try to get the module from sys.modules[name] */ |
| 2188 | modules = PyImport_GetModuleDict(); |
| 2189 | if (modules == NULL) |
| 2190 | return NULL; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 2191 | |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 2192 | nameobj = PyUnicode_FromString(name); |
| 2193 | if (nameobj == NULL) |
| 2194 | return NULL; |
| 2195 | result = PyDict_GetItem(modules, nameobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | if (result != NULL) { |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 2197 | Py_DECREF(nameobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | Py_INCREF(result); |
| 2199 | return result; |
| 2200 | } |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 2201 | PyErr_Clear(); |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 2202 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | /* check the import lock |
| 2204 | * me might be -1 but I ignore the error here, the lock function |
| 2205 | * takes care of the problem */ |
| 2206 | me = PyThread_get_thread_ident(); |
| 2207 | if (import_lock_thread == -1 || import_lock_thread == me) { |
| 2208 | /* no thread or me is holding the lock */ |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 2209 | result = PyImport_Import(nameobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2210 | } |
| 2211 | else { |
| 2212 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | c24c810 | 2011-03-13 22:38:06 -0400 | [diff] [blame] | 2213 | "Failed to import %R because the import lock" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | "is held by another thread.", |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 2215 | nameobj); |
| 2216 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 | } |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 2218 | #else |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 2219 | result = PyImport_Import(nameobj); |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 2220 | #endif |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 2221 | Py_DECREF(nameobj); |
| 2222 | return result; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2225 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2226 | PyObject * |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2227 | PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals, |
| 2228 | PyObject *locals, PyObject *given_fromlist, |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 2229 | int level) |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2230 | { |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2231 | _Py_IDENTIFIER(__import__); |
| 2232 | _Py_IDENTIFIER(__package__); |
| 2233 | _Py_IDENTIFIER(__path__); |
| 2234 | _Py_IDENTIFIER(__name__); |
| 2235 | _Py_IDENTIFIER(_find_and_load); |
| 2236 | _Py_IDENTIFIER(_handle_fromlist); |
| 2237 | _Py_static_string(single_dot, "."); |
| 2238 | PyObject *abs_name = NULL; |
| 2239 | PyObject *builtins_import = NULL; |
| 2240 | PyObject *final_mod = NULL; |
| 2241 | PyObject *mod = NULL; |
| 2242 | PyObject *package = NULL; |
| 2243 | PyObject *globals = NULL; |
| 2244 | PyObject *fromlist = NULL; |
| 2245 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2246 | |
| 2247 | /* Make sure to use default values so as to not have |
| 2248 | PyObject_CallMethodObjArgs() truncate the parameter list because of a |
| 2249 | NULL argument. */ |
| 2250 | if (given_globals == NULL) { |
| 2251 | globals = PyDict_New(); |
| 2252 | if (globals == NULL) { |
| 2253 | goto error; |
| 2254 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2256 | else { |
| 2257 | /* Only have to care what given_globals is if it will be used |
| 2258 | fortsomething. */ |
| 2259 | if (level > 0 && !PyDict_Check(given_globals)) { |
| 2260 | PyErr_SetString(PyExc_TypeError, "globals must be a dict"); |
| 2261 | goto error; |
| 2262 | } |
| 2263 | globals = given_globals; |
| 2264 | Py_INCREF(globals); |
| 2265 | } |
| 2266 | |
| 2267 | if (given_fromlist == NULL) { |
| 2268 | fromlist = PyList_New(0); |
| 2269 | if (fromlist == NULL) { |
| 2270 | goto error; |
| 2271 | } |
| 2272 | } |
| 2273 | else { |
| 2274 | fromlist = given_fromlist; |
| 2275 | Py_INCREF(fromlist); |
| 2276 | } |
| 2277 | if (name == NULL) { |
| 2278 | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
| 2279 | goto error; |
| 2280 | } |
| 2281 | |
| 2282 | /* The below code is importlib.__import__() & _gcd_import(), ported to C |
| 2283 | for added performance. */ |
| 2284 | |
| 2285 | if (!PyUnicode_Check(name)) { |
| 2286 | PyErr_SetString(PyExc_TypeError, "module name must be a string"); |
| 2287 | goto error; |
| 2288 | } |
| 2289 | else if (PyUnicode_READY(name) < 0) { |
| 2290 | goto error; |
| 2291 | } |
| 2292 | if (level < 0) { |
| 2293 | PyErr_SetString(PyExc_ValueError, "level must be >= 0"); |
| 2294 | goto error; |
| 2295 | } |
| 2296 | else if (level > 0) { |
| 2297 | package = _PyDict_GetItemId(globals, &PyId___package__); |
| 2298 | if (package != NULL && package != Py_None) { |
| 2299 | Py_INCREF(package); |
| 2300 | if (!PyUnicode_Check(package)) { |
| 2301 | PyErr_SetString(PyExc_TypeError, "package must be a string"); |
| 2302 | goto error; |
| 2303 | } |
| 2304 | } |
| 2305 | else { |
Brett Cannon | 273323c | 2012-04-17 19:05:11 -0400 | [diff] [blame] | 2306 | package = _PyDict_GetItemId(globals, &PyId___name__); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2307 | if (package == NULL) { |
Brett Cannon | 273323c | 2012-04-17 19:05:11 -0400 | [diff] [blame] | 2308 | PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2309 | goto error; |
| 2310 | } |
| 2311 | else if (!PyUnicode_Check(package)) { |
| 2312 | PyErr_SetString(PyExc_TypeError, "__name__ must be a string"); |
| 2313 | } |
| 2314 | Py_INCREF(package); |
| 2315 | |
| 2316 | if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) { |
Brett Cannon | 740fce0 | 2012-04-14 14:23:49 -0400 | [diff] [blame] | 2317 | PyObject *partition = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2318 | PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot); |
| 2319 | if (borrowed_dot == NULL) { |
| 2320 | goto error; |
| 2321 | } |
Brett Cannon | 740fce0 | 2012-04-14 14:23:49 -0400 | [diff] [blame] | 2322 | partition = PyUnicode_RPartition(package, borrowed_dot); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2323 | Py_DECREF(package); |
| 2324 | if (partition == NULL) { |
| 2325 | goto error; |
| 2326 | } |
| 2327 | package = PyTuple_GET_ITEM(partition, 0); |
| 2328 | Py_INCREF(package); |
| 2329 | Py_DECREF(partition); |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | if (PyDict_GetItem(interp->modules, package) == NULL) { |
| 2334 | PyErr_Format(PyExc_SystemError, |
| 2335 | "Parent module %R not loaded, cannot perform relative " |
| 2336 | "import", package); |
| 2337 | goto error; |
| 2338 | } |
| 2339 | } |
| 2340 | else { /* level == 0 */ |
| 2341 | if (PyUnicode_GET_LENGTH(name) == 0) { |
| 2342 | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
| 2343 | goto error; |
| 2344 | } |
| 2345 | package = Py_None; |
| 2346 | Py_INCREF(package); |
| 2347 | } |
| 2348 | |
| 2349 | if (level > 0) { |
| 2350 | Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package); |
| 2351 | PyObject *base = NULL; |
| 2352 | int level_up = 1; |
| 2353 | |
| 2354 | for (level_up = 1; level_up < level; level_up += 1) { |
| 2355 | last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1); |
| 2356 | if (last_dot == -2) { |
| 2357 | goto error; |
| 2358 | } |
| 2359 | else if (last_dot == -1) { |
| 2360 | PyErr_SetString(PyExc_ValueError, |
| 2361 | "attempted relative import beyond top-level " |
| 2362 | "package"); |
| 2363 | goto error; |
| 2364 | } |
| 2365 | } |
| 2366 | base = PyUnicode_Substring(package, 0, last_dot); |
| 2367 | if (PyUnicode_GET_LENGTH(name) > 0) { |
Antoine Pitrou | 538ba2a | 2012-04-16 21:52:45 +0200 | [diff] [blame] | 2368 | PyObject *borrowed_dot, *seq = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2369 | |
| 2370 | borrowed_dot = _PyUnicode_FromId(&single_dot); |
Antoine Pitrou | 538ba2a | 2012-04-16 21:52:45 +0200 | [diff] [blame] | 2371 | seq = PyTuple_Pack(2, base, name); |
| 2372 | Py_DECREF(base); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2373 | if (borrowed_dot == NULL || seq == NULL) { |
| 2374 | goto error; |
| 2375 | } |
| 2376 | |
| 2377 | abs_name = PyUnicode_Join(borrowed_dot, seq); |
| 2378 | Py_DECREF(seq); |
| 2379 | if (abs_name == NULL) { |
| 2380 | goto error; |
| 2381 | } |
| 2382 | } |
| 2383 | else { |
| 2384 | abs_name = base; |
| 2385 | } |
| 2386 | } |
| 2387 | else { |
| 2388 | abs_name = name; |
| 2389 | Py_INCREF(abs_name); |
| 2390 | } |
| 2391 | |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 2392 | #ifdef WITH_THREAD |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2393 | _PyImport_AcquireLock(); |
| 2394 | #endif |
| 2395 | /* From this point forward, goto error_with_unlock! */ |
| 2396 | if (PyDict_Check(globals)) { |
| 2397 | builtins_import = _PyDict_GetItemId(globals, &PyId___import__); |
| 2398 | } |
| 2399 | if (builtins_import == NULL) { |
| 2400 | builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__); |
| 2401 | if (builtins_import == NULL) { |
| 2402 | Py_FatalError("__import__ missing"); |
| 2403 | } |
| 2404 | } |
| 2405 | Py_INCREF(builtins_import); |
| 2406 | |
| 2407 | mod = PyDict_GetItem(interp->modules, abs_name); |
| 2408 | if (mod == Py_None) { |
Brett Cannon | 27fc528 | 2012-04-15 14:15:31 -0400 | [diff] [blame] | 2409 | PyObject *msg = PyUnicode_FromFormat("import of %R halted; " |
| 2410 | "None in sys.modules", abs_name); |
| 2411 | if (msg != NULL) { |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 2412 | PyErr_SetImportError(msg, abs_name, NULL); |
| 2413 | Py_DECREF(msg); |
Brett Cannon | 27fc528 | 2012-04-15 14:15:31 -0400 | [diff] [blame] | 2414 | } |
Antoine Pitrou | 71382cb | 2012-04-16 18:48:49 +0200 | [diff] [blame] | 2415 | mod = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2416 | goto error_with_unlock; |
| 2417 | } |
| 2418 | else if (mod != NULL) { |
| 2419 | Py_INCREF(mod); |
| 2420 | } |
| 2421 | else { |
| 2422 | mod = _PyObject_CallMethodObjIdArgs(interp->importlib, |
| 2423 | &PyId__find_and_load, abs_name, |
| 2424 | builtins_import, NULL); |
| 2425 | if (mod == NULL) { |
| 2426 | goto error_with_unlock; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | if (PyObject_Not(fromlist)) { |
| 2431 | if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) { |
| 2432 | PyObject *front = NULL; |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 2433 | PyObject *partition = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2434 | PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot); |
| 2435 | |
| 2436 | if (borrowed_dot == NULL) { |
| 2437 | goto error_with_unlock; |
| 2438 | } |
| 2439 | |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 2440 | partition = PyUnicode_Partition(name, borrowed_dot); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2441 | if (partition == NULL) { |
| 2442 | goto error_with_unlock; |
| 2443 | } |
| 2444 | |
| 2445 | front = PyTuple_GET_ITEM(partition, 0); |
| 2446 | Py_INCREF(front); |
| 2447 | Py_DECREF(partition); |
| 2448 | |
| 2449 | if (level == 0) { |
Brett Cannon | 881535b | 2012-04-15 15:24:04 -0400 | [diff] [blame] | 2450 | if (PyUnicode_GET_LENGTH(name) == |
| 2451 | PyUnicode_GET_LENGTH(front)) { |
| 2452 | final_mod = mod; |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 2453 | } |
| 2454 | else { |
Brett Cannon | 881535b | 2012-04-15 15:24:04 -0400 | [diff] [blame] | 2455 | final_mod = PyDict_GetItem(interp->modules, front); |
| 2456 | if (final_mod == NULL) { |
| 2457 | PyErr_Format(PyExc_KeyError, |
| 2458 | "%R not in sys.modules as expected", front); |
| 2459 | } |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 2460 | } |
Brett Cannon | 881535b | 2012-04-15 15:24:04 -0400 | [diff] [blame] | 2461 | Py_DECREF(front); |
| 2462 | if (final_mod == NULL) { |
| 2463 | goto error_with_unlock; |
| 2464 | } |
| 2465 | Py_INCREF(final_mod); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2466 | } |
| 2467 | else { |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 2468 | Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) - |
| 2469 | PyUnicode_GET_LENGTH(front); |
| 2470 | Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name); |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 2471 | PyObject *to_return = PyUnicode_Substring(abs_name, 0, |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2472 | abs_name_len - cut_off); |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 2473 | Py_DECREF(front); |
| 2474 | if (to_return == NULL) { |
| 2475 | goto error_with_unlock; |
| 2476 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2477 | |
| 2478 | final_mod = PyDict_GetItem(interp->modules, to_return); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2479 | Py_DECREF(to_return); |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 2480 | if (final_mod == NULL) { |
| 2481 | PyErr_Format(PyExc_KeyError, |
| 2482 | "%R not in sys.modules as expected", |
| 2483 | to_return); |
| 2484 | } |
| 2485 | else { |
| 2486 | Py_INCREF(final_mod); |
| 2487 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2488 | } |
| 2489 | } |
| 2490 | else { |
| 2491 | final_mod = mod; |
| 2492 | Py_INCREF(mod); |
| 2493 | } |
| 2494 | } |
| 2495 | else { |
| 2496 | final_mod = _PyObject_CallMethodObjIdArgs(interp->importlib, |
| 2497 | &PyId__handle_fromlist, mod, |
| 2498 | fromlist, builtins_import, |
| 2499 | NULL); |
| 2500 | } |
| 2501 | error_with_unlock: |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 2502 | #ifdef WITH_THREAD |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 2503 | if (_PyImport_ReleaseLock() < 0) { |
| 2504 | PyErr_SetString(PyExc_RuntimeError, "not holding the import lock"); |
| 2505 | } |
| 2506 | #endif |
| 2507 | error: |
| 2508 | Py_XDECREF(abs_name); |
| 2509 | Py_XDECREF(builtins_import); |
| 2510 | Py_XDECREF(mod); |
| 2511 | Py_XDECREF(package); |
| 2512 | Py_XDECREF(globals); |
| 2513 | Py_XDECREF(fromlist); |
| 2514 | return final_mod; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 2517 | PyObject * |
Benjamin Peterson | 04778a8 | 2011-05-25 09:29:00 -0500 | [diff] [blame] | 2518 | PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 2519 | PyObject *fromlist, int level) |
| 2520 | { |
| 2521 | PyObject *nameobj, *mod; |
| 2522 | nameobj = PyUnicode_FromString(name); |
| 2523 | if (nameobj == NULL) |
| 2524 | return NULL; |
| 2525 | mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals, |
| 2526 | fromlist, level); |
| 2527 | Py_DECREF(nameobj); |
| 2528 | return mod; |
| 2529 | } |
| 2530 | |
| 2531 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2532 | /* Re-import a module of any kind and return its module object, WITH |
| 2533 | INCREMENTED REFERENCE COUNT */ |
| 2534 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2535 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2536 | PyImport_ReloadModule(PyObject *m) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 2539 | PyObject *modules_reloading = interp->modules_reloading; |
| 2540 | PyObject *modules = PyImport_GetModuleDict(); |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 2541 | PyObject *loader = NULL, *existing_m = NULL; |
| 2542 | PyObject *name; |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2543 | Py_ssize_t subname_start; |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 2544 | PyObject *newm = NULL; |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 2545 | _Py_IDENTIFIER(__loader__); |
| 2546 | _Py_IDENTIFIER(load_module); |
Brett Cannon | 9a5b25a | 2010-03-01 02:09:17 +0000 | [diff] [blame] | 2547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | if (modules_reloading == NULL) { |
| 2549 | Py_FatalError("PyImport_ReloadModule: " |
| 2550 | "no modules_reloading dictionary!"); |
| 2551 | return NULL; |
| 2552 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2554 | if (m == NULL || !PyModule_Check(m)) { |
| 2555 | PyErr_SetString(PyExc_TypeError, |
| 2556 | "reload() argument must be module"); |
| 2557 | return NULL; |
| 2558 | } |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2559 | name = PyModule_GetNameObject(m); |
| 2560 | if (name == NULL || PyUnicode_READY(name) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2561 | return NULL; |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2562 | if (m != PyDict_GetItem(modules, name)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 98dbba5 | 2011-03-14 15:15:47 -0400 | [diff] [blame] | 2564 | "reload(): module %R not in sys.modules", |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2565 | name); |
| 2566 | Py_DECREF(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | return NULL; |
| 2568 | } |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2569 | existing_m = PyDict_GetItem(modules_reloading, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 | if (existing_m != NULL) { |
| 2571 | /* Due to a recursive reload, this module is already |
| 2572 | being reloaded. */ |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2573 | Py_DECREF(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 | Py_INCREF(existing_m); |
| 2575 | return existing_m; |
| 2576 | } |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2577 | if (PyDict_SetItem(modules_reloading, name, m) < 0) { |
| 2578 | Py_DECREF(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | return NULL; |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 2580 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2581 | |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2582 | subname_start = PyUnicode_FindChar(name, '.', 0, |
| 2583 | PyUnicode_GET_LENGTH(name), -1); |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 2584 | if (subname_start != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2585 | PyObject *parentname, *parent; |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2586 | parentname = PyUnicode_Substring(name, 0, subname_start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2587 | if (parentname == NULL) { |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 2588 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2589 | } |
| 2590 | parent = PyDict_GetItem(modules, parentname); |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 2591 | Py_XDECREF(parent); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2592 | if (parent == NULL) { |
| 2593 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 98dbba5 | 2011-03-14 15:15:47 -0400 | [diff] [blame] | 2594 | "reload(): parent %R not in sys.modules", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | parentname); |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 2596 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2598 | } |
Phillip J. Eby | 7ec642a | 2004-09-23 04:37:36 +0000 | [diff] [blame] | 2599 | |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 2600 | loader = _PyObject_GetAttrId(m, &PyId___loader__); |
| 2601 | if (loader == NULL) { |
| 2602 | goto error; |
| 2603 | } |
| 2604 | newm = _PyObject_CallMethodId(loader, &PyId_load_module, "O", name); |
| 2605 | Py_DECREF(loader); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2606 | if (newm == NULL) { |
| 2607 | /* load_module probably removed name from modules because of |
| 2608 | * the error. Put back the original module object. We're |
| 2609 | * going to return NULL in this case regardless of whether |
| 2610 | * replacing name succeeds, so the return value is ignored. |
| 2611 | */ |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2612 | PyDict_SetItem(modules, name, m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2613 | } |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 2614 | |
| 2615 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | imp_modules_reloading_clear(); |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 2617 | Py_DECREF(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | return newm; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2622 | /* Higher-level import emulator which emulates the "import" statement |
| 2623 | more accurately -- it invokes the __import__() function from the |
| 2624 | builtins of the current globals. This means that the import is |
| 2625 | done using whatever import hooks are installed in the current |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 2626 | environment. |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 2627 | A dummy list ["__doc__"] is passed as the 4th argument so that |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 2628 | e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 2629 | will return <module "gencache"> instead of <module "win32com">. */ |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2630 | |
| 2631 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2632 | PyImport_Import(PyObject *module_name) |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2633 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | static PyObject *silly_list = NULL; |
| 2635 | static PyObject *builtins_str = NULL; |
| 2636 | static PyObject *import_str = NULL; |
| 2637 | PyObject *globals = NULL; |
| 2638 | PyObject *import = NULL; |
| 2639 | PyObject *builtins = NULL; |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 2640 | PyObject *modules = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | PyObject *r = NULL; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | /* Initialize constant string objects */ |
| 2644 | if (silly_list == NULL) { |
| 2645 | import_str = PyUnicode_InternFromString("__import__"); |
| 2646 | if (import_str == NULL) |
| 2647 | return NULL; |
| 2648 | builtins_str = PyUnicode_InternFromString("__builtins__"); |
| 2649 | if (builtins_str == NULL) |
| 2650 | return NULL; |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 2651 | silly_list = PyList_New(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | if (silly_list == NULL) |
| 2653 | return NULL; |
| 2654 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2656 | /* Get the builtins from current globals */ |
| 2657 | globals = PyEval_GetGlobals(); |
| 2658 | if (globals != NULL) { |
| 2659 | Py_INCREF(globals); |
| 2660 | builtins = PyObject_GetItem(globals, builtins_str); |
| 2661 | if (builtins == NULL) |
| 2662 | goto err; |
| 2663 | } |
| 2664 | else { |
| 2665 | /* No globals -- use standard builtins, and fake globals */ |
| 2666 | builtins = PyImport_ImportModuleLevel("builtins", |
| 2667 | NULL, NULL, NULL, 0); |
| 2668 | if (builtins == NULL) |
| 2669 | return NULL; |
| 2670 | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
| 2671 | if (globals == NULL) |
| 2672 | goto err; |
| 2673 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2675 | /* Get the __import__ function from the builtins */ |
| 2676 | if (PyDict_Check(builtins)) { |
| 2677 | import = PyObject_GetItem(builtins, import_str); |
| 2678 | if (import == NULL) |
| 2679 | PyErr_SetObject(PyExc_KeyError, import_str); |
| 2680 | } |
| 2681 | else |
| 2682 | import = PyObject_GetAttr(builtins, import_str); |
| 2683 | if (import == NULL) |
| 2684 | goto err; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | /* Call the __import__ function with the proper argument list |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 2687 | Always use absolute import here. |
| 2688 | Calling for side-effect of import. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | r = PyObject_CallFunction(import, "OOOOi", module_name, globals, |
| 2690 | globals, silly_list, 0, NULL); |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 2691 | if (r == NULL) |
| 2692 | goto err; |
| 2693 | Py_DECREF(r); |
| 2694 | |
| 2695 | modules = PyImport_GetModuleDict(); |
| 2696 | r = PyDict_GetItem(modules, module_name); |
| 2697 | if (r != NULL) |
| 2698 | Py_INCREF(r); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2699 | |
| 2700 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2701 | Py_XDECREF(globals); |
| 2702 | Py_XDECREF(builtins); |
| 2703 | Py_XDECREF(import); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 2704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | return r; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2709 | /* Module 'imp' provides Python access to the primitives used for |
| 2710 | importing modules. |
| 2711 | */ |
| 2712 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2713 | static PyObject * |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 2714 | imp_make_magic(long magic) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | char buf[4]; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | buf[0] = (char) ((magic >> 0) & 0xff); |
| 2719 | buf[1] = (char) ((magic >> 8) & 0xff); |
| 2720 | buf[2] = (char) ((magic >> 16) & 0xff); |
| 2721 | buf[3] = (char) ((magic >> 24) & 0xff); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | return PyBytes_FromStringAndSize(buf, 4); |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 2724 | } |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 2725 | |
| 2726 | static PyObject * |
| 2727 | imp_get_magic(PyObject *self, PyObject *noargs) |
| 2728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | return imp_make_magic(pyc_magic); |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | static PyObject * |
| 2733 | imp_get_tag(PyObject *self, PyObject *noargs) |
| 2734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | return PyUnicode_FromString(pyc_tag); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2738 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 2739 | imp_get_suffixes(PyObject *self, PyObject *noargs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | PyObject *list; |
| 2742 | struct filedescr *fdp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | list = PyList_New(0); |
| 2745 | if (list == NULL) |
| 2746 | return NULL; |
| 2747 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
| 2748 | PyObject *item = Py_BuildValue("ssi", |
| 2749 | fdp->suffix, fdp->mode, fdp->type); |
| 2750 | if (item == NULL) { |
| 2751 | Py_DECREF(list); |
| 2752 | return NULL; |
| 2753 | } |
| 2754 | if (PyList_Append(list, item) < 0) { |
| 2755 | Py_DECREF(list); |
| 2756 | Py_DECREF(item); |
| 2757 | return NULL; |
| 2758 | } |
| 2759 | Py_DECREF(item); |
| 2760 | } |
| 2761 | return list; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2764 | static PyObject * |
Victor Stinner | 533d783 | 2011-03-14 13:22:54 -0400 | [diff] [blame] | 2765 | call_find_module(PyObject *name, PyObject *path_list) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | extern int fclose(FILE *); |
| 2768 | PyObject *fob, *ret; |
| 2769 | PyObject *pathobj; |
| 2770 | struct filedescr *fdp; |
Victor Stinner | 7d8b77c | 2011-03-12 08:45:02 -0500 | [diff] [blame] | 2771 | FILE *fp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | int fd = -1; |
| 2773 | char *found_encoding = NULL; |
| 2774 | char *encoding = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2775 | |
Victor Stinner | 533d783 | 2011-03-14 13:22:54 -0400 | [diff] [blame] | 2776 | if (path_list == Py_None) |
| 2777 | path_list = NULL; |
| 2778 | fdp = find_module(NULL, name, path_list, |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 2779 | &pathobj, &fp, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2780 | if (fdp == NULL) |
| 2781 | return NULL; |
| 2782 | if (fp != NULL) { |
| 2783 | fd = fileno(fp); |
| 2784 | if (fd != -1) |
| 2785 | fd = dup(fd); |
| 2786 | fclose(fp); |
| 2787 | fp = NULL; |
Antoine Pitrou | 7214612 | 2012-02-22 18:03:04 +0100 | [diff] [blame] | 2788 | if (fd == -1) |
| 2789 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2790 | } |
| 2791 | if (fd != -1) { |
| 2792 | if (strchr(fdp->mode, 'b') == NULL) { |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 2793 | /* PyTokenizer_FindEncodingFilename() returns PyMem_MALLOC'ed |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2794 | memory. */ |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 2795 | found_encoding = PyTokenizer_FindEncodingFilename(fd, pathobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | lseek(fd, 0, 0); /* Reset position */ |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 2797 | if (found_encoding == NULL && PyErr_Occurred()) { |
| 2798 | Py_XDECREF(pathobj); |
Antoine Pitrou | 4f22a8d | 2012-02-22 18:05:43 +0100 | [diff] [blame] | 2799 | close(fd); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | return NULL; |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 2801 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2802 | encoding = (found_encoding != NULL) ? found_encoding : |
| 2803 | (char*)PyUnicode_GetDefaultEncoding(); |
| 2804 | } |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 2805 | fob = PyFile_FromFd(fd, NULL, fdp->mode, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | (char*)encoding, NULL, NULL, 1); |
| 2807 | if (fob == NULL) { |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 2808 | Py_XDECREF(pathobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | close(fd); |
| 2810 | PyMem_FREE(found_encoding); |
| 2811 | return NULL; |
| 2812 | } |
| 2813 | } |
| 2814 | else { |
| 2815 | fob = Py_None; |
| 2816 | Py_INCREF(fob); |
| 2817 | } |
Victor Stinner | 2fd76e4 | 2011-03-14 15:19:39 -0400 | [diff] [blame] | 2818 | if (pathobj == NULL) { |
| 2819 | Py_INCREF(Py_None); |
| 2820 | pathobj = Py_None; |
| 2821 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | ret = Py_BuildValue("NN(ssi)", |
| 2823 | fob, pathobj, fdp->suffix, fdp->mode, fdp->type); |
| 2824 | PyMem_FREE(found_encoding); |
Brett Cannon | 3bb42d9 | 2007-10-20 03:43:15 +0000 | [diff] [blame] | 2825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2826 | return ret; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2829 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2830 | imp_find_module(PyObject *self, PyObject *args) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2831 | { |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 2832 | PyObject *name, *path_list = NULL; |
| 2833 | if (!PyArg_ParseTuple(args, "U|O:find_module", |
| 2834 | &name, &path_list)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | return NULL; |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 2836 | return call_find_module(name, path_list); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2840 | imp_init_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2841 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 2842 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | int ret; |
| 2844 | PyObject *m; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 2845 | if (!PyArg_ParseTuple(args, "U:init_builtin", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | return NULL; |
| 2847 | ret = init_builtin(name); |
| 2848 | if (ret < 0) |
| 2849 | return NULL; |
| 2850 | if (ret == 0) { |
| 2851 | Py_INCREF(Py_None); |
| 2852 | return Py_None; |
| 2853 | } |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 2854 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | Py_XINCREF(m); |
| 2856 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2859 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2860 | imp_init_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2861 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2862 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | int ret; |
| 2864 | PyObject *m; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2865 | if (!PyArg_ParseTuple(args, "U:init_frozen", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | return NULL; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2867 | ret = PyImport_ImportFrozenModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | if (ret < 0) |
| 2869 | return NULL; |
| 2870 | if (ret == 0) { |
| 2871 | Py_INCREF(Py_None); |
| 2872 | return Py_None; |
| 2873 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2874 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | Py_XINCREF(m); |
| 2876 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2879 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2880 | imp_get_frozen_object(PyObject *self, PyObject *args) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2881 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2882 | PyObject *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 2883 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2884 | if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | return NULL; |
| 2886 | return get_frozen_object(name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2889 | static PyObject * |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2890 | imp_is_frozen_package(PyObject *self, PyObject *args) |
| 2891 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2892 | PyObject *name; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2893 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2894 | if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | return NULL; |
| 2896 | return is_frozen_package(name); |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2900 | imp_is_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2901 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 2902 | PyObject *name; |
| 2903 | if (!PyArg_ParseTuple(args, "U:is_builtin", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2904 | return NULL; |
| 2905 | return PyLong_FromLong(is_builtin(name)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2908 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2909 | imp_is_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2910 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2911 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2912 | struct _frozen *p; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2913 | if (!PyArg_ParseTuple(args, "U:is_frozen", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2914 | return NULL; |
| 2915 | p = find_frozen(name); |
| 2916 | return PyBool_FromLong((long) (p == NULL ? 0 : p->size)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | static FILE * |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 2920 | get_file(PyObject *pathname, PyObject *fob, char *mode) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2921 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 | FILE *fp; |
| 2923 | if (mode[0] == 'U') |
| 2924 | mode = "r" PY_STDIOTEXTMODE; |
| 2925 | if (fob == NULL) { |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 2926 | fp = _Py_fopen(pathname, mode); |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2927 | if (!fp) { |
| 2928 | if (!PyErr_Occurred()) |
| 2929 | PyErr_SetFromErrno(PyExc_IOError); |
| 2930 | return NULL; |
| 2931 | } |
| 2932 | return fp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2933 | } |
| 2934 | else { |
| 2935 | int fd = PyObject_AsFileDescriptor(fob); |
| 2936 | if (fd == -1) |
| 2937 | return NULL; |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2938 | if (!_PyVerify_fd(fd)) { |
| 2939 | PyErr_SetFromErrno(PyExc_IOError); |
| 2940 | return NULL; |
| 2941 | } |
| 2942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | /* the FILE struct gets a new fd, so that it can be closed |
| 2944 | * independently of the file descriptor given |
| 2945 | */ |
| 2946 | fd = dup(fd); |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2947 | if (fd == -1) { |
| 2948 | PyErr_SetFromErrno(PyExc_IOError); |
| 2949 | return NULL; |
| 2950 | } |
| 2951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | fp = fdopen(fd, mode); |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2953 | if (!fp) { |
| 2954 | PyErr_SetFromErrno(PyExc_IOError); |
| 2955 | return NULL; |
| 2956 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2957 | return fp; |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2958 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2961 | #ifdef HAVE_DYNAMIC_LOADING |
| 2962 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2963 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2964 | imp_load_dynamic(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2965 | { |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2966 | PyObject *name, *pathname, *fob = NULL, *mod; |
| 2967 | FILE *fp; |
| 2968 | |
| 2969 | if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic", |
| 2970 | &name, PyUnicode_FSDecoder, &pathname, &fob)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2971 | return NULL; |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2972 | if (fob != NULL) { |
| 2973 | fp = get_file(NULL, fob, "r"); |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 2974 | if (fp == NULL) { |
| 2975 | Py_DECREF(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | return NULL; |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 2977 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2978 | } |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2979 | else |
| 2980 | fp = NULL; |
| 2981 | mod = _PyImport_LoadDynamicModule(name, pathname, fp); |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 2982 | Py_DECREF(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | if (fp) |
| 2984 | fclose(fp); |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2985 | return mod; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2988 | #endif /* HAVE_DYNAMIC_LOADING */ |
| 2989 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2990 | static PyObject * |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 2991 | imp_reload(PyObject *self, PyObject *v) |
| 2992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | return PyImport_ReloadModule(v); |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | PyDoc_STRVAR(doc_reload, |
| 2997 | "reload(module) -> module\n\ |
| 2998 | \n\ |
| 2999 | Reload the module. The module must have been successfully imported before."); |
| 3000 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3001 | static PyObject * |
| 3002 | imp_cache_from_source(PyObject *self, PyObject *args, PyObject *kws) |
| 3003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3004 | static char *kwlist[] = {"path", "debug_override", NULL}; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3005 | |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 3006 | PyObject *pathname, *cpathname; |
Benjamin Peterson | 294a9fc | 2010-10-16 03:12:39 +0000 | [diff] [blame] | 3007 | PyObject *debug_override = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | int debug = !Py_OptimizeFlag; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | if (!PyArg_ParseTupleAndKeywords( |
Victor Stinner | 3ea23dd | 2010-10-15 20:34:32 +0000 | [diff] [blame] | 3011 | args, kws, "O&|O", kwlist, |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 3012 | PyUnicode_FSDecoder, &pathname, &debug_override)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3014 | |
Benjamin Peterson | 294a9fc | 2010-10-16 03:12:39 +0000 | [diff] [blame] | 3015 | if (debug_override != NULL && |
| 3016 | (debug = PyObject_IsTrue(debug_override)) < 0) { |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 3017 | Py_DECREF(pathname); |
Benjamin Peterson | 294a9fc | 2010-10-16 03:12:39 +0000 | [diff] [blame] | 3018 | return NULL; |
| 3019 | } |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3020 | |
Martin v. Löwis | 30260a7 | 2011-10-23 17:35:46 +0200 | [diff] [blame] | 3021 | if (PyUnicode_READY(pathname) < 0) |
| 3022 | return NULL; |
| 3023 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3024 | cpathname = make_compiled_pathname(pathname, debug); |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 3025 | Py_DECREF(pathname); |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3027 | if (cpathname == NULL) { |
| 3028 | PyErr_Format(PyExc_SystemError, "path buffer too short"); |
| 3029 | return NULL; |
| 3030 | } |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 3031 | return cpathname; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | PyDoc_STRVAR(doc_cache_from_source, |
Éric Araujo | 5df1108 | 2011-11-03 03:38:44 +0100 | [diff] [blame] | 3035 | "cache_from_source(path, [debug_override]) -> path\n\ |
| 3036 | 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] | 3037 | \n\ |
| 3038 | The .py file does not need to exist; this simply returns the path to the\n\ |
| 3039 | .pyc/.pyo file calculated as if the .py file were imported. The extension\n\ |
| 3040 | will be .pyc unless __debug__ is not defined, then it will be .pyo.\n\ |
| 3041 | \n\ |
| 3042 | If debug_override is not None, then it must be a boolean and is taken as\n\ |
| 3043 | the value of __debug__ instead."); |
| 3044 | |
| 3045 | static PyObject * |
| 3046 | imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws) |
| 3047 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | static char *kwlist[] = {"path", NULL}; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 3049 | PyObject *pathname, *source; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | if (!PyArg_ParseTupleAndKeywords( |
Victor Stinner | ebc0052 | 2010-12-03 17:06:43 +0000 | [diff] [blame] | 3052 | args, kws, "O&", kwlist, |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 3053 | PyUnicode_FSDecoder, &pathname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3055 | |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 3056 | source = make_source_pathname(pathname); |
| 3057 | if (source == NULL) { |
| 3058 | PyErr_Format(PyExc_ValueError, "Not a PEP 3147 pyc path: %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | pathname); |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 3060 | Py_DECREF(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3061 | return NULL; |
| 3062 | } |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 3063 | Py_DECREF(pathname); |
| 3064 | return source; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | PyDoc_STRVAR(doc_source_from_cache, |
Éric Araujo | 5df1108 | 2011-11-03 03:38:44 +0100 | [diff] [blame] | 3068 | "source_from_cache(path) -> path\n\ |
| 3069 | 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] | 3070 | \n\ |
| 3071 | The .pyc/.pyo file does not need to exist; this simply returns the path to\n\ |
| 3072 | the .py file calculated to correspond to the .pyc/.pyo file. If path\n\ |
| 3073 | does not conform to PEP 3147 format, ValueError will be raised."); |
| 3074 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3075 | /* Doc strings */ |
| 3076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3077 | PyDoc_STRVAR(doc_imp, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 3078 | "(Extremely) low-level import machinery bits as used by importlib and imp."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3080 | PyDoc_STRVAR(doc_find_module, |
| 3081 | "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3082 | Search for a module. If path is omitted or None, search for a\n\ |
| 3083 | built-in, frozen or special module and continue search in sys.path.\n\ |
| 3084 | 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] | 3085 | package, pass the submodule name and the package's __path__."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3086 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3087 | PyDoc_STRVAR(doc_get_magic, |
| 3088 | "get_magic() -> string\n\ |
| 3089 | Return the magic number for .pyc or .pyo files."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3090 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 3091 | PyDoc_STRVAR(doc_get_tag, |
| 3092 | "get_tag() -> string\n\ |
| 3093 | Return the magic tag for .pyc or .pyo files."); |
| 3094 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3095 | PyDoc_STRVAR(doc_get_suffixes, |
| 3096 | "get_suffixes() -> [(suffix, mode, type), ...]\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3097 | 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] | 3098 | that find_module() looks for."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 3099 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3100 | PyDoc_STRVAR(doc_lock_held, |
Tim Peters | a7c6509 | 2004-08-01 23:26:05 +0000 | [diff] [blame] | 3101 | "lock_held() -> boolean\n\ |
| 3102 | Return True if the import lock is currently held, else False.\n\ |
| 3103 | On platforms without threads, return False."); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 3104 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 3105 | PyDoc_STRVAR(doc_acquire_lock, |
| 3106 | "acquire_lock() -> None\n\ |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 3107 | Acquires the interpreter's import lock for the current thread.\n\ |
| 3108 | This lock should be used by import hooks to ensure thread-safety\n\ |
| 3109 | when importing modules.\n\ |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 3110 | On platforms without threads, this function does nothing."); |
| 3111 | |
| 3112 | PyDoc_STRVAR(doc_release_lock, |
| 3113 | "release_lock() -> None\n\ |
| 3114 | Release the interpreter's import lock.\n\ |
| 3115 | On platforms without threads, this function does nothing."); |
| 3116 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3117 | static PyMethodDef imp_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | {"find_module", imp_find_module, METH_VARARGS, doc_find_module}, |
| 3119 | {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic}, |
| 3120 | {"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag}, |
| 3121 | {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, |
| 3123 | {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock}, |
| 3124 | {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock}, |
| 3125 | {"reload", imp_reload, METH_O, doc_reload}, |
| 3126 | {"cache_from_source", (PyCFunction)imp_cache_from_source, |
| 3127 | METH_VARARGS | METH_KEYWORDS, doc_cache_from_source}, |
| 3128 | {"source_from_cache", (PyCFunction)imp_source_from_cache, |
| 3129 | METH_VARARGS | METH_KEYWORDS, doc_source_from_cache}, |
| 3130 | /* The rest are obsolete */ |
| 3131 | {"get_frozen_object", imp_get_frozen_object, METH_VARARGS}, |
| 3132 | {"is_frozen_package", imp_is_frozen_package, METH_VARARGS}, |
| 3133 | {"init_builtin", imp_init_builtin, METH_VARARGS}, |
| 3134 | {"init_frozen", imp_init_frozen, METH_VARARGS}, |
| 3135 | {"is_builtin", imp_is_builtin, METH_VARARGS}, |
| 3136 | {"is_frozen", imp_is_frozen, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 3137 | #ifdef HAVE_DYNAMIC_LOADING |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | {"load_dynamic", imp_load_dynamic, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 3139 | #endif |
Brett Cannon | 442c9b9 | 2011-03-23 16:14:42 -0700 | [diff] [blame] | 3140 | {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3142 | }; |
| 3143 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 3144 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3145 | setint(PyObject *d, char *name, int value) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | PyObject *v; |
| 3148 | int err; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | v = PyLong_FromLong((long)value); |
| 3151 | err = PyDict_SetItemString(d, name, v); |
| 3152 | Py_XDECREF(v); |
| 3153 | return err; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3156 | typedef struct { |
| 3157 | PyObject_HEAD |
| 3158 | } NullImporter; |
| 3159 | |
| 3160 | static int |
| 3161 | NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds) |
| 3162 | { |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 3163 | #ifndef MS_WINDOWS |
| 3164 | PyObject *path; |
| 3165 | struct stat statbuf; |
| 3166 | int rv; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | if (!_PyArg_NoKeywords("NullImporter()", kwds)) |
| 3169 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3170 | |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 3171 | if (!PyArg_ParseTuple(args, "O&:NullImporter", |
| 3172 | PyUnicode_FSConverter, &path)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3174 | |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 3175 | if (PyBytes_GET_SIZE(path) == 0) { |
| 3176 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | PyErr_SetString(PyExc_ImportError, "empty pathname"); |
| 3178 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3179 | } |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 3180 | |
| 3181 | rv = stat(PyBytes_AS_STRING(path), &statbuf); |
| 3182 | Py_DECREF(path); |
| 3183 | if (rv == 0) { |
| 3184 | /* it exists */ |
| 3185 | if (S_ISDIR(statbuf.st_mode)) { |
| 3186 | /* it's a directory */ |
| 3187 | PyErr_SetString(PyExc_ImportError, "existing directory"); |
| 3188 | return -1; |
| 3189 | } |
| 3190 | } |
| 3191 | #else /* MS_WINDOWS */ |
| 3192 | PyObject *pathobj; |
| 3193 | DWORD rv; |
Victor Stinner | 255dfdb | 2010-09-29 10:28:51 +0000 | [diff] [blame] | 3194 | wchar_t *path; |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 3195 | |
| 3196 | if (!_PyArg_NoKeywords("NullImporter()", kwds)) |
| 3197 | return -1; |
| 3198 | |
| 3199 | if (!PyArg_ParseTuple(args, "U:NullImporter", |
| 3200 | &pathobj)) |
| 3201 | return -1; |
| 3202 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3203 | if (PyUnicode_GET_LENGTH(pathobj) == 0) { |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 3204 | PyErr_SetString(PyExc_ImportError, "empty pathname"); |
| 3205 | return -1; |
| 3206 | } |
| 3207 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 3208 | path = PyUnicode_AsWideCharString(pathobj, NULL); |
Victor Stinner | 255dfdb | 2010-09-29 10:28:51 +0000 | [diff] [blame] | 3209 | if (path == NULL) |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 3210 | return -1; |
| 3211 | /* see issue1293 and issue3677: |
| 3212 | * stat() on Windows doesn't recognise paths like |
| 3213 | * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs. |
| 3214 | */ |
| 3215 | rv = GetFileAttributesW(path); |
Victor Stinner | 255dfdb | 2010-09-29 10:28:51 +0000 | [diff] [blame] | 3216 | PyMem_Free(path); |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 3217 | if (rv != INVALID_FILE_ATTRIBUTES) { |
| 3218 | /* it exists */ |
| 3219 | if (rv & FILE_ATTRIBUTE_DIRECTORY) { |
| 3220 | /* it's a directory */ |
| 3221 | PyErr_SetString(PyExc_ImportError, "existing directory"); |
| 3222 | return -1; |
| 3223 | } |
| 3224 | } |
| 3225 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | return 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | static PyObject * |
| 3230 | NullImporter_find_module(NullImporter *self, PyObject *args) |
| 3231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3232 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | static PyMethodDef NullImporter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS, |
| 3237 | "Always return None" |
| 3238 | }, |
| 3239 | {NULL} /* Sentinel */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3240 | }; |
| 3241 | |
| 3242 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 3243 | PyTypeObject PyNullImporter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3245 | "imp.NullImporter", /*tp_name*/ |
| 3246 | sizeof(NullImporter), /*tp_basicsize*/ |
| 3247 | 0, /*tp_itemsize*/ |
| 3248 | 0, /*tp_dealloc*/ |
| 3249 | 0, /*tp_print*/ |
| 3250 | 0, /*tp_getattr*/ |
| 3251 | 0, /*tp_setattr*/ |
| 3252 | 0, /*tp_reserved*/ |
| 3253 | 0, /*tp_repr*/ |
| 3254 | 0, /*tp_as_number*/ |
| 3255 | 0, /*tp_as_sequence*/ |
| 3256 | 0, /*tp_as_mapping*/ |
| 3257 | 0, /*tp_hash */ |
| 3258 | 0, /*tp_call*/ |
| 3259 | 0, /*tp_str*/ |
| 3260 | 0, /*tp_getattro*/ |
| 3261 | 0, /*tp_setattro*/ |
| 3262 | 0, /*tp_as_buffer*/ |
| 3263 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3264 | "Null importer object", /* tp_doc */ |
| 3265 | 0, /* tp_traverse */ |
| 3266 | 0, /* tp_clear */ |
| 3267 | 0, /* tp_richcompare */ |
| 3268 | 0, /* tp_weaklistoffset */ |
| 3269 | 0, /* tp_iter */ |
| 3270 | 0, /* tp_iternext */ |
| 3271 | NullImporter_methods, /* tp_methods */ |
| 3272 | 0, /* tp_members */ |
| 3273 | 0, /* tp_getset */ |
| 3274 | 0, /* tp_base */ |
| 3275 | 0, /* tp_dict */ |
| 3276 | 0, /* tp_descr_get */ |
| 3277 | 0, /* tp_descr_set */ |
| 3278 | 0, /* tp_dictoffset */ |
| 3279 | (initproc)NullImporter_init, /* tp_init */ |
| 3280 | 0, /* tp_alloc */ |
| 3281 | PyType_GenericNew /* tp_new */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3282 | }; |
| 3283 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3284 | static struct PyModuleDef impmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | PyModuleDef_HEAD_INIT, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 3286 | "_imp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3287 | doc_imp, |
| 3288 | 0, |
| 3289 | imp_methods, |
| 3290 | NULL, |
| 3291 | NULL, |
| 3292 | NULL, |
| 3293 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3294 | }; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3295 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 3296 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3297 | PyInit_imp(void) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3299 | PyObject *m, *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3301 | if (PyType_Ready(&PyNullImporter_Type) < 0) |
| 3302 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | m = PyModule_Create(&impmodule); |
| 3305 | if (m == NULL) |
| 3306 | goto failure; |
| 3307 | d = PyModule_GetDict(m); |
| 3308 | if (d == NULL) |
| 3309 | goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3311 | if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; |
| 3312 | if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; |
| 3313 | if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure; |
| 3314 | if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure; |
| 3315 | if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure; |
| 3316 | if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure; |
| 3317 | if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure; |
| 3318 | if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure; |
| 3319 | if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure; |
| 3320 | if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | Py_INCREF(&PyNullImporter_Type); |
| 3323 | PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type); |
| 3324 | return m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 3325 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | Py_XDECREF(m); |
| 3327 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3328 | } |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3329 | |
| 3330 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 3331 | /* API for embedding applications that want to add their own entries |
| 3332 | to the table of built-in modules. This should normally be called |
| 3333 | *before* Py_Initialize(). When the table resize fails, -1 is |
| 3334 | returned and the existing table is unchanged. |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3335 | |
| 3336 | After a similar function by Just van Rossum. */ |
| 3337 | |
| 3338 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3339 | PyImport_ExtendInittab(struct _inittab *newtab) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3341 | static struct _inittab *our_copy = NULL; |
| 3342 | struct _inittab *p; |
| 3343 | int i, n; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | /* Count the number of entries in both tables */ |
| 3346 | for (n = 0; newtab[n].name != NULL; n++) |
| 3347 | ; |
| 3348 | if (n == 0) |
| 3349 | return 0; /* Nothing to do */ |
| 3350 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) |
| 3351 | ; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3353 | /* Allocate new memory for the combined table */ |
| 3354 | p = our_copy; |
| 3355 | PyMem_RESIZE(p, struct _inittab, i+n+1); |
| 3356 | if (p == NULL) |
| 3357 | return -1; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3359 | /* Copy the tables into the new memory */ |
| 3360 | if (our_copy != PyImport_Inittab) |
| 3361 | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
| 3362 | PyImport_Inittab = our_copy = p; |
| 3363 | memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | return 0; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | /* Shorthand to add a single entry given a name and a function */ |
| 3369 | |
| 3370 | int |
Brett Cannon | a826f32 | 2009-04-02 03:41:46 +0000 | [diff] [blame] | 3371 | PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3372 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3373 | struct _inittab newtab[2]; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3375 | memset(newtab, '\0', sizeof newtab); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3377 | newtab[0].name = (char *)name; |
| 3378 | newtab[0].initfunc = initfunc; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3380 | return PyImport_ExtendInittab(newtab); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 3381 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3382 | |
| 3383 | #ifdef __cplusplus |
| 3384 | } |
| 3385 | #endif |