blob: 0bb46d2e1e419cea39feab452704b475aeddc568 [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
Christian Heimesd3eb5a152008-02-24 00:38:49 +000022#ifdef MS_WINDOWS
23/* for stat.st_mode */
24typedef unsigned short mode_t;
Daniel Stutzbachc7937792010-09-09 21:18:04 +000025/* for _mkdir */
26#include <direct.h>
Christian Heimesd3eb5a152008-02-24 00:38:49 +000027#endif
28
Guido van Rossum21d335e1993-10-15 13:01:11 +000029
Barry Warsaw28a691b2010-04-17 00:19:56 +000030#define CACHEDIR "__pycache__"
Guido van Rossum96774c12000-05-01 20:19:08 +000031
Victor Stinner95872862011-03-07 18:20:56 +010032/* See _PyImport_FixupExtensionObject() below */
Guido van Rossum25ce5661997-08-02 03:10:38 +000033static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000034
Victor Stinnerfe7c5b52011-04-05 01:48:03 +020035/* Function from Parser/tokenizer.c */
36extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
37
Guido van Rossum771c6c81997-10-31 18:37:24 +000038/* This table is defined in config.c: */
39extern struct _inittab _PyImport_Inittab[];
40
41struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000042
Victor Stinnerd0296212011-03-14 14:04:10 -040043static PyObject *initstr = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000044
Guido van Rossum1ae940a1995-01-02 19:04:15 +000045/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
47void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000048_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049{
Victor Stinnerd0296212011-03-14 14:04:10 -040050 initstr = PyUnicode_InternFromString("__init__");
51 if (initstr == NULL)
52 Py_FatalError("Can't initialize import variables");
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");
96 if (path_hooks == NULL)
97 goto error;
98
99 if (Py_VerboseFlag)
100 PySys_WriteStderr("# installing zipimport hook\n");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 zimpimport = PyImport_ImportModule("zipimport");
103 if (zimpimport == NULL) {
104 PyErr_Clear(); /* No zip import module -- okay */
105 if (Py_VerboseFlag)
106 PySys_WriteStderr("# can't import zipimport\n");
107 }
108 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200109 _Py_IDENTIFIER(zipimporter);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200110 PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
111 &PyId_zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 Py_DECREF(zimpimport);
113 if (zipimporter == NULL) {
114 PyErr_Clear(); /* No zipimporter object -- okay */
115 if (Py_VerboseFlag)
116 PySys_WriteStderr(
117 "# can't import zipimport.zipimporter\n");
118 }
119 else {
Brett Cannon8923a4d2012-04-24 22:03:46 -0400120 /* sys.path_hooks.insert(0, zipimporter) */
121 err = PyList_Insert(path_hooks, 0, zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 Py_DECREF(zipimporter);
Brett Cannonfd074152012-04-14 14:10:13 -0400123 if (err < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -0400125 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 if (Py_VerboseFlag)
127 PySys_WriteStderr(
128 "# installed zipimport hook\n");
129 }
130 }
Brett Cannonfd074152012-04-14 14:10:13 -0400131
132 return;
133
134 error:
135 PyErr_Print();
Brett Cannonacf85cd2012-04-29 12:50:03 -0400136 Py_FatalError("initializing zipimport failed");
Just van Rossum52e14d62002-12-30 22:08:05 +0000137}
138
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000139/* Locking primitives to prevent parallel imports of the same module
140 in different threads to return with a partially loaded module.
141 These calls are serialized by the global interpreter lock. */
142
143#ifdef WITH_THREAD
144
Guido van Rossum49b56061998-10-01 20:42:43 +0000145#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000146
Guido van Rossum65d5b571998-12-21 19:32:43 +0000147static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000148static long import_lock_thread = -1;
149static int import_lock_level = 0;
150
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000151void
152_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 long me = PyThread_get_thread_ident();
155 if (me == -1)
156 return; /* Too bad */
157 if (import_lock == NULL) {
158 import_lock = PyThread_allocate_lock();
159 if (import_lock == NULL)
160 return; /* Nothing much we can do. */
161 }
162 if (import_lock_thread == me) {
163 import_lock_level++;
164 return;
165 }
166 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
167 {
168 PyThreadState *tstate = PyEval_SaveThread();
169 PyThread_acquire_lock(import_lock, 1);
170 PyEval_RestoreThread(tstate);
171 }
Antoine Pitrou202b6062012-12-18 22:18:17 +0100172 assert(import_lock_level == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 import_lock_thread = me;
174 import_lock_level = 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000175}
176
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000177int
178_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 long me = PyThread_get_thread_ident();
181 if (me == -1 || import_lock == NULL)
182 return 0; /* Too bad */
183 if (import_lock_thread != me)
184 return -1;
185 import_lock_level--;
Antoine Pitrou202b6062012-12-18 22:18:17 +0100186 assert(import_lock_level >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 if (import_lock_level == 0) {
188 import_lock_thread = -1;
189 PyThread_release_lock(import_lock);
190 }
191 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000192}
193
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000194/* This function is called from PyOS_AfterFork to ensure that newly
195 created child processes do not share locks with the parent.
196 We now acquire the import lock around fork() calls but on some platforms
197 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000198
199void
200_PyImport_ReInitLock(void)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 if (import_lock != NULL)
203 import_lock = PyThread_allocate_lock();
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000204 if (import_lock_level > 1) {
205 /* Forked as a side effect of import */
206 long me = PyThread_get_thread_ident();
Brett Cannone4710cf2012-11-15 21:39:36 -0500207 /* The following could fail if the lock is already held, but forking as
208 a side-effect of an import is a) rare, b) nuts, and c) difficult to
209 do thanks to the lock only being held when doing individual module
210 locks per import. */
211 PyThread_acquire_lock(import_lock, NOWAIT_LOCK);
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000212 import_lock_thread = me;
213 import_lock_level--;
214 } else {
215 import_lock_thread = -1;
216 import_lock_level = 0;
217 }
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000218}
219
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000220#endif
221
Tim Peters69232342001-08-30 05:16:13 +0000222static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000223imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000224{
Tim Peters69232342001-08-30 05:16:13 +0000225#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000227#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000229#endif
230}
231
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000232static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000233imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000234{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000235#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000237#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 Py_INCREF(Py_None);
239 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000240}
241
242static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000243imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000244{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000245#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (_PyImport_ReleaseLock() < 0) {
247 PyErr_SetString(PyExc_RuntimeError,
248 "not holding the import lock");
249 return NULL;
250 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 Py_INCREF(Py_None);
253 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000254}
255
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100256void
257_PyImport_Fini(void)
258{
259 Py_XDECREF(extensions);
260 extensions = NULL;
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100261#ifdef WITH_THREAD
262 if (import_lock != NULL) {
263 PyThread_free_lock(import_lock);
264 import_lock = NULL;
265 }
266#endif
267}
268
Guido van Rossum25ce5661997-08-02 03:10:38 +0000269/* Helper for sys */
270
271PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000272PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 PyInterpreterState *interp = PyThreadState_GET()->interp;
275 if (interp->modules == NULL)
276 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
277 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000278}
279
Guido van Rossum3f5da241990-12-20 15:06:42 +0000280
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000281/* List of names to clear in sys */
282static char* sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 "path", "argv", "ps1", "ps2",
284 "last_type", "last_value", "last_traceback",
285 "path_hooks", "path_importer_cache", "meta_path",
286 /* misc stuff */
287 "flags", "float_info",
288 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000289};
290
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000291static char* sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 "stdin", "__stdin__",
293 "stdout", "__stdout__",
294 "stderr", "__stderr__",
295 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000296};
297
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200298static int
299is_essential_module(PyObject *name)
300{
301 Py_ssize_t name_len;
302 char *name_str = PyUnicode_AsUTF8AndSize(name, &name_len);
303
304 if (name_str == NULL) {
305 PyErr_Clear();
306 return 0;
307 }
308 if (strcmp(name_str, "builtins") == 0)
309 return 1;
310 if (strcmp(name_str, "sys") == 0)
311 return 1;
312 /* These are all needed for stderr to still function */
313 if (strcmp(name_str, "codecs") == 0)
314 return 1;
315 if (strcmp(name_str, "_codecs") == 0)
316 return 1;
317 if (strncmp(name_str, "encodings.", 10) == 0)
318 return 1;
319 return 0;
320}
321
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000322
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000323/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000324
Guido van Rossum3f5da241990-12-20 15:06:42 +0000325void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 Py_ssize_t pos, ndone;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 PyObject *key, *value, *dict;
330 PyInterpreterState *interp = PyThreadState_GET()->interp;
331 PyObject *modules = interp->modules;
Guido van Rossum758eec01998-01-19 21:58:26 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 if (modules == NULL)
334 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* Delete some special variables first. These are common
337 places where user values hide and people complain when their
338 destructors fail. Since the modules containing them are
339 deleted *last* of all, they would come too late in the normal
340 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 value = PyDict_GetItemString(modules, "builtins");
343 if (value != NULL && PyModule_Check(value)) {
344 dict = PyModule_GetDict(value);
345 if (Py_VerboseFlag)
346 PySys_WriteStderr("# clear builtins._\n");
347 PyDict_SetItemString(dict, "_", Py_None);
348 }
349 value = PyDict_GetItemString(modules, "sys");
350 if (value != NULL && PyModule_Check(value)) {
351 char **p;
352 PyObject *v;
353 dict = PyModule_GetDict(value);
354 for (p = sys_deletes; *p != NULL; p++) {
355 if (Py_VerboseFlag)
356 PySys_WriteStderr("# clear sys.%s\n", *p);
357 PyDict_SetItemString(dict, *p, Py_None);
358 }
359 for (p = sys_files; *p != NULL; p+=2) {
360 if (Py_VerboseFlag)
361 PySys_WriteStderr("# restore sys.%s\n", *p);
362 v = PyDict_GetItemString(dict, *(p+1));
363 if (v == NULL)
364 v = Py_None;
365 PyDict_SetItemString(dict, *p, v);
366 }
367 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* First, delete __main__ */
370 value = PyDict_GetItemString(modules, "__main__");
371 if (value != NULL && PyModule_Check(value)) {
372 if (Py_VerboseFlag)
373 PySys_WriteStderr("# cleanup __main__\n");
374 _PyModule_Clear(value);
375 PyDict_SetItemString(modules, "__main__", Py_None);
376 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 /* The special treatment of "builtins" here is because even
379 when it's not referenced as a module, its dictionary is
380 referenced by almost every module's __builtins__. Since
381 deleting a module clears its dictionary (even if there are
382 references left to it), we need to delete the "builtins"
383 module last. Likewise, we don't delete sys until the very
384 end because it is implicitly referenced (e.g. by print).
Guido van Rossum758eec01998-01-19 21:58:26 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 Also note that we 'delete' modules by replacing their entry
387 in the modules dict with None, rather than really deleting
388 them; this avoids a rehash of the modules dictionary and
389 also marks them as "non existent" so they won't be
390 re-imported. */
Guido van Rossum758eec01998-01-19 21:58:26 +0000391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 /* Next, repeatedly delete modules with a reference count of
393 one (skipping builtins and sys) and delete them */
394 do {
395 ndone = 0;
396 pos = 0;
397 while (PyDict_Next(modules, &pos, &key, &value)) {
398 if (value->ob_refcnt != 1)
399 continue;
400 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200401 if (is_essential_module(key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 continue;
403 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100404 PySys_FormatStderr(
405 "# cleanup[1] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 _PyModule_Clear(value);
407 PyDict_SetItem(modules, key, Py_None);
408 ndone++;
409 }
410 }
411 } while (ndone > 0);
Guido van Rossum758eec01998-01-19 21:58:26 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 /* Next, delete all modules (still skipping builtins and sys) */
414 pos = 0;
415 while (PyDict_Next(modules, &pos, &key, &value)) {
416 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200417 if (is_essential_module(key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 continue;
419 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100420 PySys_FormatStderr("# cleanup[2] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 _PyModule_Clear(value);
422 PyDict_SetItem(modules, key, Py_None);
423 }
424 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000425
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200426 /* Collect garbage remaining after deleting the modules. Mostly
427 reference cycles created by classes. */
428 PyGC_Collect();
429
430 /* Dump GC stats before it's too late, since it uses the warnings
431 machinery. */
432 _PyGC_DumpShutdownStats();
433
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200434 /* Next, delete all remaining modules */
435 pos = 0;
436 while (PyDict_Next(modules, &pos, &key, &value)) {
437 if (PyUnicode_Check(key) && PyModule_Check(value)) {
438 if (Py_VerboseFlag)
439 PySys_FormatStderr("# cleanup[3] %U\n", key);
440 _PyModule_Clear(value);
441 PyDict_SetItem(modules, key, Py_None);
442 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 /* Finally, clear and delete the modules directory */
446 PyDict_Clear(modules);
Antoine Pitroufef34e32013-05-19 01:11:58 +0200447 _PyGC_CollectNoFail();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 interp->modules = NULL;
449 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000450}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000451
452
Barry Warsaw28a691b2010-04-17 00:19:56 +0000453/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000454
455long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000456PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000457{
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200458 long res;
Brett Cannon77b2abd2012-07-09 16:09:00 -0400459 PyInterpreterState *interp = PyThreadState_Get()->interp;
460 PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
461 "_RAW_MAGIC_NUMBER");
462 if (pyc_magic == NULL)
463 return -1;
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200464 res = PyLong_AsLong(pyc_magic);
Benjamin Peterson66f36592012-07-09 22:21:55 -0700465 Py_DECREF(pyc_magic);
466 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000467}
468
469
Brett Cannon3adc7b72012-07-09 14:22:12 -0400470extern const char * _PySys_ImplCacheTag;
471
Barry Warsaw28a691b2010-04-17 00:19:56 +0000472const char *
473PyImport_GetMagicTag(void)
474{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400475 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000476}
477
Brett Cannon98979b82012-07-02 15:13:11 -0400478
Guido van Rossum25ce5661997-08-02 03:10:38 +0000479/* Magic for extension modules (built-in as well as dynamically
480 loaded). To prevent initializing an extension module more than
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200481 once, we keep a static dictionary 'extensions' keyed by the tuple
482 (module name, module name) (for built-in modules) or by
483 (filename, module name) (for dynamically loaded modules), containing these
484 modules. A copy of the module's dictionary is stored by calling
485 _PyImport_FixupExtensionObject() immediately after the module initialization
486 function succeeds. A copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100487 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000488
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000489 Modules which do support multiple initialization set their m_size
490 field to a non-negative number (indicating the size of the
491 module-specific state). They are still recorded in the extensions
492 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000493*/
494
495int
Victor Stinner95872862011-03-07 18:20:56 +0100496_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
497 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500499 PyObject *modules, *dict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 struct PyModuleDef *def;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500501 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (extensions == NULL) {
503 extensions = PyDict_New();
504 if (extensions == NULL)
505 return -1;
506 }
507 if (mod == NULL || !PyModule_Check(mod)) {
508 PyErr_BadInternalCall();
509 return -1;
510 }
511 def = PyModule_GetDef(mod);
512 if (!def) {
513 PyErr_BadInternalCall();
514 return -1;
515 }
516 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100517 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 return -1;
519 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100520 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 return -1;
522 }
523 if (def->m_size == -1) {
524 if (def->m_base.m_copy) {
525 /* Somebody already imported the module,
526 likely under a different name.
527 XXX this should really not happen. */
528 Py_DECREF(def->m_base.m_copy);
529 def->m_base.m_copy = NULL;
530 }
531 dict = PyModule_GetDict(mod);
532 if (dict == NULL)
533 return -1;
534 def->m_base.m_copy = PyDict_Copy(dict);
535 if (def->m_base.m_copy == NULL)
536 return -1;
537 }
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500538 key = PyTuple_Pack(2, filename, name);
539 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200540 return -1;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500541 res = PyDict_SetItem(extensions, key, (PyObject *)def);
542 Py_DECREF(key);
543 if (res < 0)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200544 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000546}
547
Victor Stinner49d3f252010-10-17 01:24:53 +0000548int
549_PyImport_FixupBuiltin(PyObject *mod, char *name)
550{
551 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100552 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100553 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100554 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000555 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100556 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
557 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000558 return res;
559}
560
Guido van Rossum25ce5661997-08-02 03:10:38 +0000561PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100562_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000563{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500564 PyObject *mod, *mdict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 PyModuleDef* def;
566 if (extensions == NULL)
567 return NULL;
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 NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500571 def = (PyModuleDef *)PyDict_GetItem(extensions, key);
572 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (def == NULL)
574 return NULL;
575 if (def->m_size == -1) {
576 /* Module does not support repeated initialization */
577 if (def->m_base.m_copy == NULL)
578 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100579 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (mod == NULL)
581 return NULL;
582 mdict = PyModule_GetDict(mod);
583 if (mdict == NULL)
584 return NULL;
585 if (PyDict_Update(mdict, def->m_base.m_copy))
586 return NULL;
587 }
588 else {
589 if (def->m_base.m_init == NULL)
590 return NULL;
591 mod = def->m_base.m_init();
592 if (mod == NULL)
593 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100594 PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 Py_DECREF(mod);
596 }
597 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100598 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 Py_DECREF(mod);
600 return NULL;
601 }
602 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100603 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 name, filename);
605 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000606
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000607}
608
Victor Stinner49d3f252010-10-17 01:24:53 +0000609PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000610_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000611{
Victor Stinner95872862011-03-07 18:20:56 +0100612 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100613 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100614 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000615 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100616 res = _PyImport_FindExtensionObject(nameobj, nameobj);
617 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000618 return res;
619}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620
621/* Get the module object corresponding to a module name.
622 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000623 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000624 Because the former action is most common, THIS DOES NOT RETURN A
625 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000626
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000628PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 PyObject *modules = PyImport_GetModuleDict();
631 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632
Victor Stinner27ee0892011-03-04 12:57:09 +0000633 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 PyModule_Check(m))
635 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000636 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (m == NULL)
638 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000639 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 Py_DECREF(m);
641 return NULL;
642 }
643 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646}
647
Victor Stinner27ee0892011-03-04 12:57:09 +0000648PyObject *
649PyImport_AddModule(const char *name)
650{
651 PyObject *nameobj, *module;
652 nameobj = PyUnicode_FromString(name);
653 if (nameobj == NULL)
654 return NULL;
655 module = PyImport_AddModuleObject(nameobj);
656 Py_DECREF(nameobj);
657 return module;
658}
659
660
Tim Peters1cd70172004-08-02 03:52:12 +0000661/* Remove name from sys.modules, if it's there. */
662static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000663remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000666 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000668 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 Py_FatalError("import: deleting existing key in"
670 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000671}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000672
Christian Heimes3b06e532008-01-07 20:12:44 +0000673
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000674/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000675 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
676 * removed from sys.modules, to avoid leaving damaged module objects
677 * in sys.modules. The caller may wish to restore the original
678 * module object (if any) in this case; PyImport_ReloadModule is an
679 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000680 *
681 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
682 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000683 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000684PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000685PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 return PyImport_ExecCodeModuleWithPathnames(
688 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000689}
690
691PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000692PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 return PyImport_ExecCodeModuleWithPathnames(
695 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000696}
697
698PyObject *
699PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000701{
Victor Stinner27ee0892011-03-04 12:57:09 +0000702 PyObject *m = NULL;
703 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
704
705 nameobj = PyUnicode_FromString(name);
706 if (nameobj == NULL)
707 return NULL;
708
Victor Stinner27ee0892011-03-04 12:57:09 +0000709 if (cpathname != NULL) {
710 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
711 if (cpathobj == NULL)
712 goto error;
Brett Cannona6473f92012-07-13 13:57:03 -0400713 }
714 else
Victor Stinner27ee0892011-03-04 12:57:09 +0000715 cpathobj = NULL;
Brett Cannona6473f92012-07-13 13:57:03 -0400716
717 if (pathname != NULL) {
718 pathobj = PyUnicode_DecodeFSDefault(pathname);
719 if (pathobj == NULL)
720 goto error;
721 }
722 else if (cpathobj != NULL) {
723 PyInterpreterState *interp = PyThreadState_GET()->interp;
724 _Py_IDENTIFIER(_get_sourcefile);
725
726 if (interp == NULL) {
727 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
728 "no interpreter!");
729 }
730
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -0700731 pathobj = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannona6473f92012-07-13 13:57:03 -0400732 &PyId__get_sourcefile, cpathobj,
733 NULL);
734 if (pathobj == NULL)
735 PyErr_Clear();
736 }
737 else
738 pathobj = NULL;
739
Victor Stinner27ee0892011-03-04 12:57:09 +0000740 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
741error:
742 Py_DECREF(nameobj);
743 Py_XDECREF(pathobj);
744 Py_XDECREF(cpathobj);
745 return m;
746}
747
748PyObject*
749PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
750 PyObject *cpathname)
751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PyObject *modules = PyImport_GetModuleDict();
753 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754
Victor Stinner27ee0892011-03-04 12:57:09 +0000755 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 if (m == NULL)
757 return NULL;
758 /* If the module is being reloaded, we get the old module back
759 and re-use its dict to exec the new code. */
760 d = PyModule_GetDict(m);
761 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
762 if (PyDict_SetItemString(d, "__builtins__",
763 PyEval_GetBuiltins()) != 0)
764 goto error;
765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 if (pathname != NULL) {
Brett Cannona6473f92012-07-13 13:57:03 -0400767 v = pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 }
Brett Cannona6473f92012-07-13 13:57:03 -0400769 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 v = ((PyCodeObject *)co)->co_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 }
Brett Cannona6473f92012-07-13 13:57:03 -0400772 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 if (PyDict_SetItemString(d, "__file__", v) != 0)
774 PyErr_Clear(); /* Not important enough to report */
775 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000778 if (cpathname != NULL)
779 v = cpathname;
780 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 if (PyDict_SetItemString(d, "__cached__", v) != 0)
783 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000784
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000785 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 if (v == NULL)
787 goto error;
788 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000789
Victor Stinner27ee0892011-03-04 12:57:09 +0000790 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000792 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 name);
794 return NULL;
795 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000800
801 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 remove_module(name);
803 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000804}
805
806
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000807static void
808update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyObject *constants, *tmp;
811 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (PyUnicode_Compare(co->co_filename, oldname))
814 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 tmp = co->co_filename;
817 co->co_filename = newname;
818 Py_INCREF(co->co_filename);
819 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 constants = co->co_consts;
822 n = PyTuple_GET_SIZE(constants);
823 for (i = 0; i < n; i++) {
824 tmp = PyTuple_GET_ITEM(constants, i);
825 if (PyCode_Check(tmp))
826 update_code_filenames((PyCodeObject *)tmp,
827 oldname, newname);
828 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000829}
830
Victor Stinner2f42ae52011-03-20 00:41:24 +0100831static void
832update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000833{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100834 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000835
Victor Stinner2f42ae52011-03-20 00:41:24 +0100836 if (PyUnicode_Compare(co->co_filename, newname) == 0)
837 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 oldname = co->co_filename;
840 Py_INCREF(oldname);
841 update_code_filenames(co, oldname, newname);
842 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000843}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844
Brett Cannon442c9b92011-03-23 16:14:42 -0700845static PyObject *
846imp_fix_co_filename(PyObject *self, PyObject *args)
847{
848 PyObject *co;
849 PyObject *file_path;
850
851 if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
852 return NULL;
853
854 if (!PyCode_Check(co)) {
855 PyErr_SetString(PyExc_TypeError,
856 "first argument must be a code object");
857 return NULL;
858 }
859
860 if (!PyUnicode_Check(file_path)) {
861 PyErr_SetString(PyExc_TypeError,
862 "second argument must be a string");
863 return NULL;
864 }
865
866 update_compiled_module((PyCodeObject*)co, file_path);
867
868 Py_RETURN_NONE;
869}
870
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000871
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000872/* Forward */
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700873static const struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000874
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000875
876/* Helper to test for built-in module */
877
878static int
Victor Stinner95872862011-03-07 18:20:56 +0100879is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000880{
Victor Stinner95872862011-03-07 18:20:56 +0100881 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +0100883 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
884 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 if (PyImport_Inittab[i].initfunc == NULL)
886 return -1;
887 else
888 return 1;
889 }
890 }
891 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000892}
893
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000894
Just van Rossum52e14d62002-12-30 22:08:05 +0000895/* Return an importer object for a sys.path/pkg.__path__ item 'p',
896 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +0000897 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +0000898 that can handle the path item. Return None if no hook could;
899 this tells our caller it should fall back to the builtin
900 import mechanism. Cache the result in path_importer_cache.
901 Returns a borrowed reference. */
902
903static PyObject *
904get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +0000906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 PyObject *importer;
908 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 /* These conditions are the caller's responsibility: */
911 assert(PyList_Check(path_hooks));
912 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 nhooks = PyList_Size(path_hooks);
915 if (nhooks < 0)
916 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 importer = PyDict_GetItem(path_importer_cache, p);
919 if (importer != NULL)
920 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 /* set path_importer_cache[p] to None to avoid recursion */
923 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
924 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 for (j = 0; j < nhooks; j++) {
927 PyObject *hook = PyList_GetItem(path_hooks, j);
928 if (hook == NULL)
929 return NULL;
930 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
931 if (importer != NULL)
932 break;
Just van Rossum52e14d62002-12-30 22:08:05 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
935 return NULL;
936 }
937 PyErr_Clear();
938 }
939 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -0400940 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 }
942 if (importer != NULL) {
943 int err = PyDict_SetItem(path_importer_cache, p, importer);
944 Py_DECREF(importer);
945 if (err != 0)
946 return NULL;
947 }
948 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000949}
950
Christian Heimes9cd17752007-11-18 19:35:23 +0000951PyAPI_FUNC(PyObject *)
952PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
956 if ((path_hooks = PySys_GetObject("path_hooks"))) {
957 importer = get_path_importer(path_importer_cache,
958 path_hooks, path);
959 }
960 }
961 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
962 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000963}
964
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000965
Victor Stinner95872862011-03-07 18:20:56 +0100966static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000967
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000968/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000969 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000970 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000971
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000972static int
Victor Stinner95872862011-03-07 18:20:56 +0100973init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000976
Victor Stinner95872862011-03-07 18:20:56 +0100977 if (_PyImport_FindExtensionObject(name, name) != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 for (p = PyImport_Inittab; p->name != NULL; p++) {
981 PyObject *mod;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100982 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +0100983 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 if (p->initfunc == NULL) {
985 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +0100986 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 name);
988 return -1;
989 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 mod = (*p->initfunc)();
991 if (mod == 0)
992 return -1;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100993 /* Remember pointer to module init function. */
994 def = PyModule_GetDef(mod);
995 def->m_base.m_init = p->initfunc;
Victor Stinner95872862011-03-07 18:20:56 +0100996 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 return -1;
998 /* FixupExtension has put the module into sys.modules,
999 so we can release our own reference. */
1000 Py_DECREF(mod);
1001 return 1;
1002 }
1003 }
1004 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001005}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001006
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001007
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001008/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001009
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001010static const struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +01001011find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001012{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001013 const struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001014
Victor Stinner53dc7352011-03-20 01:50:21 +01001015 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 for (p = PyImport_FrozenModules; ; p++) {
1019 if (p->name == NULL)
1020 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001021 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 break;
1023 }
1024 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001025}
1026
Guido van Rossum79f25d91997-04-29 20:08:16 +00001027static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001028get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001029{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001030 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 if (p == NULL) {
1034 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001035 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 name);
1037 return NULL;
1038 }
1039 if (p->code == NULL) {
1040 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001041 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 name);
1043 return NULL;
1044 }
1045 size = p->size;
1046 if (size < 0)
1047 size = -size;
1048 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001049}
1050
Brett Cannon8d110132009-03-15 02:20:16 +00001051static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001052is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001053{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001054 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 if (p == NULL) {
1058 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001059 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 name);
1061 return NULL;
1062 }
Brett Cannon8d110132009-03-15 02:20:16 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (size < 0)
1067 Py_RETURN_TRUE;
1068 else
1069 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001070}
1071
1072
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001073/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001074 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001075 an exception set if the initialization failed.
1076 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001077
1078int
Victor Stinner53dc7352011-03-20 01:50:21 +01001079PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001080{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001081 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001082 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 int ispackage;
1084 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001085
Victor Stinner53dc7352011-03-20 01:50:21 +01001086 p = find_frozen(name);
1087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 if (p == NULL)
1089 return 0;
1090 if (p->code == NULL) {
1091 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001092 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 name);
1094 return -1;
1095 }
1096 size = p->size;
1097 ispackage = (size < 0);
1098 if (ispackage)
1099 size = -size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1101 if (co == NULL)
1102 return -1;
1103 if (!PyCode_Check(co)) {
1104 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001105 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 name);
1107 goto err_return;
1108 }
1109 if (ispackage) {
Brett Cannon3e0651b2013-05-31 23:18:39 -04001110 /* Set __path__ to the empty list */
Victor Stinner53dc7352011-03-20 01:50:21 +01001111 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001113 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (m == NULL)
1115 goto err_return;
1116 d = PyModule_GetDict(m);
Brett Cannon3e0651b2013-05-31 23:18:39 -04001117 l = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 goto err_return;
1120 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 err = PyDict_SetItemString(d, "__path__", l);
1122 Py_DECREF(l);
1123 if (err != 0)
1124 goto err_return;
1125 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001126 path = PyUnicode_FromString("<frozen>");
1127 if (path == NULL)
1128 goto err_return;
1129 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1130 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (m == NULL)
1132 goto err_return;
1133 Py_DECREF(co);
1134 Py_DECREF(m);
1135 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001136err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 Py_DECREF(co);
1138 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001139}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001140
Victor Stinner53dc7352011-03-20 01:50:21 +01001141int
1142PyImport_ImportFrozenModule(char *name)
1143{
1144 PyObject *nameobj;
1145 int ret;
1146 nameobj = PyUnicode_InternFromString(name);
1147 if (nameobj == NULL)
1148 return -1;
1149 ret = PyImport_ImportFrozenModuleObject(nameobj);
1150 Py_DECREF(nameobj);
1151 return ret;
1152}
1153
Guido van Rossum74e6a111994-08-29 12:54:38 +00001154
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001155/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001156 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001157
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001159PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 PyObject *pname;
1162 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 pname = PyUnicode_FromString(name);
1165 if (pname == NULL)
1166 return NULL;
1167 result = PyImport_Import(pname);
1168 Py_DECREF(pname);
1169 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001170}
1171
Christian Heimes072c0f12008-01-03 23:01:04 +00001172/* Import a module without blocking
1173 *
1174 * At first it tries to fetch the module from sys.modules. If the module was
1175 * never loaded before it loads it with PyImport_ImportModule() unless another
1176 * thread holds the import lock. In the latter case the function raises an
1177 * ImportError instead of blocking.
1178 *
1179 * Returns the module object with incremented ref count.
1180 */
1181PyObject *
1182PyImport_ImportModuleNoBlock(const char *name)
1183{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001184 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001185}
1186
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001187
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001188/* Remove importlib frames from the traceback,
1189 * except in Verbose mode. */
1190static void
1191remove_importlib_frames(void)
1192{
1193 const char *importlib_filename = "<frozen importlib._bootstrap>";
Nick Coghlan42c07662012-07-31 21:14:18 +10001194 const char *remove_frames = "_call_with_frames_removed";
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001195 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001196 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001197 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001198 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001199
1200 /* Synopsis: if it's an ImportError, we trim all importlib chunks
Nick Coghlan42c07662012-07-31 21:14:18 +10001201 from the traceback. We always trim chunks
1202 which end with a call to "_call_with_frames_removed". */
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001203
1204 PyErr_Fetch(&exception, &value, &base_tb);
1205 if (!exception || Py_VerboseFlag)
1206 goto done;
1207 if (PyType_IsSubtype((PyTypeObject *) exception,
1208 (PyTypeObject *) PyExc_ImportError))
1209 always_trim = 1;
1210
1211 prev_link = &base_tb;
1212 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001213 while (tb != NULL) {
1214 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1215 PyObject *next = (PyObject *) traceback->tb_next;
1216 PyFrameObject *frame = traceback->tb_frame;
1217 PyCodeObject *code = frame->f_code;
1218 int now_in_importlib;
1219
1220 assert(PyTraceBack_Check(tb));
1221 now_in_importlib = (PyUnicode_CompareWithASCIIString(
1222 code->co_filename,
1223 importlib_filename) == 0);
1224 if (now_in_importlib && !in_importlib) {
1225 /* This is the link to this chunk of importlib tracebacks */
1226 outer_link = prev_link;
1227 }
1228 in_importlib = now_in_importlib;
1229
1230 if (in_importlib &&
1231 (always_trim ||
Nick Coghlan42c07662012-07-31 21:14:18 +10001232 PyUnicode_CompareWithASCIIString(code->co_name,
1233 remove_frames) == 0)) {
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001234 PyObject *tmp = *outer_link;
1235 *outer_link = next;
1236 Py_XINCREF(next);
1237 Py_DECREF(tmp);
1238 prev_link = outer_link;
1239 }
1240 else {
1241 prev_link = (PyObject **) &traceback->tb_next;
1242 }
1243 tb = next;
1244 }
1245done:
1246 PyErr_Restore(exception, value, base_tb);
1247}
1248
1249
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001250PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04001251PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1252 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001253 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001254{
Brett Cannonfd074152012-04-14 14:10:13 -04001255 _Py_IDENTIFIER(__import__);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001256 _Py_IDENTIFIER(__initializing__);
Brett Cannonfd074152012-04-14 14:10:13 -04001257 _Py_IDENTIFIER(__package__);
1258 _Py_IDENTIFIER(__path__);
1259 _Py_IDENTIFIER(__name__);
1260 _Py_IDENTIFIER(_find_and_load);
1261 _Py_IDENTIFIER(_handle_fromlist);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001262 _Py_IDENTIFIER(_lock_unlock_module);
Brett Cannonfd074152012-04-14 14:10:13 -04001263 _Py_static_string(single_dot, ".");
1264 PyObject *abs_name = NULL;
1265 PyObject *builtins_import = NULL;
1266 PyObject *final_mod = NULL;
1267 PyObject *mod = NULL;
1268 PyObject *package = NULL;
1269 PyObject *globals = NULL;
1270 PyObject *fromlist = NULL;
1271 PyInterpreterState *interp = PyThreadState_GET()->interp;
1272
1273 /* Make sure to use default values so as to not have
1274 PyObject_CallMethodObjArgs() truncate the parameter list because of a
1275 NULL argument. */
1276 if (given_globals == NULL) {
1277 globals = PyDict_New();
1278 if (globals == NULL) {
1279 goto error;
1280 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 }
Brett Cannonfd074152012-04-14 14:10:13 -04001282 else {
1283 /* Only have to care what given_globals is if it will be used
Brett Cannonecfefb72012-08-05 19:24:57 -04001284 for something. */
Brett Cannonfd074152012-04-14 14:10:13 -04001285 if (level > 0 && !PyDict_Check(given_globals)) {
1286 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1287 goto error;
1288 }
1289 globals = given_globals;
1290 Py_INCREF(globals);
1291 }
1292
1293 if (given_fromlist == NULL) {
1294 fromlist = PyList_New(0);
1295 if (fromlist == NULL) {
1296 goto error;
1297 }
1298 }
1299 else {
1300 fromlist = given_fromlist;
1301 Py_INCREF(fromlist);
1302 }
1303 if (name == NULL) {
1304 PyErr_SetString(PyExc_ValueError, "Empty module name");
1305 goto error;
1306 }
1307
1308 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1309 for added performance. */
1310
1311 if (!PyUnicode_Check(name)) {
1312 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1313 goto error;
1314 }
1315 else if (PyUnicode_READY(name) < 0) {
1316 goto error;
1317 }
1318 if (level < 0) {
1319 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1320 goto error;
1321 }
1322 else if (level > 0) {
1323 package = _PyDict_GetItemId(globals, &PyId___package__);
1324 if (package != NULL && package != Py_None) {
1325 Py_INCREF(package);
1326 if (!PyUnicode_Check(package)) {
1327 PyErr_SetString(PyExc_TypeError, "package must be a string");
1328 goto error;
1329 }
1330 }
1331 else {
Brett Cannon273323c2012-04-17 19:05:11 -04001332 package = _PyDict_GetItemId(globals, &PyId___name__);
Brett Cannonfd074152012-04-14 14:10:13 -04001333 if (package == NULL) {
Brett Cannon273323c2012-04-17 19:05:11 -04001334 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
Brett Cannonfd074152012-04-14 14:10:13 -04001335 goto error;
1336 }
1337 else if (!PyUnicode_Check(package)) {
1338 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1339 }
1340 Py_INCREF(package);
1341
1342 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Brett Cannon740fce02012-04-14 14:23:49 -04001343 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001344 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1345 if (borrowed_dot == NULL) {
1346 goto error;
1347 }
Brett Cannon740fce02012-04-14 14:23:49 -04001348 partition = PyUnicode_RPartition(package, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001349 Py_DECREF(package);
1350 if (partition == NULL) {
1351 goto error;
1352 }
1353 package = PyTuple_GET_ITEM(partition, 0);
1354 Py_INCREF(package);
1355 Py_DECREF(partition);
1356 }
1357 }
1358
1359 if (PyDict_GetItem(interp->modules, package) == NULL) {
1360 PyErr_Format(PyExc_SystemError,
1361 "Parent module %R not loaded, cannot perform relative "
1362 "import", package);
1363 goto error;
1364 }
1365 }
1366 else { /* level == 0 */
1367 if (PyUnicode_GET_LENGTH(name) == 0) {
1368 PyErr_SetString(PyExc_ValueError, "Empty module name");
1369 goto error;
1370 }
1371 package = Py_None;
1372 Py_INCREF(package);
1373 }
1374
1375 if (level > 0) {
1376 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1377 PyObject *base = NULL;
1378 int level_up = 1;
1379
1380 for (level_up = 1; level_up < level; level_up += 1) {
1381 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1382 if (last_dot == -2) {
1383 goto error;
1384 }
1385 else if (last_dot == -1) {
1386 PyErr_SetString(PyExc_ValueError,
1387 "attempted relative import beyond top-level "
1388 "package");
1389 goto error;
1390 }
1391 }
1392 base = PyUnicode_Substring(package, 0, last_dot);
1393 if (PyUnicode_GET_LENGTH(name) > 0) {
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001394 PyObject *borrowed_dot, *seq = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001395
1396 borrowed_dot = _PyUnicode_FromId(&single_dot);
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001397 seq = PyTuple_Pack(2, base, name);
1398 Py_DECREF(base);
Brett Cannonfd074152012-04-14 14:10:13 -04001399 if (borrowed_dot == NULL || seq == NULL) {
1400 goto error;
1401 }
1402
1403 abs_name = PyUnicode_Join(borrowed_dot, seq);
1404 Py_DECREF(seq);
1405 if (abs_name == NULL) {
1406 goto error;
1407 }
1408 }
1409 else {
1410 abs_name = base;
1411 }
1412 }
1413 else {
1414 abs_name = name;
1415 Py_INCREF(abs_name);
1416 }
1417
Brian Curtine6b299f2012-04-14 14:19:33 -05001418#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001419 _PyImport_AcquireLock();
1420#endif
1421 /* From this point forward, goto error_with_unlock! */
1422 if (PyDict_Check(globals)) {
1423 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1424 }
1425 if (builtins_import == NULL) {
1426 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1427 if (builtins_import == NULL) {
Benjamin Peterson7d110042013-04-29 09:08:14 -04001428 PyErr_SetString(PyExc_ImportError, "__import__ not found");
1429 goto error_with_unlock;
Brett Cannonfd074152012-04-14 14:10:13 -04001430 }
1431 }
1432 Py_INCREF(builtins_import);
1433
1434 mod = PyDict_GetItem(interp->modules, abs_name);
1435 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001436 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1437 "None in sys.modules", abs_name);
1438 if (msg != NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -05001439 PyErr_SetImportError(msg, abs_name, NULL);
1440 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001441 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001442 mod = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001443 goto error_with_unlock;
1444 }
1445 else if (mod != NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001446 PyObject *value;
1447 int initializing = 0;
1448
Brett Cannonfd074152012-04-14 14:10:13 -04001449 Py_INCREF(mod);
Antoine Pitrou03989852012-08-28 00:24:52 +02001450 /* Optimization: only call _bootstrap._lock_unlock_module() if
1451 __initializing__ is true.
1452 NOTE: because of this, __initializing__ must be set *before*
1453 stuffing the new module in sys.modules.
1454 */
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001455 value = _PyObject_GetAttrId(mod, &PyId___initializing__);
1456 if (value == NULL)
1457 PyErr_Clear();
1458 else {
1459 initializing = PyObject_IsTrue(value);
1460 Py_DECREF(value);
1461 if (initializing == -1)
1462 PyErr_Clear();
1463 }
1464 if (initializing > 0) {
1465 /* _bootstrap._lock_unlock_module() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001466 value = _PyObject_CallMethodIdObjArgs(interp->importlib,
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001467 &PyId__lock_unlock_module, abs_name,
1468 NULL);
1469 if (value == NULL)
1470 goto error;
1471 Py_DECREF(value);
1472 }
1473 else {
1474#ifdef WITH_THREAD
1475 if (_PyImport_ReleaseLock() < 0) {
1476 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1477 goto error;
1478 }
1479#endif
1480 }
Brett Cannonfd074152012-04-14 14:10:13 -04001481 }
1482 else {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001483 /* _bootstrap._find_and_load() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001484 mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001485 &PyId__find_and_load, abs_name,
1486 builtins_import, NULL);
1487 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001488 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001489 }
1490 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001491 /* From now on we don't hold the import lock anymore. */
Brett Cannonfd074152012-04-14 14:10:13 -04001492
1493 if (PyObject_Not(fromlist)) {
1494 if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1495 PyObject *front = NULL;
Brian Curtine6b299f2012-04-14 14:19:33 -05001496 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001497 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1498
1499 if (borrowed_dot == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001500 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001501 }
1502
Brian Curtine6b299f2012-04-14 14:19:33 -05001503 partition = PyUnicode_Partition(name, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001504 if (partition == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001505 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001506 }
1507
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001508 if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1509 /* No dot in module name, simple exit */
1510 Py_DECREF(partition);
1511 final_mod = mod;
1512 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001513 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001514 }
1515
Brett Cannonfd074152012-04-14 14:10:13 -04001516 front = PyTuple_GET_ITEM(partition, 0);
1517 Py_INCREF(front);
1518 Py_DECREF(partition);
1519
1520 if (level == 0) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001521 final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001522 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001523 }
1524 else {
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001525 Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1526 PyUnicode_GET_LENGTH(front);
1527 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001528 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001529 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001530 Py_DECREF(front);
1531 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001532 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001533 }
Brett Cannonfd074152012-04-14 14:10:13 -04001534
1535 final_mod = PyDict_GetItem(interp->modules, to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001536 if (final_mod == NULL) {
1537 PyErr_Format(PyExc_KeyError,
1538 "%R not in sys.modules as expected",
1539 to_return);
1540 }
1541 else {
1542 Py_INCREF(final_mod);
1543 }
Antoine Pitroub78174c2012-05-06 17:15:23 +02001544 Py_DECREF(to_return);
Brett Cannonfd074152012-04-14 14:10:13 -04001545 }
1546 }
1547 else {
1548 final_mod = mod;
1549 Py_INCREF(mod);
1550 }
1551 }
1552 else {
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001553 final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001554 &PyId__handle_fromlist, mod,
1555 fromlist, builtins_import,
1556 NULL);
1557 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001558 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001559
Brett Cannonfd074152012-04-14 14:10:13 -04001560 error_with_unlock:
Brian Curtine6b299f2012-04-14 14:19:33 -05001561#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001562 if (_PyImport_ReleaseLock() < 0) {
1563 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1564 }
1565#endif
1566 error:
1567 Py_XDECREF(abs_name);
1568 Py_XDECREF(builtins_import);
1569 Py_XDECREF(mod);
1570 Py_XDECREF(package);
1571 Py_XDECREF(globals);
1572 Py_XDECREF(fromlist);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001573 if (final_mod == NULL)
1574 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001575 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001576}
1577
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001578PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001579PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001580 PyObject *fromlist, int level)
1581{
1582 PyObject *nameobj, *mod;
1583 nameobj = PyUnicode_FromString(name);
1584 if (nameobj == NULL)
1585 return NULL;
1586 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1587 fromlist, level);
1588 Py_DECREF(nameobj);
1589 return mod;
1590}
1591
1592
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001593/* Re-import a module of any kind and return its module object, WITH
1594 INCREMENTED REFERENCE COUNT */
1595
Guido van Rossum79f25d91997-04-29 20:08:16 +00001596PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001597PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001598{
Brett Cannon62228db2012-04-29 14:38:11 -04001599 _Py_IDENTIFIER(reload);
1600 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001602 PyObject *imp = PyDict_GetItemString(modules, "imp");
1603 if (imp == NULL) {
1604 imp = PyImport_ImportModule("imp");
1605 if (imp == NULL) {
1606 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 }
Brett Cannon62228db2012-04-29 14:38:11 -04001609 else {
1610 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001612
Brett Cannon62228db2012-04-29 14:38:11 -04001613 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1614 Py_DECREF(imp);
1615 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001616}
1617
1618
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001619/* Higher-level import emulator which emulates the "import" statement
1620 more accurately -- it invokes the __import__() function from the
1621 builtins of the current globals. This means that the import is
1622 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001623 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001624 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001625 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001626 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001627
1628PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001629PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 static PyObject *silly_list = NULL;
1632 static PyObject *builtins_str = NULL;
1633 static PyObject *import_str = NULL;
1634 PyObject *globals = NULL;
1635 PyObject *import = NULL;
1636 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001637 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 /* Initialize constant string objects */
1641 if (silly_list == NULL) {
1642 import_str = PyUnicode_InternFromString("__import__");
1643 if (import_str == NULL)
1644 return NULL;
1645 builtins_str = PyUnicode_InternFromString("__builtins__");
1646 if (builtins_str == NULL)
1647 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001648 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (silly_list == NULL)
1650 return NULL;
1651 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 /* Get the builtins from current globals */
1654 globals = PyEval_GetGlobals();
1655 if (globals != NULL) {
1656 Py_INCREF(globals);
1657 builtins = PyObject_GetItem(globals, builtins_str);
1658 if (builtins == NULL)
1659 goto err;
1660 }
1661 else {
1662 /* No globals -- use standard builtins, and fake globals */
1663 builtins = PyImport_ImportModuleLevel("builtins",
1664 NULL, NULL, NULL, 0);
1665 if (builtins == NULL)
1666 return NULL;
1667 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1668 if (globals == NULL)
1669 goto err;
1670 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 /* Get the __import__ function from the builtins */
1673 if (PyDict_Check(builtins)) {
1674 import = PyObject_GetItem(builtins, import_str);
1675 if (import == NULL)
1676 PyErr_SetObject(PyExc_KeyError, import_str);
1677 }
1678 else
1679 import = PyObject_GetAttr(builtins, import_str);
1680 if (import == NULL)
1681 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001684 Always use absolute import here.
1685 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1687 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001688 if (r == NULL)
1689 goto err;
1690 Py_DECREF(r);
1691
1692 modules = PyImport_GetModuleDict();
1693 r = PyDict_GetItem(modules, module_name);
1694 if (r != NULL)
1695 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001696
1697 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 Py_XDECREF(globals);
1699 Py_XDECREF(builtins);
1700 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001703}
1704
Barry Warsaw28a691b2010-04-17 00:19:56 +00001705static PyObject *
Brett Cannon2657df42012-05-04 15:20:40 -04001706imp_extension_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001709 const char *suffix;
1710 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 list = PyList_New(0);
1713 if (list == NULL)
1714 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001715#ifdef HAVE_DYNAMIC_LOADING
1716 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1717 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 if (item == NULL) {
1719 Py_DECREF(list);
1720 return NULL;
1721 }
1722 if (PyList_Append(list, item) < 0) {
1723 Py_DECREF(list);
1724 Py_DECREF(item);
1725 return NULL;
1726 }
1727 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001728 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 }
Brett Cannon2657df42012-05-04 15:20:40 -04001730#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001732}
1733
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001735imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001736{
Victor Stinner95872862011-03-07 18:20:56 +01001737 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 int ret;
1739 PyObject *m;
Victor Stinner95872862011-03-07 18:20:56 +01001740 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 return NULL;
1742 ret = init_builtin(name);
1743 if (ret < 0)
1744 return NULL;
1745 if (ret == 0) {
1746 Py_INCREF(Py_None);
1747 return Py_None;
1748 }
Victor Stinner95872862011-03-07 18:20:56 +01001749 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 Py_XINCREF(m);
1751 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001752}
1753
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001755imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001756{
Victor Stinner53dc7352011-03-20 01:50:21 +01001757 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 int ret;
1759 PyObject *m;
Victor Stinner53dc7352011-03-20 01:50:21 +01001760 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001762 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 if (ret < 0)
1764 return NULL;
1765 if (ret == 0) {
1766 Py_INCREF(Py_None);
1767 return Py_None;
1768 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001769 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 Py_XINCREF(m);
1771 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001772}
1773
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001775imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001776{
Victor Stinner53dc7352011-03-20 01:50:21 +01001777 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00001778
Victor Stinner53dc7352011-03-20 01:50:21 +01001779 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 return NULL;
1781 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001782}
1783
Guido van Rossum79f25d91997-04-29 20:08:16 +00001784static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00001785imp_is_frozen_package(PyObject *self, PyObject *args)
1786{
Victor Stinner53dc7352011-03-20 01:50:21 +01001787 PyObject *name;
Brett Cannon8d110132009-03-15 02:20:16 +00001788
Victor Stinner53dc7352011-03-20 01:50:21 +01001789 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 return NULL;
1791 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00001792}
1793
1794static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001795imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001796{
Victor Stinner95872862011-03-07 18:20:56 +01001797 PyObject *name;
1798 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 return NULL;
1800 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001801}
1802
Guido van Rossum79f25d91997-04-29 20:08:16 +00001803static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001804imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001805{
Victor Stinner53dc7352011-03-20 01:50:21 +01001806 PyObject *name;
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001807 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001808 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 return NULL;
1810 p = find_frozen(name);
1811 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001812}
1813
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001814#ifdef HAVE_DYNAMIC_LOADING
1815
Guido van Rossum79f25d91997-04-29 20:08:16 +00001816static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001817imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001818{
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001819 PyObject *name, *pathname, *fob = NULL, *mod;
1820 FILE *fp;
1821
1822 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
1823 &name, PyUnicode_FSDecoder, &pathname, &fob))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 return NULL;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001825 if (fob != NULL) {
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001826 fp = _Py_fopen(pathname, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001827 if (fp == NULL) {
1828 Py_DECREF(pathname);
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001829 if (!PyErr_Occurred())
1830 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001832 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001834 else
1835 fp = NULL;
1836 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001837 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 if (fp)
1839 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001840 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001841}
1842
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001843#endif /* HAVE_DYNAMIC_LOADING */
1844
Barry Warsaw28a691b2010-04-17 00:19:56 +00001845
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001846/* Doc strings */
1847
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001848PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04001849"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001850
Brett Cannon2657df42012-05-04 15:20:40 -04001851PyDoc_STRVAR(doc_extension_suffixes,
1852"extension_suffixes() -> list of strings\n\
1853Returns the list of file suffixes used to identify extension modules.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001854
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001855PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00001856"lock_held() -> boolean\n\
1857Return True if the import lock is currently held, else False.\n\
1858On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00001859
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001860PyDoc_STRVAR(doc_acquire_lock,
1861"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001862Acquires the interpreter's import lock for the current thread.\n\
1863This lock should be used by import hooks to ensure thread-safety\n\
1864when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001865On platforms without threads, this function does nothing.");
1866
1867PyDoc_STRVAR(doc_release_lock,
1868"release_lock() -> None\n\
1869Release the interpreter's import lock.\n\
1870On platforms without threads, this function does nothing.");
1871
Guido van Rossum79f25d91997-04-29 20:08:16 +00001872static PyMethodDef imp_methods[] = {
Brett Cannon2657df42012-05-04 15:20:40 -04001873 {"extension_suffixes", imp_extension_suffixes, METH_NOARGS,
1874 doc_extension_suffixes},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
1876 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
1877 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
1879 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
1880 {"init_builtin", imp_init_builtin, METH_VARARGS},
1881 {"init_frozen", imp_init_frozen, METH_VARARGS},
1882 {"is_builtin", imp_is_builtin, METH_VARARGS},
1883 {"is_frozen", imp_is_frozen, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001884#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001886#endif
Brett Cannon442c9b92011-03-23 16:14:42 -07001887 {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001889};
1890
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001891
Martin v. Löwis1a214512008-06-11 05:26:20 +00001892static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04001894 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 doc_imp,
1896 0,
1897 imp_methods,
1898 NULL,
1899 NULL,
1900 NULL,
1901 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001902};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001903
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001904PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001905PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 m = PyModule_Create(&impmodule);
1910 if (m == NULL)
1911 goto failure;
1912 d = PyModule_GetDict(m);
1913 if (d == NULL)
1914 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001917 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 Py_XDECREF(m);
1919 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001920}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001921
1922
Guido van Rossumb18618d2000-05-03 23:44:39 +00001923/* API for embedding applications that want to add their own entries
1924 to the table of built-in modules. This should normally be called
1925 *before* Py_Initialize(). When the table resize fails, -1 is
1926 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001927
1928 After a similar function by Just van Rossum. */
1929
1930int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001931PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 static struct _inittab *our_copy = NULL;
1934 struct _inittab *p;
1935 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 /* Count the number of entries in both tables */
1938 for (n = 0; newtab[n].name != NULL; n++)
1939 ;
1940 if (n == 0)
1941 return 0; /* Nothing to do */
1942 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
1943 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 /* Allocate new memory for the combined table */
1946 p = our_copy;
1947 PyMem_RESIZE(p, struct _inittab, i+n+1);
1948 if (p == NULL)
1949 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 /* Copy the tables into the new memory */
1952 if (our_copy != PyImport_Inittab)
1953 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
1954 PyImport_Inittab = our_copy = p;
1955 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001958}
1959
1960/* Shorthand to add a single entry given a name and a function */
1961
1962int
Brett Cannona826f322009-04-02 03:41:46 +00001963PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 newtab[0].name = (char *)name;
1970 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001973}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001974
1975#ifdef __cplusplus
1976}
1977#endif