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