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