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