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