Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Module definition and import implementation */ |
| 3 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "Python-ast.h" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 7 | #undef Yield /* undefine macro conflicting with winbase.h */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 | #include "errcode.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 9 | #include "marshal.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 10 | #include "code.h" |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 11 | #include "frameobject.h" |
Guido van Rossum | d8bac6d | 1992-02-26 15:19:13 +0000 | [diff] [blame] | 12 | #include "osdefs.h" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 13 | #include "importdl.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 15 | #ifdef HAVE_FCNTL_H |
| 16 | #include <fcntl.h> |
| 17 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 18 | #ifdef __cplusplus |
Brett Cannon | 9a5b25a | 2010-03-01 02:09:17 +0000 | [diff] [blame] | 19 | extern "C" { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 20 | #endif |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 21 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 22 | #ifdef MS_WINDOWS |
| 23 | /* for stat.st_mode */ |
| 24 | typedef unsigned short mode_t; |
Daniel Stutzbach | c793779 | 2010-09-09 21:18:04 +0000 | [diff] [blame] | 25 | /* for _mkdir */ |
| 26 | #include <direct.h> |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 29 | |
Jeremy Hylton | d4ceb31 | 2004-04-01 02:45:22 +0000 | [diff] [blame] | 30 | /* Magic word to reject .pyc files generated by other Python versions. |
| 31 | It should change for each incompatible change to the bytecode. |
| 32 | |
| 33 | 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] | 34 | 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] | 35 | Apple MPW compiler swaps their values, botching string constants. |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 37 | 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] | 38 | -U interpeter flag will cause MAGIC+1 being used. They have been |
| 39 | odd numbers for some time now. |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 40 | |
Jeremy Hylton | d4ceb31 | 2004-04-01 02:45:22 +0000 | [diff] [blame] | 41 | There were a variety of old schemes for setting the magic number. |
| 42 | The current working scheme is to increment the previous value by |
| 43 | 10. |
Guido van Rossum | f689492 | 2002-08-31 15:16:14 +0000 | [diff] [blame] | 44 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 45 | Starting with the adoption of PEP 3147 in Python 3.2, every bump in magic |
| 46 | number also includes a new "magic tag", i.e. a human readable string used |
| 47 | to represent the magic number in __pycache__ directories. When you change |
| 48 | the magic number, you must also set a new unique magic tag. Generally this |
| 49 | can be named after the Python major version of the magic number bump, but |
| 50 | it can really be anything, as long as it's different than anything else |
| 51 | that's come before. The tags are included in the following table, starting |
| 52 | with Python 3.2a0. |
| 53 | |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 54 | Known values: |
| 55 | Python 1.5: 20121 |
| 56 | Python 1.5.1: 20121 |
| 57 | Python 1.5.2: 20121 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | Python 1.6: 50428 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 59 | Python 2.0: 50823 |
| 60 | Python 2.0.1: 50823 |
| 61 | Python 2.1: 60202 |
| 62 | Python 2.1.1: 60202 |
| 63 | Python 2.1.2: 60202 |
| 64 | Python 2.2: 60717 |
Neal Norwitz | 7fdcb41 | 2002-06-14 01:07:39 +0000 | [diff] [blame] | 65 | Python 2.3a0: 62011 |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 66 | Python 2.3a0: 62021 |
Guido van Rossum | f689492 | 2002-08-31 15:16:14 +0000 | [diff] [blame] | 67 | Python 2.3a0: 62011 (!) |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 68 | Python 2.4a0: 62041 |
Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 69 | Python 2.4a3: 62051 |
Raymond Hettinger | 2c31a05 | 2004-09-22 18:44:21 +0000 | [diff] [blame] | 70 | Python 2.4b1: 62061 |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 71 | Python 2.5a0: 62071 |
Michael W. Hudson | aee2e28 | 2005-10-21 11:32:20 +0000 | [diff] [blame] | 72 | Python 2.5a0: 62081 (ast-branch) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 73 | Python 2.5a0: 62091 (with) |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 74 | Python 2.5a0: 62092 (changed WITH_CLEANUP opcode) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | Python 2.5b3: 62101 (fix wrong code: for x, in ...) |
| 76 | Python 2.5b3: 62111 (fix wrong code: x += yield) |
| 77 | Python 2.5c1: 62121 (fix wrong lnotab with for loops and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | storing constants that should have been removed) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 79 | 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] | 80 | Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 81 | Python 2.6a1: 62161 (WITH_CLEANUP optimization) |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 82 | Python 3000: 3000 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | 3010 (removed UNARY_CONVERT) |
| 84 | 3020 (added BUILD_SET) |
| 85 | 3030 (added keyword-only parameters) |
| 86 | 3040 (added signature annotations) |
| 87 | 3050 (print becomes a function) |
| 88 | 3060 (PEP 3115 metaclass syntax) |
| 89 | 3061 (string literals become unicode) |
| 90 | 3071 (PEP 3109 raise changes) |
| 91 | 3081 (PEP 3137 make __file__ and __name__ unicode) |
| 92 | 3091 (kill str8 interning) |
| 93 | 3101 (merge from 2.6a0, see 62151) |
| 94 | 3103 (__file__ points to source file) |
Benjamin Peterson | 0f6cae0 | 2009-12-10 02:09:08 +0000 | [diff] [blame] | 95 | Python 3.0a4: 3111 (WITH_CLEANUP optimization). |
| 96 | Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT) |
| 97 | Python 3.1a0: 3141 (optimize list, set and dict comprehensions: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | change LIST_APPEND and SET_ADD, add MAP_ADD) |
Benjamin Peterson | 0f6cae0 | 2009-12-10 02:09:08 +0000 | [diff] [blame] | 99 | Python 3.1a0: 3151 (optimize conditional branches: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 101 | Python 3.2a0: 3160 (add SETUP_WITH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | tag: cpython-32 |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 103 | Python 3.2a1: 3170 (add DUP_TOP_TWO, remove DUP_TOPX and ROT_FOUR) |
| 104 | tag: cpython-32 |
Benjamin Peterson | 6246d6d | 2010-09-10 21:51:44 +0000 | [diff] [blame] | 105 | Python 3.2a2 3180 (add DELETE_DEREF) |
Benjamin Peterson | e7c15fa | 2011-06-19 19:54:45 -0500 | [diff] [blame] | 106 | Python 3.3a0 3190 __class__ super closure changed |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 107 | Python 3.3a0 3200 (__qualname__ added) |
Benjamin Peterson | abdb552 | 2012-03-15 15:40:37 -0500 | [diff] [blame] | 108 | 3210 (added size modulo 2**32 to the pyc header) |
| 109 | Python 3.3a1 3220 (changed PEP 380 implementation) |
Nick Coghlan | 0b43bcf | 2012-05-27 18:17:07 +1000 | [diff] [blame] | 110 | Python 3.3a4 3230 (revert changes to implicit __class__ closure) |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 111 | */ |
Guido van Rossum | 3ddee71 | 1991-12-16 13:06:34 +0000 | [diff] [blame] | 112 | |
Nick Coghlan | cd419ab | 2010-09-11 00:39:25 +0000 | [diff] [blame] | 113 | /* MAGIC must change whenever the bytecode emitted by the compiler may no |
| 114 | longer be understood by older implementations of the eval loop (usually |
| 115 | due to the addition of new opcodes) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 116 | */ |
Nick Coghlan | 0b43bcf | 2012-05-27 18:17:07 +1000 | [diff] [blame] | 117 | #define MAGIC (3230 | ((long)'\r'<<16) | ((long)'\n'<<24)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 118 | #define CACHEDIR "__pycache__" |
| 119 | /* Current magic word and string tag as globals. */ |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 120 | static long pyc_magic = MAGIC; |
| 121 | |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 122 | /* See _PyImport_FixupExtensionObject() below */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 123 | static PyObject *extensions = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 124 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 125 | /* Function from Parser/tokenizer.c */ |
| 126 | extern char * PyTokenizer_FindEncodingFilename(int, PyObject *); |
| 127 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 128 | /* This table is defined in config.c: */ |
| 129 | extern struct _inittab _PyImport_Inittab[]; |
| 130 | |
| 131 | struct _inittab *PyImport_Inittab = _PyImport_Inittab; |
Guido van Rossum | 66f1fa8 | 1991-04-03 19:03:52 +0000 | [diff] [blame] | 132 | |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 133 | static PyObject *initstr = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 135 | /* Initialize things */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | |
| 137 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 138 | _PyImport_Init(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 139 | { |
Victor Stinner | d029621 | 2011-03-14 14:04:10 -0400 | [diff] [blame] | 140 | initstr = PyUnicode_InternFromString("__init__"); |
| 141 | if (initstr == NULL) |
| 142 | Py_FatalError("Can't initialize import variables"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 145 | void |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 146 | _PyImportHooks_Init(void) |
| 147 | { |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 148 | PyObject *v, *path_hooks = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | int err = 0; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 150 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 151 | /* adding sys.path_hooks and sys.path_importer_cache */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | v = PyList_New(0); |
| 153 | if (v == NULL) |
| 154 | goto error; |
| 155 | err = PySys_SetObject("meta_path", v); |
| 156 | Py_DECREF(v); |
| 157 | if (err) |
| 158 | goto error; |
| 159 | v = PyDict_New(); |
| 160 | if (v == NULL) |
| 161 | goto error; |
| 162 | err = PySys_SetObject("path_importer_cache", v); |
| 163 | Py_DECREF(v); |
| 164 | if (err) |
| 165 | goto error; |
| 166 | path_hooks = PyList_New(0); |
| 167 | if (path_hooks == NULL) |
| 168 | goto error; |
| 169 | err = PySys_SetObject("path_hooks", path_hooks); |
| 170 | if (err) { |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 171 | error: |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 172 | PyErr_Print(); |
| 173 | Py_FatalError("initializing sys.meta_path, sys.path_hooks, " |
Nadeem Vawda | 8f46d65 | 2012-05-05 12:27:30 +0200 | [diff] [blame] | 174 | "or path_importer_cache failed"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 176 | Py_DECREF(path_hooks); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | _PyImportZip_Init(void) |
| 181 | { |
| 182 | PyObject *path_hooks, *zimpimport; |
| 183 | int err = 0; |
| 184 | |
| 185 | path_hooks = PySys_GetObject("path_hooks"); |
| 186 | if (path_hooks == NULL) |
| 187 | goto error; |
| 188 | |
| 189 | if (Py_VerboseFlag) |
| 190 | PySys_WriteStderr("# installing zipimport hook\n"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | zimpimport = PyImport_ImportModule("zipimport"); |
| 193 | if (zimpimport == NULL) { |
| 194 | PyErr_Clear(); /* No zip import module -- okay */ |
| 195 | if (Py_VerboseFlag) |
| 196 | PySys_WriteStderr("# can't import zipimport\n"); |
| 197 | } |
| 198 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 199 | _Py_IDENTIFIER(zipimporter); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 200 | PyObject *zipimporter = _PyObject_GetAttrId(zimpimport, |
| 201 | &PyId_zipimporter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | Py_DECREF(zimpimport); |
| 203 | if (zipimporter == NULL) { |
| 204 | PyErr_Clear(); /* No zipimporter object -- okay */ |
| 205 | if (Py_VerboseFlag) |
| 206 | PySys_WriteStderr( |
| 207 | "# can't import zipimport.zipimporter\n"); |
| 208 | } |
| 209 | else { |
Brett Cannon | 8923a4d | 2012-04-24 22:03:46 -0400 | [diff] [blame] | 210 | /* sys.path_hooks.insert(0, zipimporter) */ |
| 211 | err = PyList_Insert(path_hooks, 0, zipimporter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | Py_DECREF(zipimporter); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 213 | if (err < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | goto error; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 215 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if (Py_VerboseFlag) |
| 217 | PySys_WriteStderr( |
| 218 | "# installed zipimport hook\n"); |
| 219 | } |
| 220 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 221 | |
| 222 | return; |
| 223 | |
| 224 | error: |
| 225 | PyErr_Print(); |
Brett Cannon | acf85cd | 2012-04-29 12:50:03 -0400 | [diff] [blame] | 226 | Py_FatalError("initializing zipimport failed"); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 229 | /* Locking primitives to prevent parallel imports of the same module |
| 230 | in different threads to return with a partially loaded module. |
| 231 | These calls are serialized by the global interpreter lock. */ |
| 232 | |
| 233 | #ifdef WITH_THREAD |
| 234 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 235 | #include "pythread.h" |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 237 | static PyThread_type_lock import_lock = 0; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 238 | static long import_lock_thread = -1; |
| 239 | static int import_lock_level = 0; |
| 240 | |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 241 | void |
| 242 | _PyImport_AcquireLock(void) |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 243 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | long me = PyThread_get_thread_ident(); |
| 245 | if (me == -1) |
| 246 | return; /* Too bad */ |
| 247 | if (import_lock == NULL) { |
| 248 | import_lock = PyThread_allocate_lock(); |
| 249 | if (import_lock == NULL) |
| 250 | return; /* Nothing much we can do. */ |
| 251 | } |
| 252 | if (import_lock_thread == me) { |
| 253 | import_lock_level++; |
| 254 | return; |
| 255 | } |
| 256 | if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) |
| 257 | { |
| 258 | PyThreadState *tstate = PyEval_SaveThread(); |
| 259 | PyThread_acquire_lock(import_lock, 1); |
| 260 | PyEval_RestoreThread(tstate); |
| 261 | } |
| 262 | import_lock_thread = me; |
| 263 | import_lock_level = 1; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 266 | int |
| 267 | _PyImport_ReleaseLock(void) |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | long me = PyThread_get_thread_ident(); |
| 270 | if (me == -1 || import_lock == NULL) |
| 271 | return 0; /* Too bad */ |
| 272 | if (import_lock_thread != me) |
| 273 | return -1; |
| 274 | import_lock_level--; |
| 275 | if (import_lock_level == 0) { |
| 276 | import_lock_thread = -1; |
| 277 | PyThread_release_lock(import_lock); |
| 278 | } |
| 279 | return 1; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 282 | /* This function is called from PyOS_AfterFork to ensure that newly |
| 283 | created child processes do not share locks with the parent. |
| 284 | We now acquire the import lock around fork() calls but on some platforms |
| 285 | (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] | 286 | |
| 287 | void |
| 288 | _PyImport_ReInitLock(void) |
| 289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | if (import_lock != NULL) |
| 291 | import_lock = PyThread_allocate_lock(); |
Nick Coghlan | b2ddf79 | 2010-12-02 04:11:46 +0000 | [diff] [blame] | 292 | if (import_lock_level > 1) { |
| 293 | /* Forked as a side effect of import */ |
| 294 | long me = PyThread_get_thread_ident(); |
| 295 | PyThread_acquire_lock(import_lock, 0); |
Victor Stinner | 942003c | 2011-03-07 16:57:48 +0100 | [diff] [blame] | 296 | /* XXX: can the previous line fail? */ |
Nick Coghlan | b2ddf79 | 2010-12-02 04:11:46 +0000 | [diff] [blame] | 297 | import_lock_thread = me; |
| 298 | import_lock_level--; |
| 299 | } else { |
| 300 | import_lock_thread = -1; |
| 301 | import_lock_level = 0; |
| 302 | } |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 305 | #endif |
| 306 | |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 307 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 308 | imp_lock_held(PyObject *self, PyObject *noargs) |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 309 | { |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 310 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | return PyBool_FromLong(import_lock_thread != -1); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 312 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | return PyBool_FromLong(0); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 314 | #endif |
| 315 | } |
| 316 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 317 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 318 | imp_acquire_lock(PyObject *self, PyObject *noargs) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 319 | { |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 320 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | _PyImport_AcquireLock(); |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 322 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | Py_INCREF(Py_None); |
| 324 | return Py_None; |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 328 | imp_release_lock(PyObject *self, PyObject *noargs) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 329 | { |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 330 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | if (_PyImport_ReleaseLock() < 0) { |
| 332 | PyErr_SetString(PyExc_RuntimeError, |
| 333 | "not holding the import lock"); |
| 334 | return NULL; |
| 335 | } |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 336 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | Py_INCREF(Py_None); |
| 338 | return Py_None; |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Antoine Pitrou | 8db076c | 2011-10-30 19:13:55 +0100 | [diff] [blame] | 341 | void |
| 342 | _PyImport_Fini(void) |
| 343 | { |
| 344 | Py_XDECREF(extensions); |
| 345 | extensions = NULL; |
Antoine Pitrou | 8db076c | 2011-10-30 19:13:55 +0100 | [diff] [blame] | 346 | #ifdef WITH_THREAD |
| 347 | if (import_lock != NULL) { |
| 348 | PyThread_free_lock(import_lock); |
| 349 | import_lock = NULL; |
| 350 | } |
| 351 | #endif |
| 352 | } |
| 353 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 354 | /* Helper for sys */ |
| 355 | |
| 356 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 357 | PyImport_GetModuleDict(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 358 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 360 | if (interp->modules == NULL) |
| 361 | Py_FatalError("PyImport_GetModuleDict: no module dictionary!"); |
| 362 | return interp->modules; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 366 | /* List of names to clear in sys */ |
| 367 | static char* sys_deletes[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | "path", "argv", "ps1", "ps2", |
| 369 | "last_type", "last_value", "last_traceback", |
| 370 | "path_hooks", "path_importer_cache", "meta_path", |
| 371 | /* misc stuff */ |
| 372 | "flags", "float_info", |
| 373 | NULL |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 374 | }; |
| 375 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 376 | static char* sys_files[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | "stdin", "__stdin__", |
| 378 | "stdout", "__stdout__", |
| 379 | "stderr", "__stderr__", |
| 380 | NULL |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 381 | }; |
| 382 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 383 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 384 | /* Un-initialize things, as good as we can */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 385 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 386 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 387 | PyImport_Cleanup(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | Py_ssize_t pos, ndone; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | PyObject *key, *value, *dict; |
| 391 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 392 | PyObject *modules = interp->modules; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | if (modules == NULL) |
| 395 | return; /* Already done */ |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | /* Delete some special variables first. These are common |
| 398 | places where user values hide and people complain when their |
| 399 | destructors fail. Since the modules containing them are |
| 400 | deleted *last* of all, they would come too late in the normal |
| 401 | destruction order. Sigh. */ |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | value = PyDict_GetItemString(modules, "builtins"); |
| 404 | if (value != NULL && PyModule_Check(value)) { |
| 405 | dict = PyModule_GetDict(value); |
| 406 | if (Py_VerboseFlag) |
| 407 | PySys_WriteStderr("# clear builtins._\n"); |
| 408 | PyDict_SetItemString(dict, "_", Py_None); |
| 409 | } |
| 410 | value = PyDict_GetItemString(modules, "sys"); |
| 411 | if (value != NULL && PyModule_Check(value)) { |
| 412 | char **p; |
| 413 | PyObject *v; |
| 414 | dict = PyModule_GetDict(value); |
| 415 | for (p = sys_deletes; *p != NULL; p++) { |
| 416 | if (Py_VerboseFlag) |
| 417 | PySys_WriteStderr("# clear sys.%s\n", *p); |
| 418 | PyDict_SetItemString(dict, *p, Py_None); |
| 419 | } |
| 420 | for (p = sys_files; *p != NULL; p+=2) { |
| 421 | if (Py_VerboseFlag) |
| 422 | PySys_WriteStderr("# restore sys.%s\n", *p); |
| 423 | v = PyDict_GetItemString(dict, *(p+1)); |
| 424 | if (v == NULL) |
| 425 | v = Py_None; |
| 426 | PyDict_SetItemString(dict, *p, v); |
| 427 | } |
| 428 | } |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | /* First, delete __main__ */ |
| 431 | value = PyDict_GetItemString(modules, "__main__"); |
| 432 | if (value != NULL && PyModule_Check(value)) { |
| 433 | if (Py_VerboseFlag) |
| 434 | PySys_WriteStderr("# cleanup __main__\n"); |
| 435 | _PyModule_Clear(value); |
| 436 | PyDict_SetItemString(modules, "__main__", Py_None); |
| 437 | } |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | /* The special treatment of "builtins" here is because even |
| 440 | when it's not referenced as a module, its dictionary is |
| 441 | referenced by almost every module's __builtins__. Since |
| 442 | deleting a module clears its dictionary (even if there are |
| 443 | references left to it), we need to delete the "builtins" |
| 444 | module last. Likewise, we don't delete sys until the very |
| 445 | end because it is implicitly referenced (e.g. by print). |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | Also note that we 'delete' modules by replacing their entry |
| 448 | in the modules dict with None, rather than really deleting |
| 449 | them; this avoids a rehash of the modules dictionary and |
| 450 | also marks them as "non existent" so they won't be |
| 451 | re-imported. */ |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | /* Next, repeatedly delete modules with a reference count of |
| 454 | one (skipping builtins and sys) and delete them */ |
| 455 | do { |
| 456 | ndone = 0; |
| 457 | pos = 0; |
| 458 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 459 | if (value->ob_refcnt != 1) |
| 460 | continue; |
| 461 | if (PyUnicode_Check(key) && PyModule_Check(value)) { |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 462 | if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | continue; |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 464 | if (PyUnicode_CompareWithASCIIString(key, "sys") == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | continue; |
| 466 | if (Py_VerboseFlag) |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 467 | PySys_FormatStderr( |
| 468 | "# cleanup[1] %U\n", key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | _PyModule_Clear(value); |
| 470 | PyDict_SetItem(modules, key, Py_None); |
| 471 | ndone++; |
| 472 | } |
| 473 | } |
| 474 | } while (ndone > 0); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | /* Next, delete all modules (still skipping builtins and sys) */ |
| 477 | pos = 0; |
| 478 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 479 | if (PyUnicode_Check(key) && PyModule_Check(value)) { |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 480 | if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | continue; |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 482 | if (PyUnicode_CompareWithASCIIString(key, "sys") == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | continue; |
| 484 | if (Py_VerboseFlag) |
Victor Stinner | 9464d61 | 2011-03-07 17:08:21 +0100 | [diff] [blame] | 485 | PySys_FormatStderr("# cleanup[2] %U\n", key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | _PyModule_Clear(value); |
| 487 | PyDict_SetItem(modules, key, Py_None); |
| 488 | } |
| 489 | } |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | /* Next, delete sys and builtins (in that order) */ |
| 492 | value = PyDict_GetItemString(modules, "sys"); |
| 493 | if (value != NULL && PyModule_Check(value)) { |
| 494 | if (Py_VerboseFlag) |
| 495 | PySys_WriteStderr("# cleanup sys\n"); |
| 496 | _PyModule_Clear(value); |
| 497 | PyDict_SetItemString(modules, "sys", Py_None); |
| 498 | } |
| 499 | value = PyDict_GetItemString(modules, "builtins"); |
| 500 | if (value != NULL && PyModule_Check(value)) { |
| 501 | if (Py_VerboseFlag) |
| 502 | PySys_WriteStderr("# cleanup builtins\n"); |
| 503 | _PyModule_Clear(value); |
| 504 | PyDict_SetItemString(modules, "builtins", Py_None); |
| 505 | } |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | /* Finally, clear and delete the modules directory */ |
| 508 | PyDict_Clear(modules); |
| 509 | interp->modules = NULL; |
| 510 | Py_DECREF(modules); |
Guido van Rossum | 8d15b5d | 1990-10-26 14:58:58 +0000 | [diff] [blame] | 511 | } |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 512 | |
| 513 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 514 | /* Helper for pythonrun.c -- return magic number and tag. */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 515 | |
| 516 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 517 | PyImport_GetMagicNumber(void) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 518 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | return pyc_magic; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 523 | const char * |
| 524 | PyImport_GetMagicTag(void) |
| 525 | { |
Brett Cannon | 98979b8 | 2012-07-02 15:13:11 -0400 | [diff] [blame] | 526 | PyObject *impl, *tag; |
| 527 | const char *raw_tag; |
| 528 | |
| 529 | /* We could also pull it from imp or importlib. */ |
| 530 | impl = PySys_GetObject("implementation"); |
| 531 | if (impl == NULL) |
| 532 | return NULL; |
| 533 | tag = PyObject_GetAttrString(impl, "cache_tag"); |
| 534 | if (tag == NULL) |
| 535 | return NULL; |
| 536 | raw_tag = PyUnicode_DATA(tag); |
| 537 | Py_DECREF(tag); |
| 538 | return raw_tag; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Brett Cannon | 98979b8 | 2012-07-02 15:13:11 -0400 | [diff] [blame] | 541 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 542 | /* Magic for extension modules (built-in as well as dynamically |
| 543 | loaded). To prevent initializing an extension module more than |
| 544 | once, we keep a static dictionary 'extensions' keyed by module name |
| 545 | (for built-in modules) or by filename (for dynamically loaded |
Barry Warsaw | 9288338 | 2001-08-13 23:05:44 +0000 | [diff] [blame] | 546 | modules), containing these modules. A copy of the module's |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 547 | dictionary is stored by calling _PyImport_FixupExtensionObject() |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 548 | immediately after the module initialization function succeeds. A |
| 549 | copy can be retrieved from there by calling |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 550 | _PyImport_FindExtensionObject(). |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 551 | |
Barry Warsaw | 7c9627b | 2010-06-17 18:38:20 +0000 | [diff] [blame] | 552 | Modules which do support multiple initialization set their m_size |
| 553 | field to a non-negative number (indicating the size of the |
| 554 | module-specific state). They are still recorded in the extensions |
| 555 | dictionary, to avoid loading shared libraries twice. |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 556 | */ |
| 557 | |
| 558 | int |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 559 | _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, |
| 560 | PyObject *filename) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | PyObject *modules, *dict; |
| 563 | struct PyModuleDef *def; |
| 564 | if (extensions == NULL) { |
| 565 | extensions = PyDict_New(); |
| 566 | if (extensions == NULL) |
| 567 | return -1; |
| 568 | } |
| 569 | if (mod == NULL || !PyModule_Check(mod)) { |
| 570 | PyErr_BadInternalCall(); |
| 571 | return -1; |
| 572 | } |
| 573 | def = PyModule_GetDef(mod); |
| 574 | if (!def) { |
| 575 | PyErr_BadInternalCall(); |
| 576 | return -1; |
| 577 | } |
| 578 | modules = PyImport_GetModuleDict(); |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 579 | if (PyDict_SetItem(modules, name, mod) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | return -1; |
| 581 | if (_PyState_AddModule(mod, def) < 0) { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 582 | PyDict_DelItem(modules, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | return -1; |
| 584 | } |
| 585 | if (def->m_size == -1) { |
| 586 | if (def->m_base.m_copy) { |
| 587 | /* Somebody already imported the module, |
| 588 | likely under a different name. |
| 589 | XXX this should really not happen. */ |
| 590 | Py_DECREF(def->m_base.m_copy); |
| 591 | def->m_base.m_copy = NULL; |
| 592 | } |
| 593 | dict = PyModule_GetDict(mod); |
| 594 | if (dict == NULL) |
| 595 | return -1; |
| 596 | def->m_base.m_copy = PyDict_Copy(dict); |
| 597 | if (def->m_base.m_copy == NULL) |
| 598 | return -1; |
| 599 | } |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 600 | PyDict_SetItem(extensions, filename, (PyObject*)def); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | return 0; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 604 | int |
| 605 | _PyImport_FixupBuiltin(PyObject *mod, char *name) |
| 606 | { |
| 607 | int res; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 608 | PyObject *nameobj; |
Victor Stinner | 21fcd0c | 2011-03-07 18:28:15 +0100 | [diff] [blame] | 609 | nameobj = PyUnicode_InternFromString(name); |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 610 | if (nameobj == NULL) |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 611 | return -1; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 612 | res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj); |
| 613 | Py_DECREF(nameobj); |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 614 | return res; |
| 615 | } |
| 616 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 617 | PyObject * |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 618 | _PyImport_FindExtensionObject(PyObject *name, PyObject *filename) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 619 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | PyObject *mod, *mdict; |
| 621 | PyModuleDef* def; |
| 622 | if (extensions == NULL) |
| 623 | return NULL; |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 624 | def = (PyModuleDef*)PyDict_GetItem(extensions, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | if (def == NULL) |
| 626 | return NULL; |
| 627 | if (def->m_size == -1) { |
| 628 | /* Module does not support repeated initialization */ |
| 629 | if (def->m_base.m_copy == NULL) |
| 630 | return NULL; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 631 | mod = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | if (mod == NULL) |
| 633 | return NULL; |
| 634 | mdict = PyModule_GetDict(mod); |
| 635 | if (mdict == NULL) |
| 636 | return NULL; |
| 637 | if (PyDict_Update(mdict, def->m_base.m_copy)) |
| 638 | return NULL; |
| 639 | } |
| 640 | else { |
| 641 | if (def->m_base.m_init == NULL) |
| 642 | return NULL; |
| 643 | mod = def->m_base.m_init(); |
| 644 | if (mod == NULL) |
| 645 | return NULL; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 646 | PyDict_SetItem(PyImport_GetModuleDict(), name, mod); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | Py_DECREF(mod); |
| 648 | } |
| 649 | if (_PyState_AddModule(mod, def) < 0) { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 650 | PyDict_DelItem(PyImport_GetModuleDict(), name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | Py_DECREF(mod); |
| 652 | return NULL; |
| 653 | } |
| 654 | if (Py_VerboseFlag) |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 655 | PySys_FormatStderr("import %U # previously loaded (%R)\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | name, filename); |
| 657 | return mod; |
Brett Cannon | 9a5b25a | 2010-03-01 02:09:17 +0000 | [diff] [blame] | 658 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 661 | PyObject * |
Victor Stinner | 501c09a | 2011-02-23 00:02:00 +0000 | [diff] [blame] | 662 | _PyImport_FindBuiltin(const char *name) |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 663 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 664 | PyObject *res, *nameobj; |
Victor Stinner | 21fcd0c | 2011-03-07 18:28:15 +0100 | [diff] [blame] | 665 | nameobj = PyUnicode_InternFromString(name); |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 666 | if (nameobj == NULL) |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 667 | return NULL; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 668 | res = _PyImport_FindExtensionObject(nameobj, nameobj); |
| 669 | Py_DECREF(nameobj); |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 670 | return res; |
| 671 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 672 | |
| 673 | /* Get the module object corresponding to a module name. |
| 674 | First check the modules dictionary if there's one there, |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 675 | 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] | 676 | Because the former action is most common, THIS DOES NOT RETURN A |
| 677 | 'NEW' REFERENCE! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 678 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 679 | PyObject * |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 680 | PyImport_AddModuleObject(PyObject *name) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 681 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | PyObject *modules = PyImport_GetModuleDict(); |
| 683 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 684 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 685 | if ((m = PyDict_GetItem(modules, name)) != NULL && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | PyModule_Check(m)) |
| 687 | return m; |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 688 | m = PyModule_NewObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | if (m == NULL) |
| 690 | return NULL; |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 691 | if (PyDict_SetItem(modules, name, m) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | Py_DECREF(m); |
| 693 | return NULL; |
| 694 | } |
| 695 | Py_DECREF(m); /* Yes, it still exists, in modules! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 700 | PyObject * |
| 701 | PyImport_AddModule(const char *name) |
| 702 | { |
| 703 | PyObject *nameobj, *module; |
| 704 | nameobj = PyUnicode_FromString(name); |
| 705 | if (nameobj == NULL) |
| 706 | return NULL; |
| 707 | module = PyImport_AddModuleObject(nameobj); |
| 708 | Py_DECREF(nameobj); |
| 709 | return module; |
| 710 | } |
| 711 | |
| 712 | |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 713 | /* Remove name from sys.modules, if it's there. */ |
| 714 | static void |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 715 | remove_module(PyObject *name) |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 716 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | PyObject *modules = PyImport_GetModuleDict(); |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 718 | if (PyDict_GetItem(modules, name) == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | return; |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 720 | if (PyDict_DelItem(modules, name) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | Py_FatalError("import: deleting existing key in" |
| 722 | "sys.modules failed"); |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 723 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 724 | |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 725 | static PyObject * get_sourcefile(PyObject *filename); |
| 726 | static PyObject *make_source_pathname(PyObject *pathname); |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 727 | |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 728 | /* Execute a code object in a module and return the module object |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 729 | * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is |
| 730 | * removed from sys.modules, to avoid leaving damaged module objects |
| 731 | * in sys.modules. The caller may wish to restore the original |
| 732 | * module object (if any) in this case; PyImport_ReloadModule is an |
| 733 | * example. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 734 | * |
| 735 | * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer |
| 736 | * interface. The other two exist primarily for backward compatibility. |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 737 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 738 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 739 | PyImport_ExecCodeModule(char *name, PyObject *co) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | return PyImport_ExecCodeModuleWithPathnames( |
| 742 | name, co, (char *)NULL, (char *)NULL); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 746 | PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 747 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | return PyImport_ExecCodeModuleWithPathnames( |
| 749 | name, co, pathname, (char *)NULL); |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | PyObject * |
| 753 | PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | char *cpathname) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 755 | { |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 756 | PyObject *m = NULL; |
| 757 | PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL; |
| 758 | |
| 759 | nameobj = PyUnicode_FromString(name); |
| 760 | if (nameobj == NULL) |
| 761 | return NULL; |
| 762 | |
| 763 | if (pathname != NULL) { |
| 764 | pathobj = PyUnicode_DecodeFSDefault(pathname); |
| 765 | if (pathobj == NULL) |
| 766 | goto error; |
| 767 | } else |
| 768 | pathobj = NULL; |
| 769 | if (cpathname != NULL) { |
| 770 | cpathobj = PyUnicode_DecodeFSDefault(cpathname); |
| 771 | if (cpathobj == NULL) |
| 772 | goto error; |
| 773 | } else |
| 774 | cpathobj = NULL; |
| 775 | m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); |
| 776 | error: |
| 777 | Py_DECREF(nameobj); |
| 778 | Py_XDECREF(pathobj); |
| 779 | Py_XDECREF(cpathobj); |
| 780 | return m; |
| 781 | } |
| 782 | |
| 783 | PyObject* |
| 784 | PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, |
| 785 | PyObject *cpathname) |
| 786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | PyObject *modules = PyImport_GetModuleDict(); |
| 788 | PyObject *m, *d, *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 789 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 790 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 791 | if (m == NULL) |
| 792 | return NULL; |
| 793 | /* If the module is being reloaded, we get the old module back |
| 794 | and re-use its dict to exec the new code. */ |
| 795 | d = PyModule_GetDict(m); |
| 796 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 797 | if (PyDict_SetItemString(d, "__builtins__", |
| 798 | PyEval_GetBuiltins()) != 0) |
| 799 | goto error; |
| 800 | } |
| 801 | /* Remember the filename as the __file__ attribute */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 802 | if (pathname != NULL) { |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 803 | v = get_sourcefile(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | if (v == NULL) |
| 805 | PyErr_Clear(); |
| 806 | } |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 807 | else |
| 808 | v = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | if (v == NULL) { |
| 810 | v = ((PyCodeObject *)co)->co_filename; |
| 811 | Py_INCREF(v); |
| 812 | } |
| 813 | if (PyDict_SetItemString(d, "__file__", v) != 0) |
| 814 | PyErr_Clear(); /* Not important enough to report */ |
| 815 | Py_DECREF(v); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | /* Remember the pyc path name as the __cached__ attribute. */ |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 818 | if (cpathname != NULL) |
| 819 | v = cpathname; |
| 820 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | v = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | if (PyDict_SetItemString(d, "__cached__", v) != 0) |
| 823 | PyErr_Clear(); /* Not important enough to report */ |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 824 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 825 | v = PyEval_EvalCode(co, d, d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | if (v == NULL) |
| 827 | goto error; |
| 828 | Py_DECREF(v); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 829 | |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 830 | if ((m = PyDict_GetItem(modules, name)) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 831 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 27ee089 | 2011-03-04 12:57:09 +0000 | [diff] [blame] | 832 | "Loaded module %R not found in sys.modules", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | name); |
| 834 | return NULL; |
| 835 | } |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | return m; |
Tim Peters | 1cd7017 | 2004-08-02 03:52:12 +0000 | [diff] [blame] | 840 | |
| 841 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | remove_module(name); |
| 843 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 847 | /* Like rightmost_sep, but operate on unicode objects. */ |
| 848 | static Py_ssize_t |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 849 | rightmost_sep_obj(PyObject* o, Py_ssize_t start, Py_ssize_t end) |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 850 | { |
| 851 | Py_ssize_t found, i; |
| 852 | Py_UCS4 c; |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 853 | for (found = -1, i = start; i < end; i++) { |
Martin v. Löwis | 2db7286 | 2011-10-23 17:29:08 +0200 | [diff] [blame] | 854 | c = PyUnicode_READ_CHAR(o, i); |
| 855 | if (c == SEP |
| 856 | #ifdef ALTSEP |
| 857 | || c == ALTSEP |
| 858 | #endif |
| 859 | ) |
| 860 | { |
| 861 | found = i; |
| 862 | } |
| 863 | } |
| 864 | return found; |
| 865 | } |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 866 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 867 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 868 | /* Given a pathname to a Python byte compiled file, return the path to the |
| 869 | source file, if the path matches the PEP 3147 format. This does not check |
| 870 | for any file existence, however, if the pyc file name does not match PEP |
| 871 | 3147 style, NULL is returned. buf must be at least as big as pathname; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 872 | the resulting path will always be shorter. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 874 | (...)/__pycache__/foo.<tag>.pyc -> (...)/foo.py */ |
| 875 | |
| 876 | static PyObject* |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 877 | make_source_pathname(PyObject *path) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 878 | { |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 879 | Py_ssize_t left, right, dot0, dot1, len; |
| 880 | Py_ssize_t i, j; |
| 881 | PyObject *result; |
| 882 | int kind; |
| 883 | void *data; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 884 | |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 885 | len = PyUnicode_GET_LENGTH(path); |
| 886 | if (len > MAXPATHLEN) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 887 | return NULL; |
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 | /* Look back two slashes from the end. In between these two slashes |
| 890 | must be the string __pycache__ or this is not a PEP 3147 style |
| 891 | path. It's possible for there to be only one slash. |
| 892 | */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 893 | right = rightmost_sep_obj(path, 0, len); |
| 894 | if (right == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | return NULL; |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 896 | left = rightmost_sep_obj(path, 0, right); |
| 897 | if (left == -1) |
| 898 | left = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | else |
| 900 | left++; |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 901 | if (right-left != sizeof(CACHEDIR)-1) |
| 902 | return NULL; |
| 903 | for (i = 0; i < sizeof(CACHEDIR)-1; i++) |
| 904 | if (PyUnicode_READ_CHAR(path, left+i) != CACHEDIR[i]) |
| 905 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | /* Now verify that the path component to the right of the last slash |
| 908 | has two dots in it. |
| 909 | */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 910 | dot0 = PyUnicode_FindChar(path, '.', right+1, len, 1); |
| 911 | if (dot0 < 0) |
| 912 | return NULL; |
| 913 | dot1 = PyUnicode_FindChar(path, '.', dot0+1, len, 1); |
| 914 | if (dot1 < 0) |
| 915 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | /* Too many dots? */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 917 | if (PyUnicode_FindChar(path, '.', dot1+1, len, 1) != -1) |
| 918 | return NULL; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | /* This is a PEP 3147 path. Start by copying everything from the |
| 921 | start of pathname up to and including the leftmost slash. Then |
| 922 | copy the file's basename, removing the magic tag and adding a .py |
| 923 | suffix. |
| 924 | */ |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 925 | result = PyUnicode_New(left + (dot0-right) + 2, |
| 926 | PyUnicode_MAX_CHAR_VALUE(path)); |
| 927 | if (!result) |
| 928 | return NULL; |
| 929 | kind = PyUnicode_KIND(result); |
| 930 | data = PyUnicode_DATA(result); |
| 931 | PyUnicode_CopyCharacters(result, 0, path, 0, (i = left)); |
| 932 | PyUnicode_CopyCharacters(result, left, path, right+1, |
| 933 | (j = dot0-right)); |
| 934 | PyUnicode_WRITE(kind, data, i+j, 'p'); |
| 935 | PyUnicode_WRITE(kind, data, i+j+1, 'y'); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 936 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | 8a0ef78 | 2011-10-23 18:05:06 +0200 | [diff] [blame] | 937 | return result; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 941 | static void |
| 942 | update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) |
| 943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | PyObject *constants, *tmp; |
| 945 | Py_ssize_t i, n; |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | if (PyUnicode_Compare(co->co_filename, oldname)) |
| 948 | return; |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | tmp = co->co_filename; |
| 951 | co->co_filename = newname; |
| 952 | Py_INCREF(co->co_filename); |
| 953 | Py_DECREF(tmp); |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | constants = co->co_consts; |
| 956 | n = PyTuple_GET_SIZE(constants); |
| 957 | for (i = 0; i < n; i++) { |
| 958 | tmp = PyTuple_GET_ITEM(constants, i); |
| 959 | if (PyCode_Check(tmp)) |
| 960 | update_code_filenames((PyCodeObject *)tmp, |
| 961 | oldname, newname); |
| 962 | } |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 965 | static void |
| 966 | update_compiled_module(PyCodeObject *co, PyObject *newname) |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 967 | { |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 968 | PyObject *oldname; |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 969 | |
Victor Stinner | 2f42ae5 | 2011-03-20 00:41:24 +0100 | [diff] [blame] | 970 | if (PyUnicode_Compare(co->co_filename, newname) == 0) |
| 971 | return; |
Hirokazu Yamamoto | 4f447fb | 2009-03-04 01:52:10 +0000 | [diff] [blame] | 972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | oldname = co->co_filename; |
| 974 | Py_INCREF(oldname); |
| 975 | update_code_filenames(co, oldname, newname); |
| 976 | Py_DECREF(oldname); |
Antoine Pitrou | d35cbf6 | 2009-01-06 19:02:24 +0000 | [diff] [blame] | 977 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 978 | |
Brett Cannon | 442c9b9 | 2011-03-23 16:14:42 -0700 | [diff] [blame] | 979 | static PyObject * |
| 980 | imp_fix_co_filename(PyObject *self, PyObject *args) |
| 981 | { |
| 982 | PyObject *co; |
| 983 | PyObject *file_path; |
| 984 | |
| 985 | if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path)) |
| 986 | return NULL; |
| 987 | |
| 988 | if (!PyCode_Check(co)) { |
| 989 | PyErr_SetString(PyExc_TypeError, |
| 990 | "first argument must be a code object"); |
| 991 | return NULL; |
| 992 | } |
| 993 | |
| 994 | if (!PyUnicode_Check(file_path)) { |
| 995 | PyErr_SetString(PyExc_TypeError, |
| 996 | "second argument must be a string"); |
| 997 | return NULL; |
| 998 | } |
| 999 | |
| 1000 | update_compiled_module((PyCodeObject*)co, file_path); |
| 1001 | |
| 1002 | Py_RETURN_NONE; |
| 1003 | } |
| 1004 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1005 | |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1006 | /* Get source file -> unicode or None |
| 1007 | * Returns the path to the py file if available, else the given path |
| 1008 | */ |
| 1009 | static PyObject * |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1010 | get_sourcefile(PyObject *filename) |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1011 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | Py_ssize_t len; |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1013 | PyObject *py; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | struct stat statbuf; |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1015 | int err; |
Victor Stinner | 81c39a8 | 2012-06-16 03:22:05 +0200 | [diff] [blame] | 1016 | void *data; |
| 1017 | unsigned int kind; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1018 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1019 | len = PyUnicode_GET_LENGTH(filename); |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1020 | if (len == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | Py_RETURN_NONE; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1022 | |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1023 | /* don't match *.pyc or *.pyo? */ |
Victor Stinner | 81c39a8 | 2012-06-16 03:22:05 +0200 | [diff] [blame] | 1024 | data = PyUnicode_DATA(filename); |
| 1025 | kind = PyUnicode_KIND(filename); |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1026 | if (len < 5 |
Victor Stinner | 81c39a8 | 2012-06-16 03:22:05 +0200 | [diff] [blame] | 1027 | || PyUnicode_READ(kind, data, len-4) != '.' |
| 1028 | || (PyUnicode_READ(kind, data, len-3) != 'p' |
| 1029 | && PyUnicode_READ(kind, data, len-3) != 'P') |
| 1030 | || (PyUnicode_READ(kind, data, len-2) != 'y' |
| 1031 | && PyUnicode_READ(kind, data, len-2) != 'Y')) |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1032 | goto unchanged; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | /* Start by trying to turn PEP 3147 path into source path. If that |
| 1035 | * fails, just chop off the trailing character, i.e. legacy pyc path |
| 1036 | * to py. |
| 1037 | */ |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1038 | py = make_source_pathname(filename); |
| 1039 | if (py == NULL) { |
| 1040 | PyErr_Clear(); |
Victor Stinner | 81c39a8 | 2012-06-16 03:22:05 +0200 | [diff] [blame] | 1041 | py = PyUnicode_Substring(filename, 0, len - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | } |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1043 | if (py == NULL) |
| 1044 | goto error; |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1045 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1046 | err = _Py_stat(py, &statbuf); |
| 1047 | if (err == -2) |
| 1048 | goto error; |
Victor Stinner | 81c39a8 | 2012-06-16 03:22:05 +0200 | [diff] [blame] | 1049 | if (err == 0 && S_ISREG(statbuf.st_mode)) |
Victor Stinner | c9abda0 | 2011-03-14 13:33:46 -0400 | [diff] [blame] | 1050 | return py; |
| 1051 | Py_DECREF(py); |
| 1052 | goto unchanged; |
| 1053 | |
| 1054 | error: |
| 1055 | PyErr_Clear(); |
| 1056 | unchanged: |
| 1057 | Py_INCREF(filename); |
| 1058 | return filename; |
Christian Heimes | 3b06e53 | 2008-01-07 20:12:44 +0000 | [diff] [blame] | 1059 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1060 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1061 | /* Forward */ |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1062 | static struct _frozen * find_frozen(PyObject *); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1063 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1064 | |
| 1065 | /* Helper to test for built-in module */ |
| 1066 | |
| 1067 | static int |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1068 | is_builtin(PyObject *name) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1069 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1070 | int i, cmp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1072 | cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name); |
| 1073 | if (cmp == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | if (PyImport_Inittab[i].initfunc == NULL) |
| 1075 | return -1; |
| 1076 | else |
| 1077 | return 1; |
| 1078 | } |
| 1079 | } |
| 1080 | return 0; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1083 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1084 | /* Return an importer object for a sys.path/pkg.__path__ item 'p', |
| 1085 | possibly by fetching it from the path_importer_cache dict. If it |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1086 | 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] | 1087 | that can handle the path item. Return None if no hook could; |
| 1088 | this tells our caller it should fall back to the builtin |
| 1089 | import mechanism. Cache the result in path_importer_cache. |
| 1090 | Returns a borrowed reference. */ |
| 1091 | |
| 1092 | static PyObject * |
| 1093 | get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | PyObject *p) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1095 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | PyObject *importer; |
| 1097 | Py_ssize_t j, nhooks; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | /* These conditions are the caller's responsibility: */ |
| 1100 | assert(PyList_Check(path_hooks)); |
| 1101 | assert(PyDict_Check(path_importer_cache)); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | nhooks = PyList_Size(path_hooks); |
| 1104 | if (nhooks < 0) |
| 1105 | return NULL; /* Shouldn't happen */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | importer = PyDict_GetItem(path_importer_cache, p); |
| 1108 | if (importer != NULL) |
| 1109 | return importer; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | /* set path_importer_cache[p] to None to avoid recursion */ |
| 1112 | if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) |
| 1113 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | for (j = 0; j < nhooks; j++) { |
| 1116 | PyObject *hook = PyList_GetItem(path_hooks, j); |
| 1117 | if (hook == NULL) |
| 1118 | return NULL; |
| 1119 | importer = PyObject_CallFunctionObjArgs(hook, p, NULL); |
| 1120 | if (importer != NULL) |
| 1121 | break; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | PyErr_Clear(); |
| 1127 | } |
| 1128 | if (importer == NULL) { |
Brett Cannon | aa93642 | 2012-04-27 15:30:58 -0400 | [diff] [blame] | 1129 | return Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | } |
| 1131 | if (importer != NULL) { |
| 1132 | int err = PyDict_SetItem(path_importer_cache, p, importer); |
| 1133 | Py_DECREF(importer); |
| 1134 | if (err != 0) |
| 1135 | return NULL; |
| 1136 | } |
| 1137 | return importer; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1140 | PyAPI_FUNC(PyObject *) |
| 1141 | PyImport_GetImporter(PyObject *path) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) { |
| 1145 | if ((path_hooks = PySys_GetObject("path_hooks"))) { |
| 1146 | importer = get_path_importer(path_importer_cache, |
| 1147 | path_hooks, path); |
| 1148 | } |
| 1149 | } |
| 1150 | Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */ |
| 1151 | return importer; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1154 | |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1155 | static int init_builtin(PyObject *); /* Forward */ |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1156 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1157 | /* Initialize a built-in module. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1158 | 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] | 1159 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1160 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1161 | static int |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1162 | init_builtin(PyObject *name) |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1163 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | struct _inittab *p; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1165 | |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1166 | if (_PyImport_FindExtensionObject(name, name) != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | return 1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | for (p = PyImport_Inittab; p->name != NULL; p++) { |
| 1170 | PyObject *mod; |
Antoine Pitrou | 6c40eb7 | 2012-01-18 20:16:09 +0100 | [diff] [blame] | 1171 | PyModuleDef *def; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1172 | if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | if (p->initfunc == NULL) { |
| 1174 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1175 | "Cannot re-init internal module %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1176 | name); |
| 1177 | return -1; |
| 1178 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | mod = (*p->initfunc)(); |
| 1180 | if (mod == 0) |
| 1181 | return -1; |
Antoine Pitrou | 6c40eb7 | 2012-01-18 20:16:09 +0100 | [diff] [blame] | 1182 | /* Remember pointer to module init function. */ |
| 1183 | def = PyModule_GetDef(mod); |
| 1184 | def->m_base.m_init = p->initfunc; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1185 | if (_PyImport_FixupExtensionObject(mod, name, name) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | return -1; |
| 1187 | /* FixupExtension has put the module into sys.modules, |
| 1188 | so we can release our own reference. */ |
| 1189 | Py_DECREF(mod); |
| 1190 | return 1; |
| 1191 | } |
| 1192 | } |
| 1193 | return 0; |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1194 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1195 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1196 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1197 | /* Frozen modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1198 | |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1199 | static struct _frozen * |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1200 | find_frozen(PyObject *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | struct _frozen *p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1203 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1204 | if (name == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | return NULL; |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 1206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | for (p = PyImport_FrozenModules; ; p++) { |
| 1208 | if (p->name == NULL) |
| 1209 | return NULL; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1210 | if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | break; |
| 1212 | } |
| 1213 | return p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1216 | static PyObject * |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1217 | get_frozen_object(PyObject *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | struct _frozen *p = find_frozen(name); |
| 1220 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | if (p == NULL) { |
| 1223 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1224 | "No such frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | name); |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | if (p->code == NULL) { |
| 1229 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1230 | "Excluded frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1231 | name); |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | size = p->size; |
| 1235 | if (size < 0) |
| 1236 | size = -size; |
| 1237 | return PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1240 | static PyObject * |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1241 | is_frozen_package(PyObject *name) |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | struct _frozen *p = find_frozen(name); |
| 1244 | int size; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | if (p == NULL) { |
| 1247 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1248 | "No such frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | name); |
| 1250 | return NULL; |
| 1251 | } |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | size = p->size; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | if (size < 0) |
| 1256 | Py_RETURN_TRUE; |
| 1257 | else |
| 1258 | Py_RETURN_FALSE; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1262 | /* Initialize a frozen module. |
Brett Cannon | 3c2ac44 | 2009-03-08 20:49:47 +0000 | [diff] [blame] | 1263 | 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] | 1264 | an exception set if the initialization failed. |
| 1265 | This function is also used from frozenmain.c */ |
Guido van Rossum | 0b34490 | 1995-02-07 15:35:27 +0000 | [diff] [blame] | 1266 | |
| 1267 | int |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1268 | PyImport_ImportFrozenModuleObject(PyObject *name) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1269 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1270 | struct _frozen *p; |
| 1271 | PyObject *co, *m, *path; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | int ispackage; |
| 1273 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1274 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1275 | p = find_frozen(name); |
| 1276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | if (p == NULL) |
| 1278 | return 0; |
| 1279 | if (p->code == NULL) { |
| 1280 | PyErr_Format(PyExc_ImportError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1281 | "Excluded frozen object named %R", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | name); |
| 1283 | return -1; |
| 1284 | } |
| 1285 | size = p->size; |
| 1286 | ispackage = (size < 0); |
| 1287 | if (ispackage) |
| 1288 | size = -size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | co = PyMarshal_ReadObjectFromString((char *)p->code, size); |
| 1290 | if (co == NULL) |
| 1291 | return -1; |
| 1292 | if (!PyCode_Check(co)) { |
| 1293 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1294 | "frozen object %R is not a code object", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1295 | name); |
| 1296 | goto err_return; |
| 1297 | } |
| 1298 | if (ispackage) { |
| 1299 | /* Set __path__ to the package name */ |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1300 | PyObject *d, *l; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | int err; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1302 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | if (m == NULL) |
| 1304 | goto err_return; |
| 1305 | d = PyModule_GetDict(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | l = PyList_New(1); |
| 1307 | if (l == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1308 | goto err_return; |
| 1309 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1310 | Py_INCREF(name); |
| 1311 | PyList_SET_ITEM(l, 0, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | err = PyDict_SetItemString(d, "__path__", l); |
| 1313 | Py_DECREF(l); |
| 1314 | if (err != 0) |
| 1315 | goto err_return; |
| 1316 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1317 | path = PyUnicode_FromString("<frozen>"); |
| 1318 | if (path == NULL) |
| 1319 | goto err_return; |
| 1320 | m = PyImport_ExecCodeModuleObject(name, co, path, NULL); |
| 1321 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | if (m == NULL) |
| 1323 | goto err_return; |
| 1324 | Py_DECREF(co); |
| 1325 | Py_DECREF(m); |
| 1326 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1327 | err_return: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | Py_DECREF(co); |
| 1329 | return -1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1330 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1331 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1332 | int |
| 1333 | PyImport_ImportFrozenModule(char *name) |
| 1334 | { |
| 1335 | PyObject *nameobj; |
| 1336 | int ret; |
| 1337 | nameobj = PyUnicode_InternFromString(name); |
| 1338 | if (nameobj == NULL) |
| 1339 | return -1; |
| 1340 | ret = PyImport_ImportFrozenModuleObject(nameobj); |
| 1341 | Py_DECREF(nameobj); |
| 1342 | return ret; |
| 1343 | } |
| 1344 | |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1345 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1346 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1347 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1348 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1349 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1350 | PyImport_ImportModule(const char *name) |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1351 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1352 | PyObject *pname; |
| 1353 | PyObject *result; |
Marc-André Lemburg | 3c61c35 | 2001-02-09 19:40:15 +0000 | [diff] [blame] | 1354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | pname = PyUnicode_FromString(name); |
| 1356 | if (pname == NULL) |
| 1357 | return NULL; |
| 1358 | result = PyImport_Import(pname); |
| 1359 | Py_DECREF(pname); |
| 1360 | return result; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1363 | /* Import a module without blocking |
| 1364 | * |
| 1365 | * At first it tries to fetch the module from sys.modules. If the module was |
| 1366 | * never loaded before it loads it with PyImport_ImportModule() unless another |
| 1367 | * thread holds the import lock. In the latter case the function raises an |
| 1368 | * ImportError instead of blocking. |
| 1369 | * |
| 1370 | * Returns the module object with incremented ref count. |
| 1371 | */ |
| 1372 | PyObject * |
| 1373 | PyImport_ImportModuleNoBlock(const char *name) |
| 1374 | { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1375 | return PyImport_ImportModule(name); |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1378 | |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1379 | /* Remove importlib frames from the traceback, |
| 1380 | * except in Verbose mode. */ |
| 1381 | static void |
| 1382 | remove_importlib_frames(void) |
| 1383 | { |
| 1384 | const char *importlib_filename = "<frozen importlib._bootstrap>"; |
| 1385 | const char *exec_funcname = "_exec_module"; |
| 1386 | int always_trim = 0; |
| 1387 | int in_importlib; |
| 1388 | PyObject *exception, *value, *base_tb, *tb; |
| 1389 | PyObject **prev_link, **outer_link; |
| 1390 | |
| 1391 | /* Synopsis: if it's an ImportError, we trim all importlib chunks |
| 1392 | from the traceback. Otherwise, we trim only those chunks which |
| 1393 | end with a call to "_exec_module". */ |
| 1394 | |
| 1395 | PyErr_Fetch(&exception, &value, &base_tb); |
| 1396 | if (!exception || Py_VerboseFlag) |
| 1397 | goto done; |
| 1398 | if (PyType_IsSubtype((PyTypeObject *) exception, |
| 1399 | (PyTypeObject *) PyExc_ImportError)) |
| 1400 | always_trim = 1; |
| 1401 | |
| 1402 | prev_link = &base_tb; |
| 1403 | tb = base_tb; |
| 1404 | in_importlib = 0; |
| 1405 | while (tb != NULL) { |
| 1406 | PyTracebackObject *traceback = (PyTracebackObject *)tb; |
| 1407 | PyObject *next = (PyObject *) traceback->tb_next; |
| 1408 | PyFrameObject *frame = traceback->tb_frame; |
| 1409 | PyCodeObject *code = frame->f_code; |
| 1410 | int now_in_importlib; |
| 1411 | |
| 1412 | assert(PyTraceBack_Check(tb)); |
| 1413 | now_in_importlib = (PyUnicode_CompareWithASCIIString( |
| 1414 | code->co_filename, |
| 1415 | importlib_filename) == 0); |
| 1416 | if (now_in_importlib && !in_importlib) { |
| 1417 | /* This is the link to this chunk of importlib tracebacks */ |
| 1418 | outer_link = prev_link; |
| 1419 | } |
| 1420 | in_importlib = now_in_importlib; |
| 1421 | |
| 1422 | if (in_importlib && |
| 1423 | (always_trim || |
| 1424 | PyUnicode_CompareWithASCIIString(code->co_name, |
| 1425 | exec_funcname) == 0)) { |
| 1426 | PyObject *tmp = *outer_link; |
| 1427 | *outer_link = next; |
| 1428 | Py_XINCREF(next); |
| 1429 | Py_DECREF(tmp); |
| 1430 | prev_link = outer_link; |
| 1431 | } |
| 1432 | else { |
| 1433 | prev_link = (PyObject **) &traceback->tb_next; |
| 1434 | } |
| 1435 | tb = next; |
| 1436 | } |
| 1437 | done: |
| 1438 | PyErr_Restore(exception, value, base_tb); |
| 1439 | } |
| 1440 | |
| 1441 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1442 | PyObject * |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1443 | PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals, |
| 1444 | PyObject *locals, PyObject *given_fromlist, |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 1445 | int level) |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1446 | { |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1447 | _Py_IDENTIFIER(__import__); |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1448 | _Py_IDENTIFIER(__initializing__); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1449 | _Py_IDENTIFIER(__package__); |
| 1450 | _Py_IDENTIFIER(__path__); |
| 1451 | _Py_IDENTIFIER(__name__); |
| 1452 | _Py_IDENTIFIER(_find_and_load); |
| 1453 | _Py_IDENTIFIER(_handle_fromlist); |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1454 | _Py_IDENTIFIER(_lock_unlock_module); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1455 | _Py_static_string(single_dot, "."); |
| 1456 | PyObject *abs_name = NULL; |
| 1457 | PyObject *builtins_import = NULL; |
| 1458 | PyObject *final_mod = NULL; |
| 1459 | PyObject *mod = NULL; |
| 1460 | PyObject *package = NULL; |
| 1461 | PyObject *globals = NULL; |
| 1462 | PyObject *fromlist = NULL; |
| 1463 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 1464 | |
| 1465 | /* Make sure to use default values so as to not have |
| 1466 | PyObject_CallMethodObjArgs() truncate the parameter list because of a |
| 1467 | NULL argument. */ |
| 1468 | if (given_globals == NULL) { |
| 1469 | globals = PyDict_New(); |
| 1470 | if (globals == NULL) { |
| 1471 | goto error; |
| 1472 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1474 | else { |
| 1475 | /* Only have to care what given_globals is if it will be used |
| 1476 | fortsomething. */ |
| 1477 | if (level > 0 && !PyDict_Check(given_globals)) { |
| 1478 | PyErr_SetString(PyExc_TypeError, "globals must be a dict"); |
| 1479 | goto error; |
| 1480 | } |
| 1481 | globals = given_globals; |
| 1482 | Py_INCREF(globals); |
| 1483 | } |
| 1484 | |
| 1485 | if (given_fromlist == NULL) { |
| 1486 | fromlist = PyList_New(0); |
| 1487 | if (fromlist == NULL) { |
| 1488 | goto error; |
| 1489 | } |
| 1490 | } |
| 1491 | else { |
| 1492 | fromlist = given_fromlist; |
| 1493 | Py_INCREF(fromlist); |
| 1494 | } |
| 1495 | if (name == NULL) { |
| 1496 | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
| 1497 | goto error; |
| 1498 | } |
| 1499 | |
| 1500 | /* The below code is importlib.__import__() & _gcd_import(), ported to C |
| 1501 | for added performance. */ |
| 1502 | |
| 1503 | if (!PyUnicode_Check(name)) { |
| 1504 | PyErr_SetString(PyExc_TypeError, "module name must be a string"); |
| 1505 | goto error; |
| 1506 | } |
| 1507 | else if (PyUnicode_READY(name) < 0) { |
| 1508 | goto error; |
| 1509 | } |
| 1510 | if (level < 0) { |
| 1511 | PyErr_SetString(PyExc_ValueError, "level must be >= 0"); |
| 1512 | goto error; |
| 1513 | } |
| 1514 | else if (level > 0) { |
| 1515 | package = _PyDict_GetItemId(globals, &PyId___package__); |
| 1516 | if (package != NULL && package != Py_None) { |
| 1517 | Py_INCREF(package); |
| 1518 | if (!PyUnicode_Check(package)) { |
| 1519 | PyErr_SetString(PyExc_TypeError, "package must be a string"); |
| 1520 | goto error; |
| 1521 | } |
| 1522 | } |
| 1523 | else { |
Brett Cannon | 273323c | 2012-04-17 19:05:11 -0400 | [diff] [blame] | 1524 | package = _PyDict_GetItemId(globals, &PyId___name__); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1525 | if (package == NULL) { |
Brett Cannon | 273323c | 2012-04-17 19:05:11 -0400 | [diff] [blame] | 1526 | PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1527 | goto error; |
| 1528 | } |
| 1529 | else if (!PyUnicode_Check(package)) { |
| 1530 | PyErr_SetString(PyExc_TypeError, "__name__ must be a string"); |
| 1531 | } |
| 1532 | Py_INCREF(package); |
| 1533 | |
| 1534 | if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) { |
Brett Cannon | 740fce0 | 2012-04-14 14:23:49 -0400 | [diff] [blame] | 1535 | PyObject *partition = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1536 | PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot); |
| 1537 | if (borrowed_dot == NULL) { |
| 1538 | goto error; |
| 1539 | } |
Brett Cannon | 740fce0 | 2012-04-14 14:23:49 -0400 | [diff] [blame] | 1540 | partition = PyUnicode_RPartition(package, borrowed_dot); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1541 | Py_DECREF(package); |
| 1542 | if (partition == NULL) { |
| 1543 | goto error; |
| 1544 | } |
| 1545 | package = PyTuple_GET_ITEM(partition, 0); |
| 1546 | Py_INCREF(package); |
| 1547 | Py_DECREF(partition); |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | if (PyDict_GetItem(interp->modules, package) == NULL) { |
| 1552 | PyErr_Format(PyExc_SystemError, |
| 1553 | "Parent module %R not loaded, cannot perform relative " |
| 1554 | "import", package); |
| 1555 | goto error; |
| 1556 | } |
| 1557 | } |
| 1558 | else { /* level == 0 */ |
| 1559 | if (PyUnicode_GET_LENGTH(name) == 0) { |
| 1560 | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
| 1561 | goto error; |
| 1562 | } |
| 1563 | package = Py_None; |
| 1564 | Py_INCREF(package); |
| 1565 | } |
| 1566 | |
| 1567 | if (level > 0) { |
| 1568 | Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package); |
| 1569 | PyObject *base = NULL; |
| 1570 | int level_up = 1; |
| 1571 | |
| 1572 | for (level_up = 1; level_up < level; level_up += 1) { |
| 1573 | last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1); |
| 1574 | if (last_dot == -2) { |
| 1575 | goto error; |
| 1576 | } |
| 1577 | else if (last_dot == -1) { |
| 1578 | PyErr_SetString(PyExc_ValueError, |
| 1579 | "attempted relative import beyond top-level " |
| 1580 | "package"); |
| 1581 | goto error; |
| 1582 | } |
| 1583 | } |
| 1584 | base = PyUnicode_Substring(package, 0, last_dot); |
| 1585 | if (PyUnicode_GET_LENGTH(name) > 0) { |
Antoine Pitrou | 538ba2a | 2012-04-16 21:52:45 +0200 | [diff] [blame] | 1586 | PyObject *borrowed_dot, *seq = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1587 | |
| 1588 | borrowed_dot = _PyUnicode_FromId(&single_dot); |
Antoine Pitrou | 538ba2a | 2012-04-16 21:52:45 +0200 | [diff] [blame] | 1589 | seq = PyTuple_Pack(2, base, name); |
| 1590 | Py_DECREF(base); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1591 | if (borrowed_dot == NULL || seq == NULL) { |
| 1592 | goto error; |
| 1593 | } |
| 1594 | |
| 1595 | abs_name = PyUnicode_Join(borrowed_dot, seq); |
| 1596 | Py_DECREF(seq); |
| 1597 | if (abs_name == NULL) { |
| 1598 | goto error; |
| 1599 | } |
| 1600 | } |
| 1601 | else { |
| 1602 | abs_name = base; |
| 1603 | } |
| 1604 | } |
| 1605 | else { |
| 1606 | abs_name = name; |
| 1607 | Py_INCREF(abs_name); |
| 1608 | } |
| 1609 | |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1610 | #ifdef WITH_THREAD |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1611 | _PyImport_AcquireLock(); |
| 1612 | #endif |
| 1613 | /* From this point forward, goto error_with_unlock! */ |
| 1614 | if (PyDict_Check(globals)) { |
| 1615 | builtins_import = _PyDict_GetItemId(globals, &PyId___import__); |
| 1616 | } |
| 1617 | if (builtins_import == NULL) { |
| 1618 | builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__); |
| 1619 | if (builtins_import == NULL) { |
| 1620 | Py_FatalError("__import__ missing"); |
| 1621 | } |
| 1622 | } |
| 1623 | Py_INCREF(builtins_import); |
| 1624 | |
| 1625 | mod = PyDict_GetItem(interp->modules, abs_name); |
| 1626 | if (mod == Py_None) { |
Brett Cannon | 27fc528 | 2012-04-15 14:15:31 -0400 | [diff] [blame] | 1627 | PyObject *msg = PyUnicode_FromFormat("import of %R halted; " |
| 1628 | "None in sys.modules", abs_name); |
| 1629 | if (msg != NULL) { |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 1630 | PyErr_SetImportError(msg, abs_name, NULL); |
| 1631 | Py_DECREF(msg); |
Brett Cannon | 27fc528 | 2012-04-15 14:15:31 -0400 | [diff] [blame] | 1632 | } |
Antoine Pitrou | 71382cb | 2012-04-16 18:48:49 +0200 | [diff] [blame] | 1633 | mod = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1634 | goto error_with_unlock; |
| 1635 | } |
| 1636 | else if (mod != NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1637 | PyObject *value; |
| 1638 | int initializing = 0; |
| 1639 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1640 | Py_INCREF(mod); |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1641 | /* Only call _bootstrap._lock_unlock_module() if __initializing__ is true. */ |
| 1642 | value = _PyObject_GetAttrId(mod, &PyId___initializing__); |
| 1643 | if (value == NULL) |
| 1644 | PyErr_Clear(); |
| 1645 | else { |
| 1646 | initializing = PyObject_IsTrue(value); |
| 1647 | Py_DECREF(value); |
| 1648 | if (initializing == -1) |
| 1649 | PyErr_Clear(); |
| 1650 | } |
| 1651 | if (initializing > 0) { |
| 1652 | /* _bootstrap._lock_unlock_module() releases the import lock */ |
| 1653 | value = _PyObject_CallMethodObjIdArgs(interp->importlib, |
| 1654 | &PyId__lock_unlock_module, abs_name, |
| 1655 | NULL); |
| 1656 | if (value == NULL) |
| 1657 | goto error; |
| 1658 | Py_DECREF(value); |
| 1659 | } |
| 1660 | else { |
| 1661 | #ifdef WITH_THREAD |
| 1662 | if (_PyImport_ReleaseLock() < 0) { |
| 1663 | PyErr_SetString(PyExc_RuntimeError, "not holding the import lock"); |
| 1664 | goto error; |
| 1665 | } |
| 1666 | #endif |
| 1667 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1668 | } |
| 1669 | else { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1670 | /* _bootstrap._find_and_load() releases the import lock */ |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1671 | mod = _PyObject_CallMethodObjIdArgs(interp->importlib, |
| 1672 | &PyId__find_and_load, abs_name, |
| 1673 | builtins_import, NULL); |
| 1674 | if (mod == NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1675 | goto error; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1676 | } |
| 1677 | } |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1678 | /* From now on we don't hold the import lock anymore. */ |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1679 | |
| 1680 | if (PyObject_Not(fromlist)) { |
| 1681 | if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) { |
| 1682 | PyObject *front = NULL; |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1683 | PyObject *partition = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1684 | PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot); |
| 1685 | |
| 1686 | if (borrowed_dot == NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1687 | goto error; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1688 | } |
| 1689 | |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1690 | partition = PyUnicode_Partition(name, borrowed_dot); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1691 | if (partition == NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1692 | goto error; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1693 | } |
| 1694 | |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 1695 | if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) { |
| 1696 | /* No dot in module name, simple exit */ |
| 1697 | Py_DECREF(partition); |
| 1698 | final_mod = mod; |
| 1699 | Py_INCREF(mod); |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1700 | goto error; |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 1701 | } |
| 1702 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1703 | front = PyTuple_GET_ITEM(partition, 0); |
| 1704 | Py_INCREF(front); |
| 1705 | Py_DECREF(partition); |
| 1706 | |
| 1707 | if (level == 0) { |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 1708 | final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL); |
Antoine Pitrou | b78174c | 2012-05-06 17:15:23 +0200 | [diff] [blame] | 1709 | Py_DECREF(front); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1710 | } |
| 1711 | else { |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 1712 | Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) - |
| 1713 | PyUnicode_GET_LENGTH(front); |
| 1714 | Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name); |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 1715 | PyObject *to_return = PyUnicode_Substring(abs_name, 0, |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1716 | abs_name_len - cut_off); |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 1717 | Py_DECREF(front); |
| 1718 | if (to_return == NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1719 | goto error; |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 1720 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1721 | |
| 1722 | final_mod = PyDict_GetItem(interp->modules, to_return); |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 1723 | if (final_mod == NULL) { |
| 1724 | PyErr_Format(PyExc_KeyError, |
| 1725 | "%R not in sys.modules as expected", |
| 1726 | to_return); |
| 1727 | } |
| 1728 | else { |
| 1729 | Py_INCREF(final_mod); |
| 1730 | } |
Antoine Pitrou | b78174c | 2012-05-06 17:15:23 +0200 | [diff] [blame] | 1731 | Py_DECREF(to_return); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | else { |
| 1735 | final_mod = mod; |
| 1736 | Py_INCREF(mod); |
| 1737 | } |
| 1738 | } |
| 1739 | else { |
| 1740 | final_mod = _PyObject_CallMethodObjIdArgs(interp->importlib, |
| 1741 | &PyId__handle_fromlist, mod, |
| 1742 | fromlist, builtins_import, |
| 1743 | NULL); |
| 1744 | } |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1745 | goto error; |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 1746 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1747 | error_with_unlock: |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1748 | #ifdef WITH_THREAD |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1749 | if (_PyImport_ReleaseLock() < 0) { |
| 1750 | PyErr_SetString(PyExc_RuntimeError, "not holding the import lock"); |
| 1751 | } |
| 1752 | #endif |
| 1753 | error: |
| 1754 | Py_XDECREF(abs_name); |
| 1755 | Py_XDECREF(builtins_import); |
| 1756 | Py_XDECREF(mod); |
| 1757 | Py_XDECREF(package); |
| 1758 | Py_XDECREF(globals); |
| 1759 | Py_XDECREF(fromlist); |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1760 | if (final_mod == NULL) |
| 1761 | remove_importlib_frames(); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1762 | return final_mod; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 1765 | PyObject * |
Benjamin Peterson | 04778a8 | 2011-05-25 09:29:00 -0500 | [diff] [blame] | 1766 | PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 1767 | PyObject *fromlist, int level) |
| 1768 | { |
| 1769 | PyObject *nameobj, *mod; |
| 1770 | nameobj = PyUnicode_FromString(name); |
| 1771 | if (nameobj == NULL) |
| 1772 | return NULL; |
| 1773 | mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals, |
| 1774 | fromlist, level); |
| 1775 | Py_DECREF(nameobj); |
| 1776 | return mod; |
| 1777 | } |
| 1778 | |
| 1779 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1780 | /* Re-import a module of any kind and return its module object, WITH |
| 1781 | INCREMENTED REFERENCE COUNT */ |
| 1782 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1783 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1784 | PyImport_ReloadModule(PyObject *m) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1785 | { |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 1786 | _Py_IDENTIFIER(reload); |
| 1787 | PyObject *reloaded_module = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | PyObject *modules = PyImport_GetModuleDict(); |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 1789 | PyObject *imp = PyDict_GetItemString(modules, "imp"); |
| 1790 | if (imp == NULL) { |
| 1791 | imp = PyImport_ImportModule("imp"); |
| 1792 | if (imp == NULL) { |
| 1793 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | } |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 1796 | else { |
| 1797 | Py_INCREF(imp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | } |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1799 | |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 1800 | reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m); |
| 1801 | Py_DECREF(imp); |
| 1802 | return reloaded_module; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1806 | /* Higher-level import emulator which emulates the "import" statement |
| 1807 | more accurately -- it invokes the __import__() function from the |
| 1808 | builtins of the current globals. This means that the import is |
| 1809 | done using whatever import hooks are installed in the current |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1810 | environment. |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 1811 | A dummy list ["__doc__"] is passed as the 4th argument so that |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 1812 | e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 1813 | will return <module "gencache"> instead of <module "win32com">. */ |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1814 | |
| 1815 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1816 | PyImport_Import(PyObject *module_name) |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1817 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1818 | static PyObject *silly_list = NULL; |
| 1819 | static PyObject *builtins_str = NULL; |
| 1820 | static PyObject *import_str = NULL; |
| 1821 | PyObject *globals = NULL; |
| 1822 | PyObject *import = NULL; |
| 1823 | PyObject *builtins = NULL; |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1824 | PyObject *modules = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | PyObject *r = NULL; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | /* Initialize constant string objects */ |
| 1828 | if (silly_list == NULL) { |
| 1829 | import_str = PyUnicode_InternFromString("__import__"); |
| 1830 | if (import_str == NULL) |
| 1831 | return NULL; |
| 1832 | builtins_str = PyUnicode_InternFromString("__builtins__"); |
| 1833 | if (builtins_str == NULL) |
| 1834 | return NULL; |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1835 | silly_list = PyList_New(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | if (silly_list == NULL) |
| 1837 | return NULL; |
| 1838 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | /* Get the builtins from current globals */ |
| 1841 | globals = PyEval_GetGlobals(); |
| 1842 | if (globals != NULL) { |
| 1843 | Py_INCREF(globals); |
| 1844 | builtins = PyObject_GetItem(globals, builtins_str); |
| 1845 | if (builtins == NULL) |
| 1846 | goto err; |
| 1847 | } |
| 1848 | else { |
| 1849 | /* No globals -- use standard builtins, and fake globals */ |
| 1850 | builtins = PyImport_ImportModuleLevel("builtins", |
| 1851 | NULL, NULL, NULL, 0); |
| 1852 | if (builtins == NULL) |
| 1853 | return NULL; |
| 1854 | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
| 1855 | if (globals == NULL) |
| 1856 | goto err; |
| 1857 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | /* Get the __import__ function from the builtins */ |
| 1860 | if (PyDict_Check(builtins)) { |
| 1861 | import = PyObject_GetItem(builtins, import_str); |
| 1862 | if (import == NULL) |
| 1863 | PyErr_SetObject(PyExc_KeyError, import_str); |
| 1864 | } |
| 1865 | else |
| 1866 | import = PyObject_GetAttr(builtins, import_str); |
| 1867 | if (import == NULL) |
| 1868 | goto err; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | /* Call the __import__ function with the proper argument list |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1871 | Always use absolute import here. |
| 1872 | Calling for side-effect of import. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | r = PyObject_CallFunction(import, "OOOOi", module_name, globals, |
| 1874 | globals, silly_list, 0, NULL); |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1875 | if (r == NULL) |
| 1876 | goto err; |
| 1877 | Py_DECREF(r); |
| 1878 | |
| 1879 | modules = PyImport_GetModuleDict(); |
| 1880 | r = PyDict_GetItem(modules, module_name); |
| 1881 | if (r != NULL) |
| 1882 | Py_INCREF(r); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1883 | |
| 1884 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | Py_XDECREF(globals); |
| 1886 | Py_XDECREF(builtins); |
| 1887 | Py_XDECREF(import); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | return r; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1893 | /* Module 'imp' provides Python access to the primitives used for |
| 1894 | importing modules. |
| 1895 | */ |
| 1896 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1897 | static PyObject * |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1898 | imp_make_magic(long magic) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | char buf[4]; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | buf[0] = (char) ((magic >> 0) & 0xff); |
| 1903 | buf[1] = (char) ((magic >> 8) & 0xff); |
| 1904 | buf[2] = (char) ((magic >> 16) & 0xff); |
| 1905 | buf[3] = (char) ((magic >> 24) & 0xff); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | return PyBytes_FromStringAndSize(buf, 4); |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 1908 | } |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1909 | |
| 1910 | static PyObject * |
| 1911 | imp_get_magic(PyObject *self, PyObject *noargs) |
| 1912 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | return imp_make_magic(pyc_magic); |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | static PyObject * |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1917 | imp_extension_suffixes(PyObject *self, PyObject *noargs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1918 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | PyObject *list; |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1920 | const char *suffix; |
| 1921 | unsigned int index = 0; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | list = PyList_New(0); |
| 1924 | if (list == NULL) |
| 1925 | return NULL; |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1926 | #ifdef HAVE_DYNAMIC_LOADING |
| 1927 | while ((suffix = _PyImport_DynLoadFiletab[index])) { |
| 1928 | PyObject *item = PyUnicode_FromString(suffix); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1929 | if (item == NULL) { |
| 1930 | Py_DECREF(list); |
| 1931 | return NULL; |
| 1932 | } |
| 1933 | if (PyList_Append(list, item) < 0) { |
| 1934 | Py_DECREF(list); |
| 1935 | Py_DECREF(item); |
| 1936 | return NULL; |
| 1937 | } |
| 1938 | Py_DECREF(item); |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1939 | index += 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1940 | } |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1941 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1942 | return list; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1945 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1946 | imp_init_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1947 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1948 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | int ret; |
| 1950 | PyObject *m; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1951 | if (!PyArg_ParseTuple(args, "U:init_builtin", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | return NULL; |
| 1953 | ret = init_builtin(name); |
| 1954 | if (ret < 0) |
| 1955 | return NULL; |
| 1956 | if (ret == 0) { |
| 1957 | Py_INCREF(Py_None); |
| 1958 | return Py_None; |
| 1959 | } |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1960 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | Py_XINCREF(m); |
| 1962 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1965 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1966 | imp_init_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1967 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1968 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | int ret; |
| 1970 | PyObject *m; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1971 | if (!PyArg_ParseTuple(args, "U:init_frozen", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1972 | return NULL; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1973 | ret = PyImport_ImportFrozenModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | if (ret < 0) |
| 1975 | return NULL; |
| 1976 | if (ret == 0) { |
| 1977 | Py_INCREF(Py_None); |
| 1978 | return Py_None; |
| 1979 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1980 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | Py_XINCREF(m); |
| 1982 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1985 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1986 | imp_get_frozen_object(PyObject *self, PyObject *args) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1987 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1988 | PyObject *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 1989 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1990 | if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | return NULL; |
| 1992 | return get_frozen_object(name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1995 | static PyObject * |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1996 | imp_is_frozen_package(PyObject *self, PyObject *args) |
| 1997 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1998 | PyObject *name; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1999 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2000 | if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | return NULL; |
| 2002 | return is_frozen_package(name); |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2006 | imp_is_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2007 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 2008 | PyObject *name; |
| 2009 | if (!PyArg_ParseTuple(args, "U:is_builtin", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | return NULL; |
| 2011 | return PyLong_FromLong(is_builtin(name)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2014 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2015 | imp_is_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2016 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2017 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2018 | struct _frozen *p; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 2019 | if (!PyArg_ParseTuple(args, "U:is_frozen", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2020 | return NULL; |
| 2021 | p = find_frozen(name); |
| 2022 | return PyBool_FromLong((long) (p == NULL ? 0 : p->size)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2025 | #ifdef HAVE_DYNAMIC_LOADING |
| 2026 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2027 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2028 | imp_load_dynamic(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2029 | { |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2030 | PyObject *name, *pathname, *fob = NULL, *mod; |
| 2031 | FILE *fp; |
| 2032 | |
| 2033 | if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic", |
| 2034 | &name, PyUnicode_FSDecoder, &pathname, &fob)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | return NULL; |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2036 | if (fob != NULL) { |
Antoine Pitrou | f3a42de | 2012-05-04 22:40:25 +0200 | [diff] [blame] | 2037 | fp = _Py_fopen(pathname, "r"); |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 2038 | if (fp == NULL) { |
| 2039 | Py_DECREF(pathname); |
Antoine Pitrou | f3a42de | 2012-05-04 22:40:25 +0200 | [diff] [blame] | 2040 | if (!PyErr_Occurred()) |
| 2041 | PyErr_SetFromErrno(PyExc_IOError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | return NULL; |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 2043 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | } |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2045 | else |
| 2046 | fp = NULL; |
| 2047 | mod = _PyImport_LoadDynamicModule(name, pathname, fp); |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 2048 | Py_DECREF(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | if (fp) |
| 2050 | fclose(fp); |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 2051 | return mod; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2054 | #endif /* HAVE_DYNAMIC_LOADING */ |
| 2055 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 2056 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2057 | /* Doc strings */ |
| 2058 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2059 | PyDoc_STRVAR(doc_imp, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 2060 | "(Extremely) low-level import machinery bits as used by importlib and imp."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2061 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2062 | PyDoc_STRVAR(doc_get_magic, |
| 2063 | "get_magic() -> string\n\ |
| 2064 | Return the magic number for .pyc or .pyo files."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2065 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 2066 | PyDoc_STRVAR(doc_extension_suffixes, |
| 2067 | "extension_suffixes() -> list of strings\n\ |
| 2068 | Returns the list of file suffixes used to identify extension modules."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2069 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2070 | PyDoc_STRVAR(doc_lock_held, |
Tim Peters | a7c6509 | 2004-08-01 23:26:05 +0000 | [diff] [blame] | 2071 | "lock_held() -> boolean\n\ |
| 2072 | Return True if the import lock is currently held, else False.\n\ |
| 2073 | On platforms without threads, return False."); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 2074 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 2075 | PyDoc_STRVAR(doc_acquire_lock, |
| 2076 | "acquire_lock() -> None\n\ |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 2077 | Acquires the interpreter's import lock for the current thread.\n\ |
| 2078 | This lock should be used by import hooks to ensure thread-safety\n\ |
| 2079 | when importing modules.\n\ |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 2080 | On platforms without threads, this function does nothing."); |
| 2081 | |
| 2082 | PyDoc_STRVAR(doc_release_lock, |
| 2083 | "release_lock() -> None\n\ |
| 2084 | Release the interpreter's import lock.\n\ |
| 2085 | On platforms without threads, this function does nothing."); |
| 2086 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2087 | static PyMethodDef imp_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic}, |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 2089 | {"extension_suffixes", imp_extension_suffixes, METH_NOARGS, |
| 2090 | doc_extension_suffixes}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 | {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, |
| 2092 | {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock}, |
| 2093 | {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2094 | {"get_frozen_object", imp_get_frozen_object, METH_VARARGS}, |
| 2095 | {"is_frozen_package", imp_is_frozen_package, METH_VARARGS}, |
| 2096 | {"init_builtin", imp_init_builtin, METH_VARARGS}, |
| 2097 | {"init_frozen", imp_init_frozen, METH_VARARGS}, |
| 2098 | {"is_builtin", imp_is_builtin, METH_VARARGS}, |
| 2099 | {"is_frozen", imp_is_frozen, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2100 | #ifdef HAVE_DYNAMIC_LOADING |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | {"load_dynamic", imp_load_dynamic, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2102 | #endif |
Brett Cannon | 442c9b9 | 2011-03-23 16:14:42 -0700 | [diff] [blame] | 2103 | {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2105 | }; |
| 2106 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2107 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2108 | static struct PyModuleDef impmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | PyModuleDef_HEAD_INIT, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 2110 | "_imp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | doc_imp, |
| 2112 | 0, |
| 2113 | imp_methods, |
| 2114 | NULL, |
| 2115 | NULL, |
| 2116 | NULL, |
| 2117 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2118 | }; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2119 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 2120 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2121 | PyInit_imp(void) |
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 | PyObject *m, *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | m = PyModule_Create(&impmodule); |
| 2126 | if (m == NULL) |
| 2127 | goto failure; |
| 2128 | d = PyModule_GetDict(m); |
| 2129 | if (d == NULL) |
| 2130 | goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | return m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2133 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | Py_XDECREF(m); |
| 2135 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2136 | } |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2137 | |
| 2138 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2139 | /* API for embedding applications that want to add their own entries |
| 2140 | to the table of built-in modules. This should normally be called |
| 2141 | *before* Py_Initialize(). When the table resize fails, -1 is |
| 2142 | returned and the existing table is unchanged. |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2143 | |
| 2144 | After a similar function by Just van Rossum. */ |
| 2145 | |
| 2146 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2147 | PyImport_ExtendInittab(struct _inittab *newtab) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | static struct _inittab *our_copy = NULL; |
| 2150 | struct _inittab *p; |
| 2151 | int i, n; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | /* Count the number of entries in both tables */ |
| 2154 | for (n = 0; newtab[n].name != NULL; n++) |
| 2155 | ; |
| 2156 | if (n == 0) |
| 2157 | return 0; /* Nothing to do */ |
| 2158 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) |
| 2159 | ; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | /* Allocate new memory for the combined table */ |
| 2162 | p = our_copy; |
| 2163 | PyMem_RESIZE(p, struct _inittab, i+n+1); |
| 2164 | if (p == NULL) |
| 2165 | return -1; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | /* Copy the tables into the new memory */ |
| 2168 | if (our_copy != PyImport_Inittab) |
| 2169 | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
| 2170 | PyImport_Inittab = our_copy = p; |
| 2171 | memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | return 0; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | /* Shorthand to add a single entry given a name and a function */ |
| 2177 | |
| 2178 | int |
Brett Cannon | a826f32 | 2009-04-02 03:41:46 +0000 | [diff] [blame] | 2179 | PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2181 | struct _inittab newtab[2]; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2183 | memset(newtab, '\0', sizeof newtab); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | newtab[0].name = (char *)name; |
| 2186 | newtab[0].initfunc = initfunc; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2188 | return PyImport_ExtendInittab(newtab); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2189 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2190 | |
| 2191 | #ifdef __cplusplus |
| 2192 | } |
| 2193 | #endif |