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 { |
Brett Cannon | 8923a4d | 2012-04-24 22:03:46 -0400 | [diff] [blame] | 271 | /* sys.path_hooks.insert(0, zipimporter) */ |
| 272 | err = PyList_Insert(path_hooks, 0, zipimporter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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); |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 786 | |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 787 | /* Execute a code object in a module and return the module object |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 788 | * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is |
| 789 | * removed from sys.modules, to avoid leaving damaged module objects |
| 790 | * in sys.modules. The caller may wish to restore the original |
| 791 | * module object (if any) in this case; PyImport_ReloadModule is an |
| 792 | * example. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 793 | * |
| 794 | * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer |
| 795 | * interface. The other two exist primarily for backward compatibility. |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 796 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 797 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 798 | PyImport_ExecCodeModule(char *name, PyObject *co) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | return PyImport_ExecCodeModuleWithPathnames( |
| 801 | name, co, (char *)NULL, (char *)NULL); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 805 | PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | return PyImport_ExecCodeModuleWithPathnames( |
| 808 | name, co, pathname, (char *)NULL); |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | PyObject * |
| 812 | PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | char *cpathname) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 814 | { |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 815 | PyObject *m = NULL; |
| 816 | PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL; |
| 817 | |
| 818 | nameobj = PyUnicode_FromString(name); |
| 819 | if (nameobj == NULL) |
| 820 | return NULL; |
| 821 | |
| 822 | if (pathname != NULL) { |
| 823 | pathobj = PyUnicode_DecodeFSDefault(pathname); |
| 824 | if (pathobj == NULL) |
| 825 | goto error; |
| 826 | } else |
| 827 | pathobj = NULL; |
| 828 | if (cpathname != NULL) { |
| 829 | cpathobj = PyUnicode_DecodeFSDefault(cpathname); |
| 830 | if (cpathobj == NULL) |
| 831 | goto error; |
| 832 | } else |
| 833 | cpathobj = NULL; |
| 834 | m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); |
| 835 | error: |
| 836 | Py_DECREF(nameobj); |
| 837 | Py_XDECREF(pathobj); |
| 838 | Py_XDECREF(cpathobj); |
| 839 | return m; |
| 840 | } |
| 841 | |
| 842 | PyObject* |
| 843 | PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, |
| 844 | PyObject *cpathname) |
| 845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | PyObject *modules = PyImport_GetModuleDict(); |
| 847 | PyObject *m, *d, *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 848 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 849 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | if (m == NULL) |
| 851 | return NULL; |
| 852 | /* If the module is being reloaded, we get the old module back |
| 853 | and re-use its dict to exec the new code. */ |
| 854 | d = PyModule_GetDict(m); |
| 855 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 856 | if (PyDict_SetItemString(d, "__builtins__", |
| 857 | PyEval_GetBuiltins()) != 0) |
| 858 | goto error; |
| 859 | } |
| 860 | /* Remember the filename as the __file__ attribute */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | if (pathname != NULL) { |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 862 | v = get_sourcefile(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 863 | if (v == NULL) |
| 864 | PyErr_Clear(); |
| 865 | } |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 866 | else |
| 867 | v = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | if (v == NULL) { |
| 869 | v = ((PyCodeObject *)co)->co_filename; |
| 870 | Py_INCREF(v); |
| 871 | } |
| 872 | if (PyDict_SetItemString(d, "__file__", v) != 0) |
| 873 | PyErr_Clear(); /* Not important enough to report */ |
| 874 | Py_DECREF(v); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | /* Remember the pyc path name as the __cached__ attribute. */ |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 877 | if (cpathname != NULL) |
| 878 | v = cpathname; |
| 879 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | v = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 881 | if (PyDict_SetItemString(d, "__cached__", v) != 0) |
| 882 | PyErr_Clear(); /* Not important enough to report */ |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 883 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 884 | v = PyEval_EvalCode(co, d, d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | if (v == NULL) |
| 886 | goto error; |
| 887 | Py_DECREF(v); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 888 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 889 | if ((m = PyDict_GetItem(modules, name)) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 891 | "Loaded module %R not found in sys.modules", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 892 | name); |
| 893 | return NULL; |
| 894 | } |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 895 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | return m; |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 899 | |
| 900 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | remove_module(name); |
| 902 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 906 | /* Like rightmost_sep, but operate on unicode objects. */ |
| 907 | static Py_ssize_t |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 908 | 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] | 909 | { |
| 910 | Py_ssize_t found, i; |
| 911 | Py_UCS4 c; |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 912 | for (found = -1, i = start; i < end; i++) { |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 913 | c = PyUnicode_READ_CHAR(o, i); |
| 914 | if (c == SEP |
| 915 | #ifdef ALTSEP |
| 916 | || c == ALTSEP |
| 917 | #endif |
| 918 | ) |
| 919 | { |
| 920 | found = i; |
| 921 | } |
| 922 | } |
| 923 | return found; |
| 924 | } |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 925 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 926 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 927 | /* Given a pathname to a Python byte compiled file, return the path to the |
| 928 | source file, if the path matches the PEP 3147 format. This does not check |
| 929 | for any file existence, however, if the pyc file name does not match PEP |
| 930 | 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] | 931 | the resulting path will always be shorter. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 933 | (...)/__pycache__/foo.<tag>.pyc -> (...)/foo.py */ |
| 934 | |
| 935 | static PyObject* |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 936 | make_source_pathname(PyObject *path) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 937 | { |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 938 | Py_ssize_t left, right, dot0, dot1, len; |
| 939 | Py_ssize_t i, j; |
| 940 | PyObject *result; |
| 941 | int kind; |
| 942 | void *data; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 943 | |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 944 | len = PyUnicode_GET_LENGTH(path); |
| 945 | if (len > MAXPATHLEN) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 946 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | /* Look back two slashes from the end. In between these two slashes |
| 949 | must be the string __pycache__ or this is not a PEP 3147 style |
| 950 | path. It's possible for there to be only one slash. |
| 951 | */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 952 | right = rightmost_sep_obj(path, 0, len); |
| 953 | if (right == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | return NULL; |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 955 | left = rightmost_sep_obj(path, 0, right); |
| 956 | if (left == -1) |
| 957 | left = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | else |
| 959 | left++; |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 960 | if (right-left != sizeof(CACHEDIR)-1) |
| 961 | return NULL; |
| 962 | for (i = 0; i < sizeof(CACHEDIR)-1; i++) |
| 963 | if (PyUnicode_READ_CHAR(path, left+i) != CACHEDIR[i]) |
| 964 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | /* Now verify that the path component to the right of the last slash |
| 967 | has two dots in it. |
| 968 | */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 969 | dot0 = PyUnicode_FindChar(path, '.', right+1, len, 1); |
| 970 | if (dot0 < 0) |
| 971 | return NULL; |
| 972 | dot1 = PyUnicode_FindChar(path, '.', dot0+1, len, 1); |
| 973 | if (dot1 < 0) |
| 974 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | /* Too many dots? */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 976 | if (PyUnicode_FindChar(path, '.', dot1+1, len, 1) != -1) |
| 977 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | /* This is a PEP 3147 path. Start by copying everything from the |
| 980 | start of pathname up to and including the leftmost slash. Then |
| 981 | copy the file's basename, removing the magic tag and adding a .py |
| 982 | suffix. |
| 983 | */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 984 | result = PyUnicode_New(left + (dot0-right) + 2, |
| 985 | PyUnicode_MAX_CHAR_VALUE(path)); |
| 986 | if (!result) |
| 987 | return NULL; |
| 988 | kind = PyUnicode_KIND(result); |
| 989 | data = PyUnicode_DATA(result); |
| 990 | PyUnicode_CopyCharacters(result, 0, path, 0, (i = left)); |
| 991 | PyUnicode_CopyCharacters(result, left, path, right+1, |
| 992 | (j = dot0-right)); |
| 993 | PyUnicode_WRITE(kind, data, i+j, 'p'); |
| 994 | PyUnicode_WRITE(kind, data, i+j+1, 'y'); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 995 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 996 | return result; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1000 | static void |
| 1001 | update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) |
| 1002 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | PyObject *constants, *tmp; |
| 1004 | Py_ssize_t i, n; |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | if (PyUnicode_Compare(co->co_filename, oldname)) |
| 1007 | return; |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | tmp = co->co_filename; |
| 1010 | co->co_filename = newname; |
| 1011 | Py_INCREF(co->co_filename); |
| 1012 | Py_DECREF(tmp); |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | constants = co->co_consts; |
| 1015 | n = PyTuple_GET_SIZE(constants); |
| 1016 | for (i = 0; i < n; i++) { |
| 1017 | tmp = PyTuple_GET_ITEM(constants, i); |
| 1018 | if (PyCode_Check(tmp)) |
| 1019 | update_code_filenames((PyCodeObject *)tmp, |
| 1020 | oldname, newname); |
| 1021 | } |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 1024 | static void |
| 1025 | update_compiled_module(PyCodeObject *co, PyObject *newname) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1026 | { |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 1027 | PyObject *oldname; |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1028 | |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 1029 | if (PyUnicode_Compare(co->co_filename, newname) == 0) |
| 1030 | return; |
Hirokazu Yamamoto | 4f447fb | 2009-03-04 01:52:10 +0000 | [diff] [blame] | 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | oldname = co->co_filename; |
| 1033 | Py_INCREF(oldname); |
| 1034 | update_code_filenames(co, oldname, newname); |
| 1035 | Py_DECREF(oldname); |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 1036 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1037 | |
Brett Cannon | 442c9b9 | 2011-03-23 16:14:42 -0700 | [diff] [blame] | 1038 | static PyObject * |
| 1039 | imp_fix_co_filename(PyObject *self, PyObject *args) |
| 1040 | { |
| 1041 | PyObject *co; |
| 1042 | PyObject *file_path; |
| 1043 | |
| 1044 | if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path)) |
| 1045 | return NULL; |
| 1046 | |
| 1047 | if (!PyCode_Check(co)) { |
| 1048 | PyErr_SetString(PyExc_TypeError, |
| 1049 | "first argument must be a code object"); |
| 1050 | return NULL; |
| 1051 | } |
| 1052 | |
| 1053 | if (!PyUnicode_Check(file_path)) { |
| 1054 | PyErr_SetString(PyExc_TypeError, |
| 1055 | "second argument must be a string"); |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | |
| 1059 | update_compiled_module((PyCodeObject*)co, file_path); |
| 1060 | |
| 1061 | Py_RETURN_NONE; |
| 1062 | } |
| 1063 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1064 | |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1065 | /* Get source file -> unicode or None |
| 1066 | * Returns the path to the py file if available, else the given path |
| 1067 | */ |
| 1068 | static PyObject * |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1069 | get_sourcefile(PyObject *filename) |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1070 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1072 | Py_UCS4 *fileuni; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1073 | PyObject *py; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | struct stat statbuf; |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1075 | int err; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1076 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1077 | len = PyUnicode_GET_LENGTH(filename); |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1078 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | Py_RETURN_NONE; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1080 | |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1081 | /* don't match *.pyc or *.pyo? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1082 | fileuni = PyUnicode_AsUCS4Copy(filename); |
| 1083 | if (!fileuni) |
| 1084 | return NULL; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1085 | if (len < 5 |
| 1086 | || fileuni[len-4] != '.' |
| 1087 | || (fileuni[len-3] != 'p' && fileuni[len-3] != 'P') |
| 1088 | || (fileuni[len-2] != 'y' && fileuni[len-2] != 'Y')) |
| 1089 | goto unchanged; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | /* Start by trying to turn PEP 3147 path into source path. If that |
| 1092 | * fails, just chop off the trailing character, i.e. legacy pyc path |
| 1093 | * to py. |
| 1094 | */ |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1095 | py = make_source_pathname(filename); |
| 1096 | if (py == NULL) { |
| 1097 | PyErr_Clear(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1098 | py = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, fileuni, len - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | } |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1100 | if (py == NULL) |
| 1101 | goto error; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1102 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1103 | err = _Py_stat(py, &statbuf); |
| 1104 | if (err == -2) |
| 1105 | goto error; |
| 1106 | if (err == 0 && S_ISREG(statbuf.st_mode)) { |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 1107 | PyMem_Free(fileuni); |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1108 | return py; |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 1109 | } |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1110 | Py_DECREF(py); |
| 1111 | goto unchanged; |
| 1112 | |
| 1113 | error: |
| 1114 | PyErr_Clear(); |
| 1115 | unchanged: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1116 | PyMem_Free(fileuni); |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1117 | Py_INCREF(filename); |
| 1118 | return filename; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1119 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1120 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1121 | /* Forward */ |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1122 | static struct _frozen * find_frozen(PyObject *); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1123 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1124 | |
| 1125 | /* Helper to test for built-in module */ |
| 1126 | |
| 1127 | static int |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1128 | is_builtin(PyObject *name) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1129 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1130 | int i, cmp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1132 | cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name); |
| 1133 | if (cmp == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | if (PyImport_Inittab[i].initfunc == NULL) |
| 1135 | return -1; |
| 1136 | else |
| 1137 | return 1; |
| 1138 | } |
| 1139 | } |
| 1140 | return 0; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1143 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1144 | /* Return an importer object for a sys.path/pkg.__path__ item 'p', |
| 1145 | possibly by fetching it from the path_importer_cache dict. If it |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1146 | 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] | 1147 | that can handle the path item. Return None if no hook could; |
| 1148 | this tells our caller it should fall back to the builtin |
| 1149 | import mechanism. Cache the result in path_importer_cache. |
| 1150 | Returns a borrowed reference. */ |
| 1151 | |
| 1152 | static PyObject * |
| 1153 | get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | PyObject *p) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | PyObject *importer; |
| 1157 | Py_ssize_t j, nhooks; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1159 | /* These conditions are the caller's responsibility: */ |
| 1160 | assert(PyList_Check(path_hooks)); |
| 1161 | assert(PyDict_Check(path_importer_cache)); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | nhooks = PyList_Size(path_hooks); |
| 1164 | if (nhooks < 0) |
| 1165 | return NULL; /* Shouldn't happen */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | importer = PyDict_GetItem(path_importer_cache, p); |
| 1168 | if (importer != NULL) |
| 1169 | return importer; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | /* set path_importer_cache[p] to None to avoid recursion */ |
| 1172 | if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) |
| 1173 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | for (j = 0; j < nhooks; j++) { |
| 1176 | PyObject *hook = PyList_GetItem(path_hooks, j); |
| 1177 | if (hook == NULL) |
| 1178 | return NULL; |
| 1179 | importer = PyObject_CallFunctionObjArgs(hook, p, NULL); |
| 1180 | if (importer != NULL) |
| 1181 | break; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 1184 | return NULL; |
| 1185 | } |
| 1186 | PyErr_Clear(); |
| 1187 | } |
| 1188 | if (importer == NULL) { |
| 1189 | importer = PyObject_CallFunctionObjArgs( |
| 1190 | (PyObject *)&PyNullImporter_Type, p, NULL |
| 1191 | ); |
| 1192 | if (importer == NULL) { |
| 1193 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 1194 | PyErr_Clear(); |
| 1195 | return Py_None; |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | if (importer != NULL) { |
| 1200 | int err = PyDict_SetItem(path_importer_cache, p, importer); |
| 1201 | Py_DECREF(importer); |
| 1202 | if (err != 0) |
| 1203 | return NULL; |
| 1204 | } |
| 1205 | return importer; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1208 | PyAPI_FUNC(PyObject *) |
| 1209 | PyImport_GetImporter(PyObject *path) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) { |
| 1213 | if ((path_hooks = PySys_GetObject("path_hooks"))) { |
| 1214 | importer = get_path_importer(path_importer_cache, |
| 1215 | path_hooks, path); |
| 1216 | } |
| 1217 | } |
| 1218 | Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */ |
| 1219 | return importer; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1222 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1223 | #ifdef MS_COREDLL |
Victor Stinner | 4d6c1c4 | 2011-03-08 23:49:04 +0100 | [diff] [blame] | 1224 | extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **, |
| 1225 | PyObject **p_path); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1226 | #endif |
| 1227 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1228 | |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1229 | static int init_builtin(PyObject *); /* Forward */ |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1230 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1231 | /* Initialize a built-in module. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1232 | 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] | 1233 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1234 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1235 | static int |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1236 | init_builtin(PyObject *name) |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | struct _inittab *p; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1239 | |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1240 | if (_PyImport_FindExtensionObject(name, name) != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | return 1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | for (p = PyImport_Inittab; p->name != NULL; p++) { |
| 1244 | PyObject *mod; |
Antoine Pitrou | 6c40eb7 | 2012-01-18 20:16:09 +0100 | [diff] [blame] | 1245 | PyModuleDef *def; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1246 | if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | if (p->initfunc == NULL) { |
| 1248 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1249 | "Cannot re-init internal module %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | name); |
| 1251 | return -1; |
| 1252 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | mod = (*p->initfunc)(); |
| 1254 | if (mod == 0) |
| 1255 | return -1; |
Antoine Pitrou | 6c40eb7 | 2012-01-18 20:16:09 +0100 | [diff] [blame] | 1256 | /* Remember pointer to module init function. */ |
| 1257 | def = PyModule_GetDef(mod); |
| 1258 | def->m_base.m_init = p->initfunc; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1259 | if (_PyImport_FixupExtensionObject(mod, name, name) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | return -1; |
| 1261 | /* FixupExtension has put the module into sys.modules, |
| 1262 | so we can release our own reference. */ |
| 1263 | Py_DECREF(mod); |
| 1264 | return 1; |
| 1265 | } |
| 1266 | } |
| 1267 | return 0; |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1268 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1269 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1270 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1271 | /* Frozen modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1272 | |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1273 | static struct _frozen * |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1274 | find_frozen(PyObject *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1275 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | struct _frozen *p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1277 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1278 | if (name == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | return NULL; |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | for (p = PyImport_FrozenModules; ; p++) { |
| 1282 | if (p->name == NULL) |
| 1283 | return NULL; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1284 | if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | break; |
| 1286 | } |
| 1287 | return p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1290 | static PyObject * |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1291 | get_frozen_object(PyObject *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | struct _frozen *p = find_frozen(name); |
| 1294 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | if (p == NULL) { |
| 1297 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1298 | "No such frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | name); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | if (p->code == NULL) { |
| 1303 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1304 | "Excluded frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | name); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | size = p->size; |
| 1309 | if (size < 0) |
| 1310 | size = -size; |
| 1311 | return PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1314 | static PyObject * |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1315 | is_frozen_package(PyObject *name) |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | struct _frozen *p = find_frozen(name); |
| 1318 | int size; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | if (p == NULL) { |
| 1321 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1322 | "No such frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | name); |
| 1324 | return NULL; |
| 1325 | } |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | size = p->size; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | if (size < 0) |
| 1330 | Py_RETURN_TRUE; |
| 1331 | else |
| 1332 | Py_RETURN_FALSE; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1336 | /* Initialize a frozen module. |
Brett Cannon | 3c2ac44 | 2009-03-08 20:49:47 +0000 | [diff] [blame] | 1337 | 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] | 1338 | an exception set if the initialization failed. |
| 1339 | This function is also used from frozenmain.c */ |
Guido van Rossum | 0b34490 | 1995-02-07 15:35:27 +0000 | [diff] [blame] | 1340 | |
| 1341 | int |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1342 | PyImport_ImportFrozenModuleObject(PyObject *name) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1343 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1344 | struct _frozen *p; |
| 1345 | PyObject *co, *m, *path; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | int ispackage; |
| 1347 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1348 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1349 | p = find_frozen(name); |
| 1350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | if (p == NULL) |
| 1352 | return 0; |
| 1353 | if (p->code == NULL) { |
| 1354 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1355 | "Excluded frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | name); |
| 1357 | return -1; |
| 1358 | } |
| 1359 | size = p->size; |
| 1360 | ispackage = (size < 0); |
| 1361 | if (ispackage) |
| 1362 | size = -size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | co = PyMarshal_ReadObjectFromString((char *)p->code, size); |
| 1364 | if (co == NULL) |
| 1365 | return -1; |
| 1366 | if (!PyCode_Check(co)) { |
| 1367 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1368 | "frozen object %R is not a code object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | name); |
| 1370 | goto err_return; |
| 1371 | } |
| 1372 | if (ispackage) { |
| 1373 | /* Set __path__ to the package name */ |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1374 | PyObject *d, *l; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | int err; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1376 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | if (m == NULL) |
| 1378 | goto err_return; |
| 1379 | d = PyModule_GetDict(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | l = PyList_New(1); |
| 1381 | if (l == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1382 | goto err_return; |
| 1383 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1384 | Py_INCREF(name); |
| 1385 | PyList_SET_ITEM(l, 0, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | err = PyDict_SetItemString(d, "__path__", l); |
| 1387 | Py_DECREF(l); |
| 1388 | if (err != 0) |
| 1389 | goto err_return; |
| 1390 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1391 | path = PyUnicode_FromString("<frozen>"); |
| 1392 | if (path == NULL) |
| 1393 | goto err_return; |
| 1394 | m = PyImport_ExecCodeModuleObject(name, co, path, NULL); |
| 1395 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | if (m == NULL) |
| 1397 | goto err_return; |
| 1398 | Py_DECREF(co); |
| 1399 | Py_DECREF(m); |
| 1400 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1401 | err_return: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | Py_DECREF(co); |
| 1403 | return -1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1404 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1405 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1406 | int |
| 1407 | PyImport_ImportFrozenModule(char *name) |
| 1408 | { |
| 1409 | PyObject *nameobj; |
| 1410 | int ret; |
| 1411 | nameobj = PyUnicode_InternFromString(name); |
| 1412 | if (nameobj == NULL) |
| 1413 | return -1; |
| 1414 | ret = PyImport_ImportFrozenModuleObject(nameobj); |
| 1415 | Py_DECREF(nameobj); |
| 1416 | return ret; |
| 1417 | } |
| 1418 | |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1419 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1420 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1421 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1422 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1423 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1424 | PyImport_ImportModule(const char *name) |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | PyObject *pname; |
| 1427 | PyObject *result; |
Marc-André Lemburg | 3c61c35 | 2001-02-09 19:40:15 +0000 | [diff] [blame] | 1428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | pname = PyUnicode_FromString(name); |
| 1430 | if (pname == NULL) |
| 1431 | return NULL; |
| 1432 | result = PyImport_Import(pname); |
| 1433 | Py_DECREF(pname); |
| 1434 | return result; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1437 | /* Import a module without blocking |
| 1438 | * |
| 1439 | * At first it tries to fetch the module from sys.modules. If the module was |
| 1440 | * never loaded before it loads it with PyImport_ImportModule() unless another |
| 1441 | * thread holds the import lock. In the latter case the function raises an |
| 1442 | * ImportError instead of blocking. |
| 1443 | * |
| 1444 | * Returns the module object with incremented ref count. |
| 1445 | */ |
| 1446 | PyObject * |
| 1447 | PyImport_ImportModuleNoBlock(const char *name) |
| 1448 | { |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 1449 | PyObject *nameobj, *modules, *result; |
Victor Stinner | 0af0306 | 2011-09-02 00:11:43 +0200 | [diff] [blame] | 1450 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | long me; |
Victor Stinner | 0af0306 | 2011-09-02 00:11:43 +0200 | [diff] [blame] | 1452 | #endif |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | /* Try to get the module from sys.modules[name] */ |
| 1455 | modules = PyImport_GetModuleDict(); |
| 1456 | if (modules == NULL) |
| 1457 | return NULL; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1458 | |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 1459 | nameobj = PyUnicode_FromString(name); |
| 1460 | if (nameobj == NULL) |
| 1461 | return NULL; |
| 1462 | result = PyDict_GetItem(modules, nameobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | if (result != NULL) { |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 1464 | Py_DECREF(nameobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | Py_INCREF(result); |
| 1466 | return result; |
| 1467 | } |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 1468 | PyErr_Clear(); |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 1469 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | /* check the import lock |
| 1471 | * me might be -1 but I ignore the error here, the lock function |
| 1472 | * takes care of the problem */ |
| 1473 | me = PyThread_get_thread_ident(); |
| 1474 | if (import_lock_thread == -1 || import_lock_thread == me) { |
| 1475 | /* no thread or me is holding the lock */ |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 1476 | result = PyImport_Import(nameobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1477 | } |
| 1478 | else { |
| 1479 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | c24c810 | 2011-03-13 22:38:06 -0400 | [diff] [blame] | 1480 | "Failed to import %R because the import lock" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | "is held by another thread.", |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 1482 | nameobj); |
| 1483 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | } |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 1485 | #else |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 1486 | result = PyImport_Import(nameobj); |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 1487 | #endif |
Victor Stinner | 2e5f11a | 2011-03-13 21:57:27 -0400 | [diff] [blame] | 1488 | Py_DECREF(nameobj); |
| 1489 | return result; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1492 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1493 | PyObject * |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1494 | PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals, |
| 1495 | PyObject *locals, PyObject *given_fromlist, |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 1496 | int level) |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1497 | { |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1498 | _Py_IDENTIFIER(__import__); |
| 1499 | _Py_IDENTIFIER(__package__); |
| 1500 | _Py_IDENTIFIER(__path__); |
| 1501 | _Py_IDENTIFIER(__name__); |
| 1502 | _Py_IDENTIFIER(_find_and_load); |
| 1503 | _Py_IDENTIFIER(_handle_fromlist); |
| 1504 | _Py_static_string(single_dot, "."); |
| 1505 | PyObject *abs_name = NULL; |
| 1506 | PyObject *builtins_import = NULL; |
| 1507 | PyObject *final_mod = NULL; |
| 1508 | PyObject *mod = NULL; |
| 1509 | PyObject *package = NULL; |
| 1510 | PyObject *globals = NULL; |
| 1511 | PyObject *fromlist = NULL; |
| 1512 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 1513 | |
| 1514 | /* Make sure to use default values so as to not have |
| 1515 | PyObject_CallMethodObjArgs() truncate the parameter list because of a |
| 1516 | NULL argument. */ |
| 1517 | if (given_globals == NULL) { |
| 1518 | globals = PyDict_New(); |
| 1519 | if (globals == NULL) { |
| 1520 | goto error; |
| 1521 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1522 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1523 | else { |
| 1524 | /* Only have to care what given_globals is if it will be used |
| 1525 | fortsomething. */ |
| 1526 | if (level > 0 && !PyDict_Check(given_globals)) { |
| 1527 | PyErr_SetString(PyExc_TypeError, "globals must be a dict"); |
| 1528 | goto error; |
| 1529 | } |
| 1530 | globals = given_globals; |
| 1531 | Py_INCREF(globals); |
| 1532 | } |
| 1533 | |
| 1534 | if (given_fromlist == NULL) { |
| 1535 | fromlist = PyList_New(0); |
| 1536 | if (fromlist == NULL) { |
| 1537 | goto error; |
| 1538 | } |
| 1539 | } |
| 1540 | else { |
| 1541 | fromlist = given_fromlist; |
| 1542 | Py_INCREF(fromlist); |
| 1543 | } |
| 1544 | if (name == NULL) { |
| 1545 | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
| 1546 | goto error; |
| 1547 | } |
| 1548 | |
| 1549 | /* The below code is importlib.__import__() & _gcd_import(), ported to C |
| 1550 | for added performance. */ |
| 1551 | |
| 1552 | if (!PyUnicode_Check(name)) { |
| 1553 | PyErr_SetString(PyExc_TypeError, "module name must be a string"); |
| 1554 | goto error; |
| 1555 | } |
| 1556 | else if (PyUnicode_READY(name) < 0) { |
| 1557 | goto error; |
| 1558 | } |
| 1559 | if (level < 0) { |
| 1560 | PyErr_SetString(PyExc_ValueError, "level must be >= 0"); |
| 1561 | goto error; |
| 1562 | } |
| 1563 | else if (level > 0) { |
| 1564 | package = _PyDict_GetItemId(globals, &PyId___package__); |
| 1565 | if (package != NULL && package != Py_None) { |
| 1566 | Py_INCREF(package); |
| 1567 | if (!PyUnicode_Check(package)) { |
| 1568 | PyErr_SetString(PyExc_TypeError, "package must be a string"); |
| 1569 | goto error; |
| 1570 | } |
| 1571 | } |
| 1572 | else { |
Brett Cannon | 273323c | 2012-04-17 19:05:11 -0400 | [diff] [blame] | 1573 | package = _PyDict_GetItemId(globals, &PyId___name__); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1574 | if (package == NULL) { |
Brett Cannon | 273323c | 2012-04-17 19:05:11 -0400 | [diff] [blame] | 1575 | PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1576 | goto error; |
| 1577 | } |
| 1578 | else if (!PyUnicode_Check(package)) { |
| 1579 | PyErr_SetString(PyExc_TypeError, "__name__ must be a string"); |
| 1580 | } |
| 1581 | Py_INCREF(package); |
| 1582 | |
| 1583 | if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) { |
Brett Cannon | 740fce0 | 2012-04-14 14:23:49 -0400 | [diff] [blame] | 1584 | PyObject *partition = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1585 | PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot); |
| 1586 | if (borrowed_dot == NULL) { |
| 1587 | goto error; |
| 1588 | } |
Brett Cannon | 740fce0 | 2012-04-14 14:23:49 -0400 | [diff] [blame] | 1589 | partition = PyUnicode_RPartition(package, borrowed_dot); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1590 | Py_DECREF(package); |
| 1591 | if (partition == NULL) { |
| 1592 | goto error; |
| 1593 | } |
| 1594 | package = PyTuple_GET_ITEM(partition, 0); |
| 1595 | Py_INCREF(package); |
| 1596 | Py_DECREF(partition); |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | if (PyDict_GetItem(interp->modules, package) == NULL) { |
| 1601 | PyErr_Format(PyExc_SystemError, |
| 1602 | "Parent module %R not loaded, cannot perform relative " |
| 1603 | "import", package); |
| 1604 | goto error; |
| 1605 | } |
| 1606 | } |
| 1607 | else { /* level == 0 */ |
| 1608 | if (PyUnicode_GET_LENGTH(name) == 0) { |
| 1609 | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
| 1610 | goto error; |
| 1611 | } |
| 1612 | package = Py_None; |
| 1613 | Py_INCREF(package); |
| 1614 | } |
| 1615 | |
| 1616 | if (level > 0) { |
| 1617 | Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package); |
| 1618 | PyObject *base = NULL; |
| 1619 | int level_up = 1; |
| 1620 | |
| 1621 | for (level_up = 1; level_up < level; level_up += 1) { |
| 1622 | last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1); |
| 1623 | if (last_dot == -2) { |
| 1624 | goto error; |
| 1625 | } |
| 1626 | else if (last_dot == -1) { |
| 1627 | PyErr_SetString(PyExc_ValueError, |
| 1628 | "attempted relative import beyond top-level " |
| 1629 | "package"); |
| 1630 | goto error; |
| 1631 | } |
| 1632 | } |
| 1633 | base = PyUnicode_Substring(package, 0, last_dot); |
| 1634 | if (PyUnicode_GET_LENGTH(name) > 0) { |
Antoine Pitrou | 538ba2a | 2012-04-16 21:52:45 +0200 | [diff] [blame] | 1635 | PyObject *borrowed_dot, *seq = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1636 | |
| 1637 | borrowed_dot = _PyUnicode_FromId(&single_dot); |
Antoine Pitrou | 538ba2a | 2012-04-16 21:52:45 +0200 | [diff] [blame] | 1638 | seq = PyTuple_Pack(2, base, name); |
| 1639 | Py_DECREF(base); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1640 | if (borrowed_dot == NULL || seq == NULL) { |
| 1641 | goto error; |
| 1642 | } |
| 1643 | |
| 1644 | abs_name = PyUnicode_Join(borrowed_dot, seq); |
| 1645 | Py_DECREF(seq); |
| 1646 | if (abs_name == NULL) { |
| 1647 | goto error; |
| 1648 | } |
| 1649 | } |
| 1650 | else { |
| 1651 | abs_name = base; |
| 1652 | } |
| 1653 | } |
| 1654 | else { |
| 1655 | abs_name = name; |
| 1656 | Py_INCREF(abs_name); |
| 1657 | } |
| 1658 | |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1659 | #ifdef WITH_THREAD |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1660 | _PyImport_AcquireLock(); |
| 1661 | #endif |
| 1662 | /* From this point forward, goto error_with_unlock! */ |
| 1663 | if (PyDict_Check(globals)) { |
| 1664 | builtins_import = _PyDict_GetItemId(globals, &PyId___import__); |
| 1665 | } |
| 1666 | if (builtins_import == NULL) { |
| 1667 | builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__); |
| 1668 | if (builtins_import == NULL) { |
| 1669 | Py_FatalError("__import__ missing"); |
| 1670 | } |
| 1671 | } |
| 1672 | Py_INCREF(builtins_import); |
| 1673 | |
| 1674 | mod = PyDict_GetItem(interp->modules, abs_name); |
| 1675 | if (mod == Py_None) { |
Brett Cannon | 27fc528 | 2012-04-15 14:15:31 -0400 | [diff] [blame] | 1676 | PyObject *msg = PyUnicode_FromFormat("import of %R halted; " |
| 1677 | "None in sys.modules", abs_name); |
| 1678 | if (msg != NULL) { |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 1679 | PyErr_SetImportError(msg, abs_name, NULL); |
| 1680 | Py_DECREF(msg); |
Brett Cannon | 27fc528 | 2012-04-15 14:15:31 -0400 | [diff] [blame] | 1681 | } |
Antoine Pitrou | 71382cb | 2012-04-16 18:48:49 +0200 | [diff] [blame] | 1682 | mod = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1683 | goto error_with_unlock; |
| 1684 | } |
| 1685 | else if (mod != NULL) { |
| 1686 | Py_INCREF(mod); |
| 1687 | } |
| 1688 | else { |
| 1689 | mod = _PyObject_CallMethodObjIdArgs(interp->importlib, |
| 1690 | &PyId__find_and_load, abs_name, |
| 1691 | builtins_import, NULL); |
| 1692 | if (mod == NULL) { |
| 1693 | goto error_with_unlock; |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | if (PyObject_Not(fromlist)) { |
| 1698 | if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) { |
| 1699 | PyObject *front = NULL; |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1700 | PyObject *partition = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1701 | PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot); |
| 1702 | |
| 1703 | if (borrowed_dot == NULL) { |
| 1704 | goto error_with_unlock; |
| 1705 | } |
| 1706 | |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1707 | partition = PyUnicode_Partition(name, borrowed_dot); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1708 | if (partition == NULL) { |
| 1709 | goto error_with_unlock; |
| 1710 | } |
| 1711 | |
| 1712 | front = PyTuple_GET_ITEM(partition, 0); |
| 1713 | Py_INCREF(front); |
| 1714 | Py_DECREF(partition); |
| 1715 | |
| 1716 | if (level == 0) { |
Benjamin Peterson | d76bc7a | 2012-04-18 10:55:43 -0400 | [diff] [blame] | 1717 | final_mod = PyDict_GetItem(interp->modules, front); |
Brett Cannon | 881535b | 2012-04-15 15:24:04 -0400 | [diff] [blame] | 1718 | Py_DECREF(front); |
| 1719 | if (final_mod == NULL) { |
Benjamin Peterson | d76bc7a | 2012-04-18 10:55:43 -0400 | [diff] [blame] | 1720 | PyErr_Format(PyExc_KeyError, |
| 1721 | "%R not in sys.modules as expected", front); |
Brett Cannon | 881535b | 2012-04-15 15:24:04 -0400 | [diff] [blame] | 1722 | } |
Benjamin Peterson | d76bc7a | 2012-04-18 10:55:43 -0400 | [diff] [blame] | 1723 | else { |
| 1724 | Py_INCREF(final_mod); |
| 1725 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1726 | } |
| 1727 | else { |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 1728 | Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) - |
| 1729 | PyUnicode_GET_LENGTH(front); |
| 1730 | Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name); |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 1731 | PyObject *to_return = PyUnicode_Substring(abs_name, 0, |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1732 | abs_name_len - cut_off); |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 1733 | Py_DECREF(front); |
| 1734 | if (to_return == NULL) { |
| 1735 | goto error_with_unlock; |
| 1736 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1737 | |
| 1738 | final_mod = PyDict_GetItem(interp->modules, to_return); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1739 | Py_DECREF(to_return); |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 1740 | if (final_mod == NULL) { |
| 1741 | PyErr_Format(PyExc_KeyError, |
| 1742 | "%R not in sys.modules as expected", |
| 1743 | to_return); |
| 1744 | } |
| 1745 | else { |
| 1746 | Py_INCREF(final_mod); |
| 1747 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | else { |
| 1751 | final_mod = mod; |
| 1752 | Py_INCREF(mod); |
| 1753 | } |
| 1754 | } |
| 1755 | else { |
| 1756 | final_mod = _PyObject_CallMethodObjIdArgs(interp->importlib, |
| 1757 | &PyId__handle_fromlist, mod, |
| 1758 | fromlist, builtins_import, |
| 1759 | NULL); |
| 1760 | } |
| 1761 | error_with_unlock: |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1762 | #ifdef WITH_THREAD |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1763 | if (_PyImport_ReleaseLock() < 0) { |
| 1764 | PyErr_SetString(PyExc_RuntimeError, "not holding the import lock"); |
| 1765 | } |
| 1766 | #endif |
| 1767 | error: |
| 1768 | Py_XDECREF(abs_name); |
| 1769 | Py_XDECREF(builtins_import); |
| 1770 | Py_XDECREF(mod); |
| 1771 | Py_XDECREF(package); |
| 1772 | Py_XDECREF(globals); |
| 1773 | Py_XDECREF(fromlist); |
| 1774 | return final_mod; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 1777 | PyObject * |
Benjamin Peterson | 04778a8 | 2011-05-25 09:29:00 -0500 | [diff] [blame] | 1778 | PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 1779 | PyObject *fromlist, int level) |
| 1780 | { |
| 1781 | PyObject *nameobj, *mod; |
| 1782 | nameobj = PyUnicode_FromString(name); |
| 1783 | if (nameobj == NULL) |
| 1784 | return NULL; |
| 1785 | mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals, |
| 1786 | fromlist, level); |
| 1787 | Py_DECREF(nameobj); |
| 1788 | return mod; |
| 1789 | } |
| 1790 | |
| 1791 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1792 | /* Re-import a module of any kind and return its module object, WITH |
| 1793 | INCREMENTED REFERENCE COUNT */ |
| 1794 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1795 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1796 | PyImport_ReloadModule(PyObject *m) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1797 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 1799 | PyObject *modules_reloading = interp->modules_reloading; |
| 1800 | PyObject *modules = PyImport_GetModuleDict(); |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 1801 | PyObject *loader = NULL, *existing_m = NULL; |
| 1802 | PyObject *name; |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1803 | Py_ssize_t subname_start; |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1804 | PyObject *newm = NULL; |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 1805 | _Py_IDENTIFIER(__loader__); |
| 1806 | _Py_IDENTIFIER(load_module); |
Brett Cannon | 9a5b25a | 2010-03-01 02:09:17 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | if (modules_reloading == NULL) { |
| 1809 | Py_FatalError("PyImport_ReloadModule: " |
| 1810 | "no modules_reloading dictionary!"); |
| 1811 | return NULL; |
| 1812 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | if (m == NULL || !PyModule_Check(m)) { |
| 1815 | PyErr_SetString(PyExc_TypeError, |
| 1816 | "reload() argument must be module"); |
| 1817 | return NULL; |
| 1818 | } |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1819 | name = PyModule_GetNameObject(m); |
| 1820 | if (name == NULL || PyUnicode_READY(name) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1821 | return NULL; |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1822 | if (m != PyDict_GetItem(modules, name)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1823 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 98dbba5 | 2011-03-14 15:15:47 -0400 | [diff] [blame] | 1824 | "reload(): module %R not in sys.modules", |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1825 | name); |
| 1826 | Py_DECREF(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | return NULL; |
| 1828 | } |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1829 | existing_m = PyDict_GetItem(modules_reloading, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | if (existing_m != NULL) { |
| 1831 | /* Due to a recursive reload, this module is already |
| 1832 | being reloaded. */ |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1833 | Py_DECREF(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | Py_INCREF(existing_m); |
| 1835 | return existing_m; |
| 1836 | } |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1837 | if (PyDict_SetItem(modules_reloading, name, m) < 0) { |
| 1838 | Py_DECREF(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | return NULL; |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1840 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1841 | |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1842 | subname_start = PyUnicode_FindChar(name, '.', 0, |
| 1843 | PyUnicode_GET_LENGTH(name), -1); |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 1844 | if (subname_start != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | PyObject *parentname, *parent; |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1846 | parentname = PyUnicode_Substring(name, 0, subname_start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | if (parentname == NULL) { |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1848 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | } |
| 1850 | parent = PyDict_GetItem(modules, parentname); |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 1851 | Py_XDECREF(parent); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | if (parent == NULL) { |
| 1853 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 98dbba5 | 2011-03-14 15:15:47 -0400 | [diff] [blame] | 1854 | "reload(): parent %R not in sys.modules", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1855 | parentname); |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1856 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | } |
Phillip J. Eby | 7ec642a | 2004-09-23 04:37:36 +0000 | [diff] [blame] | 1859 | |
Brett Cannon | 8a1d04c | 2012-04-15 17:56:09 -0400 | [diff] [blame] | 1860 | loader = _PyObject_GetAttrId(m, &PyId___loader__); |
| 1861 | if (loader == NULL) { |
| 1862 | goto error; |
| 1863 | } |
| 1864 | newm = _PyObject_CallMethodId(loader, &PyId_load_module, "O", name); |
| 1865 | Py_DECREF(loader); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1866 | if (newm == NULL) { |
| 1867 | /* load_module probably removed name from modules because of |
| 1868 | * the error. Put back the original module object. We're |
| 1869 | * going to return NULL in this case regardless of whether |
| 1870 | * replacing name succeeds, so the return value is ignored. |
| 1871 | */ |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1872 | PyDict_SetItem(modules, name, m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | } |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1874 | |
| 1875 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | imp_modules_reloading_clear(); |
Martin v. Löwis | 796ea53 | 2011-10-30 09:07:07 +0100 | [diff] [blame] | 1877 | Py_DECREF(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | return newm; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1882 | /* Higher-level import emulator which emulates the "import" statement |
| 1883 | more accurately -- it invokes the __import__() function from the |
| 1884 | builtins of the current globals. This means that the import is |
| 1885 | done using whatever import hooks are installed in the current |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1886 | environment. |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 1887 | A dummy list ["__doc__"] is passed as the 4th argument so that |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 1888 | e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 1889 | will return <module "gencache"> instead of <module "win32com">. */ |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1890 | |
| 1891 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1892 | PyImport_Import(PyObject *module_name) |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | static PyObject *silly_list = NULL; |
| 1895 | static PyObject *builtins_str = NULL; |
| 1896 | static PyObject *import_str = NULL; |
| 1897 | PyObject *globals = NULL; |
| 1898 | PyObject *import = NULL; |
| 1899 | PyObject *builtins = NULL; |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1900 | PyObject *modules = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1901 | PyObject *r = NULL; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | /* Initialize constant string objects */ |
| 1904 | if (silly_list == NULL) { |
| 1905 | import_str = PyUnicode_InternFromString("__import__"); |
| 1906 | if (import_str == NULL) |
| 1907 | return NULL; |
| 1908 | builtins_str = PyUnicode_InternFromString("__builtins__"); |
| 1909 | if (builtins_str == NULL) |
| 1910 | return NULL; |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1911 | silly_list = PyList_New(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | if (silly_list == NULL) |
| 1913 | return NULL; |
| 1914 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1916 | /* Get the builtins from current globals */ |
| 1917 | globals = PyEval_GetGlobals(); |
| 1918 | if (globals != NULL) { |
| 1919 | Py_INCREF(globals); |
| 1920 | builtins = PyObject_GetItem(globals, builtins_str); |
| 1921 | if (builtins == NULL) |
| 1922 | goto err; |
| 1923 | } |
| 1924 | else { |
| 1925 | /* No globals -- use standard builtins, and fake globals */ |
| 1926 | builtins = PyImport_ImportModuleLevel("builtins", |
| 1927 | NULL, NULL, NULL, 0); |
| 1928 | if (builtins == NULL) |
| 1929 | return NULL; |
| 1930 | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
| 1931 | if (globals == NULL) |
| 1932 | goto err; |
| 1933 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | /* Get the __import__ function from the builtins */ |
| 1936 | if (PyDict_Check(builtins)) { |
| 1937 | import = PyObject_GetItem(builtins, import_str); |
| 1938 | if (import == NULL) |
| 1939 | PyErr_SetObject(PyExc_KeyError, import_str); |
| 1940 | } |
| 1941 | else |
| 1942 | import = PyObject_GetAttr(builtins, import_str); |
| 1943 | if (import == NULL) |
| 1944 | goto err; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1946 | /* Call the __import__ function with the proper argument list |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1947 | Always use absolute import here. |
| 1948 | Calling for side-effect of import. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | r = PyObject_CallFunction(import, "OOOOi", module_name, globals, |
| 1950 | globals, silly_list, 0, NULL); |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1951 | if (r == NULL) |
| 1952 | goto err; |
| 1953 | Py_DECREF(r); |
| 1954 | |
| 1955 | modules = PyImport_GetModuleDict(); |
| 1956 | r = PyDict_GetItem(modules, module_name); |
| 1957 | if (r != NULL) |
| 1958 | Py_INCREF(r); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1959 | |
| 1960 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | Py_XDECREF(globals); |
| 1962 | Py_XDECREF(builtins); |
| 1963 | Py_XDECREF(import); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | return r; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1969 | /* Module 'imp' provides Python access to the primitives used for |
| 1970 | importing modules. |
| 1971 | */ |
| 1972 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1973 | static PyObject * |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1974 | imp_make_magic(long magic) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1975 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | char buf[4]; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | buf[0] = (char) ((magic >> 0) & 0xff); |
| 1979 | buf[1] = (char) ((magic >> 8) & 0xff); |
| 1980 | buf[2] = (char) ((magic >> 16) & 0xff); |
| 1981 | buf[3] = (char) ((magic >> 24) & 0xff); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | return PyBytes_FromStringAndSize(buf, 4); |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 1984 | } |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1985 | |
| 1986 | static PyObject * |
| 1987 | imp_get_magic(PyObject *self, PyObject *noargs) |
| 1988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | return imp_make_magic(pyc_magic); |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | static PyObject * |
| 1993 | imp_get_tag(PyObject *self, PyObject *noargs) |
| 1994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | return PyUnicode_FromString(pyc_tag); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1998 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 1999 | imp_get_suffixes(PyObject *self, PyObject *noargs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2000 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | PyObject *list; |
| 2002 | struct filedescr *fdp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | list = PyList_New(0); |
| 2005 | if (list == NULL) |
| 2006 | return NULL; |
| 2007 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
| 2008 | PyObject *item = Py_BuildValue("ssi", |
| 2009 | fdp->suffix, fdp->mode, fdp->type); |
| 2010 | if (item == NULL) { |
| 2011 | Py_DECREF(list); |
| 2012 | return NULL; |
| 2013 | } |
| 2014 | if (PyList_Append(list, item) < 0) { |
| 2015 | Py_DECREF(list); |
| 2016 | Py_DECREF(item); |
| 2017 | return NULL; |
| 2018 | } |
| 2019 | Py_DECREF(item); |
| 2020 | } |
| 2021 | return list; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2024 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2025 | imp_init_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2026 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 2027 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | int ret; |
| 2029 | PyObject *m; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 2030 | if (!PyArg_ParseTuple(args, "U:init_builtin", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 | return NULL; |
| 2032 | ret = init_builtin(name); |
| 2033 | if (ret < 0) |
| 2034 | return NULL; |
| 2035 | if (ret == 0) { |
| 2036 | Py_INCREF(Py_None); |
| 2037 | return Py_None; |
| 2038 | } |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 2039 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | Py_XINCREF(m); |
| 2041 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2044 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2045 | imp_init_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2046 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2047 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | int ret; |
| 2049 | PyObject *m; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2050 | if (!PyArg_ParseTuple(args, "U:init_frozen", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | return NULL; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2052 | ret = PyImport_ImportFrozenModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2053 | if (ret < 0) |
| 2054 | return NULL; |
| 2055 | if (ret == 0) { |
| 2056 | Py_INCREF(Py_None); |
| 2057 | return Py_None; |
| 2058 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2059 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2060 | Py_XINCREF(m); |
| 2061 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2064 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2065 | imp_get_frozen_object(PyObject *self, PyObject *args) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2066 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2067 | PyObject *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 2068 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2069 | if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | return NULL; |
| 2071 | return get_frozen_object(name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2074 | static PyObject * |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2075 | imp_is_frozen_package(PyObject *self, PyObject *args) |
| 2076 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2077 | PyObject *name; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2078 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2079 | if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | return NULL; |
| 2081 | return is_frozen_package(name); |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2085 | imp_is_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2086 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 2087 | PyObject *name; |
| 2088 | if (!PyArg_ParseTuple(args, "U:is_builtin", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2089 | return NULL; |
| 2090 | return PyLong_FromLong(is_builtin(name)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2093 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2094 | imp_is_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2095 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2096 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | struct _frozen *p; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2098 | if (!PyArg_ParseTuple(args, "U:is_frozen", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | return NULL; |
| 2100 | p = find_frozen(name); |
| 2101 | return PyBool_FromLong((long) (p == NULL ? 0 : p->size)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | static FILE * |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 2105 | get_file(PyObject *pathname, PyObject *fob, char *mode) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2106 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | FILE *fp; |
| 2108 | if (mode[0] == 'U') |
| 2109 | mode = "r" PY_STDIOTEXTMODE; |
| 2110 | if (fob == NULL) { |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 2111 | fp = _Py_fopen(pathname, mode); |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2112 | if (!fp) { |
| 2113 | if (!PyErr_Occurred()) |
| 2114 | PyErr_SetFromErrno(PyExc_IOError); |
| 2115 | return NULL; |
| 2116 | } |
| 2117 | return fp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | } |
| 2119 | else { |
| 2120 | int fd = PyObject_AsFileDescriptor(fob); |
| 2121 | if (fd == -1) |
| 2122 | return NULL; |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2123 | if (!_PyVerify_fd(fd)) { |
| 2124 | PyErr_SetFromErrno(PyExc_IOError); |
| 2125 | return NULL; |
| 2126 | } |
| 2127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | /* the FILE struct gets a new fd, so that it can be closed |
| 2129 | * independently of the file descriptor given |
| 2130 | */ |
| 2131 | fd = dup(fd); |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2132 | if (fd == -1) { |
| 2133 | PyErr_SetFromErrno(PyExc_IOError); |
| 2134 | return NULL; |
| 2135 | } |
| 2136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | fp = fdopen(fd, mode); |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2138 | if (!fp) { |
| 2139 | PyErr_SetFromErrno(PyExc_IOError); |
| 2140 | return NULL; |
| 2141 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | return fp; |
Victor Stinner | 3573476 | 2011-12-18 21:05:22 +0100 | [diff] [blame] | 2143 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2146 | #ifdef HAVE_DYNAMIC_LOADING |
| 2147 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2148 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2149 | imp_load_dynamic(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2150 | { |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2151 | PyObject *name, *pathname, *fob = NULL, *mod; |
| 2152 | FILE *fp; |
| 2153 | |
| 2154 | if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic", |
| 2155 | &name, PyUnicode_FSDecoder, &pathname, &fob)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | return NULL; |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2157 | if (fob != NULL) { |
| 2158 | fp = get_file(NULL, fob, "r"); |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 2159 | if (fp == NULL) { |
| 2160 | Py_DECREF(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | return NULL; |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 2162 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2163 | } |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2164 | else |
| 2165 | fp = NULL; |
| 2166 | mod = _PyImport_LoadDynamicModule(name, pathname, fp); |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 2167 | Py_DECREF(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | if (fp) |
| 2169 | fclose(fp); |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2170 | return mod; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2173 | #endif /* HAVE_DYNAMIC_LOADING */ |
| 2174 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2175 | static PyObject * |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 2176 | imp_reload(PyObject *self, PyObject *v) |
| 2177 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | return PyImport_ReloadModule(v); |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | PyDoc_STRVAR(doc_reload, |
| 2182 | "reload(module) -> module\n\ |
| 2183 | \n\ |
| 2184 | Reload the module. The module must have been successfully imported before."); |
| 2185 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 2186 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2187 | /* Doc strings */ |
| 2188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2189 | PyDoc_STRVAR(doc_imp, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 2190 | "(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] | 2191 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2192 | PyDoc_STRVAR(doc_get_magic, |
| 2193 | "get_magic() -> string\n\ |
| 2194 | Return the magic number for .pyc or .pyo files."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2195 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 2196 | PyDoc_STRVAR(doc_get_tag, |
| 2197 | "get_tag() -> string\n\ |
| 2198 | Return the magic tag for .pyc or .pyo files."); |
| 2199 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2200 | PyDoc_STRVAR(doc_get_suffixes, |
| 2201 | "get_suffixes() -> [(suffix, mode, type), ...]\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2202 | 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] | 2203 | that find_module() looks for."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2205 | PyDoc_STRVAR(doc_lock_held, |
Tim Peters | a7c6509 | 2004-08-01 23:26:05 +0000 | [diff] [blame] | 2206 | "lock_held() -> boolean\n\ |
| 2207 | Return True if the import lock is currently held, else False.\n\ |
| 2208 | On platforms without threads, return False."); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 2210 | PyDoc_STRVAR(doc_acquire_lock, |
| 2211 | "acquire_lock() -> None\n\ |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 2212 | Acquires the interpreter's import lock for the current thread.\n\ |
| 2213 | This lock should be used by import hooks to ensure thread-safety\n\ |
| 2214 | when importing modules.\n\ |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 2215 | On platforms without threads, this function does nothing."); |
| 2216 | |
| 2217 | PyDoc_STRVAR(doc_release_lock, |
| 2218 | "release_lock() -> None\n\ |
| 2219 | Release the interpreter's import lock.\n\ |
| 2220 | On platforms without threads, this function does nothing."); |
| 2221 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2222 | static PyMethodDef imp_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic}, |
| 2224 | {"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag}, |
| 2225 | {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2226 | {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, |
| 2227 | {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock}, |
| 2228 | {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock}, |
| 2229 | {"reload", imp_reload, METH_O, doc_reload}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | {"get_frozen_object", imp_get_frozen_object, METH_VARARGS}, |
| 2231 | {"is_frozen_package", imp_is_frozen_package, METH_VARARGS}, |
| 2232 | {"init_builtin", imp_init_builtin, METH_VARARGS}, |
| 2233 | {"init_frozen", imp_init_frozen, METH_VARARGS}, |
| 2234 | {"is_builtin", imp_is_builtin, METH_VARARGS}, |
| 2235 | {"is_frozen", imp_is_frozen, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2236 | #ifdef HAVE_DYNAMIC_LOADING |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | {"load_dynamic", imp_load_dynamic, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2238 | #endif |
Brett Cannon | 442c9b9 | 2011-03-23 16:14:42 -0700 | [diff] [blame] | 2239 | {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2241 | }; |
| 2242 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 2243 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2244 | setint(PyObject *d, char *name, int value) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | PyObject *v; |
| 2247 | int err; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2249 | v = PyLong_FromLong((long)value); |
| 2250 | err = PyDict_SetItemString(d, name, v); |
| 2251 | Py_XDECREF(v); |
| 2252 | return err; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2255 | typedef struct { |
| 2256 | PyObject_HEAD |
| 2257 | } NullImporter; |
| 2258 | |
| 2259 | static int |
| 2260 | NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds) |
| 2261 | { |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 2262 | #ifndef MS_WINDOWS |
| 2263 | PyObject *path; |
| 2264 | struct stat statbuf; |
| 2265 | int rv; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 | if (!_PyArg_NoKeywords("NullImporter()", kwds)) |
| 2268 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2269 | |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 2270 | if (!PyArg_ParseTuple(args, "O&:NullImporter", |
| 2271 | PyUnicode_FSConverter, &path)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2273 | |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 2274 | if (PyBytes_GET_SIZE(path) == 0) { |
| 2275 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | PyErr_SetString(PyExc_ImportError, "empty pathname"); |
| 2277 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | } |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 2279 | |
| 2280 | rv = stat(PyBytes_AS_STRING(path), &statbuf); |
| 2281 | Py_DECREF(path); |
| 2282 | if (rv == 0) { |
| 2283 | /* it exists */ |
| 2284 | if (S_ISDIR(statbuf.st_mode)) { |
| 2285 | /* it's a directory */ |
| 2286 | PyErr_SetString(PyExc_ImportError, "existing directory"); |
| 2287 | return -1; |
| 2288 | } |
| 2289 | } |
| 2290 | #else /* MS_WINDOWS */ |
| 2291 | PyObject *pathobj; |
| 2292 | DWORD rv; |
Victor Stinner | 255dfdb | 2010-09-29 10:28:51 +0000 | [diff] [blame] | 2293 | wchar_t *path; |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 2294 | |
| 2295 | if (!_PyArg_NoKeywords("NullImporter()", kwds)) |
| 2296 | return -1; |
| 2297 | |
| 2298 | if (!PyArg_ParseTuple(args, "U:NullImporter", |
| 2299 | &pathobj)) |
| 2300 | return -1; |
| 2301 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2302 | if (PyUnicode_GET_LENGTH(pathobj) == 0) { |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 2303 | PyErr_SetString(PyExc_ImportError, "empty pathname"); |
| 2304 | return -1; |
| 2305 | } |
| 2306 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2307 | path = PyUnicode_AsWideCharString(pathobj, NULL); |
Victor Stinner | 255dfdb | 2010-09-29 10:28:51 +0000 | [diff] [blame] | 2308 | if (path == NULL) |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 2309 | return -1; |
| 2310 | /* see issue1293 and issue3677: |
| 2311 | * stat() on Windows doesn't recognise paths like |
| 2312 | * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs. |
| 2313 | */ |
| 2314 | rv = GetFileAttributesW(path); |
Victor Stinner | 255dfdb | 2010-09-29 10:28:51 +0000 | [diff] [blame] | 2315 | PyMem_Free(path); |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 2316 | if (rv != INVALID_FILE_ATTRIBUTES) { |
| 2317 | /* it exists */ |
| 2318 | if (rv & FILE_ATTRIBUTE_DIRECTORY) { |
| 2319 | /* it's a directory */ |
| 2320 | PyErr_SetString(PyExc_ImportError, "existing directory"); |
| 2321 | return -1; |
| 2322 | } |
| 2323 | } |
| 2324 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | return 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | static PyObject * |
| 2329 | NullImporter_find_module(NullImporter *self, PyObject *args) |
| 2330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | static PyMethodDef NullImporter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS, |
| 2336 | "Always return None" |
| 2337 | }, |
| 2338 | {NULL} /* Sentinel */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2339 | }; |
| 2340 | |
| 2341 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 2342 | PyTypeObject PyNullImporter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2343 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2344 | "imp.NullImporter", /*tp_name*/ |
| 2345 | sizeof(NullImporter), /*tp_basicsize*/ |
| 2346 | 0, /*tp_itemsize*/ |
| 2347 | 0, /*tp_dealloc*/ |
| 2348 | 0, /*tp_print*/ |
| 2349 | 0, /*tp_getattr*/ |
| 2350 | 0, /*tp_setattr*/ |
| 2351 | 0, /*tp_reserved*/ |
| 2352 | 0, /*tp_repr*/ |
| 2353 | 0, /*tp_as_number*/ |
| 2354 | 0, /*tp_as_sequence*/ |
| 2355 | 0, /*tp_as_mapping*/ |
| 2356 | 0, /*tp_hash */ |
| 2357 | 0, /*tp_call*/ |
| 2358 | 0, /*tp_str*/ |
| 2359 | 0, /*tp_getattro*/ |
| 2360 | 0, /*tp_setattro*/ |
| 2361 | 0, /*tp_as_buffer*/ |
| 2362 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 2363 | "Null importer object", /* tp_doc */ |
| 2364 | 0, /* tp_traverse */ |
| 2365 | 0, /* tp_clear */ |
| 2366 | 0, /* tp_richcompare */ |
| 2367 | 0, /* tp_weaklistoffset */ |
| 2368 | 0, /* tp_iter */ |
| 2369 | 0, /* tp_iternext */ |
| 2370 | NullImporter_methods, /* tp_methods */ |
| 2371 | 0, /* tp_members */ |
| 2372 | 0, /* tp_getset */ |
| 2373 | 0, /* tp_base */ |
| 2374 | 0, /* tp_dict */ |
| 2375 | 0, /* tp_descr_get */ |
| 2376 | 0, /* tp_descr_set */ |
| 2377 | 0, /* tp_dictoffset */ |
| 2378 | (initproc)NullImporter_init, /* tp_init */ |
| 2379 | 0, /* tp_alloc */ |
| 2380 | PyType_GenericNew /* tp_new */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2381 | }; |
| 2382 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2383 | static struct PyModuleDef impmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2384 | PyModuleDef_HEAD_INIT, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 2385 | "_imp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | doc_imp, |
| 2387 | 0, |
| 2388 | imp_methods, |
| 2389 | NULL, |
| 2390 | NULL, |
| 2391 | NULL, |
| 2392 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2393 | }; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2394 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 2395 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2396 | PyInit_imp(void) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | PyObject *m, *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | if (PyType_Ready(&PyNullImporter_Type) < 0) |
| 2401 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | m = PyModule_Create(&impmodule); |
| 2404 | if (m == NULL) |
| 2405 | goto failure; |
| 2406 | d = PyModule_GetDict(m); |
| 2407 | if (d == NULL) |
| 2408 | goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; |
| 2411 | if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; |
| 2412 | if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure; |
| 2413 | if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure; |
| 2414 | if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure; |
| 2415 | if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure; |
| 2416 | if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure; |
| 2417 | if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure; |
| 2418 | if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure; |
| 2419 | if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | Py_INCREF(&PyNullImporter_Type); |
| 2422 | PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type); |
| 2423 | return m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2424 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2425 | Py_XDECREF(m); |
| 2426 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2427 | } |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2428 | |
| 2429 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2430 | /* API for embedding applications that want to add their own entries |
| 2431 | to the table of built-in modules. This should normally be called |
| 2432 | *before* Py_Initialize(). When the table resize fails, -1 is |
| 2433 | returned and the existing table is unchanged. |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2434 | |
| 2435 | After a similar function by Just van Rossum. */ |
| 2436 | |
| 2437 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2438 | PyImport_ExtendInittab(struct _inittab *newtab) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2440 | static struct _inittab *our_copy = NULL; |
| 2441 | struct _inittab *p; |
| 2442 | int i, n; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | /* Count the number of entries in both tables */ |
| 2445 | for (n = 0; newtab[n].name != NULL; n++) |
| 2446 | ; |
| 2447 | if (n == 0) |
| 2448 | return 0; /* Nothing to do */ |
| 2449 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) |
| 2450 | ; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2452 | /* Allocate new memory for the combined table */ |
| 2453 | p = our_copy; |
| 2454 | PyMem_RESIZE(p, struct _inittab, i+n+1); |
| 2455 | if (p == NULL) |
| 2456 | return -1; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | /* Copy the tables into the new memory */ |
| 2459 | if (our_copy != PyImport_Inittab) |
| 2460 | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
| 2461 | PyImport_Inittab = our_copy = p; |
| 2462 | memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 | return 0; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | /* Shorthand to add a single entry given a name and a function */ |
| 2468 | |
| 2469 | int |
Brett Cannon | a826f32 | 2009-04-02 03:41:46 +0000 | [diff] [blame] | 2470 | PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2472 | struct _inittab newtab[2]; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2474 | memset(newtab, '\0', sizeof newtab); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2476 | newtab[0].name = (char *)name; |
| 2477 | newtab[0].initfunc = initfunc; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2479 | return PyImport_ExtendInittab(newtab); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2480 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2481 | |
| 2482 | #ifdef __cplusplus |
| 2483 | } |
| 2484 | #endif |