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) { |
| 1110 | /* Set __path__ to the package name */ |
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); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | l = PyList_New(1); |
| 1118 | if (l == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | goto err_return; |
| 1120 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1121 | Py_INCREF(name); |
| 1122 | PyList_SET_ITEM(l, 0, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | err = PyDict_SetItemString(d, "__path__", l); |
| 1124 | Py_DECREF(l); |
| 1125 | if (err != 0) |
| 1126 | goto err_return; |
| 1127 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1128 | path = PyUnicode_FromString("<frozen>"); |
| 1129 | if (path == NULL) |
| 1130 | goto err_return; |
| 1131 | m = PyImport_ExecCodeModuleObject(name, co, path, NULL); |
| 1132 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | if (m == NULL) |
| 1134 | goto err_return; |
| 1135 | Py_DECREF(co); |
| 1136 | Py_DECREF(m); |
| 1137 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1138 | err_return: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | Py_DECREF(co); |
| 1140 | return -1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1141 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1142 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1143 | int |
| 1144 | PyImport_ImportFrozenModule(char *name) |
| 1145 | { |
| 1146 | PyObject *nameobj; |
| 1147 | int ret; |
| 1148 | nameobj = PyUnicode_InternFromString(name); |
| 1149 | if (nameobj == NULL) |
| 1150 | return -1; |
| 1151 | ret = PyImport_ImportFrozenModuleObject(nameobj); |
| 1152 | Py_DECREF(nameobj); |
| 1153 | return ret; |
| 1154 | } |
| 1155 | |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1156 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1157 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1158 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1159 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1160 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1161 | PyImport_ImportModule(const char *name) |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1162 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | PyObject *pname; |
| 1164 | PyObject *result; |
Marc-André Lemburg | 3c61c35 | 2001-02-09 19:40:15 +0000 | [diff] [blame] | 1165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | pname = PyUnicode_FromString(name); |
| 1167 | if (pname == NULL) |
| 1168 | return NULL; |
| 1169 | result = PyImport_Import(pname); |
| 1170 | Py_DECREF(pname); |
| 1171 | return result; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1174 | /* Import a module without blocking |
| 1175 | * |
| 1176 | * At first it tries to fetch the module from sys.modules. If the module was |
| 1177 | * never loaded before it loads it with PyImport_ImportModule() unless another |
| 1178 | * thread holds the import lock. In the latter case the function raises an |
| 1179 | * ImportError instead of blocking. |
| 1180 | * |
| 1181 | * Returns the module object with incremented ref count. |
| 1182 | */ |
| 1183 | PyObject * |
| 1184 | PyImport_ImportModuleNoBlock(const char *name) |
| 1185 | { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1186 | return PyImport_ImportModule(name); |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1189 | |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1190 | /* Remove importlib frames from the traceback, |
| 1191 | * except in Verbose mode. */ |
| 1192 | static void |
| 1193 | remove_importlib_frames(void) |
| 1194 | { |
| 1195 | const char *importlib_filename = "<frozen importlib._bootstrap>"; |
Nick Coghlan | 42c0766 | 2012-07-31 21:14:18 +1000 | [diff] [blame] | 1196 | const char *remove_frames = "_call_with_frames_removed"; |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1197 | int always_trim = 0; |
Benjamin Peterson | fa87370 | 2012-07-09 13:43:53 -0700 | [diff] [blame] | 1198 | int in_importlib = 0; |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1199 | PyObject *exception, *value, *base_tb, *tb; |
Benjamin Peterson | fa87370 | 2012-07-09 13:43:53 -0700 | [diff] [blame] | 1200 | PyObject **prev_link, **outer_link = NULL; |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1201 | |
| 1202 | /* Synopsis: if it's an ImportError, we trim all importlib chunks |
Nick Coghlan | 42c0766 | 2012-07-31 21:14:18 +1000 | [diff] [blame] | 1203 | from the traceback. We always trim chunks |
| 1204 | which end with a call to "_call_with_frames_removed". */ |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1205 | |
| 1206 | PyErr_Fetch(&exception, &value, &base_tb); |
| 1207 | if (!exception || Py_VerboseFlag) |
| 1208 | goto done; |
| 1209 | if (PyType_IsSubtype((PyTypeObject *) exception, |
| 1210 | (PyTypeObject *) PyExc_ImportError)) |
| 1211 | always_trim = 1; |
| 1212 | |
| 1213 | prev_link = &base_tb; |
| 1214 | tb = base_tb; |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1215 | while (tb != NULL) { |
| 1216 | PyTracebackObject *traceback = (PyTracebackObject *)tb; |
| 1217 | PyObject *next = (PyObject *) traceback->tb_next; |
| 1218 | PyFrameObject *frame = traceback->tb_frame; |
| 1219 | PyCodeObject *code = frame->f_code; |
| 1220 | int now_in_importlib; |
| 1221 | |
| 1222 | assert(PyTraceBack_Check(tb)); |
| 1223 | now_in_importlib = (PyUnicode_CompareWithASCIIString( |
| 1224 | code->co_filename, |
| 1225 | importlib_filename) == 0); |
| 1226 | if (now_in_importlib && !in_importlib) { |
| 1227 | /* This is the link to this chunk of importlib tracebacks */ |
| 1228 | outer_link = prev_link; |
| 1229 | } |
| 1230 | in_importlib = now_in_importlib; |
| 1231 | |
| 1232 | if (in_importlib && |
| 1233 | (always_trim || |
Nick Coghlan | 42c0766 | 2012-07-31 21:14:18 +1000 | [diff] [blame] | 1234 | PyUnicode_CompareWithASCIIString(code->co_name, |
| 1235 | remove_frames) == 0)) { |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1236 | PyObject *tmp = *outer_link; |
| 1237 | *outer_link = next; |
| 1238 | Py_XINCREF(next); |
| 1239 | Py_DECREF(tmp); |
| 1240 | prev_link = outer_link; |
| 1241 | } |
| 1242 | else { |
| 1243 | prev_link = (PyObject **) &traceback->tb_next; |
| 1244 | } |
| 1245 | tb = next; |
| 1246 | } |
| 1247 | done: |
| 1248 | PyErr_Restore(exception, value, base_tb); |
| 1249 | } |
| 1250 | |
| 1251 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1252 | PyObject * |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1253 | PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals, |
| 1254 | PyObject *locals, PyObject *given_fromlist, |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 1255 | int level) |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1256 | { |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1257 | _Py_IDENTIFIER(__import__); |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1258 | _Py_IDENTIFIER(__initializing__); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1259 | _Py_IDENTIFIER(__package__); |
| 1260 | _Py_IDENTIFIER(__path__); |
| 1261 | _Py_IDENTIFIER(__name__); |
| 1262 | _Py_IDENTIFIER(_find_and_load); |
| 1263 | _Py_IDENTIFIER(_handle_fromlist); |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1264 | _Py_IDENTIFIER(_lock_unlock_module); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1265 | _Py_static_string(single_dot, "."); |
| 1266 | PyObject *abs_name = NULL; |
| 1267 | PyObject *builtins_import = NULL; |
| 1268 | PyObject *final_mod = NULL; |
| 1269 | PyObject *mod = NULL; |
| 1270 | PyObject *package = NULL; |
| 1271 | PyObject *globals = NULL; |
| 1272 | PyObject *fromlist = NULL; |
| 1273 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 1274 | |
| 1275 | /* Make sure to use default values so as to not have |
| 1276 | PyObject_CallMethodObjArgs() truncate the parameter list because of a |
| 1277 | NULL argument. */ |
| 1278 | if (given_globals == NULL) { |
| 1279 | globals = PyDict_New(); |
| 1280 | if (globals == NULL) { |
| 1281 | goto error; |
| 1282 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1284 | else { |
| 1285 | /* 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] | 1286 | for something. */ |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1287 | if (level > 0 && !PyDict_Check(given_globals)) { |
| 1288 | PyErr_SetString(PyExc_TypeError, "globals must be a dict"); |
| 1289 | goto error; |
| 1290 | } |
| 1291 | globals = given_globals; |
| 1292 | Py_INCREF(globals); |
| 1293 | } |
| 1294 | |
| 1295 | if (given_fromlist == NULL) { |
| 1296 | fromlist = PyList_New(0); |
| 1297 | if (fromlist == NULL) { |
| 1298 | goto error; |
| 1299 | } |
| 1300 | } |
| 1301 | else { |
| 1302 | fromlist = given_fromlist; |
| 1303 | Py_INCREF(fromlist); |
| 1304 | } |
| 1305 | if (name == NULL) { |
| 1306 | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
| 1307 | goto error; |
| 1308 | } |
| 1309 | |
| 1310 | /* The below code is importlib.__import__() & _gcd_import(), ported to C |
| 1311 | for added performance. */ |
| 1312 | |
| 1313 | if (!PyUnicode_Check(name)) { |
| 1314 | PyErr_SetString(PyExc_TypeError, "module name must be a string"); |
| 1315 | goto error; |
| 1316 | } |
| 1317 | else if (PyUnicode_READY(name) < 0) { |
| 1318 | goto error; |
| 1319 | } |
| 1320 | if (level < 0) { |
| 1321 | PyErr_SetString(PyExc_ValueError, "level must be >= 0"); |
| 1322 | goto error; |
| 1323 | } |
| 1324 | else if (level > 0) { |
| 1325 | package = _PyDict_GetItemId(globals, &PyId___package__); |
| 1326 | if (package != NULL && package != Py_None) { |
| 1327 | Py_INCREF(package); |
| 1328 | if (!PyUnicode_Check(package)) { |
| 1329 | PyErr_SetString(PyExc_TypeError, "package must be a string"); |
| 1330 | goto error; |
| 1331 | } |
| 1332 | } |
| 1333 | else { |
Brett Cannon | 273323c | 2012-04-17 19:05:11 -0400 | [diff] [blame] | 1334 | package = _PyDict_GetItemId(globals, &PyId___name__); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1335 | if (package == NULL) { |
Brett Cannon | 273323c | 2012-04-17 19:05:11 -0400 | [diff] [blame] | 1336 | PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1337 | goto error; |
| 1338 | } |
| 1339 | else if (!PyUnicode_Check(package)) { |
| 1340 | PyErr_SetString(PyExc_TypeError, "__name__ must be a string"); |
| 1341 | } |
| 1342 | Py_INCREF(package); |
| 1343 | |
| 1344 | if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) { |
Brett Cannon | 740fce0 | 2012-04-14 14:23:49 -0400 | [diff] [blame] | 1345 | PyObject *partition = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1346 | PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot); |
| 1347 | if (borrowed_dot == NULL) { |
| 1348 | goto error; |
| 1349 | } |
Brett Cannon | 740fce0 | 2012-04-14 14:23:49 -0400 | [diff] [blame] | 1350 | partition = PyUnicode_RPartition(package, borrowed_dot); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1351 | Py_DECREF(package); |
| 1352 | if (partition == NULL) { |
| 1353 | goto error; |
| 1354 | } |
| 1355 | package = PyTuple_GET_ITEM(partition, 0); |
| 1356 | Py_INCREF(package); |
| 1357 | Py_DECREF(partition); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | if (PyDict_GetItem(interp->modules, package) == NULL) { |
| 1362 | PyErr_Format(PyExc_SystemError, |
| 1363 | "Parent module %R not loaded, cannot perform relative " |
| 1364 | "import", package); |
| 1365 | goto error; |
| 1366 | } |
| 1367 | } |
| 1368 | else { /* level == 0 */ |
| 1369 | if (PyUnicode_GET_LENGTH(name) == 0) { |
| 1370 | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
| 1371 | goto error; |
| 1372 | } |
| 1373 | package = Py_None; |
| 1374 | Py_INCREF(package); |
| 1375 | } |
| 1376 | |
| 1377 | if (level > 0) { |
| 1378 | Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package); |
| 1379 | PyObject *base = NULL; |
| 1380 | int level_up = 1; |
| 1381 | |
| 1382 | for (level_up = 1; level_up < level; level_up += 1) { |
| 1383 | last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1); |
| 1384 | if (last_dot == -2) { |
| 1385 | goto error; |
| 1386 | } |
| 1387 | else if (last_dot == -1) { |
| 1388 | PyErr_SetString(PyExc_ValueError, |
| 1389 | "attempted relative import beyond top-level " |
| 1390 | "package"); |
| 1391 | goto error; |
| 1392 | } |
| 1393 | } |
| 1394 | base = PyUnicode_Substring(package, 0, last_dot); |
| 1395 | if (PyUnicode_GET_LENGTH(name) > 0) { |
Antoine Pitrou | 538ba2a | 2012-04-16 21:52:45 +0200 | [diff] [blame] | 1396 | PyObject *borrowed_dot, *seq = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1397 | |
| 1398 | borrowed_dot = _PyUnicode_FromId(&single_dot); |
Antoine Pitrou | 538ba2a | 2012-04-16 21:52:45 +0200 | [diff] [blame] | 1399 | seq = PyTuple_Pack(2, base, name); |
| 1400 | Py_DECREF(base); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1401 | if (borrowed_dot == NULL || seq == NULL) { |
| 1402 | goto error; |
| 1403 | } |
| 1404 | |
| 1405 | abs_name = PyUnicode_Join(borrowed_dot, seq); |
| 1406 | Py_DECREF(seq); |
| 1407 | if (abs_name == NULL) { |
| 1408 | goto error; |
| 1409 | } |
| 1410 | } |
| 1411 | else { |
| 1412 | abs_name = base; |
| 1413 | } |
| 1414 | } |
| 1415 | else { |
| 1416 | abs_name = name; |
| 1417 | Py_INCREF(abs_name); |
| 1418 | } |
| 1419 | |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1420 | #ifdef WITH_THREAD |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1421 | _PyImport_AcquireLock(); |
| 1422 | #endif |
| 1423 | /* From this point forward, goto error_with_unlock! */ |
| 1424 | if (PyDict_Check(globals)) { |
| 1425 | builtins_import = _PyDict_GetItemId(globals, &PyId___import__); |
| 1426 | } |
| 1427 | if (builtins_import == NULL) { |
| 1428 | builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__); |
| 1429 | if (builtins_import == NULL) { |
Benjamin Peterson | 7d11004 | 2013-04-29 09:08:14 -0400 | [diff] [blame] | 1430 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 1431 | goto error_with_unlock; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | Py_INCREF(builtins_import); |
| 1435 | |
| 1436 | mod = PyDict_GetItem(interp->modules, abs_name); |
| 1437 | if (mod == Py_None) { |
Brett Cannon | 27fc528 | 2012-04-15 14:15:31 -0400 | [diff] [blame] | 1438 | PyObject *msg = PyUnicode_FromFormat("import of %R halted; " |
| 1439 | "None in sys.modules", abs_name); |
| 1440 | if (msg != NULL) { |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 1441 | PyErr_SetImportError(msg, abs_name, NULL); |
| 1442 | Py_DECREF(msg); |
Brett Cannon | 27fc528 | 2012-04-15 14:15:31 -0400 | [diff] [blame] | 1443 | } |
Antoine Pitrou | 71382cb | 2012-04-16 18:48:49 +0200 | [diff] [blame] | 1444 | mod = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1445 | goto error_with_unlock; |
| 1446 | } |
| 1447 | else if (mod != NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1448 | PyObject *value; |
| 1449 | int initializing = 0; |
| 1450 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1451 | Py_INCREF(mod); |
Antoine Pitrou | 0398985 | 2012-08-28 00:24:52 +0200 | [diff] [blame] | 1452 | /* Optimization: only call _bootstrap._lock_unlock_module() if |
| 1453 | __initializing__ is true. |
| 1454 | NOTE: because of this, __initializing__ must be set *before* |
| 1455 | stuffing the new module in sys.modules. |
| 1456 | */ |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1457 | value = _PyObject_GetAttrId(mod, &PyId___initializing__); |
| 1458 | if (value == NULL) |
| 1459 | PyErr_Clear(); |
| 1460 | else { |
| 1461 | initializing = PyObject_IsTrue(value); |
| 1462 | Py_DECREF(value); |
| 1463 | if (initializing == -1) |
| 1464 | PyErr_Clear(); |
| 1465 | } |
| 1466 | if (initializing > 0) { |
| 1467 | /* _bootstrap._lock_unlock_module() releases the import lock */ |
Alexandre Vassalotti | 865eaa1 | 2013-05-02 10:44:04 -0700 | [diff] [blame] | 1468 | value = _PyObject_CallMethodIdObjArgs(interp->importlib, |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1469 | &PyId__lock_unlock_module, abs_name, |
| 1470 | NULL); |
| 1471 | if (value == NULL) |
| 1472 | goto error; |
| 1473 | Py_DECREF(value); |
| 1474 | } |
| 1475 | else { |
| 1476 | #ifdef WITH_THREAD |
| 1477 | if (_PyImport_ReleaseLock() < 0) { |
| 1478 | PyErr_SetString(PyExc_RuntimeError, "not holding the import lock"); |
| 1479 | goto error; |
| 1480 | } |
| 1481 | #endif |
| 1482 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1483 | } |
| 1484 | else { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1485 | /* _bootstrap._find_and_load() releases the import lock */ |
Alexandre Vassalotti | 865eaa1 | 2013-05-02 10:44:04 -0700 | [diff] [blame] | 1486 | mod = _PyObject_CallMethodIdObjArgs(interp->importlib, |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1487 | &PyId__find_and_load, abs_name, |
| 1488 | builtins_import, NULL); |
| 1489 | if (mod == NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1490 | goto error; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1491 | } |
| 1492 | } |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1493 | /* From now on we don't hold the import lock anymore. */ |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1494 | |
| 1495 | if (PyObject_Not(fromlist)) { |
| 1496 | if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) { |
| 1497 | PyObject *front = NULL; |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1498 | PyObject *partition = NULL; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1499 | PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot); |
| 1500 | |
| 1501 | if (borrowed_dot == NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1502 | goto error; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1503 | } |
| 1504 | |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1505 | partition = PyUnicode_Partition(name, borrowed_dot); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1506 | if (partition == NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1507 | goto error; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1508 | } |
| 1509 | |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 1510 | if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) { |
| 1511 | /* No dot in module name, simple exit */ |
| 1512 | Py_DECREF(partition); |
| 1513 | final_mod = mod; |
| 1514 | Py_INCREF(mod); |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1515 | goto error; |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 1516 | } |
| 1517 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1518 | front = PyTuple_GET_ITEM(partition, 0); |
| 1519 | Py_INCREF(front); |
| 1520 | Py_DECREF(partition); |
| 1521 | |
| 1522 | if (level == 0) { |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 1523 | final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL); |
Antoine Pitrou | b78174c | 2012-05-06 17:15:23 +0200 | [diff] [blame] | 1524 | Py_DECREF(front); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1525 | } |
| 1526 | else { |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 1527 | Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) - |
| 1528 | PyUnicode_GET_LENGTH(front); |
| 1529 | Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name); |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 1530 | PyObject *to_return = PyUnicode_Substring(abs_name, 0, |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1531 | abs_name_len - cut_off); |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 1532 | Py_DECREF(front); |
| 1533 | if (to_return == NULL) { |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1534 | goto error; |
Antoine Pitrou | 22a1d17 | 2012-04-16 22:06:21 +0200 | [diff] [blame] | 1535 | } |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1536 | |
| 1537 | final_mod = PyDict_GetItem(interp->modules, to_return); |
Brett Cannon | 49f8d8b | 2012-04-14 21:50:00 -0400 | [diff] [blame] | 1538 | if (final_mod == NULL) { |
| 1539 | PyErr_Format(PyExc_KeyError, |
| 1540 | "%R not in sys.modules as expected", |
| 1541 | to_return); |
| 1542 | } |
| 1543 | else { |
| 1544 | Py_INCREF(final_mod); |
| 1545 | } |
Antoine Pitrou | b78174c | 2012-05-06 17:15:23 +0200 | [diff] [blame] | 1546 | Py_DECREF(to_return); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | else { |
| 1550 | final_mod = mod; |
| 1551 | Py_INCREF(mod); |
| 1552 | } |
| 1553 | } |
| 1554 | else { |
Alexandre Vassalotti | 865eaa1 | 2013-05-02 10:44:04 -0700 | [diff] [blame] | 1555 | final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib, |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1556 | &PyId__handle_fromlist, mod, |
| 1557 | fromlist, builtins_import, |
| 1558 | NULL); |
| 1559 | } |
Antoine Pitrou | ea3eb88 | 2012-05-17 18:55:59 +0200 | [diff] [blame] | 1560 | goto error; |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 1561 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1562 | error_with_unlock: |
Brian Curtin | e6b299f | 2012-04-14 14:19:33 -0500 | [diff] [blame] | 1563 | #ifdef WITH_THREAD |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1564 | if (_PyImport_ReleaseLock() < 0) { |
| 1565 | PyErr_SetString(PyExc_RuntimeError, "not holding the import lock"); |
| 1566 | } |
| 1567 | #endif |
| 1568 | error: |
| 1569 | Py_XDECREF(abs_name); |
| 1570 | Py_XDECREF(builtins_import); |
| 1571 | Py_XDECREF(mod); |
| 1572 | Py_XDECREF(package); |
| 1573 | Py_XDECREF(globals); |
| 1574 | Py_XDECREF(fromlist); |
Antoine Pitrou | bc07a5c | 2012-07-08 12:01:27 +0200 | [diff] [blame] | 1575 | if (final_mod == NULL) |
| 1576 | remove_importlib_frames(); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1577 | return final_mod; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 1580 | PyObject * |
Benjamin Peterson | 04778a8 | 2011-05-25 09:29:00 -0500 | [diff] [blame] | 1581 | PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, |
Victor Stinner | fe93faf | 2011-03-14 15:54:52 -0400 | [diff] [blame] | 1582 | PyObject *fromlist, int level) |
| 1583 | { |
| 1584 | PyObject *nameobj, *mod; |
| 1585 | nameobj = PyUnicode_FromString(name); |
| 1586 | if (nameobj == NULL) |
| 1587 | return NULL; |
| 1588 | mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals, |
| 1589 | fromlist, level); |
| 1590 | Py_DECREF(nameobj); |
| 1591 | return mod; |
| 1592 | } |
| 1593 | |
| 1594 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1595 | /* Re-import a module of any kind and return its module object, WITH |
| 1596 | INCREMENTED REFERENCE COUNT */ |
| 1597 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1598 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1599 | PyImport_ReloadModule(PyObject *m) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1600 | { |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 1601 | _Py_IDENTIFIER(reload); |
| 1602 | PyObject *reloaded_module = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | PyObject *modules = PyImport_GetModuleDict(); |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 1604 | PyObject *imp = PyDict_GetItemString(modules, "imp"); |
| 1605 | if (imp == NULL) { |
| 1606 | imp = PyImport_ImportModule("imp"); |
| 1607 | if (imp == NULL) { |
| 1608 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | } |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 1611 | else { |
| 1612 | Py_INCREF(imp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | } |
Victor Stinner | ad3c03b | 2011-03-14 09:21:33 -0400 | [diff] [blame] | 1614 | |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 1615 | reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m); |
| 1616 | Py_DECREF(imp); |
| 1617 | return reloaded_module; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1621 | /* Higher-level import emulator which emulates the "import" statement |
| 1622 | more accurately -- it invokes the __import__() function from the |
| 1623 | builtins of the current globals. This means that the import is |
| 1624 | done using whatever import hooks are installed in the current |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1625 | environment. |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 1626 | A dummy list ["__doc__"] is passed as the 4th argument so that |
Neal Norwitz | 80e7f27 | 2007-08-26 06:45:23 +0000 | [diff] [blame] | 1627 | e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 1628 | will return <module "gencache"> instead of <module "win32com">. */ |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1629 | |
| 1630 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1631 | PyImport_Import(PyObject *module_name) |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | static PyObject *silly_list = NULL; |
| 1634 | static PyObject *builtins_str = NULL; |
| 1635 | static PyObject *import_str = NULL; |
| 1636 | PyObject *globals = NULL; |
| 1637 | PyObject *import = NULL; |
| 1638 | PyObject *builtins = NULL; |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1639 | PyObject *modules = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | PyObject *r = NULL; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | /* Initialize constant string objects */ |
| 1643 | if (silly_list == NULL) { |
| 1644 | import_str = PyUnicode_InternFromString("__import__"); |
| 1645 | if (import_str == NULL) |
| 1646 | return NULL; |
| 1647 | builtins_str = PyUnicode_InternFromString("__builtins__"); |
| 1648 | if (builtins_str == NULL) |
| 1649 | return NULL; |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1650 | silly_list = PyList_New(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | if (silly_list == NULL) |
| 1652 | return NULL; |
| 1653 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | /* Get the builtins from current globals */ |
| 1656 | globals = PyEval_GetGlobals(); |
| 1657 | if (globals != NULL) { |
| 1658 | Py_INCREF(globals); |
| 1659 | builtins = PyObject_GetItem(globals, builtins_str); |
| 1660 | if (builtins == NULL) |
| 1661 | goto err; |
| 1662 | } |
| 1663 | else { |
| 1664 | /* No globals -- use standard builtins, and fake globals */ |
| 1665 | builtins = PyImport_ImportModuleLevel("builtins", |
| 1666 | NULL, NULL, NULL, 0); |
| 1667 | if (builtins == NULL) |
| 1668 | return NULL; |
| 1669 | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
| 1670 | if (globals == NULL) |
| 1671 | goto err; |
| 1672 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | /* Get the __import__ function from the builtins */ |
| 1675 | if (PyDict_Check(builtins)) { |
| 1676 | import = PyObject_GetItem(builtins, import_str); |
| 1677 | if (import == NULL) |
| 1678 | PyErr_SetObject(PyExc_KeyError, import_str); |
| 1679 | } |
| 1680 | else |
| 1681 | import = PyObject_GetAttr(builtins, import_str); |
| 1682 | if (import == NULL) |
| 1683 | goto err; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | /* Call the __import__ function with the proper argument list |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1686 | Always use absolute import here. |
| 1687 | Calling for side-effect of import. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | r = PyObject_CallFunction(import, "OOOOi", module_name, globals, |
| 1689 | globals, silly_list, 0, NULL); |
Brett Cannon | bc2eff3 | 2010-09-19 21:39:02 +0000 | [diff] [blame] | 1690 | if (r == NULL) |
| 1691 | goto err; |
| 1692 | Py_DECREF(r); |
| 1693 | |
| 1694 | modules = PyImport_GetModuleDict(); |
| 1695 | r = PyDict_GetItem(modules, module_name); |
| 1696 | if (r != NULL) |
| 1697 | Py_INCREF(r); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1698 | |
| 1699 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | Py_XDECREF(globals); |
| 1701 | Py_XDECREF(builtins); |
| 1702 | Py_XDECREF(import); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | return r; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1707 | static PyObject * |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1708 | imp_extension_suffixes(PyObject *self, PyObject *noargs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | PyObject *list; |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1711 | const char *suffix; |
| 1712 | unsigned int index = 0; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1714 | list = PyList_New(0); |
| 1715 | if (list == NULL) |
| 1716 | return NULL; |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1717 | #ifdef HAVE_DYNAMIC_LOADING |
| 1718 | while ((suffix = _PyImport_DynLoadFiletab[index])) { |
| 1719 | PyObject *item = PyUnicode_FromString(suffix); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | if (item == NULL) { |
| 1721 | Py_DECREF(list); |
| 1722 | return NULL; |
| 1723 | } |
| 1724 | if (PyList_Append(list, item) < 0) { |
| 1725 | Py_DECREF(list); |
| 1726 | Py_DECREF(item); |
| 1727 | return NULL; |
| 1728 | } |
| 1729 | Py_DECREF(item); |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1730 | index += 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | } |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1732 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | return list; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1736 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1737 | imp_init_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1738 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1739 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | int ret; |
| 1741 | PyObject *m; |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1742 | if (!PyArg_ParseTuple(args, "U:init_builtin", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | return NULL; |
| 1744 | ret = init_builtin(name); |
| 1745 | if (ret < 0) |
| 1746 | return NULL; |
| 1747 | if (ret == 0) { |
| 1748 | Py_INCREF(Py_None); |
| 1749 | return Py_None; |
| 1750 | } |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1751 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | Py_XINCREF(m); |
| 1753 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1756 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1757 | imp_init_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1758 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1759 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | int ret; |
| 1761 | PyObject *m; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1762 | if (!PyArg_ParseTuple(args, "U:init_frozen", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | return NULL; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1764 | ret = PyImport_ImportFrozenModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | if (ret < 0) |
| 1766 | return NULL; |
| 1767 | if (ret == 0) { |
| 1768 | Py_INCREF(Py_None); |
| 1769 | return Py_None; |
| 1770 | } |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1771 | m = PyImport_AddModuleObject(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | Py_XINCREF(m); |
| 1773 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1776 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1777 | imp_get_frozen_object(PyObject *self, PyObject *args) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1778 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1779 | PyObject *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 1780 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1781 | if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | return NULL; |
| 1783 | return get_frozen_object(name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1786 | static PyObject * |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1787 | imp_is_frozen_package(PyObject *self, PyObject *args) |
| 1788 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1789 | PyObject *name; |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1790 | |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1791 | if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | return NULL; |
| 1793 | return is_frozen_package(name); |
Brett Cannon | 8d11013 | 2009-03-15 02:20:16 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1797 | imp_is_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1798 | { |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 1799 | PyObject *name; |
| 1800 | if (!PyArg_ParseTuple(args, "U:is_builtin", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | return NULL; |
| 1802 | return PyLong_FromLong(is_builtin(name)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1805 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1806 | imp_is_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1807 | { |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1808 | PyObject *name; |
Benjamin Peterson | 6fba3db | 2013-03-18 23:13:31 -0700 | [diff] [blame] | 1809 | const struct _frozen *p; |
Victor Stinner | 53dc735 | 2011-03-20 01:50:21 +0100 | [diff] [blame] | 1810 | if (!PyArg_ParseTuple(args, "U:is_frozen", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | return NULL; |
| 1812 | p = find_frozen(name); |
| 1813 | return PyBool_FromLong((long) (p == NULL ? 0 : p->size)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 1816 | #ifdef HAVE_DYNAMIC_LOADING |
| 1817 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1818 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1819 | imp_load_dynamic(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1820 | { |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 1821 | PyObject *name, *pathname, *fob = NULL, *mod; |
| 1822 | FILE *fp; |
| 1823 | |
| 1824 | if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic", |
| 1825 | &name, PyUnicode_FSDecoder, &pathname, &fob)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | return NULL; |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 1827 | if (fob != NULL) { |
Antoine Pitrou | f3a42de | 2012-05-04 22:40:25 +0200 | [diff] [blame] | 1828 | fp = _Py_fopen(pathname, "r"); |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 1829 | if (fp == NULL) { |
| 1830 | Py_DECREF(pathname); |
Antoine Pitrou | f3a42de | 2012-05-04 22:40:25 +0200 | [diff] [blame] | 1831 | if (!PyErr_Occurred()) |
| 1832 | PyErr_SetFromErrno(PyExc_IOError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | return NULL; |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 1834 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | } |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 1836 | else |
| 1837 | fp = NULL; |
| 1838 | mod = _PyImport_LoadDynamicModule(name, pathname, fp); |
Victor Stinner | e9ddbf6 | 2011-03-22 10:46:35 +0100 | [diff] [blame] | 1839 | Py_DECREF(pathname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | if (fp) |
| 1841 | fclose(fp); |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 1842 | return mod; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 1845 | #endif /* HAVE_DYNAMIC_LOADING */ |
| 1846 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1847 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 1848 | /* Doc strings */ |
| 1849 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1850 | PyDoc_STRVAR(doc_imp, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 1851 | "(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] | 1852 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1853 | PyDoc_STRVAR(doc_extension_suffixes, |
| 1854 | "extension_suffixes() -> list of strings\n\ |
| 1855 | Returns the list of file suffixes used to identify extension modules."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 1856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1857 | PyDoc_STRVAR(doc_lock_held, |
Tim Peters | a7c6509 | 2004-08-01 23:26:05 +0000 | [diff] [blame] | 1858 | "lock_held() -> boolean\n\ |
| 1859 | Return True if the import lock is currently held, else False.\n\ |
| 1860 | On platforms without threads, return False."); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 1861 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 1862 | PyDoc_STRVAR(doc_acquire_lock, |
| 1863 | "acquire_lock() -> None\n\ |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 1864 | Acquires the interpreter's import lock for the current thread.\n\ |
| 1865 | This lock should be used by import hooks to ensure thread-safety\n\ |
| 1866 | when importing modules.\n\ |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 1867 | On platforms without threads, this function does nothing."); |
| 1868 | |
| 1869 | PyDoc_STRVAR(doc_release_lock, |
| 1870 | "release_lock() -> None\n\ |
| 1871 | Release the interpreter's import lock.\n\ |
| 1872 | On platforms without threads, this function does nothing."); |
| 1873 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1874 | static PyMethodDef imp_methods[] = { |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 1875 | {"extension_suffixes", imp_extension_suffixes, METH_NOARGS, |
| 1876 | doc_extension_suffixes}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 | {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, |
| 1878 | {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock}, |
| 1879 | {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | {"get_frozen_object", imp_get_frozen_object, METH_VARARGS}, |
| 1881 | {"is_frozen_package", imp_is_frozen_package, METH_VARARGS}, |
| 1882 | {"init_builtin", imp_init_builtin, METH_VARARGS}, |
| 1883 | {"init_frozen", imp_init_frozen, METH_VARARGS}, |
| 1884 | {"is_builtin", imp_is_builtin, METH_VARARGS}, |
| 1885 | {"is_frozen", imp_is_frozen, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 1886 | #ifdef HAVE_DYNAMIC_LOADING |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | {"load_dynamic", imp_load_dynamic, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 1888 | #endif |
Brett Cannon | 442c9b9 | 2011-03-23 16:14:42 -0700 | [diff] [blame] | 1889 | {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1891 | }; |
| 1892 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1893 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1894 | static struct PyModuleDef impmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | PyModuleDef_HEAD_INIT, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 1896 | "_imp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | doc_imp, |
| 1898 | 0, |
| 1899 | imp_methods, |
| 1900 | NULL, |
| 1901 | NULL, |
| 1902 | NULL, |
| 1903 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1904 | }; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1905 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 1906 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1907 | PyInit_imp(void) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | PyObject *m, *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1911 | m = PyModule_Create(&impmodule); |
| 1912 | if (m == NULL) |
| 1913 | goto failure; |
| 1914 | d = PyModule_GetDict(m); |
| 1915 | if (d == NULL) |
| 1916 | goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | return m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1919 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | Py_XDECREF(m); |
| 1921 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1922 | } |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1923 | |
| 1924 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1925 | /* API for embedding applications that want to add their own entries |
| 1926 | to the table of built-in modules. This should normally be called |
| 1927 | *before* Py_Initialize(). When the table resize fails, -1 is |
| 1928 | returned and the existing table is unchanged. |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1929 | |
| 1930 | After a similar function by Just van Rossum. */ |
| 1931 | |
| 1932 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1933 | PyImport_ExtendInittab(struct _inittab *newtab) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | static struct _inittab *our_copy = NULL; |
| 1936 | struct _inittab *p; |
| 1937 | int i, n; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1939 | /* Count the number of entries in both tables */ |
| 1940 | for (n = 0; newtab[n].name != NULL; n++) |
| 1941 | ; |
| 1942 | if (n == 0) |
| 1943 | return 0; /* Nothing to do */ |
| 1944 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) |
| 1945 | ; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1947 | /* Allocate new memory for the combined table */ |
| 1948 | p = our_copy; |
| 1949 | PyMem_RESIZE(p, struct _inittab, i+n+1); |
| 1950 | if (p == NULL) |
| 1951 | return -1; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | /* Copy the tables into the new memory */ |
| 1954 | if (our_copy != PyImport_Inittab) |
| 1955 | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
| 1956 | PyImport_Inittab = our_copy = p; |
| 1957 | memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 | return 0; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /* Shorthand to add a single entry given a name and a function */ |
| 1963 | |
| 1964 | int |
Brett Cannon | a826f32 | 2009-04-02 03:41:46 +0000 | [diff] [blame] | 1965 | PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1967 | struct _inittab newtab[2]; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | memset(newtab, '\0', sizeof newtab); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | newtab[0].name = (char *)name; |
| 1972 | newtab[0].initfunc = initfunc; |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | return PyImport_ExtendInittab(newtab); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 1975 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1976 | |
| 1977 | #ifdef __cplusplus |
| 1978 | } |
| 1979 | #endif |