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