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