blob: 584b1b41cd3c9f6b5b99742321106dbf28d4b786 [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
Guido van Rossum1ae940a1995-01-02 19:04:15 +000034/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
36void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000037_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038{
Victor Stinnerd0296212011-03-14 14:04:10 -040039 initstr = PyUnicode_InternFromString("__init__");
40 if (initstr == NULL)
41 Py_FatalError("Can't initialize import variables");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042}
43
Guido van Rossum25ce5661997-08-02 03:10:38 +000044void
Just van Rossum52e14d62002-12-30 22:08:05 +000045_PyImportHooks_Init(void)
46{
Brett Cannonfd074152012-04-14 14:10:13 -040047 PyObject *v, *path_hooks = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 int err = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +000049
Brett Cannonfd074152012-04-14 14:10:13 -040050 /* adding sys.path_hooks and sys.path_importer_cache */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 v = PyList_New(0);
52 if (v == NULL)
53 goto error;
54 err = PySys_SetObject("meta_path", v);
55 Py_DECREF(v);
56 if (err)
57 goto error;
58 v = PyDict_New();
59 if (v == NULL)
60 goto error;
61 err = PySys_SetObject("path_importer_cache", v);
62 Py_DECREF(v);
63 if (err)
64 goto error;
65 path_hooks = PyList_New(0);
66 if (path_hooks == NULL)
67 goto error;
68 err = PySys_SetObject("path_hooks", path_hooks);
69 if (err) {
Just van Rossum52e14d62002-12-30 22:08:05 +000070 error:
Brett Cannonfd074152012-04-14 14:10:13 -040071 PyErr_Print();
72 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
Nadeem Vawda8f46d652012-05-05 12:27:30 +020073 "or path_importer_cache failed");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 }
Brett Cannonfd074152012-04-14 14:10:13 -040075 Py_DECREF(path_hooks);
76}
77
78void
79_PyImportZip_Init(void)
80{
81 PyObject *path_hooks, *zimpimport;
82 int err = 0;
83
84 path_hooks = PySys_GetObject("path_hooks");
Victor Stinner1e53bba2013-07-16 22:26:05 +020085 if (path_hooks == NULL) {
86 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks");
Brett Cannonfd074152012-04-14 14:10:13 -040087 goto error;
Victor Stinner1e53bba2013-07-16 22:26:05 +020088 }
Brett Cannonfd074152012-04-14 14:10:13 -040089
90 if (Py_VerboseFlag)
91 PySys_WriteStderr("# installing zipimport hook\n");
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 zimpimport = PyImport_ImportModule("zipimport");
94 if (zimpimport == NULL) {
95 PyErr_Clear(); /* No zip import module -- okay */
96 if (Py_VerboseFlag)
97 PySys_WriteStderr("# can't import zipimport\n");
98 }
99 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200100 _Py_IDENTIFIER(zipimporter);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200101 PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
102 &PyId_zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 Py_DECREF(zimpimport);
104 if (zipimporter == NULL) {
105 PyErr_Clear(); /* No zipimporter object -- okay */
106 if (Py_VerboseFlag)
107 PySys_WriteStderr(
108 "# can't import zipimport.zipimporter\n");
109 }
110 else {
Brett Cannon8923a4d2012-04-24 22:03:46 -0400111 /* sys.path_hooks.insert(0, zipimporter) */
112 err = PyList_Insert(path_hooks, 0, zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 Py_DECREF(zipimporter);
Brett Cannonfd074152012-04-14 14:10:13 -0400114 if (err < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -0400116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (Py_VerboseFlag)
118 PySys_WriteStderr(
119 "# installed zipimport hook\n");
120 }
121 }
Brett Cannonfd074152012-04-14 14:10:13 -0400122
123 return;
124
125 error:
126 PyErr_Print();
Brett Cannonacf85cd2012-04-29 12:50:03 -0400127 Py_FatalError("initializing zipimport failed");
Just van Rossum52e14d62002-12-30 22:08:05 +0000128}
129
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000130/* Locking primitives to prevent parallel imports of the same module
131 in different threads to return with a partially loaded module.
132 These calls are serialized by the global interpreter lock. */
133
134#ifdef WITH_THREAD
135
Guido van Rossum49b56061998-10-01 20:42:43 +0000136#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000137
Guido van Rossum65d5b571998-12-21 19:32:43 +0000138static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000139static long import_lock_thread = -1;
140static int import_lock_level = 0;
141
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000142void
143_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 long me = PyThread_get_thread_ident();
146 if (me == -1)
147 return; /* Too bad */
148 if (import_lock == NULL) {
149 import_lock = PyThread_allocate_lock();
150 if (import_lock == NULL)
151 return; /* Nothing much we can do. */
152 }
153 if (import_lock_thread == me) {
154 import_lock_level++;
155 return;
156 }
157 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
158 {
159 PyThreadState *tstate = PyEval_SaveThread();
160 PyThread_acquire_lock(import_lock, 1);
161 PyEval_RestoreThread(tstate);
162 }
Antoine Pitrou202b6062012-12-18 22:18:17 +0100163 assert(import_lock_level == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 import_lock_thread = me;
165 import_lock_level = 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000166}
167
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000168int
169_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 long me = PyThread_get_thread_ident();
172 if (me == -1 || import_lock == NULL)
173 return 0; /* Too bad */
174 if (import_lock_thread != me)
175 return -1;
176 import_lock_level--;
Antoine Pitrou202b6062012-12-18 22:18:17 +0100177 assert(import_lock_level >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 if (import_lock_level == 0) {
179 import_lock_thread = -1;
180 PyThread_release_lock(import_lock);
181 }
182 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000183}
184
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000185/* This function is called from PyOS_AfterFork to ensure that newly
186 created child processes do not share locks with the parent.
187 We now acquire the import lock around fork() calls but on some platforms
188 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000189
190void
191_PyImport_ReInitLock(void)
192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (import_lock != NULL)
194 import_lock = PyThread_allocate_lock();
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000195 if (import_lock_level > 1) {
196 /* Forked as a side effect of import */
197 long me = PyThread_get_thread_ident();
Brett Cannone4710cf2012-11-15 21:39:36 -0500198 /* The following could fail if the lock is already held, but forking as
199 a side-effect of an import is a) rare, b) nuts, and c) difficult to
200 do thanks to the lock only being held when doing individual module
201 locks per import. */
202 PyThread_acquire_lock(import_lock, NOWAIT_LOCK);
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000203 import_lock_thread = me;
204 import_lock_level--;
205 } else {
206 import_lock_thread = -1;
207 import_lock_level = 0;
208 }
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000209}
210
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000211#endif
212
Tim Peters69232342001-08-30 05:16:13 +0000213static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000214imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000215{
Tim Peters69232342001-08-30 05:16:13 +0000216#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000218#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000220#endif
221}
222
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000223static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000224imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000225{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000226#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000228#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 Py_INCREF(Py_None);
230 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000231}
232
233static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000234imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000235{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000236#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 if (_PyImport_ReleaseLock() < 0) {
238 PyErr_SetString(PyExc_RuntimeError,
239 "not holding the import lock");
240 return NULL;
241 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000242#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 Py_INCREF(Py_None);
244 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000245}
246
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100247void
248_PyImport_Fini(void)
249{
250 Py_XDECREF(extensions);
251 extensions = NULL;
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100252#ifdef WITH_THREAD
253 if (import_lock != NULL) {
254 PyThread_free_lock(import_lock);
255 import_lock = NULL;
256 }
257#endif
258}
259
Guido van Rossum25ce5661997-08-02 03:10:38 +0000260/* Helper for sys */
261
262PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyInterpreterState *interp = PyThreadState_GET()->interp;
266 if (interp->modules == NULL)
267 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
268 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000269}
270
Guido van Rossum3f5da241990-12-20 15:06:42 +0000271
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000272/* List of names to clear in sys */
273static char* sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 "path", "argv", "ps1", "ps2",
275 "last_type", "last_value", "last_traceback",
276 "path_hooks", "path_importer_cache", "meta_path",
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200277 "__interactivehook__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 /* misc stuff */
279 "flags", "float_info",
280 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000281};
282
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000283static char* sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 "stdin", "__stdin__",
285 "stdout", "__stdout__",
286 "stderr", "__stderr__",
287 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000288};
289
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000290/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000291
Guido van Rossum3f5da241990-12-20 15:06:42 +0000292void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000293PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000294{
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200295 Py_ssize_t pos;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyObject *key, *value, *dict;
297 PyInterpreterState *interp = PyThreadState_GET()->interp;
298 PyObject *modules = interp->modules;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200299 PyObject *builtins = interp->builtins;
300 PyObject *weaklist = NULL;
Guido van Rossum758eec01998-01-19 21:58:26 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (modules == NULL)
303 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 /* Delete some special variables first. These are common
306 places where user values hide and people complain when their
307 destructors fail. Since the modules containing them are
308 deleted *last* of all, they would come too late in the normal
309 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000310
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200311 /* XXX Perhaps these precautions are obsolete. Who knows? */
312
Victor Stinnerbd303c12013-11-07 23:07:29 +0100313 value = PyDict_GetItemString(modules, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 if (value != NULL && PyModule_Check(value)) {
315 dict = PyModule_GetDict(value);
316 if (Py_VerboseFlag)
317 PySys_WriteStderr("# clear builtins._\n");
318 PyDict_SetItemString(dict, "_", Py_None);
319 }
320 value = PyDict_GetItemString(modules, "sys");
321 if (value != NULL && PyModule_Check(value)) {
322 char **p;
323 PyObject *v;
324 dict = PyModule_GetDict(value);
325 for (p = sys_deletes; *p != NULL; p++) {
326 if (Py_VerboseFlag)
327 PySys_WriteStderr("# clear sys.%s\n", *p);
328 PyDict_SetItemString(dict, *p, Py_None);
329 }
330 for (p = sys_files; *p != NULL; p+=2) {
331 if (Py_VerboseFlag)
332 PySys_WriteStderr("# restore sys.%s\n", *p);
333 v = PyDict_GetItemString(dict, *(p+1));
334 if (v == NULL)
335 v = Py_None;
336 PyDict_SetItemString(dict, *p, v);
337 }
338 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000339
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200340 /* We prepare a list which will receive (name, weakref) tuples of
341 modules when they are removed from sys.modules. The name is used
342 for diagnosis messages (in verbose mode), while the weakref helps
343 detect those modules which have been held alive. */
344 weaklist = PyList_New(0);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200345 if (weaklist == NULL)
346 PyErr_Clear();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200347
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200348#define STORE_MODULE_WEAKREF(name, mod) \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200349 if (weaklist != NULL) { \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200350 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
351 if (name && wr) { \
352 PyObject *tup = PyTuple_Pack(2, name, wr); \
353 PyList_Append(weaklist, tup); \
354 Py_XDECREF(tup); \
355 } \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200356 Py_XDECREF(wr); \
357 if (PyErr_Occurred()) \
358 PyErr_Clear(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000360
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200361 /* Remove all modules from sys.modules, hoping that garbage collection
362 can reclaim most of them. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 pos = 0;
364 while (PyDict_Next(modules, &pos, &key, &value)) {
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200365 if (PyModule_Check(value)) {
366 if (Py_VerboseFlag && PyUnicode_Check(key))
367 PySys_FormatStderr("# cleanup[2] removing %U\n", key, value);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200368 STORE_MODULE_WEAKREF(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 PyDict_SetItem(modules, key, Py_None);
370 }
371 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000372
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200373 /* Clear the modules dict. */
374 PyDict_Clear(modules);
375 /* Replace the interpreter's reference to builtins with an empty dict
376 (module globals still have a reference to the original builtins). */
377 builtins = interp->builtins;
378 interp->builtins = PyDict_New();
379 Py_DECREF(builtins);
Antoine Pitrou40322e62013-08-11 00:30:09 +0200380 /* Clear module dict copies stored in the interpreter state */
381 _PyState_ClearModules();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200382 /* Collect references */
383 _PyGC_CollectNoFail();
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200384 /* Dump GC stats before it's too late, since it uses the warnings
385 machinery. */
386 _PyGC_DumpShutdownStats();
387
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200388 /* Now, if there are any modules left alive, clear their globals to
389 minimize potential leaks. All C extension modules actually end
390 up here, since they are kept alive in the interpreter state. */
391 if (weaklist != NULL) {
392 Py_ssize_t i, n;
393 n = PyList_GET_SIZE(weaklist);
394 for (i = 0; i < n; i++) {
395 PyObject *tup = PyList_GET_ITEM(weaklist, i);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200396 PyObject *name = PyTuple_GET_ITEM(tup, 0);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200397 PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
398 if (mod == Py_None)
399 continue;
400 Py_INCREF(mod);
401 assert(PyModule_Check(mod));
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200402 if (Py_VerboseFlag && PyUnicode_Check(name))
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200403 PySys_FormatStderr("# cleanup[3] wiping %U\n",
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200404 name, mod);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200405 _PyModule_Clear(mod);
406 Py_DECREF(mod);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200407 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200408 Py_DECREF(weaklist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000410
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200411 /* Clear and delete the modules directory. Actual modules will
412 still be there only if imported during the execution of some
413 destructor. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 interp->modules = NULL;
415 Py_DECREF(modules);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200416
417 /* Once more */
418 _PyGC_CollectNoFail();
419
420#undef STORE_MODULE_WEAKREF
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000421}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000422
423
Barry Warsaw28a691b2010-04-17 00:19:56 +0000424/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000425
426long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000427PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000428{
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200429 long res;
Brett Cannon77b2abd2012-07-09 16:09:00 -0400430 PyInterpreterState *interp = PyThreadState_Get()->interp;
431 PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
432 "_RAW_MAGIC_NUMBER");
433 if (pyc_magic == NULL)
434 return -1;
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200435 res = PyLong_AsLong(pyc_magic);
Benjamin Peterson66f36592012-07-09 22:21:55 -0700436 Py_DECREF(pyc_magic);
437 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000438}
439
440
Brett Cannon3adc7b72012-07-09 14:22:12 -0400441extern const char * _PySys_ImplCacheTag;
442
Barry Warsaw28a691b2010-04-17 00:19:56 +0000443const char *
444PyImport_GetMagicTag(void)
445{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400446 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000447}
448
Brett Cannon98979b82012-07-02 15:13:11 -0400449
Guido van Rossum25ce5661997-08-02 03:10:38 +0000450/* Magic for extension modules (built-in as well as dynamically
451 loaded). To prevent initializing an extension module more than
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200452 once, we keep a static dictionary 'extensions' keyed by the tuple
453 (module name, module name) (for built-in modules) or by
454 (filename, module name) (for dynamically loaded modules), containing these
455 modules. A copy of the module's dictionary is stored by calling
456 _PyImport_FixupExtensionObject() immediately after the module initialization
457 function succeeds. A copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100458 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000459
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000460 Modules which do support multiple initialization set their m_size
461 field to a non-negative number (indicating the size of the
462 module-specific state). They are still recorded in the extensions
463 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000464*/
465
466int
Victor Stinner95872862011-03-07 18:20:56 +0100467_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
468 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000469{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500470 PyObject *modules, *dict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 struct PyModuleDef *def;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500472 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (extensions == NULL) {
474 extensions = PyDict_New();
475 if (extensions == NULL)
476 return -1;
477 }
478 if (mod == NULL || !PyModule_Check(mod)) {
479 PyErr_BadInternalCall();
480 return -1;
481 }
482 def = PyModule_GetDef(mod);
483 if (!def) {
484 PyErr_BadInternalCall();
485 return -1;
486 }
487 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100488 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 return -1;
490 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100491 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return -1;
493 }
494 if (def->m_size == -1) {
495 if (def->m_base.m_copy) {
496 /* Somebody already imported the module,
497 likely under a different name.
498 XXX this should really not happen. */
499 Py_DECREF(def->m_base.m_copy);
500 def->m_base.m_copy = NULL;
501 }
502 dict = PyModule_GetDict(mod);
503 if (dict == NULL)
504 return -1;
505 def->m_base.m_copy = PyDict_Copy(dict);
506 if (def->m_base.m_copy == NULL)
507 return -1;
508 }
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500509 key = PyTuple_Pack(2, filename, name);
510 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200511 return -1;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500512 res = PyDict_SetItem(extensions, key, (PyObject *)def);
513 Py_DECREF(key);
514 if (res < 0)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200515 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000517}
518
Victor Stinner49d3f252010-10-17 01:24:53 +0000519int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300520_PyImport_FixupBuiltin(PyObject *mod, const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000521{
522 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100523 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100524 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100525 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000526 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100527 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
528 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000529 return res;
530}
531
Guido van Rossum25ce5661997-08-02 03:10:38 +0000532PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100533_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000534{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500535 PyObject *mod, *mdict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 PyModuleDef* def;
537 if (extensions == NULL)
538 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500539 key = PyTuple_Pack(2, filename, name);
540 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200541 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500542 def = (PyModuleDef *)PyDict_GetItem(extensions, key);
543 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (def == NULL)
545 return NULL;
546 if (def->m_size == -1) {
547 /* Module does not support repeated initialization */
548 if (def->m_base.m_copy == NULL)
549 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100550 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 if (mod == NULL)
552 return NULL;
553 mdict = PyModule_GetDict(mod);
554 if (mdict == NULL)
555 return NULL;
556 if (PyDict_Update(mdict, def->m_base.m_copy))
557 return NULL;
558 }
559 else {
560 if (def->m_base.m_init == NULL)
561 return NULL;
562 mod = def->m_base.m_init();
563 if (mod == NULL)
564 return NULL;
Christian Heimes09ca7942013-07-20 14:51:53 +0200565 if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) {
566 Py_DECREF(mod);
567 return NULL;
568 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 Py_DECREF(mod);
570 }
571 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100572 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 Py_DECREF(mod);
574 return NULL;
575 }
576 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100577 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 name, filename);
579 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000580
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000581}
582
Victor Stinner49d3f252010-10-17 01:24:53 +0000583PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000584_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000585{
Victor Stinner95872862011-03-07 18:20:56 +0100586 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100587 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100588 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000589 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100590 res = _PyImport_FindExtensionObject(nameobj, nameobj);
591 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000592 return res;
593}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594
595/* Get the module object corresponding to a module name.
596 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000597 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000598 Because the former action is most common, THIS DOES NOT RETURN A
599 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600
Guido van Rossum79f25d91997-04-29 20:08:16 +0000601PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000602PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 PyObject *modules = PyImport_GetModuleDict();
605 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606
Victor Stinner27ee0892011-03-04 12:57:09 +0000607 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 PyModule_Check(m))
609 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000610 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 if (m == NULL)
612 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000613 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 Py_DECREF(m);
615 return NULL;
616 }
617 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620}
621
Victor Stinner27ee0892011-03-04 12:57:09 +0000622PyObject *
623PyImport_AddModule(const char *name)
624{
625 PyObject *nameobj, *module;
626 nameobj = PyUnicode_FromString(name);
627 if (nameobj == NULL)
628 return NULL;
629 module = PyImport_AddModuleObject(nameobj);
630 Py_DECREF(nameobj);
631 return module;
632}
633
634
Tim Peters1cd70172004-08-02 03:52:12 +0000635/* Remove name from sys.modules, if it's there. */
636static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000637remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000640 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000642 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 Py_FatalError("import: deleting existing key in"
644 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000645}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646
Christian Heimes3b06e532008-01-07 20:12:44 +0000647
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000648/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000649 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
650 * removed from sys.modules, to avoid leaving damaged module objects
651 * in sys.modules. The caller may wish to restore the original
652 * module object (if any) in this case; PyImport_ReloadModule is an
653 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000654 *
655 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
656 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000657 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300659PyImport_ExecCodeModule(const char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 return PyImport_ExecCodeModuleWithPathnames(
662 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000663}
664
665PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300666PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 return PyImport_ExecCodeModuleWithPathnames(
669 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000670}
671
672PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300673PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
674 const char *pathname,
675 const char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000676{
Victor Stinner27ee0892011-03-04 12:57:09 +0000677 PyObject *m = NULL;
678 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
679
680 nameobj = PyUnicode_FromString(name);
681 if (nameobj == NULL)
682 return NULL;
683
Victor Stinner27ee0892011-03-04 12:57:09 +0000684 if (cpathname != NULL) {
685 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
686 if (cpathobj == NULL)
687 goto error;
Brett Cannona6473f92012-07-13 13:57:03 -0400688 }
689 else
Victor Stinner27ee0892011-03-04 12:57:09 +0000690 cpathobj = NULL;
Brett Cannona6473f92012-07-13 13:57:03 -0400691
692 if (pathname != NULL) {
693 pathobj = PyUnicode_DecodeFSDefault(pathname);
694 if (pathobj == NULL)
695 goto error;
696 }
697 else if (cpathobj != NULL) {
698 PyInterpreterState *interp = PyThreadState_GET()->interp;
699 _Py_IDENTIFIER(_get_sourcefile);
700
701 if (interp == NULL) {
702 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
703 "no interpreter!");
704 }
705
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -0700706 pathobj = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannona6473f92012-07-13 13:57:03 -0400707 &PyId__get_sourcefile, cpathobj,
708 NULL);
709 if (pathobj == NULL)
710 PyErr_Clear();
711 }
712 else
713 pathobj = NULL;
714
Victor Stinner27ee0892011-03-04 12:57:09 +0000715 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
716error:
717 Py_DECREF(nameobj);
718 Py_XDECREF(pathobj);
719 Py_XDECREF(cpathobj);
720 return m;
721}
722
723PyObject*
724PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
725 PyObject *cpathname)
726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyObject *modules = PyImport_GetModuleDict();
728 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000729
Victor Stinner27ee0892011-03-04 12:57:09 +0000730 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 if (m == NULL)
732 return NULL;
733 /* If the module is being reloaded, we get the old module back
734 and re-use its dict to exec the new code. */
735 d = PyModule_GetDict(m);
736 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
737 if (PyDict_SetItemString(d, "__builtins__",
738 PyEval_GetBuiltins()) != 0)
739 goto error;
740 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 if (pathname != NULL) {
Brett Cannona6473f92012-07-13 13:57:03 -0400742 v = pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 }
Brett Cannona6473f92012-07-13 13:57:03 -0400744 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 v = ((PyCodeObject *)co)->co_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 }
Brett Cannona6473f92012-07-13 13:57:03 -0400747 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 if (PyDict_SetItemString(d, "__file__", v) != 0)
749 PyErr_Clear(); /* Not important enough to report */
750 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000753 if (cpathname != NULL)
754 v = cpathname;
755 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (PyDict_SetItemString(d, "__cached__", v) != 0)
758 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000759
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000760 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 if (v == NULL)
762 goto error;
763 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000764
Victor Stinner27ee0892011-03-04 12:57:09 +0000765 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000767 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 name);
769 return NULL;
770 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000775
776 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 remove_module(name);
778 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000779}
780
781
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000782static void
783update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 PyObject *constants, *tmp;
786 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (PyUnicode_Compare(co->co_filename, oldname))
789 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 tmp = co->co_filename;
792 co->co_filename = newname;
793 Py_INCREF(co->co_filename);
794 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 constants = co->co_consts;
797 n = PyTuple_GET_SIZE(constants);
798 for (i = 0; i < n; i++) {
799 tmp = PyTuple_GET_ITEM(constants, i);
800 if (PyCode_Check(tmp))
801 update_code_filenames((PyCodeObject *)tmp,
802 oldname, newname);
803 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000804}
805
Victor Stinner2f42ae52011-03-20 00:41:24 +0100806static void
807update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000808{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100809 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000810
Victor Stinner2f42ae52011-03-20 00:41:24 +0100811 if (PyUnicode_Compare(co->co_filename, newname) == 0)
812 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 oldname = co->co_filename;
815 Py_INCREF(oldname);
816 update_code_filenames(co, oldname, newname);
817 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000818}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000819
Brett Cannon442c9b92011-03-23 16:14:42 -0700820static PyObject *
821imp_fix_co_filename(PyObject *self, PyObject *args)
822{
823 PyObject *co;
824 PyObject *file_path;
825
826 if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
827 return NULL;
828
829 if (!PyCode_Check(co)) {
830 PyErr_SetString(PyExc_TypeError,
831 "first argument must be a code object");
832 return NULL;
833 }
834
835 if (!PyUnicode_Check(file_path)) {
836 PyErr_SetString(PyExc_TypeError,
837 "second argument must be a string");
838 return NULL;
839 }
840
841 update_compiled_module((PyCodeObject*)co, file_path);
842
843 Py_RETURN_NONE;
844}
845
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000846
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000847/* Forward */
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700848static const struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000849
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000850
851/* Helper to test for built-in module */
852
853static int
Victor Stinner95872862011-03-07 18:20:56 +0100854is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000855{
Victor Stinner95872862011-03-07 18:20:56 +0100856 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +0100858 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
859 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 if (PyImport_Inittab[i].initfunc == NULL)
861 return -1;
862 else
863 return 1;
864 }
865 }
866 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000867}
868
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000869
Just van Rossum52e14d62002-12-30 22:08:05 +0000870/* Return an importer object for a sys.path/pkg.__path__ item 'p',
871 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +0000872 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +0000873 that can handle the path item. Return None if no hook could;
874 this tells our caller it should fall back to the builtin
875 import mechanism. Cache the result in path_importer_cache.
876 Returns a borrowed reference. */
877
878static PyObject *
879get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +0000881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 PyObject *importer;
883 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +0000884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 /* These conditions are the caller's responsibility: */
886 assert(PyList_Check(path_hooks));
887 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 nhooks = PyList_Size(path_hooks);
890 if (nhooks < 0)
891 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 importer = PyDict_GetItem(path_importer_cache, p);
894 if (importer != NULL)
895 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 /* set path_importer_cache[p] to None to avoid recursion */
898 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
899 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 for (j = 0; j < nhooks; j++) {
902 PyObject *hook = PyList_GetItem(path_hooks, j);
903 if (hook == NULL)
904 return NULL;
905 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
906 if (importer != NULL)
907 break;
Just van Rossum52e14d62002-12-30 22:08:05 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
910 return NULL;
911 }
912 PyErr_Clear();
913 }
914 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -0400915 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 }
917 if (importer != NULL) {
918 int err = PyDict_SetItem(path_importer_cache, p, importer);
919 Py_DECREF(importer);
920 if (err != 0)
921 return NULL;
922 }
923 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000924}
925
Christian Heimes9cd17752007-11-18 19:35:23 +0000926PyAPI_FUNC(PyObject *)
927PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +0000929
Victor Stinner1e53bba2013-07-16 22:26:05 +0200930 path_importer_cache = PySys_GetObject("path_importer_cache");
931 path_hooks = PySys_GetObject("path_hooks");
932 if (path_importer_cache != NULL && path_hooks != NULL) {
933 importer = get_path_importer(path_importer_cache,
934 path_hooks, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 }
936 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
937 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000938}
939
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000940
Victor Stinner95872862011-03-07 18:20:56 +0100941static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000942
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000943/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000944 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000945 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000946
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000947static int
Victor Stinner95872862011-03-07 18:20:56 +0100948init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 struct _inittab *p;
Victor Stinner5eb4f592013-11-14 22:38:52 +0100951 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000952
Victor Stinner5eb4f592013-11-14 22:38:52 +0100953 mod = _PyImport_FindExtensionObject(name, name);
954 if (PyErr_Occurred())
955 return -1;
956 if (mod != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 for (p = PyImport_Inittab; p->name != NULL; p++) {
960 PyObject *mod;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100961 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +0100962 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 if (p->initfunc == NULL) {
964 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +0100965 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 name);
967 return -1;
968 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 mod = (*p->initfunc)();
970 if (mod == 0)
971 return -1;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100972 /* Remember pointer to module init function. */
973 def = PyModule_GetDef(mod);
974 def->m_base.m_init = p->initfunc;
Victor Stinner95872862011-03-07 18:20:56 +0100975 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 return -1;
977 /* FixupExtension has put the module into sys.modules,
978 so we can release our own reference. */
979 Py_DECREF(mod);
980 return 1;
981 }
982 }
983 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000984}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000985
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000986
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000987/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000988
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700989static const struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +0100990find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000991{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700992 const struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000993
Victor Stinner53dc7352011-03-20 01:50:21 +0100994 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 for (p = PyImport_FrozenModules; ; p++) {
998 if (p->name == NULL)
999 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001000 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 break;
1002 }
1003 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001004}
1005
Guido van Rossum79f25d91997-04-29 20:08:16 +00001006static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001007get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001008{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001009 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (p == NULL) {
1013 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001014 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 name);
1016 return NULL;
1017 }
1018 if (p->code == NULL) {
1019 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001020 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 name);
1022 return NULL;
1023 }
1024 size = p->size;
1025 if (size < 0)
1026 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001027 return PyMarshal_ReadObjectFromString((const char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001028}
1029
Brett Cannon8d110132009-03-15 02:20:16 +00001030static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001031is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001032{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001033 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 if (p == NULL) {
1037 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001038 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 name);
1040 return NULL;
1041 }
Brett Cannon8d110132009-03-15 02:20:16 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if (size < 0)
1046 Py_RETURN_TRUE;
1047 else
1048 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001049}
1050
1051
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001052/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001053 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001054 an exception set if the initialization failed.
1055 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001056
1057int
Victor Stinner53dc7352011-03-20 01:50:21 +01001058PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001059{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001060 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001061 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 int ispackage;
1063 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001064
Victor Stinner53dc7352011-03-20 01:50:21 +01001065 p = find_frozen(name);
1066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (p == NULL)
1068 return 0;
1069 if (p->code == NULL) {
1070 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001071 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 name);
1073 return -1;
1074 }
1075 size = p->size;
1076 ispackage = (size < 0);
1077 if (ispackage)
1078 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001079 co = PyMarshal_ReadObjectFromString((const char *)p->code, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (co == NULL)
1081 return -1;
1082 if (!PyCode_Check(co)) {
1083 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001084 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 name);
1086 goto err_return;
1087 }
1088 if (ispackage) {
Brett Cannon3e0651b2013-05-31 23:18:39 -04001089 /* Set __path__ to the empty list */
Victor Stinner53dc7352011-03-20 01:50:21 +01001090 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001092 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (m == NULL)
1094 goto err_return;
1095 d = PyModule_GetDict(m);
Brett Cannon3e0651b2013-05-31 23:18:39 -04001096 l = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 goto err_return;
1099 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 err = PyDict_SetItemString(d, "__path__", l);
1101 Py_DECREF(l);
1102 if (err != 0)
1103 goto err_return;
1104 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001105 path = PyUnicode_FromString("<frozen>");
1106 if (path == NULL)
1107 goto err_return;
1108 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1109 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 if (m == NULL)
1111 goto err_return;
1112 Py_DECREF(co);
1113 Py_DECREF(m);
1114 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001115err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 Py_DECREF(co);
1117 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001118}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001119
Victor Stinner53dc7352011-03-20 01:50:21 +01001120int
Serhiy Storchakac6792272013-10-19 21:03:34 +03001121PyImport_ImportFrozenModule(const char *name)
Victor Stinner53dc7352011-03-20 01:50:21 +01001122{
1123 PyObject *nameobj;
1124 int ret;
1125 nameobj = PyUnicode_InternFromString(name);
1126 if (nameobj == NULL)
1127 return -1;
1128 ret = PyImport_ImportFrozenModuleObject(nameobj);
1129 Py_DECREF(nameobj);
1130 return ret;
1131}
1132
Guido van Rossum74e6a111994-08-29 12:54:38 +00001133
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001134/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001135 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001136
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001138PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 PyObject *pname;
1141 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 pname = PyUnicode_FromString(name);
1144 if (pname == NULL)
1145 return NULL;
1146 result = PyImport_Import(pname);
1147 Py_DECREF(pname);
1148 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001149}
1150
Christian Heimes072c0f12008-01-03 23:01:04 +00001151/* Import a module without blocking
1152 *
1153 * At first it tries to fetch the module from sys.modules. If the module was
1154 * never loaded before it loads it with PyImport_ImportModule() unless another
1155 * thread holds the import lock. In the latter case the function raises an
1156 * ImportError instead of blocking.
1157 *
1158 * Returns the module object with incremented ref count.
1159 */
1160PyObject *
1161PyImport_ImportModuleNoBlock(const char *name)
1162{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001163 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001164}
1165
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001166
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001167/* Remove importlib frames from the traceback,
1168 * except in Verbose mode. */
1169static void
1170remove_importlib_frames(void)
1171{
1172 const char *importlib_filename = "<frozen importlib._bootstrap>";
Nick Coghlan42c07662012-07-31 21:14:18 +10001173 const char *remove_frames = "_call_with_frames_removed";
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001174 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001175 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001176 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001177 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001178
1179 /* Synopsis: if it's an ImportError, we trim all importlib chunks
Nick Coghlan42c07662012-07-31 21:14:18 +10001180 from the traceback. We always trim chunks
1181 which end with a call to "_call_with_frames_removed". */
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001182
1183 PyErr_Fetch(&exception, &value, &base_tb);
1184 if (!exception || Py_VerboseFlag)
1185 goto done;
1186 if (PyType_IsSubtype((PyTypeObject *) exception,
1187 (PyTypeObject *) PyExc_ImportError))
1188 always_trim = 1;
1189
1190 prev_link = &base_tb;
1191 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001192 while (tb != NULL) {
1193 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1194 PyObject *next = (PyObject *) traceback->tb_next;
1195 PyFrameObject *frame = traceback->tb_frame;
1196 PyCodeObject *code = frame->f_code;
1197 int now_in_importlib;
1198
1199 assert(PyTraceBack_Check(tb));
1200 now_in_importlib = (PyUnicode_CompareWithASCIIString(
1201 code->co_filename,
1202 importlib_filename) == 0);
1203 if (now_in_importlib && !in_importlib) {
1204 /* This is the link to this chunk of importlib tracebacks */
1205 outer_link = prev_link;
1206 }
1207 in_importlib = now_in_importlib;
1208
1209 if (in_importlib &&
1210 (always_trim ||
Nick Coghlan42c07662012-07-31 21:14:18 +10001211 PyUnicode_CompareWithASCIIString(code->co_name,
1212 remove_frames) == 0)) {
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001213 PyObject *tmp = *outer_link;
1214 *outer_link = next;
1215 Py_XINCREF(next);
1216 Py_DECREF(tmp);
1217 prev_link = outer_link;
1218 }
1219 else {
1220 prev_link = (PyObject **) &traceback->tb_next;
1221 }
1222 tb = next;
1223 }
1224done:
1225 PyErr_Restore(exception, value, base_tb);
1226}
1227
1228
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001229PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04001230PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1231 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001232 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001233{
Brett Cannonfd074152012-04-14 14:10:13 -04001234 _Py_IDENTIFIER(__import__);
Eric Snowb523f842013-11-22 09:05:39 -07001235 _Py_IDENTIFIER(__spec__);
1236 _Py_IDENTIFIER(_initializing);
Brett Cannonfd074152012-04-14 14:10:13 -04001237 _Py_IDENTIFIER(__package__);
1238 _Py_IDENTIFIER(__path__);
1239 _Py_IDENTIFIER(__name__);
1240 _Py_IDENTIFIER(_find_and_load);
1241 _Py_IDENTIFIER(_handle_fromlist);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001242 _Py_IDENTIFIER(_lock_unlock_module);
Brett Cannonfd074152012-04-14 14:10:13 -04001243 _Py_static_string(single_dot, ".");
1244 PyObject *abs_name = NULL;
1245 PyObject *builtins_import = NULL;
1246 PyObject *final_mod = NULL;
1247 PyObject *mod = NULL;
1248 PyObject *package = NULL;
1249 PyObject *globals = NULL;
1250 PyObject *fromlist = NULL;
1251 PyInterpreterState *interp = PyThreadState_GET()->interp;
1252
1253 /* Make sure to use default values so as to not have
1254 PyObject_CallMethodObjArgs() truncate the parameter list because of a
1255 NULL argument. */
1256 if (given_globals == NULL) {
1257 globals = PyDict_New();
1258 if (globals == NULL) {
1259 goto error;
1260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 }
Brett Cannonfd074152012-04-14 14:10:13 -04001262 else {
1263 /* Only have to care what given_globals is if it will be used
Brett Cannonecfefb72012-08-05 19:24:57 -04001264 for something. */
Brett Cannonfd074152012-04-14 14:10:13 -04001265 if (level > 0 && !PyDict_Check(given_globals)) {
1266 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1267 goto error;
1268 }
1269 globals = given_globals;
1270 Py_INCREF(globals);
1271 }
1272
1273 if (given_fromlist == NULL) {
1274 fromlist = PyList_New(0);
1275 if (fromlist == NULL) {
1276 goto error;
1277 }
1278 }
1279 else {
1280 fromlist = given_fromlist;
1281 Py_INCREF(fromlist);
1282 }
1283 if (name == NULL) {
1284 PyErr_SetString(PyExc_ValueError, "Empty module name");
1285 goto error;
1286 }
1287
1288 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1289 for added performance. */
1290
1291 if (!PyUnicode_Check(name)) {
1292 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1293 goto error;
1294 }
1295 else if (PyUnicode_READY(name) < 0) {
1296 goto error;
1297 }
1298 if (level < 0) {
1299 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1300 goto error;
1301 }
1302 else if (level > 0) {
1303 package = _PyDict_GetItemId(globals, &PyId___package__);
1304 if (package != NULL && package != Py_None) {
1305 Py_INCREF(package);
1306 if (!PyUnicode_Check(package)) {
1307 PyErr_SetString(PyExc_TypeError, "package must be a string");
1308 goto error;
1309 }
1310 }
1311 else {
Brett Cannon273323c2012-04-17 19:05:11 -04001312 package = _PyDict_GetItemId(globals, &PyId___name__);
Brett Cannonfd074152012-04-14 14:10:13 -04001313 if (package == NULL) {
Brett Cannon273323c2012-04-17 19:05:11 -04001314 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
Brett Cannonfd074152012-04-14 14:10:13 -04001315 goto error;
1316 }
1317 else if (!PyUnicode_Check(package)) {
1318 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1319 }
1320 Py_INCREF(package);
1321
1322 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Brett Cannon740fce02012-04-14 14:23:49 -04001323 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001324 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1325 if (borrowed_dot == NULL) {
1326 goto error;
1327 }
Brett Cannon740fce02012-04-14 14:23:49 -04001328 partition = PyUnicode_RPartition(package, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001329 Py_DECREF(package);
1330 if (partition == NULL) {
1331 goto error;
1332 }
1333 package = PyTuple_GET_ITEM(partition, 0);
1334 Py_INCREF(package);
1335 Py_DECREF(partition);
1336 }
1337 }
1338
1339 if (PyDict_GetItem(interp->modules, package) == NULL) {
1340 PyErr_Format(PyExc_SystemError,
1341 "Parent module %R not loaded, cannot perform relative "
1342 "import", package);
1343 goto error;
1344 }
1345 }
1346 else { /* level == 0 */
1347 if (PyUnicode_GET_LENGTH(name) == 0) {
1348 PyErr_SetString(PyExc_ValueError, "Empty module name");
1349 goto error;
1350 }
1351 package = Py_None;
1352 Py_INCREF(package);
1353 }
1354
1355 if (level > 0) {
1356 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1357 PyObject *base = NULL;
1358 int level_up = 1;
1359
1360 for (level_up = 1; level_up < level; level_up += 1) {
1361 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1362 if (last_dot == -2) {
1363 goto error;
1364 }
1365 else if (last_dot == -1) {
1366 PyErr_SetString(PyExc_ValueError,
1367 "attempted relative import beyond top-level "
1368 "package");
1369 goto error;
1370 }
1371 }
Victor Stinner22af2592013-11-13 12:11:36 +01001372
Brett Cannonfd074152012-04-14 14:10:13 -04001373 base = PyUnicode_Substring(package, 0, last_dot);
Victor Stinner22af2592013-11-13 12:11:36 +01001374 if (base == NULL)
1375 goto error;
1376
Brett Cannonfd074152012-04-14 14:10:13 -04001377 if (PyUnicode_GET_LENGTH(name) > 0) {
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001378 PyObject *borrowed_dot, *seq = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001379
1380 borrowed_dot = _PyUnicode_FromId(&single_dot);
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001381 seq = PyTuple_Pack(2, base, name);
1382 Py_DECREF(base);
Brett Cannonfd074152012-04-14 14:10:13 -04001383 if (borrowed_dot == NULL || seq == NULL) {
1384 goto error;
1385 }
1386
1387 abs_name = PyUnicode_Join(borrowed_dot, seq);
1388 Py_DECREF(seq);
1389 if (abs_name == NULL) {
1390 goto error;
1391 }
1392 }
1393 else {
1394 abs_name = base;
1395 }
1396 }
1397 else {
1398 abs_name = name;
1399 Py_INCREF(abs_name);
1400 }
1401
Brian Curtine6b299f2012-04-14 14:19:33 -05001402#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001403 _PyImport_AcquireLock();
1404#endif
1405 /* From this point forward, goto error_with_unlock! */
1406 if (PyDict_Check(globals)) {
1407 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1408 }
1409 if (builtins_import == NULL) {
1410 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1411 if (builtins_import == NULL) {
Benjamin Peterson7d110042013-04-29 09:08:14 -04001412 PyErr_SetString(PyExc_ImportError, "__import__ not found");
1413 goto error_with_unlock;
Brett Cannonfd074152012-04-14 14:10:13 -04001414 }
1415 }
1416 Py_INCREF(builtins_import);
1417
1418 mod = PyDict_GetItem(interp->modules, abs_name);
1419 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001420 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1421 "None in sys.modules", abs_name);
1422 if (msg != NULL) {
Brett Cannon82da8882013-07-04 17:48:16 -04001423 PyErr_SetImportError(msg, abs_name, NULL);
Brian Curtin09b86d12012-04-17 16:57:09 -05001424 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001425 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001426 mod = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001427 goto error_with_unlock;
1428 }
1429 else if (mod != NULL) {
Eric Snowb523f842013-11-22 09:05:39 -07001430 PyObject *value = NULL;
1431 PyObject *spec;
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001432 int initializing = 0;
1433
Brett Cannonfd074152012-04-14 14:10:13 -04001434 Py_INCREF(mod);
Antoine Pitrou03989852012-08-28 00:24:52 +02001435 /* Optimization: only call _bootstrap._lock_unlock_module() if
Eric Snowb523f842013-11-22 09:05:39 -07001436 __spec__._initializing is true.
1437 NOTE: because of this, initializing must be set *before*
Antoine Pitrou03989852012-08-28 00:24:52 +02001438 stuffing the new module in sys.modules.
1439 */
Eric Snowb523f842013-11-22 09:05:39 -07001440 spec = _PyObject_GetAttrId(mod, &PyId___spec__);
1441 if (spec != NULL) {
1442 value = _PyObject_GetAttrId(spec, &PyId__initializing);
1443 Py_DECREF(spec);
1444 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001445 if (value == NULL)
1446 PyErr_Clear();
1447 else {
1448 initializing = PyObject_IsTrue(value);
1449 Py_DECREF(value);
1450 if (initializing == -1)
1451 PyErr_Clear();
1452 }
1453 if (initializing > 0) {
1454 /* _bootstrap._lock_unlock_module() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001455 value = _PyObject_CallMethodIdObjArgs(interp->importlib,
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001456 &PyId__lock_unlock_module, abs_name,
1457 NULL);
1458 if (value == NULL)
1459 goto error;
1460 Py_DECREF(value);
1461 }
1462 else {
1463#ifdef WITH_THREAD
1464 if (_PyImport_ReleaseLock() < 0) {
1465 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1466 goto error;
1467 }
1468#endif
1469 }
Brett Cannonfd074152012-04-14 14:10:13 -04001470 }
1471 else {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001472 /* _bootstrap._find_and_load() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001473 mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001474 &PyId__find_and_load, abs_name,
1475 builtins_import, NULL);
1476 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001477 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001478 }
1479 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001480 /* From now on we don't hold the import lock anymore. */
Brett Cannonfd074152012-04-14 14:10:13 -04001481
1482 if (PyObject_Not(fromlist)) {
1483 if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1484 PyObject *front = NULL;
Brian Curtine6b299f2012-04-14 14:19:33 -05001485 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001486 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1487
1488 if (borrowed_dot == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001489 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001490 }
1491
Brian Curtine6b299f2012-04-14 14:19:33 -05001492 partition = PyUnicode_Partition(name, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001493 if (partition == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001494 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001495 }
1496
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001497 if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1498 /* No dot in module name, simple exit */
1499 Py_DECREF(partition);
1500 final_mod = mod;
1501 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001502 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001503 }
1504
Brett Cannonfd074152012-04-14 14:10:13 -04001505 front = PyTuple_GET_ITEM(partition, 0);
1506 Py_INCREF(front);
1507 Py_DECREF(partition);
1508
1509 if (level == 0) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001510 final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001511 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001512 }
1513 else {
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001514 Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1515 PyUnicode_GET_LENGTH(front);
1516 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001517 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001518 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001519 Py_DECREF(front);
1520 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001521 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001522 }
Brett Cannonfd074152012-04-14 14:10:13 -04001523
1524 final_mod = PyDict_GetItem(interp->modules, to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001525 if (final_mod == NULL) {
1526 PyErr_Format(PyExc_KeyError,
1527 "%R not in sys.modules as expected",
1528 to_return);
1529 }
1530 else {
1531 Py_INCREF(final_mod);
1532 }
Antoine Pitroub78174c2012-05-06 17:15:23 +02001533 Py_DECREF(to_return);
Brett Cannonfd074152012-04-14 14:10:13 -04001534 }
1535 }
1536 else {
1537 final_mod = mod;
1538 Py_INCREF(mod);
1539 }
1540 }
1541 else {
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001542 final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001543 &PyId__handle_fromlist, mod,
1544 fromlist, builtins_import,
1545 NULL);
1546 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001547 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001548
Brett Cannonfd074152012-04-14 14:10:13 -04001549 error_with_unlock:
Brian Curtine6b299f2012-04-14 14:19:33 -05001550#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001551 if (_PyImport_ReleaseLock() < 0) {
1552 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1553 }
1554#endif
1555 error:
1556 Py_XDECREF(abs_name);
1557 Py_XDECREF(builtins_import);
1558 Py_XDECREF(mod);
1559 Py_XDECREF(package);
1560 Py_XDECREF(globals);
1561 Py_XDECREF(fromlist);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001562 if (final_mod == NULL)
1563 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001564 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001565}
1566
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001567PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001568PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001569 PyObject *fromlist, int level)
1570{
1571 PyObject *nameobj, *mod;
1572 nameobj = PyUnicode_FromString(name);
1573 if (nameobj == NULL)
1574 return NULL;
1575 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1576 fromlist, level);
1577 Py_DECREF(nameobj);
1578 return mod;
1579}
1580
1581
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001582/* Re-import a module of any kind and return its module object, WITH
1583 INCREMENTED REFERENCE COUNT */
1584
Guido van Rossum79f25d91997-04-29 20:08:16 +00001585PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001586PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001587{
Brett Cannon62228db2012-04-29 14:38:11 -04001588 _Py_IDENTIFIER(reload);
1589 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001591 PyObject *imp = PyDict_GetItemString(modules, "imp");
1592 if (imp == NULL) {
1593 imp = PyImport_ImportModule("imp");
1594 if (imp == NULL) {
1595 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 }
Brett Cannon62228db2012-04-29 14:38:11 -04001598 else {
1599 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001601
Brett Cannon62228db2012-04-29 14:38:11 -04001602 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1603 Py_DECREF(imp);
1604 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001605}
1606
1607
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001608/* Higher-level import emulator which emulates the "import" statement
1609 more accurately -- it invokes the __import__() function from the
1610 builtins of the current globals. This means that the import is
1611 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001612 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001613 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001614 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001615 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001616
1617PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001618PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 static PyObject *silly_list = NULL;
1621 static PyObject *builtins_str = NULL;
1622 static PyObject *import_str = NULL;
1623 PyObject *globals = NULL;
1624 PyObject *import = NULL;
1625 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001626 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 /* Initialize constant string objects */
1630 if (silly_list == NULL) {
1631 import_str = PyUnicode_InternFromString("__import__");
1632 if (import_str == NULL)
1633 return NULL;
1634 builtins_str = PyUnicode_InternFromString("__builtins__");
1635 if (builtins_str == NULL)
1636 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001637 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if (silly_list == NULL)
1639 return NULL;
1640 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 /* Get the builtins from current globals */
1643 globals = PyEval_GetGlobals();
1644 if (globals != NULL) {
1645 Py_INCREF(globals);
1646 builtins = PyObject_GetItem(globals, builtins_str);
1647 if (builtins == NULL)
1648 goto err;
1649 }
1650 else {
1651 /* No globals -- use standard builtins, and fake globals */
1652 builtins = PyImport_ImportModuleLevel("builtins",
1653 NULL, NULL, NULL, 0);
1654 if (builtins == NULL)
1655 return NULL;
1656 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1657 if (globals == NULL)
1658 goto err;
1659 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 /* Get the __import__ function from the builtins */
1662 if (PyDict_Check(builtins)) {
1663 import = PyObject_GetItem(builtins, import_str);
1664 if (import == NULL)
1665 PyErr_SetObject(PyExc_KeyError, import_str);
1666 }
1667 else
1668 import = PyObject_GetAttr(builtins, import_str);
1669 if (import == NULL)
1670 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001673 Always use absolute import here.
1674 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1676 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001677 if (r == NULL)
1678 goto err;
1679 Py_DECREF(r);
1680
1681 modules = PyImport_GetModuleDict();
1682 r = PyDict_GetItem(modules, module_name);
1683 if (r != NULL)
1684 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001685
1686 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 Py_XDECREF(globals);
1688 Py_XDECREF(builtins);
1689 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001692}
1693
Barry Warsaw28a691b2010-04-17 00:19:56 +00001694static PyObject *
Brett Cannon2657df42012-05-04 15:20:40 -04001695imp_extension_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001698 const char *suffix;
1699 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 list = PyList_New(0);
1702 if (list == NULL)
1703 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001704#ifdef HAVE_DYNAMIC_LOADING
1705 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1706 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 if (item == NULL) {
1708 Py_DECREF(list);
1709 return NULL;
1710 }
1711 if (PyList_Append(list, item) < 0) {
1712 Py_DECREF(list);
1713 Py_DECREF(item);
1714 return NULL;
1715 }
1716 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001717 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 }
Brett Cannon2657df42012-05-04 15:20:40 -04001719#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001721}
1722
Guido van Rossum79f25d91997-04-29 20:08:16 +00001723static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001724imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001725{
Victor Stinner95872862011-03-07 18:20:56 +01001726 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 int ret;
1728 PyObject *m;
Victor Stinner95872862011-03-07 18:20:56 +01001729 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 return NULL;
1731 ret = init_builtin(name);
1732 if (ret < 0)
1733 return NULL;
1734 if (ret == 0) {
1735 Py_INCREF(Py_None);
1736 return Py_None;
1737 }
Victor Stinner95872862011-03-07 18:20:56 +01001738 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 Py_XINCREF(m);
1740 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001741}
1742
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001744imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001745{
Victor Stinner53dc7352011-03-20 01:50:21 +01001746 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 int ret;
1748 PyObject *m;
Victor Stinner53dc7352011-03-20 01:50:21 +01001749 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001751 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 if (ret < 0)
1753 return NULL;
1754 if (ret == 0) {
1755 Py_INCREF(Py_None);
1756 return Py_None;
1757 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001758 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 Py_XINCREF(m);
1760 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001761}
1762
Guido van Rossum79f25d91997-04-29 20:08:16 +00001763static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001764imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001765{
Victor Stinner53dc7352011-03-20 01:50:21 +01001766 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00001767
Victor Stinner53dc7352011-03-20 01:50:21 +01001768 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 return NULL;
1770 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001771}
1772
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00001774imp_is_frozen_package(PyObject *self, PyObject *args)
1775{
Victor Stinner53dc7352011-03-20 01:50:21 +01001776 PyObject *name;
Brett Cannon8d110132009-03-15 02:20:16 +00001777
Victor Stinner53dc7352011-03-20 01:50:21 +01001778 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 return NULL;
1780 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00001781}
1782
1783static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001784imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001785{
Victor Stinner95872862011-03-07 18:20:56 +01001786 PyObject *name;
1787 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 return NULL;
1789 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001790}
1791
Guido van Rossum79f25d91997-04-29 20:08:16 +00001792static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001793imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001794{
Victor Stinner53dc7352011-03-20 01:50:21 +01001795 PyObject *name;
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001796 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001797 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 return NULL;
1799 p = find_frozen(name);
1800 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001801}
1802
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001803#ifdef HAVE_DYNAMIC_LOADING
1804
Guido van Rossum79f25d91997-04-29 20:08:16 +00001805static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001806imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001807{
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001808 PyObject *name, *pathname, *fob = NULL, *mod;
1809 FILE *fp;
1810
1811 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
1812 &name, PyUnicode_FSDecoder, &pathname, &fob))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 return NULL;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001814 if (fob != NULL) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001815 fp = _Py_fopen_obj(pathname, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001816 if (fp == NULL) {
1817 Py_DECREF(pathname);
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001818 if (!PyErr_Occurred())
1819 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001821 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001823 else
1824 fp = NULL;
1825 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001826 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 if (fp)
1828 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001829 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001830}
1831
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001832#endif /* HAVE_DYNAMIC_LOADING */
1833
Barry Warsaw28a691b2010-04-17 00:19:56 +00001834
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001835/* Doc strings */
1836
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001837PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04001838"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001839
Brett Cannon2657df42012-05-04 15:20:40 -04001840PyDoc_STRVAR(doc_extension_suffixes,
1841"extension_suffixes() -> list of strings\n\
1842Returns the list of file suffixes used to identify extension modules.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001843
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001844PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00001845"lock_held() -> boolean\n\
1846Return True if the import lock is currently held, else False.\n\
1847On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00001848
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001849PyDoc_STRVAR(doc_acquire_lock,
1850"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001851Acquires the interpreter's import lock for the current thread.\n\
1852This lock should be used by import hooks to ensure thread-safety\n\
1853when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001854On platforms without threads, this function does nothing.");
1855
1856PyDoc_STRVAR(doc_release_lock,
1857"release_lock() -> None\n\
1858Release the interpreter's import lock.\n\
1859On platforms without threads, this function does nothing.");
1860
Guido van Rossum79f25d91997-04-29 20:08:16 +00001861static PyMethodDef imp_methods[] = {
Brett Cannon2657df42012-05-04 15:20:40 -04001862 {"extension_suffixes", imp_extension_suffixes, METH_NOARGS,
1863 doc_extension_suffixes},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
1865 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
1866 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
1868 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
1869 {"init_builtin", imp_init_builtin, METH_VARARGS},
1870 {"init_frozen", imp_init_frozen, METH_VARARGS},
1871 {"is_builtin", imp_is_builtin, METH_VARARGS},
1872 {"is_frozen", imp_is_frozen, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001873#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001875#endif
Brett Cannon442c9b92011-03-23 16:14:42 -07001876 {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001878};
1879
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001880
Martin v. Löwis1a214512008-06-11 05:26:20 +00001881static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04001883 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 doc_imp,
1885 0,
1886 imp_methods,
1887 NULL,
1888 NULL,
1889 NULL,
1890 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001891};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001892
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001893PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001894PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 m = PyModule_Create(&impmodule);
1899 if (m == NULL)
1900 goto failure;
1901 d = PyModule_GetDict(m);
1902 if (d == NULL)
1903 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001906 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 Py_XDECREF(m);
1908 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001909}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001910
1911
Guido van Rossumb18618d2000-05-03 23:44:39 +00001912/* API for embedding applications that want to add their own entries
1913 to the table of built-in modules. This should normally be called
1914 *before* Py_Initialize(). When the table resize fails, -1 is
1915 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001916
1917 After a similar function by Just van Rossum. */
1918
1919int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001920PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 static struct _inittab *our_copy = NULL;
1923 struct _inittab *p;
1924 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 /* Count the number of entries in both tables */
1927 for (n = 0; newtab[n].name != NULL; n++)
1928 ;
1929 if (n == 0)
1930 return 0; /* Nothing to do */
1931 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
1932 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 /* Allocate new memory for the combined table */
1935 p = our_copy;
1936 PyMem_RESIZE(p, struct _inittab, i+n+1);
1937 if (p == NULL)
1938 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 /* Copy the tables into the new memory */
1941 if (our_copy != PyImport_Inittab)
1942 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
1943 PyImport_Inittab = our_copy = p;
1944 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001947}
1948
1949/* Shorthand to add a single entry given a name and a function */
1950
1951int
Brett Cannona826f322009-04-02 03:41:46 +00001952PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 newtab[0].name = (char *)name;
1959 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001962}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001963
1964#ifdef __cplusplus
1965}
1966#endif