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