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