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