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