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