blob: 53c0f4defb40dd28093d8dc1e74f2b1bb109a289 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module definition and import implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +00009#include "marshal.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010#include "code.h"
Antoine Pitroubc07a5c2012-07-08 12:01:27 +020011#include "frameobject.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000012#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000013#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000014
Guido van Rossum55a83382000-09-20 20:31:38 +000015#ifdef HAVE_FCNTL_H
16#include <fcntl.h>
17#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000018#ifdef __cplusplus
Brett Cannon9a5b25a2010-03-01 02:09:17 +000019extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000020#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000021
Barry Warsaw28a691b2010-04-17 00:19:56 +000022#define CACHEDIR "__pycache__"
Guido van Rossum96774c12000-05-01 20:19:08 +000023
Victor Stinner95872862011-03-07 18:20:56 +010024/* See _PyImport_FixupExtensionObject() below */
Guido van Rossum25ce5661997-08-02 03:10:38 +000025static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000026
Guido van Rossum771c6c81997-10-31 18:37:24 +000027/* This table is defined in config.c: */
28extern struct _inittab _PyImport_Inittab[];
29
30struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000031
Victor Stinnerd0296212011-03-14 14:04:10 -040032static PyObject *initstr = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000033
Brett Cannon4caa61d2014-01-09 19:03:32 -050034/*[clinic input]
35module _imp
36[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030037/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/
Brett Cannonfd4d0502014-05-30 11:21:14 -040038
39#include "clinic/import.c.h"
Brett Cannon4caa61d2014-01-09 19:03:32 -050040
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042
43void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000044_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045{
Serhiy Storchaka013bb912014-02-10 18:21:34 +020046 PyInterpreterState *interp = PyThreadState_Get()->interp;
Victor Stinnerd0296212011-03-14 14:04:10 -040047 initstr = PyUnicode_InternFromString("__init__");
48 if (initstr == NULL)
49 Py_FatalError("Can't initialize import variables");
Serhiy Storchaka013bb912014-02-10 18:21:34 +020050 interp->builtins_copy = PyDict_Copy(interp->builtins);
51 if (interp->builtins_copy == NULL)
52 Py_FatalError("Can't backup builtins dict");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053}
54
Guido van Rossum25ce5661997-08-02 03:10:38 +000055void
Just van Rossum52e14d62002-12-30 22:08:05 +000056_PyImportHooks_Init(void)
57{
Brett Cannonfd074152012-04-14 14:10:13 -040058 PyObject *v, *path_hooks = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 int err = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +000060
Brett Cannonfd074152012-04-14 14:10:13 -040061 /* adding sys.path_hooks and sys.path_importer_cache */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 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 Rossum52e14d62002-12-30 22:08:05 +000081 error:
Brett Cannonfd074152012-04-14 14:10:13 -040082 PyErr_Print();
83 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
Nadeem Vawda8f46d652012-05-05 12:27:30 +020084 "or path_importer_cache failed");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 }
Brett Cannonfd074152012-04-14 14:10:13 -040086 Py_DECREF(path_hooks);
87}
88
89void
90_PyImportZip_Init(void)
91{
92 PyObject *path_hooks, *zimpimport;
93 int err = 0;
94
95 path_hooks = PySys_GetObject("path_hooks");
Victor Stinner1e53bba2013-07-16 22:26:05 +020096 if (path_hooks == NULL) {
97 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks");
Brett Cannonfd074152012-04-14 14:10:13 -040098 goto error;
Victor Stinner1e53bba2013-07-16 22:26:05 +020099 }
Brett Cannonfd074152012-04-14 14:10:13 -0400100
101 if (Py_VerboseFlag)
102 PySys_WriteStderr("# installing zipimport hook\n");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 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öwisbd928fe2011-10-14 10:20:37 +0200111 _Py_IDENTIFIER(zipimporter);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200112 PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
113 &PyId_zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 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 Cannon8923a4d2012-04-24 22:03:46 -0400122 /* sys.path_hooks.insert(0, zipimporter) */
123 err = PyList_Insert(path_hooks, 0, zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 Py_DECREF(zipimporter);
Brett Cannonfd074152012-04-14 14:10:13 -0400125 if (err < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -0400127 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 if (Py_VerboseFlag)
129 PySys_WriteStderr(
130 "# installed zipimport hook\n");
131 }
132 }
Brett Cannonfd074152012-04-14 14:10:13 -0400133
134 return;
135
136 error:
137 PyErr_Print();
Brett Cannonacf85cd2012-04-29 12:50:03 -0400138 Py_FatalError("initializing zipimport failed");
Just van Rossum52e14d62002-12-30 22:08:05 +0000139}
140
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000141/* 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 Rossum49b56061998-10-01 20:42:43 +0000147#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000148
Guido van Rossum65d5b571998-12-21 19:32:43 +0000149static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000150static long import_lock_thread = -1;
151static int import_lock_level = 0;
152
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000153void
154_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 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 Pitrou202b6062012-12-18 22:18:17 +0100174 assert(import_lock_level == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 import_lock_thread = me;
176 import_lock_level = 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000177}
178
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000179int
180_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 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 Pitrou202b6062012-12-18 22:18:17 +0100188 assert(import_lock_level >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 if (import_lock_level == 0) {
190 import_lock_thread = -1;
191 PyThread_release_lock(import_lock);
192 }
193 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000194}
195
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000196/* 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 Rossum8ee3e5a2005-09-14 18:09:42 +0000200
201void
202_PyImport_ReInitLock(void)
203{
Christian Heimes418fd742015-04-19 21:08:42 +0200204 if (import_lock != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 import_lock = PyThread_allocate_lock();
Christian Heimes418fd742015-04-19 21:08:42 +0200206 if (import_lock == NULL) {
207 Py_FatalError("PyImport_ReInitLock failed to create a new lock");
208 }
209 }
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000210 if (import_lock_level > 1) {
211 /* Forked as a side effect of import */
212 long me = PyThread_get_thread_ident();
Brett Cannone4710cf2012-11-15 21:39:36 -0500213 /* 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 Coghlanb2ddf792010-12-02 04:11:46 +0000218 import_lock_thread = me;
219 import_lock_level--;
220 } else {
221 import_lock_thread = -1;
222 import_lock_level = 0;
223 }
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000224}
225
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000226#endif
227
Brett Cannon4caa61d2014-01-09 19:03:32 -0500228/*[clinic input]
229_imp.lock_held
230
231Return True if the import lock is currently held, else False.
232
233On platforms without threads, return False.
234[clinic start generated code]*/
235
Brett Cannon4caa61d2014-01-09 19:03:32 -0500236static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300237_imp_lock_held_impl(PyObject *module)
238/*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/
Tim Peters69232342001-08-30 05:16:13 +0000239{
Tim Peters69232342001-08-30 05:16:13 +0000240#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000242#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000244#endif
245}
246
Brett Cannon4caa61d2014-01-09 19:03:32 -0500247/*[clinic input]
248_imp.acquire_lock
249
250Acquires the interpreter's import lock for the current thread.
251
252This lock should be used by import hooks to ensure thread-safety when importing
253modules. On platforms without threads, this function does nothing.
254[clinic start generated code]*/
255
Brett Cannon4caa61d2014-01-09 19:03:32 -0500256static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300257_imp_acquire_lock_impl(PyObject *module)
258/*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000259{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000260#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000262#endif
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200263 Py_RETURN_NONE;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000264}
265
Brett Cannon4caa61d2014-01-09 19:03:32 -0500266/*[clinic input]
267_imp.release_lock
268
269Release the interpreter's import lock.
270
271On platforms without threads, this function does nothing.
272[clinic start generated code]*/
273
Brett Cannon4caa61d2014-01-09 19:03:32 -0500274static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300275_imp_release_lock_impl(PyObject *module)
276/*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000277{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000278#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 if (_PyImport_ReleaseLock() < 0) {
280 PyErr_SetString(PyExc_RuntimeError,
281 "not holding the import lock");
282 return NULL;
283 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000284#endif
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200285 Py_RETURN_NONE;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000286}
287
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100288void
289_PyImport_Fini(void)
290{
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200291 Py_CLEAR(extensions);
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100292#ifdef WITH_THREAD
293 if (import_lock != NULL) {
294 PyThread_free_lock(import_lock);
295 import_lock = NULL;
296 }
297#endif
298}
299
Guido van Rossum25ce5661997-08-02 03:10:38 +0000300/* Helper for sys */
301
302PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 PyInterpreterState *interp = PyThreadState_GET()->interp;
306 if (interp->modules == NULL)
307 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
308 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000309}
310
Guido van Rossum3f5da241990-12-20 15:06:42 +0000311
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000312/* List of names to clear in sys */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200313static const char * const sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 "path", "argv", "ps1", "ps2",
315 "last_type", "last_value", "last_traceback",
316 "path_hooks", "path_importer_cache", "meta_path",
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200317 "__interactivehook__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 /* misc stuff */
319 "flags", "float_info",
320 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000321};
322
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200323static const char * const sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 "stdin", "__stdin__",
325 "stdout", "__stdout__",
326 "stderr", "__stderr__",
327 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000328};
329
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000330/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331
Guido van Rossum3f5da241990-12-20 15:06:42 +0000332void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000334{
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200335 Py_ssize_t pos;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 PyObject *key, *value, *dict;
337 PyInterpreterState *interp = PyThreadState_GET()->interp;
338 PyObject *modules = interp->modules;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200339 PyObject *weaklist = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200340 const char * const *p;
Guido van Rossum758eec01998-01-19 21:58:26 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 if (modules == NULL)
343 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 /* Delete some special variables first. These are common
346 places where user values hide and people complain when their
347 destructors fail. Since the modules containing them are
348 deleted *last* of all, they would come too late in the normal
349 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000350
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200351 /* XXX Perhaps these precautions are obsolete. Who knows? */
352
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200353 if (Py_VerboseFlag)
354 PySys_WriteStderr("# clear builtins._\n");
355 PyDict_SetItemString(interp->builtins, "_", Py_None);
356
357 for (p = sys_deletes; *p != NULL; p++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 if (Py_VerboseFlag)
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200359 PySys_WriteStderr("# clear sys.%s\n", *p);
360 PyDict_SetItemString(interp->sysdict, *p, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 }
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200362 for (p = sys_files; *p != NULL; p+=2) {
363 if (Py_VerboseFlag)
364 PySys_WriteStderr("# restore sys.%s\n", *p);
365 value = PyDict_GetItemString(interp->sysdict, *(p+1));
366 if (value == NULL)
367 value = Py_None;
368 PyDict_SetItemString(interp->sysdict, *p, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000370
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200371 /* We prepare a list which will receive (name, weakref) tuples of
372 modules when they are removed from sys.modules. The name is used
373 for diagnosis messages (in verbose mode), while the weakref helps
374 detect those modules which have been held alive. */
375 weaklist = PyList_New(0);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200376 if (weaklist == NULL)
377 PyErr_Clear();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200378
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200379#define STORE_MODULE_WEAKREF(name, mod) \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200380 if (weaklist != NULL) { \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200381 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
382 if (name && wr) { \
383 PyObject *tup = PyTuple_Pack(2, name, wr); \
384 PyList_Append(weaklist, tup); \
385 Py_XDECREF(tup); \
386 } \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200387 Py_XDECREF(wr); \
388 if (PyErr_Occurred()) \
389 PyErr_Clear(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000391
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200392 /* Remove all modules from sys.modules, hoping that garbage collection
393 can reclaim most of them. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 pos = 0;
395 while (PyDict_Next(modules, &pos, &key, &value)) {
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200396 if (PyModule_Check(value)) {
397 if (Py_VerboseFlag && PyUnicode_Check(key))
Victor Stinnerab826d12014-07-07 23:06:15 +0200398 PySys_FormatStderr("# cleanup[2] removing %U\n", key);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200399 STORE_MODULE_WEAKREF(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 PyDict_SetItem(modules, key, Py_None);
401 }
402 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000403
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200404 /* Clear the modules dict. */
405 PyDict_Clear(modules);
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200406 /* Restore the original builtins dict, to ensure that any
407 user data gets cleared. */
408 dict = PyDict_Copy(interp->builtins);
409 if (dict == NULL)
410 PyErr_Clear();
411 PyDict_Clear(interp->builtins);
412 if (PyDict_Update(interp->builtins, interp->builtins_copy))
413 PyErr_Clear();
414 Py_XDECREF(dict);
Antoine Pitrou40322e62013-08-11 00:30:09 +0200415 /* Clear module dict copies stored in the interpreter state */
416 _PyState_ClearModules();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200417 /* Collect references */
418 _PyGC_CollectNoFail();
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200419 /* Dump GC stats before it's too late, since it uses the warnings
420 machinery. */
421 _PyGC_DumpShutdownStats();
422
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200423 /* Now, if there are any modules left alive, clear their globals to
424 minimize potential leaks. All C extension modules actually end
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200425 up here, since they are kept alive in the interpreter state.
426
427 The special treatment of "builtins" here is because even
428 when it's not referenced as a module, its dictionary is
429 referenced by almost every module's __builtins__. Since
430 deleting a module clears its dictionary (even if there are
431 references left to it), we need to delete the "builtins"
432 module last. Likewise, we don't delete sys until the very
433 end because it is implicitly referenced (e.g. by print). */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200434 if (weaklist != NULL) {
435 Py_ssize_t i, n;
436 n = PyList_GET_SIZE(weaklist);
437 for (i = 0; i < n; i++) {
438 PyObject *tup = PyList_GET_ITEM(weaklist, i);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200439 PyObject *name = PyTuple_GET_ITEM(tup, 0);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200440 PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
441 if (mod == Py_None)
442 continue;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200443 assert(PyModule_Check(mod));
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200444 dict = PyModule_GetDict(mod);
445 if (dict == interp->builtins || dict == interp->sysdict)
446 continue;
447 Py_INCREF(mod);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200448 if (Py_VerboseFlag && PyUnicode_Check(name))
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200449 PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200450 _PyModule_Clear(mod);
451 Py_DECREF(mod);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200452 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200453 Py_DECREF(weaklist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000455
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200456 /* Next, delete sys and builtins (in that order) */
457 if (Py_VerboseFlag)
458 PySys_FormatStderr("# cleanup[3] wiping sys\n");
459 _PyModule_ClearDict(interp->sysdict);
460 if (Py_VerboseFlag)
461 PySys_FormatStderr("# cleanup[3] wiping builtins\n");
462 _PyModule_ClearDict(interp->builtins);
463
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200464 /* Clear and delete the modules directory. Actual modules will
465 still be there only if imported during the execution of some
466 destructor. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 interp->modules = NULL;
468 Py_DECREF(modules);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200469
470 /* Once more */
471 _PyGC_CollectNoFail();
472
473#undef STORE_MODULE_WEAKREF
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000474}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000475
476
Barry Warsaw28a691b2010-04-17 00:19:56 +0000477/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000478
479long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000480PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000481{
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200482 long res;
Brett Cannon77b2abd2012-07-09 16:09:00 -0400483 PyInterpreterState *interp = PyThreadState_Get()->interp;
Eric Snow32439d62015-05-02 19:15:18 -0600484 PyObject *external, *pyc_magic;
485
486 external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
487 if (external == NULL)
488 return -1;
489 pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER");
490 Py_DECREF(external);
Brett Cannon77b2abd2012-07-09 16:09:00 -0400491 if (pyc_magic == NULL)
492 return -1;
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200493 res = PyLong_AsLong(pyc_magic);
Benjamin Peterson66f36592012-07-09 22:21:55 -0700494 Py_DECREF(pyc_magic);
495 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000496}
497
498
Brett Cannon3adc7b72012-07-09 14:22:12 -0400499extern const char * _PySys_ImplCacheTag;
500
Barry Warsaw28a691b2010-04-17 00:19:56 +0000501const char *
502PyImport_GetMagicTag(void)
503{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400504 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000505}
506
Brett Cannon98979b82012-07-02 15:13:11 -0400507
Guido van Rossum25ce5661997-08-02 03:10:38 +0000508/* Magic for extension modules (built-in as well as dynamically
509 loaded). To prevent initializing an extension module more than
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200510 once, we keep a static dictionary 'extensions' keyed by the tuple
511 (module name, module name) (for built-in modules) or by
512 (filename, module name) (for dynamically loaded modules), containing these
513 modules. A copy of the module's dictionary is stored by calling
514 _PyImport_FixupExtensionObject() immediately after the module initialization
515 function succeeds. A copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100516 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000517
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000518 Modules which do support multiple initialization set their m_size
519 field to a non-negative number (indicating the size of the
520 module-specific state). They are still recorded in the extensions
521 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000522*/
523
524int
Victor Stinner95872862011-03-07 18:20:56 +0100525_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
526 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000527{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500528 PyObject *modules, *dict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 struct PyModuleDef *def;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500530 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (extensions == NULL) {
532 extensions = PyDict_New();
533 if (extensions == NULL)
534 return -1;
535 }
536 if (mod == NULL || !PyModule_Check(mod)) {
537 PyErr_BadInternalCall();
538 return -1;
539 }
540 def = PyModule_GetDef(mod);
541 if (!def) {
542 PyErr_BadInternalCall();
543 return -1;
544 }
545 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100546 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 return -1;
548 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100549 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 return -1;
551 }
552 if (def->m_size == -1) {
553 if (def->m_base.m_copy) {
554 /* Somebody already imported the module,
555 likely under a different name.
556 XXX this should really not happen. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200557 Py_CLEAR(def->m_base.m_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 }
559 dict = PyModule_GetDict(mod);
560 if (dict == NULL)
561 return -1;
562 def->m_base.m_copy = PyDict_Copy(dict);
563 if (def->m_base.m_copy == NULL)
564 return -1;
565 }
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500566 key = PyTuple_Pack(2, filename, name);
567 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200568 return -1;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500569 res = PyDict_SetItem(extensions, key, (PyObject *)def);
570 Py_DECREF(key);
571 if (res < 0)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200572 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000574}
575
Victor Stinner49d3f252010-10-17 01:24:53 +0000576int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300577_PyImport_FixupBuiltin(PyObject *mod, const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000578{
579 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100580 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100581 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100582 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000583 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100584 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
585 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000586 return res;
587}
588
Guido van Rossum25ce5661997-08-02 03:10:38 +0000589PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100590_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500592 PyObject *mod, *mdict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyModuleDef* def;
594 if (extensions == NULL)
595 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500596 key = PyTuple_Pack(2, filename, name);
597 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200598 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500599 def = (PyModuleDef *)PyDict_GetItem(extensions, key);
600 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 if (def == NULL)
602 return NULL;
603 if (def->m_size == -1) {
604 /* Module does not support repeated initialization */
605 if (def->m_base.m_copy == NULL)
606 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100607 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 if (mod == NULL)
609 return NULL;
610 mdict = PyModule_GetDict(mod);
611 if (mdict == NULL)
612 return NULL;
613 if (PyDict_Update(mdict, def->m_base.m_copy))
614 return NULL;
615 }
616 else {
617 if (def->m_base.m_init == NULL)
618 return NULL;
619 mod = def->m_base.m_init();
620 if (mod == NULL)
621 return NULL;
Christian Heimes09ca7942013-07-20 14:51:53 +0200622 if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) {
623 Py_DECREF(mod);
624 return NULL;
625 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 Py_DECREF(mod);
627 }
628 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100629 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 Py_DECREF(mod);
631 return NULL;
632 }
633 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100634 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 name, filename);
636 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000637
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000638}
639
Victor Stinner49d3f252010-10-17 01:24:53 +0000640PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000641_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000642{
Victor Stinner95872862011-03-07 18:20:56 +0100643 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100644 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100645 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000646 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100647 res = _PyImport_FindExtensionObject(nameobj, nameobj);
648 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000649 return res;
650}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651
652/* Get the module object corresponding to a module name.
653 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000654 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000655 Because the former action is most common, THIS DOES NOT RETURN A
656 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000659PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 PyObject *modules = PyImport_GetModuleDict();
662 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000663
Serhiy Storchaka48a583b2016-02-10 10:31:20 +0200664 if ((m = PyDict_GetItemWithError(modules, name)) != NULL &&
665 PyModule_Check(m)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 return m;
Serhiy Storchaka48a583b2016-02-10 10:31:20 +0200667 }
668 if (PyErr_Occurred()) {
669 return NULL;
670 }
Victor Stinner27ee0892011-03-04 12:57:09 +0000671 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (m == NULL)
673 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000674 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 Py_DECREF(m);
676 return NULL;
677 }
678 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000681}
682
Victor Stinner27ee0892011-03-04 12:57:09 +0000683PyObject *
684PyImport_AddModule(const char *name)
685{
686 PyObject *nameobj, *module;
687 nameobj = PyUnicode_FromString(name);
688 if (nameobj == NULL)
689 return NULL;
690 module = PyImport_AddModuleObject(nameobj);
691 Py_DECREF(nameobj);
692 return module;
693}
694
695
Tim Peters1cd70172004-08-02 03:52:12 +0000696/* Remove name from sys.modules, if it's there. */
697static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000698remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000701 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000703 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 Py_FatalError("import: deleting existing key in"
705 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000706}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707
Christian Heimes3b06e532008-01-07 20:12:44 +0000708
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000709/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000710 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
711 * removed from sys.modules, to avoid leaving damaged module objects
712 * in sys.modules. The caller may wish to restore the original
713 * module object (if any) in this case; PyImport_ReloadModule is an
714 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000715 *
716 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
717 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000718 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300720PyImport_ExecCodeModule(const char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 return PyImport_ExecCodeModuleWithPathnames(
723 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000724}
725
726PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300727PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 return PyImport_ExecCodeModuleWithPathnames(
730 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000731}
732
733PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300734PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
735 const char *pathname,
736 const char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000737{
Victor Stinner27ee0892011-03-04 12:57:09 +0000738 PyObject *m = NULL;
Eric Snow32439d62015-05-02 19:15:18 -0600739 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000740
741 nameobj = PyUnicode_FromString(name);
742 if (nameobj == NULL)
743 return NULL;
744
Victor Stinner27ee0892011-03-04 12:57:09 +0000745 if (cpathname != NULL) {
746 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
747 if (cpathobj == NULL)
748 goto error;
Brett Cannona6473f92012-07-13 13:57:03 -0400749 }
750 else
Victor Stinner27ee0892011-03-04 12:57:09 +0000751 cpathobj = NULL;
Brett Cannona6473f92012-07-13 13:57:03 -0400752
753 if (pathname != NULL) {
754 pathobj = PyUnicode_DecodeFSDefault(pathname);
755 if (pathobj == NULL)
756 goto error;
757 }
758 else if (cpathobj != NULL) {
759 PyInterpreterState *interp = PyThreadState_GET()->interp;
760 _Py_IDENTIFIER(_get_sourcefile);
761
762 if (interp == NULL) {
763 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
764 "no interpreter!");
765 }
766
Eric Snow32439d62015-05-02 19:15:18 -0600767 external= PyObject_GetAttrString(interp->importlib,
768 "_bootstrap_external");
769 if (external != NULL) {
770 pathobj = _PyObject_CallMethodIdObjArgs(external,
771 &PyId__get_sourcefile, cpathobj,
772 NULL);
773 Py_DECREF(external);
774 }
Brett Cannona6473f92012-07-13 13:57:03 -0400775 if (pathobj == NULL)
776 PyErr_Clear();
777 }
778 else
779 pathobj = NULL;
780
Victor Stinner27ee0892011-03-04 12:57:09 +0000781 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
782error:
783 Py_DECREF(nameobj);
784 Py_XDECREF(pathobj);
785 Py_XDECREF(cpathobj);
786 return m;
787}
788
Brett Cannon18fc4e72014-04-04 10:01:46 -0400789static PyObject *
790module_dict_for_exec(PyObject *name)
Victor Stinner27ee0892011-03-04 12:57:09 +0000791{
Brett Cannon18fc4e72014-04-04 10:01:46 -0400792 PyObject *m, *d = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000793
Victor Stinner27ee0892011-03-04 12:57:09 +0000794 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 if (m == NULL)
796 return NULL;
797 /* If the module is being reloaded, we get the old module back
798 and re-use its dict to exec the new code. */
799 d = PyModule_GetDict(m);
800 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
801 if (PyDict_SetItemString(d, "__builtins__",
Brett Cannon18fc4e72014-04-04 10:01:46 -0400802 PyEval_GetBuiltins()) != 0) {
803 remove_module(name);
804 return NULL;
805 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 }
Brett Cannon18fc4e72014-04-04 10:01:46 -0400807
Eric Snow08197a42014-05-12 17:54:55 -0600808 return d; /* Return a borrowed reference. */
Brett Cannon18fc4e72014-04-04 10:01:46 -0400809}
810
811static PyObject *
812exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object)
813{
814 PyObject *modules = PyImport_GetModuleDict();
815 PyObject *v, *m;
816
817 v = PyEval_EvalCode(code_object, module_dict, module_dict);
818 if (v == NULL) {
819 remove_module(name);
820 return NULL;
821 }
822 Py_DECREF(v);
823
824 if ((m = PyDict_GetItem(modules, name)) == NULL) {
825 PyErr_Format(PyExc_ImportError,
826 "Loaded module %R not found in sys.modules",
827 name);
828 return NULL;
829 }
830
831 Py_INCREF(m);
832
833 return m;
834}
835
836PyObject*
837PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
838 PyObject *cpathname)
839{
Eric Snow32439d62015-05-02 19:15:18 -0600840 PyObject *d, *external, *res;
Eric Snow08197a42014-05-12 17:54:55 -0600841 PyInterpreterState *interp = PyThreadState_GET()->interp;
842 _Py_IDENTIFIER(_fix_up_module);
Brett Cannon18fc4e72014-04-04 10:01:46 -0400843
844 d = module_dict_for_exec(name);
845 if (d == NULL) {
846 return NULL;
847 }
848
Eric Snow08197a42014-05-12 17:54:55 -0600849 if (pathname == NULL) {
850 pathname = ((PyCodeObject *)co)->co_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 }
Eric Snow32439d62015-05-02 19:15:18 -0600852 external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
853 if (external == NULL)
854 return NULL;
855 res = _PyObject_CallMethodIdObjArgs(external,
Eric Snow08197a42014-05-12 17:54:55 -0600856 &PyId__fix_up_module,
857 d, name, pathname, cpathname, NULL);
Eric Snow32439d62015-05-02 19:15:18 -0600858 Py_DECREF(external);
Eric Snow08197a42014-05-12 17:54:55 -0600859 if (res != NULL) {
Eric Snow58cfdd82014-05-29 12:31:39 -0600860 Py_DECREF(res);
Eric Snow08197a42014-05-12 17:54:55 -0600861 res = exec_code_in_module(name, d, co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 }
Eric Snow08197a42014-05-12 17:54:55 -0600863 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000864}
865
866
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000867static void
868update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 PyObject *constants, *tmp;
871 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (PyUnicode_Compare(co->co_filename, oldname))
874 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000875
Serhiy Storchaka576f1322016-01-05 21:27:54 +0200876 Py_INCREF(newname);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300877 Py_XSETREF(co->co_filename, newname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 constants = co->co_consts;
880 n = PyTuple_GET_SIZE(constants);
881 for (i = 0; i < n; i++) {
882 tmp = PyTuple_GET_ITEM(constants, i);
883 if (PyCode_Check(tmp))
884 update_code_filenames((PyCodeObject *)tmp,
885 oldname, newname);
886 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000887}
888
Victor Stinner2f42ae52011-03-20 00:41:24 +0100889static void
890update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000891{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100892 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000893
Victor Stinner2f42ae52011-03-20 00:41:24 +0100894 if (PyUnicode_Compare(co->co_filename, newname) == 0)
895 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 oldname = co->co_filename;
898 Py_INCREF(oldname);
899 update_code_filenames(co, oldname, newname);
900 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000901}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000902
Brett Cannon4caa61d2014-01-09 19:03:32 -0500903/*[clinic input]
904_imp._fix_co_filename
905
906 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
907 Code object to change.
908
909 path: unicode
910 File path to use.
911 /
912
913Changes code.co_filename to specify the passed-in file path.
914[clinic start generated code]*/
915
Brett Cannon4caa61d2014-01-09 19:03:32 -0500916static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300917_imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code,
Larry Hastings89964c42015-04-14 18:07:59 -0400918 PyObject *path)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300919/*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/
Brett Cannon442c9b92011-03-23 16:14:42 -0700920
Brett Cannon4caa61d2014-01-09 19:03:32 -0500921{
Brett Cannona6dec2e2014-01-10 07:43:55 -0500922 update_compiled_module(code, path);
Brett Cannon442c9b92011-03-23 16:14:42 -0700923
924 Py_RETURN_NONE;
925}
926
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000927
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000928/* Forward */
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700929static const struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000930
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000931
932/* Helper to test for built-in module */
933
934static int
Victor Stinner95872862011-03-07 18:20:56 +0100935is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000936{
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200937 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200939 if (_PyUnicode_EqualToASCIIString(name, PyImport_Inittab[i].name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 if (PyImport_Inittab[i].initfunc == NULL)
941 return -1;
942 else
943 return 1;
944 }
945 }
946 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000947}
948
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000949
Brett Cannonfdcdd9e2016-07-08 11:00:00 -0700950/* Return a finder object for a sys.path/pkg.__path__ item 'p',
Just van Rossum52e14d62002-12-30 22:08:05 +0000951 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +0000952 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +0000953 that can handle the path item. Return None if no hook could;
Brett Cannonfdcdd9e2016-07-08 11:00:00 -0700954 this tells our caller that the path based finder could not find
955 a finder for this path item. Cache the result in
956 path_importer_cache.
Just van Rossum52e14d62002-12-30 22:08:05 +0000957 Returns a borrowed reference. */
958
959static PyObject *
960get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +0000962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 PyObject *importer;
964 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 /* These conditions are the caller's responsibility: */
967 assert(PyList_Check(path_hooks));
968 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 nhooks = PyList_Size(path_hooks);
971 if (nhooks < 0)
972 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 importer = PyDict_GetItem(path_importer_cache, p);
975 if (importer != NULL)
976 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 /* set path_importer_cache[p] to None to avoid recursion */
979 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
980 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 for (j = 0; j < nhooks; j++) {
983 PyObject *hook = PyList_GetItem(path_hooks, j);
984 if (hook == NULL)
985 return NULL;
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100986 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 if (importer != NULL)
988 break;
Just van Rossum52e14d62002-12-30 22:08:05 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
991 return NULL;
992 }
993 PyErr_Clear();
994 }
995 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -0400996 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 }
998 if (importer != NULL) {
999 int err = PyDict_SetItem(path_importer_cache, p, importer);
1000 Py_DECREF(importer);
1001 if (err != 0)
1002 return NULL;
1003 }
1004 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001005}
1006
Christian Heimes9cd17752007-11-18 19:35:23 +00001007PyAPI_FUNC(PyObject *)
1008PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +00001010
Victor Stinner1e53bba2013-07-16 22:26:05 +02001011 path_importer_cache = PySys_GetObject("path_importer_cache");
1012 path_hooks = PySys_GetObject("path_hooks");
1013 if (path_importer_cache != NULL && path_hooks != NULL) {
1014 importer = get_path_importer(path_importer_cache,
1015 path_hooks, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 }
1017 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1018 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +00001019}
1020
Nick Coghland5cacbb2015-05-23 22:24:10 +10001021/*[clinic input]
1022_imp.create_builtin
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001023
Nick Coghland5cacbb2015-05-23 22:24:10 +10001024 spec: object
1025 /
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001026
Nick Coghland5cacbb2015-05-23 22:24:10 +10001027Create an extension module.
1028[clinic start generated code]*/
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001029
Nick Coghland5cacbb2015-05-23 22:24:10 +10001030static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001031_imp_create_builtin(PyObject *module, PyObject *spec)
1032/*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 struct _inittab *p;
Nick Coghland5cacbb2015-05-23 22:24:10 +10001035 PyObject *name;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001036 const char *namestr;
Victor Stinner5eb4f592013-11-14 22:38:52 +01001037 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001038
Nick Coghland5cacbb2015-05-23 22:24:10 +10001039 name = PyObject_GetAttrString(spec, "name");
1040 if (name == NULL) {
1041 return NULL;
1042 }
1043
Victor Stinner5eb4f592013-11-14 22:38:52 +01001044 mod = _PyImport_FindExtensionObject(name, name);
Nick Coghland5cacbb2015-05-23 22:24:10 +10001045 if (mod || PyErr_Occurred()) {
1046 Py_DECREF(name);
Raymond Hettingerf0afe772016-08-31 08:44:11 -07001047 Py_XINCREF(mod);
Nick Coghland5cacbb2015-05-23 22:24:10 +10001048 return mod;
1049 }
1050
1051 namestr = PyUnicode_AsUTF8(name);
1052 if (namestr == NULL) {
1053 Py_DECREF(name);
1054 return NULL;
1055 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 for (p = PyImport_Inittab; p->name != NULL; p++) {
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01001058 PyModuleDef *def;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001059 if (_PyUnicode_EqualToASCIIString(name, p->name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 if (p->initfunc == NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +10001061 /* Cannot re-init internal module ("sys" or "builtins") */
1062 mod = PyImport_AddModule(namestr);
1063 Py_DECREF(name);
1064 return mod;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 mod = (*p->initfunc)();
Nick Coghland5cacbb2015-05-23 22:24:10 +10001067 if (mod == NULL) {
1068 Py_DECREF(name);
1069 return NULL;
1070 }
1071 if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) {
1072 Py_DECREF(name);
1073 return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec);
1074 } else {
1075 /* Remember pointer to module init function. */
1076 def = PyModule_GetDef(mod);
Christian Heimesa78b6272016-09-09 00:25:03 +02001077 if (def == NULL) {
1078 Py_DECREF(name);
1079 return NULL;
1080 }
Nick Coghland5cacbb2015-05-23 22:24:10 +10001081 def->m_base.m_init = p->initfunc;
1082 if (_PyImport_FixupExtensionObject(mod, name, name) < 0) {
1083 Py_DECREF(name);
1084 return NULL;
1085 }
1086 Py_DECREF(name);
1087 return mod;
1088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 }
1090 }
Nick Coghland5cacbb2015-05-23 22:24:10 +10001091 Py_DECREF(name);
1092 Py_RETURN_NONE;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001093}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001094
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001095
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001096/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001097
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001098static const struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +01001099find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001100{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001101 const struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001102
Victor Stinner53dc7352011-03-20 01:50:21 +01001103 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 for (p = PyImport_FrozenModules; ; p++) {
1107 if (p->name == NULL)
1108 return NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001109 if (_PyUnicode_EqualToASCIIString(name, p->name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 break;
1111 }
1112 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001113}
1114
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001116get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001117{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001118 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (p == NULL) {
1122 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001123 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 name);
1125 return NULL;
1126 }
1127 if (p->code == NULL) {
1128 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001129 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 name);
1131 return NULL;
1132 }
1133 size = p->size;
1134 if (size < 0)
1135 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001136 return PyMarshal_ReadObjectFromString((const char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001137}
1138
Brett Cannon8d110132009-03-15 02:20:16 +00001139static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001140is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001141{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001142 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 if (p == NULL) {
1146 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001147 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 name);
1149 return NULL;
1150 }
Brett Cannon8d110132009-03-15 02:20:16 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (size < 0)
1155 Py_RETURN_TRUE;
1156 else
1157 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001158}
1159
1160
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001161/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001162 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001163 an exception set if the initialization failed.
1164 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001165
1166int
Victor Stinner53dc7352011-03-20 01:50:21 +01001167PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001168{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001169 const struct _frozen *p;
Brett Cannon18fc4e72014-04-04 10:01:46 -04001170 PyObject *co, *m, *d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 int ispackage;
1172 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001173
Victor Stinner53dc7352011-03-20 01:50:21 +01001174 p = find_frozen(name);
1175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 if (p == NULL)
1177 return 0;
1178 if (p->code == NULL) {
1179 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001180 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 name);
1182 return -1;
1183 }
1184 size = p->size;
1185 ispackage = (size < 0);
1186 if (ispackage)
1187 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001188 co = PyMarshal_ReadObjectFromString((const char *)p->code, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (co == NULL)
1190 return -1;
1191 if (!PyCode_Check(co)) {
1192 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001193 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 name);
1195 goto err_return;
1196 }
1197 if (ispackage) {
Brett Cannon3e0651b2013-05-31 23:18:39 -04001198 /* Set __path__ to the empty list */
Brett Cannon18fc4e72014-04-04 10:01:46 -04001199 PyObject *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001201 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 if (m == NULL)
1203 goto err_return;
1204 d = PyModule_GetDict(m);
Brett Cannon3e0651b2013-05-31 23:18:39 -04001205 l = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 goto err_return;
1208 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 err = PyDict_SetItemString(d, "__path__", l);
1210 Py_DECREF(l);
1211 if (err != 0)
1212 goto err_return;
1213 }
Brett Cannon18fc4e72014-04-04 10:01:46 -04001214 d = module_dict_for_exec(name);
1215 if (d == NULL) {
Victor Stinner53dc7352011-03-20 01:50:21 +01001216 goto err_return;
Brett Cannon18fc4e72014-04-04 10:01:46 -04001217 }
1218 m = exec_code_in_module(name, d, co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (m == NULL)
1220 goto err_return;
1221 Py_DECREF(co);
1222 Py_DECREF(m);
1223 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001224err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 Py_DECREF(co);
1226 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001227}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001228
Victor Stinner53dc7352011-03-20 01:50:21 +01001229int
Serhiy Storchakac6792272013-10-19 21:03:34 +03001230PyImport_ImportFrozenModule(const char *name)
Victor Stinner53dc7352011-03-20 01:50:21 +01001231{
1232 PyObject *nameobj;
1233 int ret;
1234 nameobj = PyUnicode_InternFromString(name);
1235 if (nameobj == NULL)
1236 return -1;
1237 ret = PyImport_ImportFrozenModuleObject(nameobj);
1238 Py_DECREF(nameobj);
1239 return ret;
1240}
1241
Guido van Rossum74e6a111994-08-29 12:54:38 +00001242
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001243/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001244 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001245
Guido van Rossum79f25d91997-04-29 20:08:16 +00001246PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001247PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 PyObject *pname;
1250 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 pname = PyUnicode_FromString(name);
1253 if (pname == NULL)
1254 return NULL;
1255 result = PyImport_Import(pname);
1256 Py_DECREF(pname);
1257 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001258}
1259
Christian Heimes072c0f12008-01-03 23:01:04 +00001260/* Import a module without blocking
1261 *
1262 * At first it tries to fetch the module from sys.modules. If the module was
1263 * never loaded before it loads it with PyImport_ImportModule() unless another
1264 * thread holds the import lock. In the latter case the function raises an
1265 * ImportError instead of blocking.
1266 *
1267 * Returns the module object with incremented ref count.
1268 */
1269PyObject *
1270PyImport_ImportModuleNoBlock(const char *name)
1271{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001272 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001273}
1274
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001275
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001276/* Remove importlib frames from the traceback,
1277 * except in Verbose mode. */
1278static void
1279remove_importlib_frames(void)
1280{
1281 const char *importlib_filename = "<frozen importlib._bootstrap>";
Eric Snow32439d62015-05-02 19:15:18 -06001282 const char *external_filename = "<frozen importlib._bootstrap_external>";
Nick Coghlan42c07662012-07-31 21:14:18 +10001283 const char *remove_frames = "_call_with_frames_removed";
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001284 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001285 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001286 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001287 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001288
1289 /* Synopsis: if it's an ImportError, we trim all importlib chunks
Nick Coghlan42c07662012-07-31 21:14:18 +10001290 from the traceback. We always trim chunks
1291 which end with a call to "_call_with_frames_removed". */
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001292
1293 PyErr_Fetch(&exception, &value, &base_tb);
1294 if (!exception || Py_VerboseFlag)
1295 goto done;
1296 if (PyType_IsSubtype((PyTypeObject *) exception,
1297 (PyTypeObject *) PyExc_ImportError))
1298 always_trim = 1;
1299
1300 prev_link = &base_tb;
1301 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001302 while (tb != NULL) {
1303 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1304 PyObject *next = (PyObject *) traceback->tb_next;
1305 PyFrameObject *frame = traceback->tb_frame;
1306 PyCodeObject *code = frame->f_code;
1307 int now_in_importlib;
1308
1309 assert(PyTraceBack_Check(tb));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001310 now_in_importlib = _PyUnicode_EqualToASCIIString(code->co_filename, importlib_filename) ||
1311 _PyUnicode_EqualToASCIIString(code->co_filename, external_filename);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001312 if (now_in_importlib && !in_importlib) {
1313 /* This is the link to this chunk of importlib tracebacks */
1314 outer_link = prev_link;
1315 }
1316 in_importlib = now_in_importlib;
1317
1318 if (in_importlib &&
1319 (always_trim ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001320 _PyUnicode_EqualToASCIIString(code->co_name, remove_frames))) {
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001321 Py_XINCREF(next);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001322 Py_XSETREF(*outer_link, next);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001323 prev_link = outer_link;
1324 }
1325 else {
1326 prev_link = (PyObject **) &traceback->tb_next;
1327 }
1328 tb = next;
1329 }
1330done:
1331 PyErr_Restore(exception, value, base_tb);
1332}
1333
1334
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001335static PyObject *
1336resolve_name(PyObject *name, PyObject *globals, int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001337{
Eric Snowb523f842013-11-22 09:05:39 -07001338 _Py_IDENTIFIER(__spec__);
Brett Cannonfd074152012-04-14 14:10:13 -04001339 _Py_IDENTIFIER(__package__);
1340 _Py_IDENTIFIER(__path__);
1341 _Py_IDENTIFIER(__name__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001342 _Py_IDENTIFIER(parent);
1343 PyObject *abs_name;
1344 PyObject *package = NULL;
1345 PyObject *spec;
1346 PyInterpreterState *interp = PyThreadState_GET()->interp;
1347 Py_ssize_t last_dot;
1348 PyObject *base;
1349 int level_up;
1350
1351 if (globals == NULL) {
1352 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
1353 goto error;
1354 }
1355 if (!PyDict_Check(globals)) {
1356 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1357 goto error;
1358 }
1359 package = _PyDict_GetItemId(globals, &PyId___package__);
1360 if (package == Py_None) {
1361 package = NULL;
1362 }
1363 spec = _PyDict_GetItemId(globals, &PyId___spec__);
1364
1365 if (package != NULL) {
1366 Py_INCREF(package);
1367 if (!PyUnicode_Check(package)) {
1368 PyErr_SetString(PyExc_TypeError, "package must be a string");
1369 goto error;
1370 }
1371 else if (spec != NULL && spec != Py_None) {
1372 int equal;
1373 PyObject *parent = _PyObject_GetAttrId(spec, &PyId_parent);
1374 if (parent == NULL) {
1375 goto error;
1376 }
1377
1378 equal = PyObject_RichCompareBool(package, parent, Py_EQ);
1379 Py_DECREF(parent);
1380 if (equal < 0) {
1381 goto error;
1382 }
1383 else if (equal == 0) {
1384 if (PyErr_WarnEx(PyExc_ImportWarning,
1385 "__package__ != __spec__.parent", 1) < 0) {
1386 goto error;
1387 }
1388 }
1389 }
1390 }
1391 else if (spec != NULL && spec != Py_None) {
1392 package = _PyObject_GetAttrId(spec, &PyId_parent);
1393 if (package == NULL) {
1394 goto error;
1395 }
1396 else if (!PyUnicode_Check(package)) {
1397 PyErr_SetString(PyExc_TypeError,
1398 "__spec__.parent must be a string");
1399 goto error;
1400 }
1401 }
1402 else {
1403 if (PyErr_WarnEx(PyExc_ImportWarning,
1404 "can't resolve package from __spec__ or __package__, "
1405 "falling back on __name__ and __path__", 1) < 0) {
1406 goto error;
1407 }
1408
1409 package = _PyDict_GetItemId(globals, &PyId___name__);
1410 if (package == NULL) {
1411 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
1412 goto error;
1413 }
1414
1415 Py_INCREF(package);
1416 if (!PyUnicode_Check(package)) {
1417 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1418 goto error;
1419 }
1420
1421 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
1422 Py_ssize_t dot;
1423
1424 if (PyUnicode_READY(package) < 0) {
1425 goto error;
1426 }
1427
1428 dot = PyUnicode_FindChar(package, '.',
1429 0, PyUnicode_GET_LENGTH(package), -1);
1430 if (dot == -2) {
1431 goto error;
1432 }
1433
1434 if (dot >= 0) {
1435 PyObject *substr = PyUnicode_Substring(package, 0, dot);
1436 if (substr == NULL) {
1437 goto error;
1438 }
1439 Py_SETREF(package, substr);
1440 }
1441 }
1442 }
1443
1444 last_dot = PyUnicode_GET_LENGTH(package);
1445 if (last_dot == 0) {
1446 PyErr_SetString(PyExc_ImportError,
1447 "attempted relative import with no known parent package");
1448 goto error;
1449 }
1450 else if (PyDict_GetItem(interp->modules, package) == NULL) {
1451 PyErr_Format(PyExc_SystemError,
1452 "Parent module %R not loaded, cannot perform relative "
1453 "import", package);
1454 goto error;
1455 }
1456
1457 for (level_up = 1; level_up < level; level_up += 1) {
1458 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1459 if (last_dot == -2) {
1460 goto error;
1461 }
1462 else if (last_dot == -1) {
1463 PyErr_SetString(PyExc_ValueError,
1464 "attempted relative import beyond top-level "
1465 "package");
1466 goto error;
1467 }
1468 }
1469
1470 base = PyUnicode_Substring(package, 0, last_dot);
1471 Py_DECREF(package);
1472 if (base == NULL || PyUnicode_GET_LENGTH(name) == 0) {
1473 return base;
1474 }
1475
1476 abs_name = PyUnicode_FromFormat("%U.%U", base, name);
1477 Py_DECREF(base);
1478 return abs_name;
1479
1480 error:
1481 Py_XDECREF(package);
1482 return NULL;
1483}
1484
1485PyObject *
1486PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
1487 PyObject *locals, PyObject *fromlist,
1488 int level)
1489{
Brett Cannonfd074152012-04-14 14:10:13 -04001490 _Py_IDENTIFIER(_find_and_load);
1491 _Py_IDENTIFIER(_handle_fromlist);
Brett Cannonfd074152012-04-14 14:10:13 -04001492 PyObject *abs_name = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001493 PyObject *final_mod = NULL;
1494 PyObject *mod = NULL;
1495 PyObject *package = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001496 PyInterpreterState *interp = PyThreadState_GET()->interp;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001497 int has_from;
Brett Cannonfd074152012-04-14 14:10:13 -04001498
Brett Cannonfd074152012-04-14 14:10:13 -04001499 if (name == NULL) {
1500 PyErr_SetString(PyExc_ValueError, "Empty module name");
1501 goto error;
1502 }
1503
1504 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1505 for added performance. */
1506
1507 if (!PyUnicode_Check(name)) {
1508 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1509 goto error;
1510 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001511 if (PyUnicode_READY(name) < 0) {
Brett Cannonfd074152012-04-14 14:10:13 -04001512 goto error;
1513 }
1514 if (level < 0) {
1515 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1516 goto error;
1517 }
Brett Cannon849113a2016-01-22 15:25:50 -08001518
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001519 if (level > 0) {
1520 abs_name = resolve_name(name, globals, level);
1521 if (abs_name == NULL)
Brett Cannon9fa81262016-01-22 16:39:02 -08001522 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001523 }
1524 else { /* level == 0 */
1525 if (PyUnicode_GET_LENGTH(name) == 0) {
1526 PyErr_SetString(PyExc_ValueError, "Empty module name");
1527 goto error;
1528 }
Brett Cannonfd074152012-04-14 14:10:13 -04001529 abs_name = name;
1530 Py_INCREF(abs_name);
1531 }
1532
Brett Cannonfd074152012-04-14 14:10:13 -04001533 mod = PyDict_GetItem(interp->modules, abs_name);
1534 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001535 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1536 "None in sys.modules", abs_name);
1537 if (msg != NULL) {
Eric Snow46f97b82016-09-07 16:56:15 -07001538 PyErr_SetImportErrorSubclass(PyExc_ModuleNotFoundError, msg,
1539 abs_name, NULL);
Brian Curtin09b86d12012-04-17 16:57:09 -05001540 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001541 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001542 mod = NULL;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001543 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001544 }
1545 else if (mod != NULL) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001546 _Py_IDENTIFIER(__spec__);
1547 _Py_IDENTIFIER(_initializing);
1548 _Py_IDENTIFIER(_lock_unlock_module);
Eric Snowb523f842013-11-22 09:05:39 -07001549 PyObject *value = NULL;
1550 PyObject *spec;
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001551 int initializing = 0;
1552
Brett Cannonfd074152012-04-14 14:10:13 -04001553 Py_INCREF(mod);
Antoine Pitrou03989852012-08-28 00:24:52 +02001554 /* Optimization: only call _bootstrap._lock_unlock_module() if
Eric Snowb523f842013-11-22 09:05:39 -07001555 __spec__._initializing is true.
1556 NOTE: because of this, initializing must be set *before*
Antoine Pitrou03989852012-08-28 00:24:52 +02001557 stuffing the new module in sys.modules.
1558 */
Eric Snowb523f842013-11-22 09:05:39 -07001559 spec = _PyObject_GetAttrId(mod, &PyId___spec__);
1560 if (spec != NULL) {
1561 value = _PyObject_GetAttrId(spec, &PyId__initializing);
1562 Py_DECREF(spec);
1563 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001564 if (value == NULL)
1565 PyErr_Clear();
1566 else {
1567 initializing = PyObject_IsTrue(value);
1568 Py_DECREF(value);
1569 if (initializing == -1)
1570 PyErr_Clear();
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001571 if (initializing > 0) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001572#ifdef WITH_THREAD
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001573 _PyImport_AcquireLock();
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001574#endif
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001575 /* _bootstrap._lock_unlock_module() releases the import lock */
1576 value = _PyObject_CallMethodIdObjArgs(interp->importlib,
1577 &PyId__lock_unlock_module, abs_name,
1578 NULL);
1579 if (value == NULL)
1580 goto error;
1581 Py_DECREF(value);
1582 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001583 }
Brett Cannonfd074152012-04-14 14:10:13 -04001584 }
1585 else {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001586#ifdef WITH_THREAD
1587 _PyImport_AcquireLock();
1588#endif
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001589 /* _bootstrap._find_and_load() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001590 mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001591 &PyId__find_and_load, abs_name,
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001592 interp->import_func, NULL);
Brett Cannonfd074152012-04-14 14:10:13 -04001593 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001594 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001595 }
1596 }
1597
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001598 has_from = 0;
1599 if (fromlist != NULL && fromlist != Py_None) {
1600 has_from = PyObject_IsTrue(fromlist);
1601 if (has_from < 0)
1602 goto error;
1603 }
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001604 if (!has_from) {
Victor Stinner744c34e2016-05-20 11:36:13 +02001605 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
1606 if (level == 0 || len > 0) {
1607 Py_ssize_t dot;
Brett Cannonfd074152012-04-14 14:10:13 -04001608
Victor Stinner744c34e2016-05-20 11:36:13 +02001609 dot = PyUnicode_FindChar(name, '.', 0, len, 1);
1610 if (dot == -2) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001611 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001612 }
1613
Victor Stinner744c34e2016-05-20 11:36:13 +02001614 if (dot == -1) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001615 /* No dot in module name, simple exit */
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001616 final_mod = mod;
1617 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001618 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001619 }
1620
Brett Cannonfd074152012-04-14 14:10:13 -04001621 if (level == 0) {
Victor Stinner744c34e2016-05-20 11:36:13 +02001622 PyObject *front = PyUnicode_Substring(name, 0, dot);
1623 if (front == NULL) {
1624 goto error;
1625 }
1626
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001627 final_mod = PyImport_ImportModuleLevelObject(front, NULL, NULL, NULL, 0);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001628 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001629 }
1630 else {
Victor Stinner744c34e2016-05-20 11:36:13 +02001631 Py_ssize_t cut_off = len - dot;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001632 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001633 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001634 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001635 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001636 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001637 }
Brett Cannonfd074152012-04-14 14:10:13 -04001638
1639 final_mod = PyDict_GetItem(interp->modules, to_return);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001640 Py_DECREF(to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001641 if (final_mod == NULL) {
1642 PyErr_Format(PyExc_KeyError,
1643 "%R not in sys.modules as expected",
1644 to_return);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001645 goto error;
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001646 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001647 Py_INCREF(final_mod);
Brett Cannonfd074152012-04-14 14:10:13 -04001648 }
1649 }
1650 else {
1651 final_mod = mod;
1652 Py_INCREF(mod);
1653 }
1654 }
1655 else {
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001656 final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001657 &PyId__handle_fromlist, mod,
Serhiy Storchaka133138a2016-08-02 22:51:21 +03001658 fromlist, interp->import_func,
Brett Cannonfd074152012-04-14 14:10:13 -04001659 NULL);
1660 }
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001661
Brett Cannonfd074152012-04-14 14:10:13 -04001662 error:
1663 Py_XDECREF(abs_name);
Brett Cannonfd074152012-04-14 14:10:13 -04001664 Py_XDECREF(mod);
1665 Py_XDECREF(package);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001666 if (final_mod == NULL)
1667 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001668 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001669}
1670
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001671PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001672PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001673 PyObject *fromlist, int level)
1674{
1675 PyObject *nameobj, *mod;
1676 nameobj = PyUnicode_FromString(name);
1677 if (nameobj == NULL)
1678 return NULL;
1679 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1680 fromlist, level);
1681 Py_DECREF(nameobj);
1682 return mod;
1683}
1684
1685
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001686/* Re-import a module of any kind and return its module object, WITH
1687 INCREMENTED REFERENCE COUNT */
1688
Guido van Rossum79f25d91997-04-29 20:08:16 +00001689PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001690PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001691{
Brett Cannon62228db2012-04-29 14:38:11 -04001692 _Py_IDENTIFIER(reload);
1693 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001695 PyObject *imp = PyDict_GetItemString(modules, "imp");
1696 if (imp == NULL) {
1697 imp = PyImport_ImportModule("imp");
1698 if (imp == NULL) {
1699 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 }
Brett Cannon62228db2012-04-29 14:38:11 -04001702 else {
1703 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001705
Victor Stinner55ba38a2016-12-09 16:09:30 +01001706 reloaded_module = _PyObject_CallMethodIdObjArgs(imp, &PyId_reload, m, NULL);
Brett Cannon62228db2012-04-29 14:38:11 -04001707 Py_DECREF(imp);
1708 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001709}
1710
1711
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001712/* Higher-level import emulator which emulates the "import" statement
1713 more accurately -- it invokes the __import__() function from the
1714 builtins of the current globals. This means that the import is
1715 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001716 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001717 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001718 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001719 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001720
1721PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001722PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 static PyObject *silly_list = NULL;
1725 static PyObject *builtins_str = NULL;
1726 static PyObject *import_str = NULL;
1727 PyObject *globals = NULL;
1728 PyObject *import = NULL;
1729 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001730 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 /* Initialize constant string objects */
1734 if (silly_list == NULL) {
1735 import_str = PyUnicode_InternFromString("__import__");
1736 if (import_str == NULL)
1737 return NULL;
1738 builtins_str = PyUnicode_InternFromString("__builtins__");
1739 if (builtins_str == NULL)
1740 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001741 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 if (silly_list == NULL)
1743 return NULL;
1744 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 /* Get the builtins from current globals */
1747 globals = PyEval_GetGlobals();
1748 if (globals != NULL) {
1749 Py_INCREF(globals);
1750 builtins = PyObject_GetItem(globals, builtins_str);
1751 if (builtins == NULL)
1752 goto err;
1753 }
1754 else {
1755 /* No globals -- use standard builtins, and fake globals */
1756 builtins = PyImport_ImportModuleLevel("builtins",
1757 NULL, NULL, NULL, 0);
1758 if (builtins == NULL)
1759 return NULL;
1760 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1761 if (globals == NULL)
1762 goto err;
1763 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 /* Get the __import__ function from the builtins */
1766 if (PyDict_Check(builtins)) {
1767 import = PyObject_GetItem(builtins, import_str);
1768 if (import == NULL)
1769 PyErr_SetObject(PyExc_KeyError, import_str);
1770 }
1771 else
1772 import = PyObject_GetAttr(builtins, import_str);
1773 if (import == NULL)
1774 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001777 Always use absolute import here.
1778 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1780 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001781 if (r == NULL)
1782 goto err;
1783 Py_DECREF(r);
1784
1785 modules = PyImport_GetModuleDict();
1786 r = PyDict_GetItem(modules, module_name);
1787 if (r != NULL)
1788 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001789
1790 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 Py_XDECREF(globals);
1792 Py_XDECREF(builtins);
1793 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001796}
1797
Brett Cannon4caa61d2014-01-09 19:03:32 -05001798/*[clinic input]
1799_imp.extension_suffixes
1800
1801Returns the list of file suffixes used to identify extension modules.
1802[clinic start generated code]*/
1803
Brett Cannon4caa61d2014-01-09 19:03:32 -05001804static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001805_imp_extension_suffixes_impl(PyObject *module)
1806/*[clinic end generated code: output=0bf346e25a8f0cd3 input=ecdeeecfcb6f839e]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001809 const char *suffix;
1810 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 list = PyList_New(0);
1813 if (list == NULL)
1814 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001815#ifdef HAVE_DYNAMIC_LOADING
1816 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1817 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (item == NULL) {
1819 Py_DECREF(list);
1820 return NULL;
1821 }
1822 if (PyList_Append(list, item) < 0) {
1823 Py_DECREF(list);
1824 Py_DECREF(item);
1825 return NULL;
1826 }
1827 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001828 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 }
Brett Cannon2657df42012-05-04 15:20:40 -04001830#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001832}
1833
Brett Cannon4caa61d2014-01-09 19:03:32 -05001834/*[clinic input]
Brett Cannon4caa61d2014-01-09 19:03:32 -05001835_imp.init_frozen
1836
1837 name: unicode
1838 /
1839
1840Initializes a frozen module.
1841[clinic start generated code]*/
1842
Brett Cannon4caa61d2014-01-09 19:03:32 -05001843static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001844_imp_init_frozen_impl(PyObject *module, PyObject *name)
1845/*[clinic end generated code: output=fc0511ed869fd69c input=13019adfc04f3fb3]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 int ret;
1848 PyObject *m;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001849
Victor Stinner53dc7352011-03-20 01:50:21 +01001850 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 if (ret < 0)
1852 return NULL;
1853 if (ret == 0) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001854 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001856 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 Py_XINCREF(m);
1858 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001859}
1860
Brett Cannon4caa61d2014-01-09 19:03:32 -05001861/*[clinic input]
1862_imp.get_frozen_object
1863
1864 name: unicode
1865 /
1866
1867Create a code object for a frozen module.
1868[clinic start generated code]*/
1869
Brett Cannon4caa61d2014-01-09 19:03:32 -05001870static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001871_imp_get_frozen_object_impl(PyObject *module, PyObject *name)
1872/*[clinic end generated code: output=2568cc5b7aa0da63 input=ed689bc05358fdbd]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001875}
1876
Brett Cannon4caa61d2014-01-09 19:03:32 -05001877/*[clinic input]
1878_imp.is_frozen_package
1879
1880 name: unicode
1881 /
1882
1883Returns True if the module name is of a frozen package.
1884[clinic start generated code]*/
1885
Brett Cannon4caa61d2014-01-09 19:03:32 -05001886static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001887_imp_is_frozen_package_impl(PyObject *module, PyObject *name)
1888/*[clinic end generated code: output=e70cbdb45784a1c9 input=81b6cdecd080fbb8]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00001891}
1892
Brett Cannon4caa61d2014-01-09 19:03:32 -05001893/*[clinic input]
1894_imp.is_builtin
1895
1896 name: unicode
1897 /
1898
1899Returns True if the module name corresponds to a built-in module.
1900[clinic start generated code]*/
1901
Guido van Rossum79f25d91997-04-29 20:08:16 +00001902static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001903_imp_is_builtin_impl(PyObject *module, PyObject *name)
1904/*[clinic end generated code: output=3bfd1162e2d3be82 input=86befdac021dd1c7]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001905{
Brett Cannon4caa61d2014-01-09 19:03:32 -05001906 return PyLong_FromLong(is_builtin(name));
1907}
1908
1909/*[clinic input]
1910_imp.is_frozen
1911
1912 name: unicode
1913 /
1914
1915Returns True if the module name corresponds to a frozen module.
1916[clinic start generated code]*/
1917
Brett Cannon4caa61d2014-01-09 19:03:32 -05001918static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001919_imp_is_frozen_impl(PyObject *module, PyObject *name)
1920/*[clinic end generated code: output=01f408f5ec0f2577 input=7301dbca1897d66b]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001921{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001922 const struct _frozen *p;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 p = find_frozen(name);
1925 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001926}
1927
Larry Hastings1df0b352015-08-24 19:53:56 -07001928/* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */
1929static int
1930exec_builtin_or_dynamic(PyObject *mod) {
1931 PyModuleDef *def;
1932 void *state;
1933
1934 if (!PyModule_Check(mod)) {
1935 return 0;
1936 }
1937
1938 def = PyModule_GetDef(mod);
1939 if (def == NULL) {
Larry Hastings1df0b352015-08-24 19:53:56 -07001940 return 0;
1941 }
Brett Cannon52794db2016-09-07 17:00:43 -07001942
Larry Hastings1df0b352015-08-24 19:53:56 -07001943 state = PyModule_GetState(mod);
Larry Hastings1df0b352015-08-24 19:53:56 -07001944 if (state) {
1945 /* Already initialized; skip reload */
1946 return 0;
1947 }
Brett Cannon52794db2016-09-07 17:00:43 -07001948
Larry Hastings1df0b352015-08-24 19:53:56 -07001949 return PyModule_ExecDef(mod, def);
1950}
1951
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001952#ifdef HAVE_DYNAMIC_LOADING
1953
Brett Cannon4caa61d2014-01-09 19:03:32 -05001954/*[clinic input]
Nick Coghland5cacbb2015-05-23 22:24:10 +10001955_imp.create_dynamic
Brett Cannon4caa61d2014-01-09 19:03:32 -05001956
Nick Coghland5cacbb2015-05-23 22:24:10 +10001957 spec: object
Brett Cannon4caa61d2014-01-09 19:03:32 -05001958 file: object = NULL
1959 /
1960
Nick Coghland5cacbb2015-05-23 22:24:10 +10001961Create an extension module.
Brett Cannon4caa61d2014-01-09 19:03:32 -05001962[clinic start generated code]*/
1963
Brett Cannon4caa61d2014-01-09 19:03:32 -05001964static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001965_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
1966/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001967{
Nick Coghland5cacbb2015-05-23 22:24:10 +10001968 PyObject *mod, *name, *path;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001969 FILE *fp;
1970
Nick Coghland5cacbb2015-05-23 22:24:10 +10001971 name = PyObject_GetAttrString(spec, "name");
1972 if (name == NULL) {
1973 return NULL;
1974 }
1975
1976 path = PyObject_GetAttrString(spec, "origin");
1977 if (path == NULL) {
1978 Py_DECREF(name);
1979 return NULL;
1980 }
1981
1982 mod = _PyImport_FindExtensionObject(name, path);
1983 if (mod != NULL) {
1984 Py_DECREF(name);
1985 Py_DECREF(path);
1986 Py_INCREF(mod);
1987 return mod;
1988 }
1989
Brett Cannon4caa61d2014-01-09 19:03:32 -05001990 if (file != NULL) {
1991 fp = _Py_fopen_obj(path, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001992 if (fp == NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +10001993 Py_DECREF(name);
Brett Cannon4caa61d2014-01-09 19:03:32 -05001994 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001996 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001998 else
1999 fp = NULL;
Nick Coghland5cacbb2015-05-23 22:24:10 +10002000
2001 mod = _PyImport_LoadDynamicModuleWithSpec(spec, fp);
2002
2003 Py_DECREF(name);
Brett Cannon4caa61d2014-01-09 19:03:32 -05002004 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 if (fp)
2006 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002007 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002008}
2009
Nick Coghland5cacbb2015-05-23 22:24:10 +10002010/*[clinic input]
2011_imp.exec_dynamic -> int
2012
2013 mod: object
2014 /
2015
2016Initialize an extension module.
2017[clinic start generated code]*/
2018
2019static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002020_imp_exec_dynamic_impl(PyObject *module, PyObject *mod)
2021/*[clinic end generated code: output=f5720ac7b465877d input=9fdbfcb250280d3a]*/
Nick Coghland5cacbb2015-05-23 22:24:10 +10002022{
Larry Hastings1df0b352015-08-24 19:53:56 -07002023 return exec_builtin_or_dynamic(mod);
Nick Coghland5cacbb2015-05-23 22:24:10 +10002024}
2025
2026
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002027#endif /* HAVE_DYNAMIC_LOADING */
2028
Larry Hastings7726ac92014-01-31 22:03:12 -08002029/*[clinic input]
Larry Hastings1df0b352015-08-24 19:53:56 -07002030_imp.exec_builtin -> int
2031
2032 mod: object
2033 /
2034
2035Initialize a built-in module.
2036[clinic start generated code]*/
2037
2038static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002039_imp_exec_builtin_impl(PyObject *module, PyObject *mod)
2040/*[clinic end generated code: output=0262447b240c038e input=7beed5a2f12a60ca]*/
Larry Hastings1df0b352015-08-24 19:53:56 -07002041{
2042 return exec_builtin_or_dynamic(mod);
2043}
2044
Barry Warsaw28a691b2010-04-17 00:19:56 +00002045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002046PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04002047"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002048
Guido van Rossum79f25d91997-04-29 20:08:16 +00002049static PyMethodDef imp_methods[] = {
Brett Cannon4caa61d2014-01-09 19:03:32 -05002050 _IMP_EXTENSION_SUFFIXES_METHODDEF
2051 _IMP_LOCK_HELD_METHODDEF
2052 _IMP_ACQUIRE_LOCK_METHODDEF
2053 _IMP_RELEASE_LOCK_METHODDEF
2054 _IMP_GET_FROZEN_OBJECT_METHODDEF
2055 _IMP_IS_FROZEN_PACKAGE_METHODDEF
Nick Coghland5cacbb2015-05-23 22:24:10 +10002056 _IMP_CREATE_BUILTIN_METHODDEF
Brett Cannon4caa61d2014-01-09 19:03:32 -05002057 _IMP_INIT_FROZEN_METHODDEF
2058 _IMP_IS_BUILTIN_METHODDEF
2059 _IMP_IS_FROZEN_METHODDEF
Nick Coghland5cacbb2015-05-23 22:24:10 +10002060 _IMP_CREATE_DYNAMIC_METHODDEF
2061 _IMP_EXEC_DYNAMIC_METHODDEF
Larry Hastings1df0b352015-08-24 19:53:56 -07002062 _IMP_EXEC_BUILTIN_METHODDEF
Brett Cannon4caa61d2014-01-09 19:03:32 -05002063 _IMP__FIX_CO_FILENAME_METHODDEF
2064 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002065};
2066
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002067
Martin v. Löwis1a214512008-06-11 05:26:20 +00002068static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04002070 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 doc_imp,
2072 0,
2073 imp_methods,
2074 NULL,
2075 NULL,
2076 NULL,
2077 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002078};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002079
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002080PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002081PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 m = PyModule_Create(&impmodule);
2086 if (m == NULL)
2087 goto failure;
2088 d = PyModule_GetDict(m);
2089 if (d == NULL)
2090 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002093 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 Py_XDECREF(m);
2095 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002096}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002097
2098
Guido van Rossumb18618d2000-05-03 23:44:39 +00002099/* API for embedding applications that want to add their own entries
2100 to the table of built-in modules. This should normally be called
2101 *before* Py_Initialize(). When the table resize fails, -1 is
2102 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002103
2104 After a similar function by Just van Rossum. */
2105
2106int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002107PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 static struct _inittab *our_copy = NULL;
2110 struct _inittab *p;
2111 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 /* Count the number of entries in both tables */
2114 for (n = 0; newtab[n].name != NULL; n++)
2115 ;
2116 if (n == 0)
2117 return 0; /* Nothing to do */
2118 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2119 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 /* Allocate new memory for the combined table */
2122 p = our_copy;
2123 PyMem_RESIZE(p, struct _inittab, i+n+1);
2124 if (p == NULL)
2125 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 /* Copy the tables into the new memory */
2128 if (our_copy != PyImport_Inittab)
2129 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2130 PyImport_Inittab = our_copy = p;
2131 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002134}
2135
2136/* Shorthand to add a single entry given a name and a function */
2137
2138int
Brett Cannona826f322009-04-02 03:41:46 +00002139PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002144
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03002145 newtab[0].name = name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002149}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002150
2151#ifdef __cplusplus
2152}
2153#endif