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