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