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