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