blob: bdc7e4cfa77fa72be0d51885bad9ba7c332f6568 [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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 Py_INCREF(Py_None);
264 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000265}
266
Brett Cannon4caa61d2014-01-09 19:03:32 -0500267/*[clinic input]
268_imp.release_lock
269
270Release the interpreter's import lock.
271
272On platforms without threads, this function does nothing.
273[clinic start generated code]*/
274
Brett Cannon4caa61d2014-01-09 19:03:32 -0500275static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300276_imp_release_lock_impl(PyObject *module)
277/*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000278{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000279#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 if (_PyImport_ReleaseLock() < 0) {
281 PyErr_SetString(PyExc_RuntimeError,
282 "not holding the import lock");
283 return NULL;
284 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000285#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_INCREF(Py_None);
287 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000288}
289
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100290void
291_PyImport_Fini(void)
292{
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200293 Py_CLEAR(extensions);
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100294#ifdef WITH_THREAD
295 if (import_lock != NULL) {
296 PyThread_free_lock(import_lock);
297 import_lock = NULL;
298 }
299#endif
300}
301
Guido van Rossum25ce5661997-08-02 03:10:38 +0000302/* Helper for sys */
303
304PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 PyInterpreterState *interp = PyThreadState_GET()->interp;
308 if (interp->modules == NULL)
309 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
310 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000311}
312
Guido van Rossum3f5da241990-12-20 15:06:42 +0000313
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000314/* List of names to clear in sys */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200315static const char * const sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 "path", "argv", "ps1", "ps2",
317 "last_type", "last_value", "last_traceback",
318 "path_hooks", "path_importer_cache", "meta_path",
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200319 "__interactivehook__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* misc stuff */
321 "flags", "float_info",
322 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000323};
324
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200325static const char * const sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 "stdin", "__stdin__",
327 "stdout", "__stdout__",
328 "stderr", "__stderr__",
329 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000330};
331
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000332/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000333
Guido van Rossum3f5da241990-12-20 15:06:42 +0000334void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336{
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200337 Py_ssize_t pos;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 PyObject *key, *value, *dict;
339 PyInterpreterState *interp = PyThreadState_GET()->interp;
340 PyObject *modules = interp->modules;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200341 PyObject *weaklist = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200342 const char * const *p;
Guido van Rossum758eec01998-01-19 21:58:26 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 if (modules == NULL)
345 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 /* Delete some special variables first. These are common
348 places where user values hide and people complain when their
349 destructors fail. Since the modules containing them are
350 deleted *last* of all, they would come too late in the normal
351 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000352
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200353 /* XXX Perhaps these precautions are obsolete. Who knows? */
354
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200355 if (Py_VerboseFlag)
356 PySys_WriteStderr("# clear builtins._\n");
357 PyDict_SetItemString(interp->builtins, "_", Py_None);
358
359 for (p = sys_deletes; *p != NULL; p++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (Py_VerboseFlag)
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200361 PySys_WriteStderr("# clear sys.%s\n", *p);
362 PyDict_SetItemString(interp->sysdict, *p, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 }
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200364 for (p = sys_files; *p != NULL; p+=2) {
365 if (Py_VerboseFlag)
366 PySys_WriteStderr("# restore sys.%s\n", *p);
367 value = PyDict_GetItemString(interp->sysdict, *(p+1));
368 if (value == NULL)
369 value = Py_None;
370 PyDict_SetItemString(interp->sysdict, *p, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000372
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200373 /* We prepare a list which will receive (name, weakref) tuples of
374 modules when they are removed from sys.modules. The name is used
375 for diagnosis messages (in verbose mode), while the weakref helps
376 detect those modules which have been held alive. */
377 weaklist = PyList_New(0);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200378 if (weaklist == NULL)
379 PyErr_Clear();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200380
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200381#define STORE_MODULE_WEAKREF(name, mod) \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200382 if (weaklist != NULL) { \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200383 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
384 if (name && wr) { \
385 PyObject *tup = PyTuple_Pack(2, name, wr); \
386 PyList_Append(weaklist, tup); \
387 Py_XDECREF(tup); \
388 } \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200389 Py_XDECREF(wr); \
390 if (PyErr_Occurred()) \
391 PyErr_Clear(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000393
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200394 /* Remove all modules from sys.modules, hoping that garbage collection
395 can reclaim most of them. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 pos = 0;
397 while (PyDict_Next(modules, &pos, &key, &value)) {
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200398 if (PyModule_Check(value)) {
399 if (Py_VerboseFlag && PyUnicode_Check(key))
Victor Stinnerab826d12014-07-07 23:06:15 +0200400 PySys_FormatStderr("# cleanup[2] removing %U\n", key);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200401 STORE_MODULE_WEAKREF(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 PyDict_SetItem(modules, key, Py_None);
403 }
404 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000405
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200406 /* Clear the modules dict. */
407 PyDict_Clear(modules);
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200408 /* Restore the original builtins dict, to ensure that any
409 user data gets cleared. */
410 dict = PyDict_Copy(interp->builtins);
411 if (dict == NULL)
412 PyErr_Clear();
413 PyDict_Clear(interp->builtins);
414 if (PyDict_Update(interp->builtins, interp->builtins_copy))
415 PyErr_Clear();
416 Py_XDECREF(dict);
Antoine Pitrou40322e62013-08-11 00:30:09 +0200417 /* Clear module dict copies stored in the interpreter state */
418 _PyState_ClearModules();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200419 /* Collect references */
420 _PyGC_CollectNoFail();
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200421 /* Dump GC stats before it's too late, since it uses the warnings
422 machinery. */
423 _PyGC_DumpShutdownStats();
424
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200425 /* Now, if there are any modules left alive, clear their globals to
426 minimize potential leaks. All C extension modules actually end
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200427 up here, since they are kept alive in the interpreter state.
428
429 The special treatment of "builtins" here is because even
430 when it's not referenced as a module, its dictionary is
431 referenced by almost every module's __builtins__. Since
432 deleting a module clears its dictionary (even if there are
433 references left to it), we need to delete the "builtins"
434 module last. Likewise, we don't delete sys until the very
435 end because it is implicitly referenced (e.g. by print). */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200436 if (weaklist != NULL) {
437 Py_ssize_t i, n;
438 n = PyList_GET_SIZE(weaklist);
439 for (i = 0; i < n; i++) {
440 PyObject *tup = PyList_GET_ITEM(weaklist, i);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200441 PyObject *name = PyTuple_GET_ITEM(tup, 0);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200442 PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
443 if (mod == Py_None)
444 continue;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200445 assert(PyModule_Check(mod));
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200446 dict = PyModule_GetDict(mod);
447 if (dict == interp->builtins || dict == interp->sysdict)
448 continue;
449 Py_INCREF(mod);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200450 if (Py_VerboseFlag && PyUnicode_Check(name))
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200451 PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200452 _PyModule_Clear(mod);
453 Py_DECREF(mod);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200454 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200455 Py_DECREF(weaklist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000457
Serhiy Storchaka013bb912014-02-10 18:21:34 +0200458 /* Next, delete sys and builtins (in that order) */
459 if (Py_VerboseFlag)
460 PySys_FormatStderr("# cleanup[3] wiping sys\n");
461 _PyModule_ClearDict(interp->sysdict);
462 if (Py_VerboseFlag)
463 PySys_FormatStderr("# cleanup[3] wiping builtins\n");
464 _PyModule_ClearDict(interp->builtins);
465
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200466 /* Clear and delete the modules directory. Actual modules will
467 still be there only if imported during the execution of some
468 destructor. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 interp->modules = NULL;
470 Py_DECREF(modules);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200471
472 /* Once more */
473 _PyGC_CollectNoFail();
474
475#undef STORE_MODULE_WEAKREF
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000476}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000477
478
Barry Warsaw28a691b2010-04-17 00:19:56 +0000479/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000480
481long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000482PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000483{
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200484 long res;
Brett Cannon77b2abd2012-07-09 16:09:00 -0400485 PyInterpreterState *interp = PyThreadState_Get()->interp;
Eric Snow32439d62015-05-02 19:15:18 -0600486 PyObject *external, *pyc_magic;
487
488 external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
489 if (external == NULL)
490 return -1;
491 pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER");
492 Py_DECREF(external);
Brett Cannon77b2abd2012-07-09 16:09:00 -0400493 if (pyc_magic == NULL)
494 return -1;
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200495 res = PyLong_AsLong(pyc_magic);
Benjamin Peterson66f36592012-07-09 22:21:55 -0700496 Py_DECREF(pyc_magic);
497 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498}
499
500
Brett Cannon3adc7b72012-07-09 14:22:12 -0400501extern const char * _PySys_ImplCacheTag;
502
Barry Warsaw28a691b2010-04-17 00:19:56 +0000503const char *
504PyImport_GetMagicTag(void)
505{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400506 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000507}
508
Brett Cannon98979b82012-07-02 15:13:11 -0400509
Guido van Rossum25ce5661997-08-02 03:10:38 +0000510/* Magic for extension modules (built-in as well as dynamically
511 loaded). To prevent initializing an extension module more than
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200512 once, we keep a static dictionary 'extensions' keyed by the tuple
513 (module name, module name) (for built-in modules) or by
514 (filename, module name) (for dynamically loaded modules), containing these
515 modules. A copy of the module's dictionary is stored by calling
516 _PyImport_FixupExtensionObject() immediately after the module initialization
517 function succeeds. A copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100518 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000519
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000520 Modules which do support multiple initialization set their m_size
521 field to a non-negative number (indicating the size of the
522 module-specific state). They are still recorded in the extensions
523 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000524*/
525
526int
Victor Stinner95872862011-03-07 18:20:56 +0100527_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
528 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000529{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500530 PyObject *modules, *dict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 struct PyModuleDef *def;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500532 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 if (extensions == NULL) {
534 extensions = PyDict_New();
535 if (extensions == NULL)
536 return -1;
537 }
538 if (mod == NULL || !PyModule_Check(mod)) {
539 PyErr_BadInternalCall();
540 return -1;
541 }
542 def = PyModule_GetDef(mod);
543 if (!def) {
544 PyErr_BadInternalCall();
545 return -1;
546 }
547 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100548 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 return -1;
550 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100551 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 return -1;
553 }
554 if (def->m_size == -1) {
555 if (def->m_base.m_copy) {
556 /* Somebody already imported the module,
557 likely under a different name.
558 XXX this should really not happen. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200559 Py_CLEAR(def->m_base.m_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 }
561 dict = PyModule_GetDict(mod);
562 if (dict == NULL)
563 return -1;
564 def->m_base.m_copy = PyDict_Copy(dict);
565 if (def->m_base.m_copy == NULL)
566 return -1;
567 }
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500568 key = PyTuple_Pack(2, filename, name);
569 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200570 return -1;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500571 res = PyDict_SetItem(extensions, key, (PyObject *)def);
572 Py_DECREF(key);
573 if (res < 0)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200574 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000576}
577
Victor Stinner49d3f252010-10-17 01:24:53 +0000578int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300579_PyImport_FixupBuiltin(PyObject *mod, const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000580{
581 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100582 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100583 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100584 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000585 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100586 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
587 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000588 return res;
589}
590
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100592_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000593{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500594 PyObject *mod, *mdict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyModuleDef* def;
596 if (extensions == NULL)
597 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500598 key = PyTuple_Pack(2, filename, name);
599 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200600 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500601 def = (PyModuleDef *)PyDict_GetItem(extensions, key);
602 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if (def == NULL)
604 return NULL;
605 if (def->m_size == -1) {
606 /* Module does not support repeated initialization */
607 if (def->m_base.m_copy == NULL)
608 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100609 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 if (mod == NULL)
611 return NULL;
612 mdict = PyModule_GetDict(mod);
613 if (mdict == NULL)
614 return NULL;
615 if (PyDict_Update(mdict, def->m_base.m_copy))
616 return NULL;
617 }
618 else {
619 if (def->m_base.m_init == NULL)
620 return NULL;
621 mod = def->m_base.m_init();
622 if (mod == NULL)
623 return NULL;
Christian Heimes09ca7942013-07-20 14:51:53 +0200624 if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) {
625 Py_DECREF(mod);
626 return NULL;
627 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 Py_DECREF(mod);
629 }
630 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100631 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 Py_DECREF(mod);
633 return NULL;
634 }
635 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100636 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 name, filename);
638 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000639
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000640}
641
Victor Stinner49d3f252010-10-17 01:24:53 +0000642PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000643_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000644{
Victor Stinner95872862011-03-07 18:20:56 +0100645 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100646 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100647 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000648 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100649 res = _PyImport_FindExtensionObject(nameobj, nameobj);
650 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000651 return res;
652}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000653
654/* Get the module object corresponding to a module name.
655 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000656 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000657 Because the former action is most common, THIS DOES NOT RETURN A
658 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000659
Guido van Rossum79f25d91997-04-29 20:08:16 +0000660PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000661PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 PyObject *modules = PyImport_GetModuleDict();
664 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000665
Serhiy Storchaka48a583b2016-02-10 10:31:20 +0200666 if ((m = PyDict_GetItemWithError(modules, name)) != NULL &&
667 PyModule_Check(m)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 return m;
Serhiy Storchaka48a583b2016-02-10 10:31:20 +0200669 }
670 if (PyErr_Occurred()) {
671 return NULL;
672 }
Victor Stinner27ee0892011-03-04 12:57:09 +0000673 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 if (m == NULL)
675 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000676 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 Py_DECREF(m);
678 return NULL;
679 }
680 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000683}
684
Victor Stinner27ee0892011-03-04 12:57:09 +0000685PyObject *
686PyImport_AddModule(const char *name)
687{
688 PyObject *nameobj, *module;
689 nameobj = PyUnicode_FromString(name);
690 if (nameobj == NULL)
691 return NULL;
692 module = PyImport_AddModuleObject(nameobj);
693 Py_DECREF(nameobj);
694 return module;
695}
696
697
Tim Peters1cd70172004-08-02 03:52:12 +0000698/* Remove name from sys.modules, if it's there. */
699static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000700remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000703 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000705 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 Py_FatalError("import: deleting existing key in"
707 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000708}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000709
Christian Heimes3b06e532008-01-07 20:12:44 +0000710
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000711/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000712 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
713 * removed from sys.modules, to avoid leaving damaged module objects
714 * in sys.modules. The caller may wish to restore the original
715 * module object (if any) in this case; PyImport_ReloadModule is an
716 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000717 *
718 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
719 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000720 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300722PyImport_ExecCodeModule(const char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 return PyImport_ExecCodeModuleWithPathnames(
725 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000726}
727
728PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300729PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 return PyImport_ExecCodeModuleWithPathnames(
732 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000733}
734
735PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300736PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
737 const char *pathname,
738 const char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000739{
Victor Stinner27ee0892011-03-04 12:57:09 +0000740 PyObject *m = NULL;
Eric Snow32439d62015-05-02 19:15:18 -0600741 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000742
743 nameobj = PyUnicode_FromString(name);
744 if (nameobj == NULL)
745 return NULL;
746
Victor Stinner27ee0892011-03-04 12:57:09 +0000747 if (cpathname != NULL) {
748 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
749 if (cpathobj == NULL)
750 goto error;
Brett Cannona6473f92012-07-13 13:57:03 -0400751 }
752 else
Victor Stinner27ee0892011-03-04 12:57:09 +0000753 cpathobj = NULL;
Brett Cannona6473f92012-07-13 13:57:03 -0400754
755 if (pathname != NULL) {
756 pathobj = PyUnicode_DecodeFSDefault(pathname);
757 if (pathobj == NULL)
758 goto error;
759 }
760 else if (cpathobj != NULL) {
761 PyInterpreterState *interp = PyThreadState_GET()->interp;
762 _Py_IDENTIFIER(_get_sourcefile);
763
764 if (interp == NULL) {
765 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
766 "no interpreter!");
767 }
768
Eric Snow32439d62015-05-02 19:15:18 -0600769 external= PyObject_GetAttrString(interp->importlib,
770 "_bootstrap_external");
771 if (external != NULL) {
772 pathobj = _PyObject_CallMethodIdObjArgs(external,
773 &PyId__get_sourcefile, cpathobj,
774 NULL);
775 Py_DECREF(external);
776 }
Brett Cannona6473f92012-07-13 13:57:03 -0400777 if (pathobj == NULL)
778 PyErr_Clear();
779 }
780 else
781 pathobj = NULL;
782
Victor Stinner27ee0892011-03-04 12:57:09 +0000783 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
784error:
785 Py_DECREF(nameobj);
786 Py_XDECREF(pathobj);
787 Py_XDECREF(cpathobj);
788 return m;
789}
790
Brett Cannon18fc4e72014-04-04 10:01:46 -0400791static PyObject *
792module_dict_for_exec(PyObject *name)
Victor Stinner27ee0892011-03-04 12:57:09 +0000793{
Brett Cannon18fc4e72014-04-04 10:01:46 -0400794 PyObject *m, *d = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000795
Victor Stinner27ee0892011-03-04 12:57:09 +0000796 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 if (m == NULL)
798 return NULL;
799 /* If the module is being reloaded, we get the old module back
800 and re-use its dict to exec the new code. */
801 d = PyModule_GetDict(m);
802 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
803 if (PyDict_SetItemString(d, "__builtins__",
Brett Cannon18fc4e72014-04-04 10:01:46 -0400804 PyEval_GetBuiltins()) != 0) {
805 remove_module(name);
806 return NULL;
807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 }
Brett Cannon18fc4e72014-04-04 10:01:46 -0400809
Eric Snow08197a42014-05-12 17:54:55 -0600810 return d; /* Return a borrowed reference. */
Brett Cannon18fc4e72014-04-04 10:01:46 -0400811}
812
813static PyObject *
814exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object)
815{
816 PyObject *modules = PyImport_GetModuleDict();
817 PyObject *v, *m;
818
819 v = PyEval_EvalCode(code_object, module_dict, module_dict);
820 if (v == NULL) {
821 remove_module(name);
822 return NULL;
823 }
824 Py_DECREF(v);
825
826 if ((m = PyDict_GetItem(modules, name)) == NULL) {
827 PyErr_Format(PyExc_ImportError,
828 "Loaded module %R not found in sys.modules",
829 name);
830 return NULL;
831 }
832
833 Py_INCREF(m);
834
835 return m;
836}
837
838PyObject*
839PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
840 PyObject *cpathname)
841{
Eric Snow32439d62015-05-02 19:15:18 -0600842 PyObject *d, *external, *res;
Eric Snow08197a42014-05-12 17:54:55 -0600843 PyInterpreterState *interp = PyThreadState_GET()->interp;
844 _Py_IDENTIFIER(_fix_up_module);
Brett Cannon18fc4e72014-04-04 10:01:46 -0400845
846 d = module_dict_for_exec(name);
847 if (d == NULL) {
848 return NULL;
849 }
850
Eric Snow08197a42014-05-12 17:54:55 -0600851 if (pathname == NULL) {
852 pathname = ((PyCodeObject *)co)->co_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 }
Eric Snow32439d62015-05-02 19:15:18 -0600854 external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
855 if (external == NULL)
856 return NULL;
857 res = _PyObject_CallMethodIdObjArgs(external,
Eric Snow08197a42014-05-12 17:54:55 -0600858 &PyId__fix_up_module,
859 d, name, pathname, cpathname, NULL);
Eric Snow32439d62015-05-02 19:15:18 -0600860 Py_DECREF(external);
Eric Snow08197a42014-05-12 17:54:55 -0600861 if (res != NULL) {
Eric Snow58cfdd82014-05-29 12:31:39 -0600862 Py_DECREF(res);
Eric Snow08197a42014-05-12 17:54:55 -0600863 res = exec_code_in_module(name, d, co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 }
Eric Snow08197a42014-05-12 17:54:55 -0600865 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000866}
867
868
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000869static void
870update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 PyObject *constants, *tmp;
873 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (PyUnicode_Compare(co->co_filename, oldname))
876 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000877
Serhiy Storchaka576f1322016-01-05 21:27:54 +0200878 Py_INCREF(newname);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300879 Py_XSETREF(co->co_filename, newname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 constants = co->co_consts;
882 n = PyTuple_GET_SIZE(constants);
883 for (i = 0; i < n; i++) {
884 tmp = PyTuple_GET_ITEM(constants, i);
885 if (PyCode_Check(tmp))
886 update_code_filenames((PyCodeObject *)tmp,
887 oldname, newname);
888 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000889}
890
Victor Stinner2f42ae52011-03-20 00:41:24 +0100891static void
892update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000893{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100894 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000895
Victor Stinner2f42ae52011-03-20 00:41:24 +0100896 if (PyUnicode_Compare(co->co_filename, newname) == 0)
897 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 oldname = co->co_filename;
900 Py_INCREF(oldname);
901 update_code_filenames(co, oldname, newname);
902 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000903}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000904
Brett Cannon4caa61d2014-01-09 19:03:32 -0500905/*[clinic input]
906_imp._fix_co_filename
907
908 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
909 Code object to change.
910
911 path: unicode
912 File path to use.
913 /
914
915Changes code.co_filename to specify the passed-in file path.
916[clinic start generated code]*/
917
Brett Cannon4caa61d2014-01-09 19:03:32 -0500918static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300919_imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code,
Larry Hastings89964c42015-04-14 18:07:59 -0400920 PyObject *path)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300921/*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/
Brett Cannon442c9b92011-03-23 16:14:42 -0700922
Brett Cannon4caa61d2014-01-09 19:03:32 -0500923{
Brett Cannona6dec2e2014-01-10 07:43:55 -0500924 update_compiled_module(code, path);
Brett Cannon442c9b92011-03-23 16:14:42 -0700925
926 Py_RETURN_NONE;
927}
928
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000929
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000930/* Forward */
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700931static const struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000932
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000933
934/* Helper to test for built-in module */
935
936static int
Victor Stinner95872862011-03-07 18:20:56 +0100937is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000938{
Victor Stinner95872862011-03-07 18:20:56 +0100939 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +0100941 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
942 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 if (PyImport_Inittab[i].initfunc == NULL)
944 return -1;
945 else
946 return 1;
947 }
948 }
949 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000950}
951
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000952
Brett Cannonfdcdd9e2016-07-08 11:00:00 -0700953/* Return a finder object for a sys.path/pkg.__path__ item 'p',
Just van Rossum52e14d62002-12-30 22:08:05 +0000954 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +0000955 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +0000956 that can handle the path item. Return None if no hook could;
Brett Cannonfdcdd9e2016-07-08 11:00:00 -0700957 this tells our caller that the path based finder could not find
958 a finder for this path item. Cache the result in
959 path_importer_cache.
Just van Rossum52e14d62002-12-30 22:08:05 +0000960 Returns a borrowed reference. */
961
962static PyObject *
963get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +0000965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 PyObject *importer;
967 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +0000968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 /* These conditions are the caller's responsibility: */
970 assert(PyList_Check(path_hooks));
971 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 nhooks = PyList_Size(path_hooks);
974 if (nhooks < 0)
975 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 importer = PyDict_GetItem(path_importer_cache, p);
978 if (importer != NULL)
979 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 /* set path_importer_cache[p] to None to avoid recursion */
982 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
983 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 for (j = 0; j < nhooks; j++) {
986 PyObject *hook = PyList_GetItem(path_hooks, j);
987 if (hook == NULL)
988 return NULL;
989 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
990 if (importer != NULL)
991 break;
Just van Rossum52e14d62002-12-30 22:08:05 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
994 return NULL;
995 }
996 PyErr_Clear();
997 }
998 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -0400999 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 }
1001 if (importer != NULL) {
1002 int err = PyDict_SetItem(path_importer_cache, p, importer);
1003 Py_DECREF(importer);
1004 if (err != 0)
1005 return NULL;
1006 }
1007 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001008}
1009
Christian Heimes9cd17752007-11-18 19:35:23 +00001010PyAPI_FUNC(PyObject *)
1011PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +00001013
Victor Stinner1e53bba2013-07-16 22:26:05 +02001014 path_importer_cache = PySys_GetObject("path_importer_cache");
1015 path_hooks = PySys_GetObject("path_hooks");
1016 if (path_importer_cache != NULL && path_hooks != NULL) {
1017 importer = get_path_importer(path_importer_cache,
1018 path_hooks, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 }
1020 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1021 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +00001022}
1023
Nick Coghland5cacbb2015-05-23 22:24:10 +10001024/*[clinic input]
1025_imp.create_builtin
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001026
Nick Coghland5cacbb2015-05-23 22:24:10 +10001027 spec: object
1028 /
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001029
Nick Coghland5cacbb2015-05-23 22:24:10 +10001030Create an extension module.
1031[clinic start generated code]*/
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001032
Nick Coghland5cacbb2015-05-23 22:24:10 +10001033static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001034_imp_create_builtin(PyObject *module, PyObject *spec)
1035/*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 struct _inittab *p;
Nick Coghland5cacbb2015-05-23 22:24:10 +10001038 PyObject *name;
1039 char *namestr;
Victor Stinner5eb4f592013-11-14 22:38:52 +01001040 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001041
Nick Coghland5cacbb2015-05-23 22:24:10 +10001042 name = PyObject_GetAttrString(spec, "name");
1043 if (name == NULL) {
1044 return NULL;
1045 }
1046
Victor Stinner5eb4f592013-11-14 22:38:52 +01001047 mod = _PyImport_FindExtensionObject(name, name);
Nick Coghland5cacbb2015-05-23 22:24:10 +10001048 if (mod || PyErr_Occurred()) {
1049 Py_DECREF(name);
1050 Py_INCREF(mod);
1051 return mod;
1052 }
1053
1054 namestr = PyUnicode_AsUTF8(name);
1055 if (namestr == NULL) {
1056 Py_DECREF(name);
1057 return NULL;
1058 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 for (p = PyImport_Inittab; p->name != NULL; p++) {
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01001061 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +01001062 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (p->initfunc == NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +10001064 /* Cannot re-init internal module ("sys" or "builtins") */
1065 mod = PyImport_AddModule(namestr);
1066 Py_DECREF(name);
1067 return mod;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 mod = (*p->initfunc)();
Nick Coghland5cacbb2015-05-23 22:24:10 +10001070 if (mod == NULL) {
1071 Py_DECREF(name);
1072 return NULL;
1073 }
1074 if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) {
1075 Py_DECREF(name);
1076 return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec);
1077 } else {
1078 /* Remember pointer to module init function. */
1079 def = PyModule_GetDef(mod);
1080 def->m_base.m_init = p->initfunc;
1081 if (_PyImport_FixupExtensionObject(mod, name, name) < 0) {
1082 Py_DECREF(name);
1083 return NULL;
1084 }
1085 Py_DECREF(name);
1086 return mod;
1087 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 }
1089 }
Nick Coghland5cacbb2015-05-23 22:24:10 +10001090 Py_DECREF(name);
1091 Py_RETURN_NONE;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001092}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001093
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001094
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001095/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001096
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001097static const struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +01001098find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001099{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001100 const struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001101
Victor Stinner53dc7352011-03-20 01:50:21 +01001102 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00001104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 for (p = PyImport_FrozenModules; ; p++) {
1106 if (p->name == NULL)
1107 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001108 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 break;
1110 }
1111 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001112}
1113
Guido van Rossum79f25d91997-04-29 20:08:16 +00001114static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001115get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001116{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001117 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 if (p == NULL) {
1121 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001122 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 name);
1124 return NULL;
1125 }
1126 if (p->code == NULL) {
1127 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001128 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 name);
1130 return NULL;
1131 }
1132 size = p->size;
1133 if (size < 0)
1134 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001135 return PyMarshal_ReadObjectFromString((const char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001136}
1137
Brett Cannon8d110132009-03-15 02:20:16 +00001138static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001139is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001140{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001141 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (p == NULL) {
1145 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001146 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 name);
1148 return NULL;
1149 }
Brett Cannon8d110132009-03-15 02:20:16 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (size < 0)
1154 Py_RETURN_TRUE;
1155 else
1156 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001157}
1158
1159
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001160/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001161 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001162 an exception set if the initialization failed.
1163 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001164
1165int
Victor Stinner53dc7352011-03-20 01:50:21 +01001166PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001167{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001168 const struct _frozen *p;
Brett Cannon18fc4e72014-04-04 10:01:46 -04001169 PyObject *co, *m, *d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 int ispackage;
1171 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001172
Victor Stinner53dc7352011-03-20 01:50:21 +01001173 p = find_frozen(name);
1174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 if (p == NULL)
1176 return 0;
1177 if (p->code == NULL) {
1178 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001179 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 name);
1181 return -1;
1182 }
1183 size = p->size;
1184 ispackage = (size < 0);
1185 if (ispackage)
1186 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001187 co = PyMarshal_ReadObjectFromString((const char *)p->code, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 if (co == NULL)
1189 return -1;
1190 if (!PyCode_Check(co)) {
1191 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001192 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 name);
1194 goto err_return;
1195 }
1196 if (ispackage) {
Brett Cannon3e0651b2013-05-31 23:18:39 -04001197 /* Set __path__ to the empty list */
Brett Cannon18fc4e72014-04-04 10:01:46 -04001198 PyObject *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001200 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 if (m == NULL)
1202 goto err_return;
1203 d = PyModule_GetDict(m);
Brett Cannon3e0651b2013-05-31 23:18:39 -04001204 l = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 goto err_return;
1207 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 err = PyDict_SetItemString(d, "__path__", l);
1209 Py_DECREF(l);
1210 if (err != 0)
1211 goto err_return;
1212 }
Brett Cannon18fc4e72014-04-04 10:01:46 -04001213 d = module_dict_for_exec(name);
1214 if (d == NULL) {
Victor Stinner53dc7352011-03-20 01:50:21 +01001215 goto err_return;
Brett Cannon18fc4e72014-04-04 10:01:46 -04001216 }
1217 m = exec_code_in_module(name, d, co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (m == NULL)
1219 goto err_return;
1220 Py_DECREF(co);
1221 Py_DECREF(m);
1222 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001223err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 Py_DECREF(co);
1225 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001226}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001227
Victor Stinner53dc7352011-03-20 01:50:21 +01001228int
Serhiy Storchakac6792272013-10-19 21:03:34 +03001229PyImport_ImportFrozenModule(const char *name)
Victor Stinner53dc7352011-03-20 01:50:21 +01001230{
1231 PyObject *nameobj;
1232 int ret;
1233 nameobj = PyUnicode_InternFromString(name);
1234 if (nameobj == NULL)
1235 return -1;
1236 ret = PyImport_ImportFrozenModuleObject(nameobj);
1237 Py_DECREF(nameobj);
1238 return ret;
1239}
1240
Guido van Rossum74e6a111994-08-29 12:54:38 +00001241
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001242/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001243 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001244
Guido van Rossum79f25d91997-04-29 20:08:16 +00001245PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001246PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 PyObject *pname;
1249 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 pname = PyUnicode_FromString(name);
1252 if (pname == NULL)
1253 return NULL;
1254 result = PyImport_Import(pname);
1255 Py_DECREF(pname);
1256 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001257}
1258
Christian Heimes072c0f12008-01-03 23:01:04 +00001259/* Import a module without blocking
1260 *
1261 * At first it tries to fetch the module from sys.modules. If the module was
1262 * never loaded before it loads it with PyImport_ImportModule() unless another
1263 * thread holds the import lock. In the latter case the function raises an
1264 * ImportError instead of blocking.
1265 *
1266 * Returns the module object with incremented ref count.
1267 */
1268PyObject *
1269PyImport_ImportModuleNoBlock(const char *name)
1270{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001271 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001272}
1273
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001274
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001275/* Remove importlib frames from the traceback,
1276 * except in Verbose mode. */
1277static void
1278remove_importlib_frames(void)
1279{
1280 const char *importlib_filename = "<frozen importlib._bootstrap>";
Eric Snow32439d62015-05-02 19:15:18 -06001281 const char *external_filename = "<frozen importlib._bootstrap_external>";
Nick Coghlan42c07662012-07-31 21:14:18 +10001282 const char *remove_frames = "_call_with_frames_removed";
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001283 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001284 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001285 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001286 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001287
1288 /* Synopsis: if it's an ImportError, we trim all importlib chunks
Nick Coghlan42c07662012-07-31 21:14:18 +10001289 from the traceback. We always trim chunks
1290 which end with a call to "_call_with_frames_removed". */
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001291
1292 PyErr_Fetch(&exception, &value, &base_tb);
1293 if (!exception || Py_VerboseFlag)
1294 goto done;
1295 if (PyType_IsSubtype((PyTypeObject *) exception,
1296 (PyTypeObject *) PyExc_ImportError))
1297 always_trim = 1;
1298
1299 prev_link = &base_tb;
1300 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001301 while (tb != NULL) {
1302 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1303 PyObject *next = (PyObject *) traceback->tb_next;
1304 PyFrameObject *frame = traceback->tb_frame;
1305 PyCodeObject *code = frame->f_code;
1306 int now_in_importlib;
1307
1308 assert(PyTraceBack_Check(tb));
1309 now_in_importlib = (PyUnicode_CompareWithASCIIString(
1310 code->co_filename,
Eric Snow32439d62015-05-02 19:15:18 -06001311 importlib_filename) == 0) ||
1312 (PyUnicode_CompareWithASCIIString(
1313 code->co_filename,
1314 external_filename) == 0);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001315 if (now_in_importlib && !in_importlib) {
1316 /* This is the link to this chunk of importlib tracebacks */
1317 outer_link = prev_link;
1318 }
1319 in_importlib = now_in_importlib;
1320
1321 if (in_importlib &&
1322 (always_trim ||
Nick Coghlan42c07662012-07-31 21:14:18 +10001323 PyUnicode_CompareWithASCIIString(code->co_name,
1324 remove_frames) == 0)) {
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001325 Py_XINCREF(next);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001326 Py_XSETREF(*outer_link, next);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001327 prev_link = outer_link;
1328 }
1329 else {
1330 prev_link = (PyObject **) &traceback->tb_next;
1331 }
1332 tb = next;
1333 }
1334done:
1335 PyErr_Restore(exception, value, base_tb);
1336}
1337
1338
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001339PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04001340PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1341 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001342 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001343{
Brett Cannonfd074152012-04-14 14:10:13 -04001344 _Py_IDENTIFIER(__import__);
Eric Snowb523f842013-11-22 09:05:39 -07001345 _Py_IDENTIFIER(__spec__);
1346 _Py_IDENTIFIER(_initializing);
Brett Cannonfd074152012-04-14 14:10:13 -04001347 _Py_IDENTIFIER(__package__);
1348 _Py_IDENTIFIER(__path__);
1349 _Py_IDENTIFIER(__name__);
1350 _Py_IDENTIFIER(_find_and_load);
1351 _Py_IDENTIFIER(_handle_fromlist);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001352 _Py_IDENTIFIER(_lock_unlock_module);
Brett Cannonfd074152012-04-14 14:10:13 -04001353 PyObject *abs_name = NULL;
1354 PyObject *builtins_import = NULL;
1355 PyObject *final_mod = NULL;
1356 PyObject *mod = NULL;
1357 PyObject *package = NULL;
Brett Cannon63b85052016-01-15 13:33:03 -08001358 PyObject *spec = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001359 PyObject *globals = NULL;
1360 PyObject *fromlist = NULL;
1361 PyInterpreterState *interp = PyThreadState_GET()->interp;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001362 int has_from;
Brett Cannonfd074152012-04-14 14:10:13 -04001363
1364 /* Make sure to use default values so as to not have
1365 PyObject_CallMethodObjArgs() truncate the parameter list because of a
1366 NULL argument. */
1367 if (given_globals == NULL) {
1368 globals = PyDict_New();
1369 if (globals == NULL) {
1370 goto error;
1371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 }
Brett Cannonfd074152012-04-14 14:10:13 -04001373 else {
1374 /* Only have to care what given_globals is if it will be used
Brett Cannonecfefb72012-08-05 19:24:57 -04001375 for something. */
Brett Cannonfd074152012-04-14 14:10:13 -04001376 if (level > 0 && !PyDict_Check(given_globals)) {
1377 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1378 goto error;
1379 }
1380 globals = given_globals;
1381 Py_INCREF(globals);
1382 }
1383
1384 if (given_fromlist == NULL) {
1385 fromlist = PyList_New(0);
1386 if (fromlist == NULL) {
1387 goto error;
1388 }
1389 }
1390 else {
1391 fromlist = given_fromlist;
1392 Py_INCREF(fromlist);
1393 }
1394 if (name == NULL) {
1395 PyErr_SetString(PyExc_ValueError, "Empty module name");
1396 goto error;
1397 }
1398
1399 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1400 for added performance. */
1401
1402 if (!PyUnicode_Check(name)) {
1403 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1404 goto error;
1405 }
1406 else if (PyUnicode_READY(name) < 0) {
1407 goto error;
1408 }
1409 if (level < 0) {
1410 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1411 goto error;
1412 }
1413 else if (level > 0) {
Brett Cannon849113a2016-01-22 15:25:50 -08001414 package = _PyDict_GetItemId(globals, &PyId___package__);
Brett Cannon63b85052016-01-15 13:33:03 -08001415 spec = _PyDict_GetItemId(globals, &PyId___spec__);
Brett Cannon849113a2016-01-22 15:25:50 -08001416
Brett Cannonfd074152012-04-14 14:10:13 -04001417 if (package != NULL && package != Py_None) {
Brett Cannon849113a2016-01-22 15:25:50 -08001418 Py_INCREF(package);
Brett Cannonfd074152012-04-14 14:10:13 -04001419 if (!PyUnicode_Check(package)) {
Brett Cannon849113a2016-01-22 15:25:50 -08001420 PyErr_SetString(PyExc_TypeError, "package must be a string");
1421 goto error;
1422 }
Brett Cannon9fa81262016-01-22 16:39:02 -08001423 else if (spec != NULL && spec != Py_None) {
Brett Cannon849113a2016-01-22 15:25:50 -08001424 int equal;
1425 PyObject *parent = PyObject_GetAttrString(spec, "parent");
1426 if (parent == NULL) {
1427 goto error;
1428 }
1429
1430 equal = PyObject_RichCompareBool(package, parent, Py_EQ);
1431 Py_DECREF(parent);
1432 if (equal < 0) {
1433 goto error;
1434 }
1435 else if (equal == 0) {
1436 if (PyErr_WarnEx(PyExc_ImportWarning,
1437 "__package__ != __spec__.parent", 1) < 0) {
1438 goto error;
1439 }
1440 }
1441 }
1442 }
Brett Cannon9fa81262016-01-22 16:39:02 -08001443 else if (spec != NULL && spec != Py_None) {
Brett Cannon849113a2016-01-22 15:25:50 -08001444 package = PyObject_GetAttrString(spec, "parent");
1445 if (package == NULL) {
1446 goto error;
1447 }
1448 else if (!PyUnicode_Check(package)) {
Brett Cannon63b85052016-01-15 13:33:03 -08001449 PyErr_SetString(PyExc_TypeError,
1450 "__spec__.parent must be a string");
Brett Cannonfd074152012-04-14 14:10:13 -04001451 goto error;
1452 }
1453 }
1454 else {
Brett Cannon849113a2016-01-22 15:25:50 -08001455 if (PyErr_WarnEx(PyExc_ImportWarning,
1456 "can't resolve package from __spec__ or __package__, "
1457 "falling back on __name__ and __path__", 1) < 0) {
1458 goto error;
Brett Cannon63b85052016-01-15 13:33:03 -08001459 }
Brett Cannon849113a2016-01-22 15:25:50 -08001460
1461 package = _PyDict_GetItemId(globals, &PyId___name__);
1462 if (package == NULL) {
1463 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
1464 goto error;
1465 }
1466
1467 Py_INCREF(package);
1468 if (!PyUnicode_Check(package)) {
1469 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1470 goto error;
1471 }
1472
1473 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Victor Stinner744c34e2016-05-20 11:36:13 +02001474 Py_ssize_t dot;
1475
1476 if (PyUnicode_READY(package) < 0) {
Brett Cannon63b85052016-01-15 13:33:03 -08001477 goto error;
1478 }
Victor Stinner744c34e2016-05-20 11:36:13 +02001479
1480 dot = PyUnicode_FindChar(package, '.',
1481 0, PyUnicode_GET_LENGTH(package), -1);
1482 if (dot == -2) {
Brett Cannon63b85052016-01-15 13:33:03 -08001483 goto error;
1484 }
Victor Stinner744c34e2016-05-20 11:36:13 +02001485
1486 if (dot >= 0) {
1487 PyObject *substr = PyUnicode_Substring(package, 0, dot);
1488 if (substr == NULL) {
1489 goto error;
1490 }
1491 Py_SETREF(package, substr);
1492 }
Brett Cannonfd074152012-04-14 14:10:13 -04001493 }
1494 }
1495
Brett Cannon9fa81262016-01-22 16:39:02 -08001496 if (PyUnicode_CompareWithASCIIString(package, "") == 0) {
1497 PyErr_SetString(PyExc_ImportError,
1498 "attempted relative import with no known parent package");
1499 goto error;
1500 }
1501 else if (PyDict_GetItem(interp->modules, package) == NULL) {
Brett Cannonfd074152012-04-14 14:10:13 -04001502 PyErr_Format(PyExc_SystemError,
1503 "Parent module %R not loaded, cannot perform relative "
1504 "import", package);
1505 goto error;
1506 }
1507 }
1508 else { /* level == 0 */
1509 if (PyUnicode_GET_LENGTH(name) == 0) {
1510 PyErr_SetString(PyExc_ValueError, "Empty module name");
1511 goto error;
1512 }
1513 package = Py_None;
1514 Py_INCREF(package);
1515 }
1516
1517 if (level > 0) {
1518 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1519 PyObject *base = NULL;
1520 int level_up = 1;
1521
1522 for (level_up = 1; level_up < level; level_up += 1) {
1523 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1524 if (last_dot == -2) {
1525 goto error;
1526 }
1527 else if (last_dot == -1) {
1528 PyErr_SetString(PyExc_ValueError,
1529 "attempted relative import beyond top-level "
1530 "package");
1531 goto error;
1532 }
1533 }
Victor Stinner22af2592013-11-13 12:11:36 +01001534
Brett Cannonfd074152012-04-14 14:10:13 -04001535 base = PyUnicode_Substring(package, 0, last_dot);
Victor Stinner22af2592013-11-13 12:11:36 +01001536 if (base == NULL)
1537 goto error;
1538
Brett Cannonfd074152012-04-14 14:10:13 -04001539 if (PyUnicode_GET_LENGTH(name) > 0) {
Victor Stinner744c34e2016-05-20 11:36:13 +02001540 abs_name = PyUnicode_FromFormat("%U.%U", base, name);
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001541 Py_DECREF(base);
Brett Cannonfd074152012-04-14 14:10:13 -04001542 if (abs_name == NULL) {
1543 goto error;
1544 }
1545 }
1546 else {
1547 abs_name = base;
1548 }
1549 }
1550 else {
1551 abs_name = name;
1552 Py_INCREF(abs_name);
1553 }
1554
Brian Curtine6b299f2012-04-14 14:19:33 -05001555#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001556 _PyImport_AcquireLock();
1557#endif
1558 /* From this point forward, goto error_with_unlock! */
1559 if (PyDict_Check(globals)) {
1560 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1561 }
1562 if (builtins_import == NULL) {
1563 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1564 if (builtins_import == NULL) {
Benjamin Peterson7d110042013-04-29 09:08:14 -04001565 PyErr_SetString(PyExc_ImportError, "__import__ not found");
1566 goto error_with_unlock;
Brett Cannonfd074152012-04-14 14:10:13 -04001567 }
1568 }
1569 Py_INCREF(builtins_import);
1570
1571 mod = PyDict_GetItem(interp->modules, abs_name);
1572 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001573 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1574 "None in sys.modules", abs_name);
1575 if (msg != NULL) {
Brett Cannon82da8882013-07-04 17:48:16 -04001576 PyErr_SetImportError(msg, abs_name, NULL);
Brian Curtin09b86d12012-04-17 16:57:09 -05001577 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001578 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001579 mod = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001580 goto error_with_unlock;
1581 }
1582 else if (mod != NULL) {
Eric Snowb523f842013-11-22 09:05:39 -07001583 PyObject *value = NULL;
1584 PyObject *spec;
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001585 int initializing = 0;
1586
Brett Cannonfd074152012-04-14 14:10:13 -04001587 Py_INCREF(mod);
Antoine Pitrou03989852012-08-28 00:24:52 +02001588 /* Optimization: only call _bootstrap._lock_unlock_module() if
Eric Snowb523f842013-11-22 09:05:39 -07001589 __spec__._initializing is true.
1590 NOTE: because of this, initializing must be set *before*
Antoine Pitrou03989852012-08-28 00:24:52 +02001591 stuffing the new module in sys.modules.
1592 */
Eric Snowb523f842013-11-22 09:05:39 -07001593 spec = _PyObject_GetAttrId(mod, &PyId___spec__);
1594 if (spec != NULL) {
1595 value = _PyObject_GetAttrId(spec, &PyId__initializing);
1596 Py_DECREF(spec);
1597 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001598 if (value == NULL)
1599 PyErr_Clear();
1600 else {
1601 initializing = PyObject_IsTrue(value);
1602 Py_DECREF(value);
1603 if (initializing == -1)
1604 PyErr_Clear();
1605 }
1606 if (initializing > 0) {
1607 /* _bootstrap._lock_unlock_module() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001608 value = _PyObject_CallMethodIdObjArgs(interp->importlib,
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001609 &PyId__lock_unlock_module, abs_name,
1610 NULL);
1611 if (value == NULL)
1612 goto error;
1613 Py_DECREF(value);
1614 }
1615 else {
1616#ifdef WITH_THREAD
1617 if (_PyImport_ReleaseLock() < 0) {
1618 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1619 goto error;
1620 }
1621#endif
1622 }
Brett Cannonfd074152012-04-14 14:10:13 -04001623 }
1624 else {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001625 /* _bootstrap._find_and_load() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001626 mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001627 &PyId__find_and_load, abs_name,
1628 builtins_import, NULL);
1629 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001630 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001631 }
1632 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001633 /* From now on we don't hold the import lock anymore. */
Brett Cannonfd074152012-04-14 14:10:13 -04001634
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001635 has_from = PyObject_IsTrue(fromlist);
1636 if (has_from < 0)
1637 goto error;
1638 if (!has_from) {
Victor Stinner744c34e2016-05-20 11:36:13 +02001639 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
1640 if (level == 0 || len > 0) {
1641 Py_ssize_t dot;
Brett Cannonfd074152012-04-14 14:10:13 -04001642
Victor Stinner744c34e2016-05-20 11:36:13 +02001643 dot = PyUnicode_FindChar(name, '.', 0, len, 1);
1644 if (dot == -2) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001645 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001646 }
1647
Victor Stinner744c34e2016-05-20 11:36:13 +02001648 if (dot == -1) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001649 /* No dot in module name, simple exit */
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001650 final_mod = mod;
1651 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001652 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001653 }
1654
Brett Cannonfd074152012-04-14 14:10:13 -04001655 if (level == 0) {
Victor Stinner744c34e2016-05-20 11:36:13 +02001656 PyObject *front = PyUnicode_Substring(name, 0, dot);
1657 if (front == NULL) {
1658 goto error;
1659 }
1660
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001661 final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001662 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001663 }
1664 else {
Victor Stinner744c34e2016-05-20 11:36:13 +02001665 Py_ssize_t cut_off = len - dot;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001666 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001667 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001668 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001669 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001670 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001671 }
Brett Cannonfd074152012-04-14 14:10:13 -04001672
1673 final_mod = PyDict_GetItem(interp->modules, to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001674 if (final_mod == NULL) {
1675 PyErr_Format(PyExc_KeyError,
1676 "%R not in sys.modules as expected",
1677 to_return);
1678 }
1679 else {
1680 Py_INCREF(final_mod);
1681 }
Antoine Pitroub78174c2012-05-06 17:15:23 +02001682 Py_DECREF(to_return);
Brett Cannonfd074152012-04-14 14:10:13 -04001683 }
1684 }
1685 else {
1686 final_mod = mod;
1687 Py_INCREF(mod);
1688 }
1689 }
1690 else {
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001691 final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001692 &PyId__handle_fromlist, mod,
1693 fromlist, builtins_import,
1694 NULL);
1695 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001696 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001697
Brett Cannonfd074152012-04-14 14:10:13 -04001698 error_with_unlock:
Brian Curtine6b299f2012-04-14 14:19:33 -05001699#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001700 if (_PyImport_ReleaseLock() < 0) {
1701 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1702 }
1703#endif
1704 error:
1705 Py_XDECREF(abs_name);
1706 Py_XDECREF(builtins_import);
1707 Py_XDECREF(mod);
1708 Py_XDECREF(package);
1709 Py_XDECREF(globals);
1710 Py_XDECREF(fromlist);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001711 if (final_mod == NULL)
1712 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001713 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001714}
1715
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001716PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001717PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001718 PyObject *fromlist, int level)
1719{
1720 PyObject *nameobj, *mod;
1721 nameobj = PyUnicode_FromString(name);
1722 if (nameobj == NULL)
1723 return NULL;
1724 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1725 fromlist, level);
1726 Py_DECREF(nameobj);
1727 return mod;
1728}
1729
1730
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001731/* Re-import a module of any kind and return its module object, WITH
1732 INCREMENTED REFERENCE COUNT */
1733
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001735PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001736{
Brett Cannon62228db2012-04-29 14:38:11 -04001737 _Py_IDENTIFIER(reload);
1738 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001740 PyObject *imp = PyDict_GetItemString(modules, "imp");
1741 if (imp == NULL) {
1742 imp = PyImport_ImportModule("imp");
1743 if (imp == NULL) {
1744 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 }
Brett Cannon62228db2012-04-29 14:38:11 -04001747 else {
1748 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001750
Brett Cannon62228db2012-04-29 14:38:11 -04001751 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1752 Py_DECREF(imp);
1753 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001754}
1755
1756
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001757/* Higher-level import emulator which emulates the "import" statement
1758 more accurately -- it invokes the __import__() function from the
1759 builtins of the current globals. This means that the import is
1760 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001761 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001762 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001763 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001764 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001765
1766PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001767PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 static PyObject *silly_list = NULL;
1770 static PyObject *builtins_str = NULL;
1771 static PyObject *import_str = NULL;
1772 PyObject *globals = NULL;
1773 PyObject *import = NULL;
1774 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001775 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 /* Initialize constant string objects */
1779 if (silly_list == NULL) {
1780 import_str = PyUnicode_InternFromString("__import__");
1781 if (import_str == NULL)
1782 return NULL;
1783 builtins_str = PyUnicode_InternFromString("__builtins__");
1784 if (builtins_str == NULL)
1785 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001786 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (silly_list == NULL)
1788 return NULL;
1789 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 /* Get the builtins from current globals */
1792 globals = PyEval_GetGlobals();
1793 if (globals != NULL) {
1794 Py_INCREF(globals);
1795 builtins = PyObject_GetItem(globals, builtins_str);
1796 if (builtins == NULL)
1797 goto err;
1798 }
1799 else {
1800 /* No globals -- use standard builtins, and fake globals */
1801 builtins = PyImport_ImportModuleLevel("builtins",
1802 NULL, NULL, NULL, 0);
1803 if (builtins == NULL)
1804 return NULL;
1805 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1806 if (globals == NULL)
1807 goto err;
1808 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 /* Get the __import__ function from the builtins */
1811 if (PyDict_Check(builtins)) {
1812 import = PyObject_GetItem(builtins, import_str);
1813 if (import == NULL)
1814 PyErr_SetObject(PyExc_KeyError, import_str);
1815 }
1816 else
1817 import = PyObject_GetAttr(builtins, import_str);
1818 if (import == NULL)
1819 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001822 Always use absolute import here.
1823 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1825 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001826 if (r == NULL)
1827 goto err;
1828 Py_DECREF(r);
1829
1830 modules = PyImport_GetModuleDict();
1831 r = PyDict_GetItem(modules, module_name);
1832 if (r != NULL)
1833 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001834
1835 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 Py_XDECREF(globals);
1837 Py_XDECREF(builtins);
1838 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001841}
1842
Brett Cannon4caa61d2014-01-09 19:03:32 -05001843/*[clinic input]
1844_imp.extension_suffixes
1845
1846Returns the list of file suffixes used to identify extension modules.
1847[clinic start generated code]*/
1848
Brett Cannon4caa61d2014-01-09 19:03:32 -05001849static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001850_imp_extension_suffixes_impl(PyObject *module)
1851/*[clinic end generated code: output=0bf346e25a8f0cd3 input=ecdeeecfcb6f839e]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001854 const char *suffix;
1855 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 list = PyList_New(0);
1858 if (list == NULL)
1859 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001860#ifdef HAVE_DYNAMIC_LOADING
1861 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1862 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (item == NULL) {
1864 Py_DECREF(list);
1865 return NULL;
1866 }
1867 if (PyList_Append(list, item) < 0) {
1868 Py_DECREF(list);
1869 Py_DECREF(item);
1870 return NULL;
1871 }
1872 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001873 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 }
Brett Cannon2657df42012-05-04 15:20:40 -04001875#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001877}
1878
Brett Cannon4caa61d2014-01-09 19:03:32 -05001879/*[clinic input]
Brett Cannon4caa61d2014-01-09 19:03:32 -05001880_imp.init_frozen
1881
1882 name: unicode
1883 /
1884
1885Initializes a frozen module.
1886[clinic start generated code]*/
1887
Brett Cannon4caa61d2014-01-09 19:03:32 -05001888static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001889_imp_init_frozen_impl(PyObject *module, PyObject *name)
1890/*[clinic end generated code: output=fc0511ed869fd69c input=13019adfc04f3fb3]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 int ret;
1893 PyObject *m;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001894
Victor Stinner53dc7352011-03-20 01:50:21 +01001895 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 if (ret < 0)
1897 return NULL;
1898 if (ret == 0) {
1899 Py_INCREF(Py_None);
1900 return Py_None;
1901 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001902 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 Py_XINCREF(m);
1904 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001905}
1906
Brett Cannon4caa61d2014-01-09 19:03:32 -05001907/*[clinic input]
1908_imp.get_frozen_object
1909
1910 name: unicode
1911 /
1912
1913Create a code object for a frozen module.
1914[clinic start generated code]*/
1915
Brett Cannon4caa61d2014-01-09 19:03:32 -05001916static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001917_imp_get_frozen_object_impl(PyObject *module, PyObject *name)
1918/*[clinic end generated code: output=2568cc5b7aa0da63 input=ed689bc05358fdbd]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001921}
1922
Brett Cannon4caa61d2014-01-09 19:03:32 -05001923/*[clinic input]
1924_imp.is_frozen_package
1925
1926 name: unicode
1927 /
1928
1929Returns True if the module name is of a frozen package.
1930[clinic start generated code]*/
1931
Brett Cannon4caa61d2014-01-09 19:03:32 -05001932static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001933_imp_is_frozen_package_impl(PyObject *module, PyObject *name)
1934/*[clinic end generated code: output=e70cbdb45784a1c9 input=81b6cdecd080fbb8]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00001937}
1938
Brett Cannon4caa61d2014-01-09 19:03:32 -05001939/*[clinic input]
1940_imp.is_builtin
1941
1942 name: unicode
1943 /
1944
1945Returns True if the module name corresponds to a built-in module.
1946[clinic start generated code]*/
1947
Guido van Rossum79f25d91997-04-29 20:08:16 +00001948static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001949_imp_is_builtin_impl(PyObject *module, PyObject *name)
1950/*[clinic end generated code: output=3bfd1162e2d3be82 input=86befdac021dd1c7]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001951{
Brett Cannon4caa61d2014-01-09 19:03:32 -05001952 return PyLong_FromLong(is_builtin(name));
1953}
1954
1955/*[clinic input]
1956_imp.is_frozen
1957
1958 name: unicode
1959 /
1960
1961Returns True if the module name corresponds to a frozen module.
1962[clinic start generated code]*/
1963
Brett Cannon4caa61d2014-01-09 19:03:32 -05001964static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001965_imp_is_frozen_impl(PyObject *module, PyObject *name)
1966/*[clinic end generated code: output=01f408f5ec0f2577 input=7301dbca1897d66b]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001967{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001968 const struct _frozen *p;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 p = find_frozen(name);
1971 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001972}
1973
Larry Hastings1df0b352015-08-24 19:53:56 -07001974/* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */
1975static int
1976exec_builtin_or_dynamic(PyObject *mod) {
1977 PyModuleDef *def;
1978 void *state;
1979
1980 if (!PyModule_Check(mod)) {
1981 return 0;
1982 }
1983
1984 def = PyModule_GetDef(mod);
1985 if (def == NULL) {
1986 if (PyErr_Occurred()) {
1987 return -1;
1988 }
1989 return 0;
1990 }
1991 state = PyModule_GetState(mod);
1992 if (PyErr_Occurred()) {
1993 return -1;
1994 }
1995 if (state) {
1996 /* Already initialized; skip reload */
1997 return 0;
1998 }
1999 return PyModule_ExecDef(mod, def);
2000}
2001
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002002#ifdef HAVE_DYNAMIC_LOADING
2003
Brett Cannon4caa61d2014-01-09 19:03:32 -05002004/*[clinic input]
Nick Coghland5cacbb2015-05-23 22:24:10 +10002005_imp.create_dynamic
Brett Cannon4caa61d2014-01-09 19:03:32 -05002006
Nick Coghland5cacbb2015-05-23 22:24:10 +10002007 spec: object
Brett Cannon4caa61d2014-01-09 19:03:32 -05002008 file: object = NULL
2009 /
2010
Nick Coghland5cacbb2015-05-23 22:24:10 +10002011Create an extension module.
Brett Cannon4caa61d2014-01-09 19:03:32 -05002012[clinic start generated code]*/
2013
Brett Cannon4caa61d2014-01-09 19:03:32 -05002014static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002015_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
2016/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05002017{
Nick Coghland5cacbb2015-05-23 22:24:10 +10002018 PyObject *mod, *name, *path;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002019 FILE *fp;
2020
Nick Coghland5cacbb2015-05-23 22:24:10 +10002021 name = PyObject_GetAttrString(spec, "name");
2022 if (name == NULL) {
2023 return NULL;
2024 }
2025
2026 path = PyObject_GetAttrString(spec, "origin");
2027 if (path == NULL) {
2028 Py_DECREF(name);
2029 return NULL;
2030 }
2031
2032 mod = _PyImport_FindExtensionObject(name, path);
2033 if (mod != NULL) {
2034 Py_DECREF(name);
2035 Py_DECREF(path);
2036 Py_INCREF(mod);
2037 return mod;
2038 }
2039
Brett Cannon4caa61d2014-01-09 19:03:32 -05002040 if (file != NULL) {
2041 fp = _Py_fopen_obj(path, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01002042 if (fp == NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +10002043 Py_DECREF(name);
Brett Cannon4caa61d2014-01-09 19:03:32 -05002044 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01002046 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002048 else
2049 fp = NULL;
Nick Coghland5cacbb2015-05-23 22:24:10 +10002050
2051 mod = _PyImport_LoadDynamicModuleWithSpec(spec, fp);
2052
2053 Py_DECREF(name);
Brett Cannon4caa61d2014-01-09 19:03:32 -05002054 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 if (fp)
2056 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002057 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002058}
2059
Nick Coghland5cacbb2015-05-23 22:24:10 +10002060/*[clinic input]
2061_imp.exec_dynamic -> int
2062
2063 mod: object
2064 /
2065
2066Initialize an extension module.
2067[clinic start generated code]*/
2068
2069static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002070_imp_exec_dynamic_impl(PyObject *module, PyObject *mod)
2071/*[clinic end generated code: output=f5720ac7b465877d input=9fdbfcb250280d3a]*/
Nick Coghland5cacbb2015-05-23 22:24:10 +10002072{
Larry Hastings1df0b352015-08-24 19:53:56 -07002073 return exec_builtin_or_dynamic(mod);
Nick Coghland5cacbb2015-05-23 22:24:10 +10002074}
2075
2076
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002077#endif /* HAVE_DYNAMIC_LOADING */
2078
Larry Hastings7726ac92014-01-31 22:03:12 -08002079/*[clinic input]
Larry Hastings1df0b352015-08-24 19:53:56 -07002080_imp.exec_builtin -> int
2081
2082 mod: object
2083 /
2084
2085Initialize a built-in module.
2086[clinic start generated code]*/
2087
2088static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002089_imp_exec_builtin_impl(PyObject *module, PyObject *mod)
2090/*[clinic end generated code: output=0262447b240c038e input=7beed5a2f12a60ca]*/
Larry Hastings1df0b352015-08-24 19:53:56 -07002091{
2092 return exec_builtin_or_dynamic(mod);
2093}
2094
2095/*[clinic input]
Larry Hastings7726ac92014-01-31 22:03:12 -08002096dump buffer
2097[clinic start generated code]*/
Brett Cannonfd4d0502014-05-30 11:21:14 -04002098/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
Larry Hastings7726ac92014-01-31 22:03:12 -08002099
Barry Warsaw28a691b2010-04-17 00:19:56 +00002100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002101PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04002102"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002103
Guido van Rossum79f25d91997-04-29 20:08:16 +00002104static PyMethodDef imp_methods[] = {
Brett Cannon4caa61d2014-01-09 19:03:32 -05002105 _IMP_EXTENSION_SUFFIXES_METHODDEF
2106 _IMP_LOCK_HELD_METHODDEF
2107 _IMP_ACQUIRE_LOCK_METHODDEF
2108 _IMP_RELEASE_LOCK_METHODDEF
2109 _IMP_GET_FROZEN_OBJECT_METHODDEF
2110 _IMP_IS_FROZEN_PACKAGE_METHODDEF
Nick Coghland5cacbb2015-05-23 22:24:10 +10002111 _IMP_CREATE_BUILTIN_METHODDEF
Brett Cannon4caa61d2014-01-09 19:03:32 -05002112 _IMP_INIT_FROZEN_METHODDEF
2113 _IMP_IS_BUILTIN_METHODDEF
2114 _IMP_IS_FROZEN_METHODDEF
Nick Coghland5cacbb2015-05-23 22:24:10 +10002115 _IMP_CREATE_DYNAMIC_METHODDEF
2116 _IMP_EXEC_DYNAMIC_METHODDEF
Larry Hastings1df0b352015-08-24 19:53:56 -07002117 _IMP_EXEC_BUILTIN_METHODDEF
Brett Cannon4caa61d2014-01-09 19:03:32 -05002118 _IMP__FIX_CO_FILENAME_METHODDEF
2119 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002120};
2121
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002122
Martin v. Löwis1a214512008-06-11 05:26:20 +00002123static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04002125 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 doc_imp,
2127 0,
2128 imp_methods,
2129 NULL,
2130 NULL,
2131 NULL,
2132 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002133};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002134
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002135PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002136PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 m = PyModule_Create(&impmodule);
2141 if (m == NULL)
2142 goto failure;
2143 d = PyModule_GetDict(m);
2144 if (d == NULL)
2145 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002148 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 Py_XDECREF(m);
2150 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002151}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002152
2153
Guido van Rossumb18618d2000-05-03 23:44:39 +00002154/* API for embedding applications that want to add their own entries
2155 to the table of built-in modules. This should normally be called
2156 *before* Py_Initialize(). When the table resize fails, -1 is
2157 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002158
2159 After a similar function by Just van Rossum. */
2160
2161int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002162PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 static struct _inittab *our_copy = NULL;
2165 struct _inittab *p;
2166 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 /* Count the number of entries in both tables */
2169 for (n = 0; newtab[n].name != NULL; n++)
2170 ;
2171 if (n == 0)
2172 return 0; /* Nothing to do */
2173 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2174 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 /* Allocate new memory for the combined table */
2177 p = our_copy;
2178 PyMem_RESIZE(p, struct _inittab, i+n+1);
2179 if (p == NULL)
2180 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 /* Copy the tables into the new memory */
2183 if (our_copy != PyImport_Inittab)
2184 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2185 PyImport_Inittab = our_copy = p;
2186 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002189}
2190
2191/* Shorthand to add a single entry given a name and a function */
2192
2193int
Brett Cannona826f322009-04-02 03:41:46 +00002194PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002199
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03002200 newtab[0].name = name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002204}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002205
2206#ifdef __cplusplus
2207}
2208#endif