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