blob: c6222ec57f63cb69a269c43e0694387a2d185c11 [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
Victor Stinnerfe7c5b52011-04-05 01:48:03 +020027/* Function from Parser/tokenizer.c */
28extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
29
Guido van Rossum771c6c81997-10-31 18:37:24 +000030/* This table is defined in config.c: */
31extern struct _inittab _PyImport_Inittab[];
32
33struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000034
Victor Stinnerd0296212011-03-14 14:04:10 -040035static PyObject *initstr = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036
Guido van Rossum1ae940a1995-01-02 19:04:15 +000037/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038
39void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000040_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041{
Victor Stinnerd0296212011-03-14 14:04:10 -040042 initstr = PyUnicode_InternFromString("__init__");
43 if (initstr == NULL)
44 Py_FatalError("Can't initialize import variables");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045}
46
Guido van Rossum25ce5661997-08-02 03:10:38 +000047void
Just van Rossum52e14d62002-12-30 22:08:05 +000048_PyImportHooks_Init(void)
49{
Brett Cannonfd074152012-04-14 14:10:13 -040050 PyObject *v, *path_hooks = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 int err = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +000052
Brett Cannonfd074152012-04-14 14:10:13 -040053 /* adding sys.path_hooks and sys.path_importer_cache */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 v = PyList_New(0);
55 if (v == NULL)
56 goto error;
57 err = PySys_SetObject("meta_path", v);
58 Py_DECREF(v);
59 if (err)
60 goto error;
61 v = PyDict_New();
62 if (v == NULL)
63 goto error;
64 err = PySys_SetObject("path_importer_cache", v);
65 Py_DECREF(v);
66 if (err)
67 goto error;
68 path_hooks = PyList_New(0);
69 if (path_hooks == NULL)
70 goto error;
71 err = PySys_SetObject("path_hooks", path_hooks);
72 if (err) {
Just van Rossum52e14d62002-12-30 22:08:05 +000073 error:
Brett Cannonfd074152012-04-14 14:10:13 -040074 PyErr_Print();
75 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
Nadeem Vawda8f46d652012-05-05 12:27:30 +020076 "or path_importer_cache failed");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 }
Brett Cannonfd074152012-04-14 14:10:13 -040078 Py_DECREF(path_hooks);
79}
80
81void
82_PyImportZip_Init(void)
83{
84 PyObject *path_hooks, *zimpimport;
85 int err = 0;
86
87 path_hooks = PySys_GetObject("path_hooks");
Victor Stinner1e53bba2013-07-16 22:26:05 +020088 if (path_hooks == NULL) {
89 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks");
Brett Cannonfd074152012-04-14 14:10:13 -040090 goto error;
Victor Stinner1e53bba2013-07-16 22:26:05 +020091 }
Brett Cannonfd074152012-04-14 14:10:13 -040092
93 if (Py_VerboseFlag)
94 PySys_WriteStderr("# installing zipimport hook\n");
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 zimpimport = PyImport_ImportModule("zipimport");
97 if (zimpimport == NULL) {
98 PyErr_Clear(); /* No zip import module -- okay */
99 if (Py_VerboseFlag)
100 PySys_WriteStderr("# can't import zipimport\n");
101 }
102 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200103 _Py_IDENTIFIER(zipimporter);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200104 PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
105 &PyId_zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 Py_DECREF(zimpimport);
107 if (zipimporter == NULL) {
108 PyErr_Clear(); /* No zipimporter object -- okay */
109 if (Py_VerboseFlag)
110 PySys_WriteStderr(
111 "# can't import zipimport.zipimporter\n");
112 }
113 else {
Brett Cannon8923a4d2012-04-24 22:03:46 -0400114 /* sys.path_hooks.insert(0, zipimporter) */
115 err = PyList_Insert(path_hooks, 0, zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 Py_DECREF(zipimporter);
Brett Cannonfd074152012-04-14 14:10:13 -0400117 if (err < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -0400119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 if (Py_VerboseFlag)
121 PySys_WriteStderr(
122 "# installed zipimport hook\n");
123 }
124 }
Brett Cannonfd074152012-04-14 14:10:13 -0400125
126 return;
127
128 error:
129 PyErr_Print();
Brett Cannonacf85cd2012-04-29 12:50:03 -0400130 Py_FatalError("initializing zipimport failed");
Just van Rossum52e14d62002-12-30 22:08:05 +0000131}
132
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000133/* Locking primitives to prevent parallel imports of the same module
134 in different threads to return with a partially loaded module.
135 These calls are serialized by the global interpreter lock. */
136
137#ifdef WITH_THREAD
138
Guido van Rossum49b56061998-10-01 20:42:43 +0000139#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000140
Guido van Rossum65d5b571998-12-21 19:32:43 +0000141static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000142static long import_lock_thread = -1;
143static int import_lock_level = 0;
144
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000145void
146_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 long me = PyThread_get_thread_ident();
149 if (me == -1)
150 return; /* Too bad */
151 if (import_lock == NULL) {
152 import_lock = PyThread_allocate_lock();
153 if (import_lock == NULL)
154 return; /* Nothing much we can do. */
155 }
156 if (import_lock_thread == me) {
157 import_lock_level++;
158 return;
159 }
160 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
161 {
162 PyThreadState *tstate = PyEval_SaveThread();
163 PyThread_acquire_lock(import_lock, 1);
164 PyEval_RestoreThread(tstate);
165 }
Antoine Pitrou202b6062012-12-18 22:18:17 +0100166 assert(import_lock_level == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 import_lock_thread = me;
168 import_lock_level = 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000169}
170
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000171int
172_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 long me = PyThread_get_thread_ident();
175 if (me == -1 || import_lock == NULL)
176 return 0; /* Too bad */
177 if (import_lock_thread != me)
178 return -1;
179 import_lock_level--;
Antoine Pitrou202b6062012-12-18 22:18:17 +0100180 assert(import_lock_level >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (import_lock_level == 0) {
182 import_lock_thread = -1;
183 PyThread_release_lock(import_lock);
184 }
185 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000186}
187
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000188/* This function is called from PyOS_AfterFork to ensure that newly
189 created child processes do not share locks with the parent.
190 We now acquire the import lock around fork() calls but on some platforms
191 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000192
193void
194_PyImport_ReInitLock(void)
195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 if (import_lock != NULL)
197 import_lock = PyThread_allocate_lock();
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000198 if (import_lock_level > 1) {
199 /* Forked as a side effect of import */
200 long me = PyThread_get_thread_ident();
Brett Cannone4710cf2012-11-15 21:39:36 -0500201 /* The following could fail if the lock is already held, but forking as
202 a side-effect of an import is a) rare, b) nuts, and c) difficult to
203 do thanks to the lock only being held when doing individual module
204 locks per import. */
205 PyThread_acquire_lock(import_lock, NOWAIT_LOCK);
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000206 import_lock_thread = me;
207 import_lock_level--;
208 } else {
209 import_lock_thread = -1;
210 import_lock_level = 0;
211 }
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000212}
213
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000214#endif
215
Tim Peters69232342001-08-30 05:16:13 +0000216static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000217imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000218{
Tim Peters69232342001-08-30 05:16:13 +0000219#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000221#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000223#endif
224}
225
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000226static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000227imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000228{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000229#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000231#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 Py_INCREF(Py_None);
233 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000234}
235
236static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000237imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000238{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000239#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (_PyImport_ReleaseLock() < 0) {
241 PyErr_SetString(PyExc_RuntimeError,
242 "not holding the import lock");
243 return NULL;
244 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000245#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 Py_INCREF(Py_None);
247 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000248}
249
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100250void
251_PyImport_Fini(void)
252{
253 Py_XDECREF(extensions);
254 extensions = NULL;
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100255#ifdef WITH_THREAD
256 if (import_lock != NULL) {
257 PyThread_free_lock(import_lock);
258 import_lock = NULL;
259 }
260#endif
261}
262
Guido van Rossum25ce5661997-08-02 03:10:38 +0000263/* Helper for sys */
264
265PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000266PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 PyInterpreterState *interp = PyThreadState_GET()->interp;
269 if (interp->modules == NULL)
270 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
271 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000272}
273
Guido van Rossum3f5da241990-12-20 15:06:42 +0000274
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000275/* List of names to clear in sys */
276static char* sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 "path", "argv", "ps1", "ps2",
278 "last_type", "last_value", "last_traceback",
279 "path_hooks", "path_importer_cache", "meta_path",
280 /* misc stuff */
281 "flags", "float_info",
282 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000283};
284
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000285static char* sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 "stdin", "__stdin__",
287 "stdout", "__stdout__",
288 "stderr", "__stderr__",
289 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000290};
291
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200292static int
293is_essential_module(PyObject *name)
294{
295 Py_ssize_t name_len;
296 char *name_str = PyUnicode_AsUTF8AndSize(name, &name_len);
297
298 if (name_str == NULL) {
299 PyErr_Clear();
300 return 0;
301 }
302 if (strcmp(name_str, "builtins") == 0)
303 return 1;
304 if (strcmp(name_str, "sys") == 0)
305 return 1;
306 /* These are all needed for stderr to still function */
307 if (strcmp(name_str, "codecs") == 0)
308 return 1;
309 if (strcmp(name_str, "_codecs") == 0)
310 return 1;
311 if (strncmp(name_str, "encodings.", 10) == 0)
312 return 1;
313 return 0;
314}
315
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000316
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000317/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000318
Guido van Rossum3f5da241990-12-20 15:06:42 +0000319void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000320PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 Py_ssize_t pos, ndone;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 PyObject *key, *value, *dict;
324 PyInterpreterState *interp = PyThreadState_GET()->interp;
325 PyObject *modules = interp->modules;
Guido van Rossum758eec01998-01-19 21:58:26 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 if (modules == NULL)
328 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 /* Delete some special variables first. These are common
331 places where user values hide and people complain when their
332 destructors fail. Since the modules containing them are
333 deleted *last* of all, they would come too late in the normal
334 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 value = PyDict_GetItemString(modules, "builtins");
337 if (value != NULL && PyModule_Check(value)) {
338 dict = PyModule_GetDict(value);
339 if (Py_VerboseFlag)
340 PySys_WriteStderr("# clear builtins._\n");
341 PyDict_SetItemString(dict, "_", Py_None);
342 }
343 value = PyDict_GetItemString(modules, "sys");
344 if (value != NULL && PyModule_Check(value)) {
345 char **p;
346 PyObject *v;
347 dict = PyModule_GetDict(value);
348 for (p = sys_deletes; *p != NULL; p++) {
349 if (Py_VerboseFlag)
350 PySys_WriteStderr("# clear sys.%s\n", *p);
351 PyDict_SetItemString(dict, *p, Py_None);
352 }
353 for (p = sys_files; *p != NULL; p+=2) {
354 if (Py_VerboseFlag)
355 PySys_WriteStderr("# restore sys.%s\n", *p);
356 v = PyDict_GetItemString(dict, *(p+1));
357 if (v == NULL)
358 v = Py_None;
359 PyDict_SetItemString(dict, *p, v);
360 }
361 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 /* First, delete __main__ */
364 value = PyDict_GetItemString(modules, "__main__");
365 if (value != NULL && PyModule_Check(value)) {
366 if (Py_VerboseFlag)
367 PySys_WriteStderr("# cleanup __main__\n");
368 _PyModule_Clear(value);
369 PyDict_SetItemString(modules, "__main__", Py_None);
370 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* The special treatment of "builtins" here is because even
373 when it's not referenced as a module, its dictionary is
374 referenced by almost every module's __builtins__. Since
375 deleting a module clears its dictionary (even if there are
376 references left to it), we need to delete the "builtins"
377 module last. Likewise, we don't delete sys until the very
378 end because it is implicitly referenced (e.g. by print).
Guido van Rossum758eec01998-01-19 21:58:26 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 Also note that we 'delete' modules by replacing their entry
381 in the modules dict with None, rather than really deleting
382 them; this avoids a rehash of the modules dictionary and
383 also marks them as "non existent" so they won't be
384 re-imported. */
Guido van Rossum758eec01998-01-19 21:58:26 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 /* Next, repeatedly delete modules with a reference count of
387 one (skipping builtins and sys) and delete them */
388 do {
389 ndone = 0;
390 pos = 0;
391 while (PyDict_Next(modules, &pos, &key, &value)) {
392 if (value->ob_refcnt != 1)
393 continue;
394 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200395 if (is_essential_module(key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 continue;
397 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100398 PySys_FormatStderr(
399 "# cleanup[1] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 _PyModule_Clear(value);
401 PyDict_SetItem(modules, key, Py_None);
402 ndone++;
403 }
404 }
405 } while (ndone > 0);
Guido van Rossum758eec01998-01-19 21:58:26 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Next, delete all modules (still skipping builtins and sys) */
408 pos = 0;
409 while (PyDict_Next(modules, &pos, &key, &value)) {
410 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200411 if (is_essential_module(key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 continue;
413 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100414 PySys_FormatStderr("# cleanup[2] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 _PyModule_Clear(value);
416 PyDict_SetItem(modules, key, Py_None);
417 }
418 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000419
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200420 /* Collect garbage remaining after deleting the modules. Mostly
421 reference cycles created by classes. */
422 PyGC_Collect();
423
424 /* Dump GC stats before it's too late, since it uses the warnings
425 machinery. */
426 _PyGC_DumpShutdownStats();
427
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200428 /* Next, delete all remaining modules */
429 pos = 0;
430 while (PyDict_Next(modules, &pos, &key, &value)) {
431 if (PyUnicode_Check(key) && PyModule_Check(value)) {
432 if (Py_VerboseFlag)
433 PySys_FormatStderr("# cleanup[3] %U\n", key);
434 _PyModule_Clear(value);
435 PyDict_SetItem(modules, key, Py_None);
436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 /* Finally, clear and delete the modules directory */
440 PyDict_Clear(modules);
Antoine Pitroufef34e32013-05-19 01:11:58 +0200441 _PyGC_CollectNoFail();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 interp->modules = NULL;
443 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000444}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000445
446
Barry Warsaw28a691b2010-04-17 00:19:56 +0000447/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000448
449long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000450PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451{
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200452 long res;
Brett Cannon77b2abd2012-07-09 16:09:00 -0400453 PyInterpreterState *interp = PyThreadState_Get()->interp;
454 PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
455 "_RAW_MAGIC_NUMBER");
456 if (pyc_magic == NULL)
457 return -1;
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200458 res = PyLong_AsLong(pyc_magic);
Benjamin Peterson66f36592012-07-09 22:21:55 -0700459 Py_DECREF(pyc_magic);
460 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000461}
462
463
Brett Cannon3adc7b72012-07-09 14:22:12 -0400464extern const char * _PySys_ImplCacheTag;
465
Barry Warsaw28a691b2010-04-17 00:19:56 +0000466const char *
467PyImport_GetMagicTag(void)
468{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400469 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000470}
471
Brett Cannon98979b82012-07-02 15:13:11 -0400472
Guido van Rossum25ce5661997-08-02 03:10:38 +0000473/* Magic for extension modules (built-in as well as dynamically
474 loaded). To prevent initializing an extension module more than
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200475 once, we keep a static dictionary 'extensions' keyed by the tuple
476 (module name, module name) (for built-in modules) or by
477 (filename, module name) (for dynamically loaded modules), containing these
478 modules. A copy of the module's dictionary is stored by calling
479 _PyImport_FixupExtensionObject() immediately after the module initialization
480 function succeeds. A copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100481 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000483 Modules which do support multiple initialization set their m_size
484 field to a non-negative number (indicating the size of the
485 module-specific state). They are still recorded in the extensions
486 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000487*/
488
489int
Victor Stinner95872862011-03-07 18:20:56 +0100490_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
491 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000492{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500493 PyObject *modules, *dict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 struct PyModuleDef *def;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500495 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if (extensions == NULL) {
497 extensions = PyDict_New();
498 if (extensions == NULL)
499 return -1;
500 }
501 if (mod == NULL || !PyModule_Check(mod)) {
502 PyErr_BadInternalCall();
503 return -1;
504 }
505 def = PyModule_GetDef(mod);
506 if (!def) {
507 PyErr_BadInternalCall();
508 return -1;
509 }
510 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100511 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return -1;
513 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100514 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return -1;
516 }
517 if (def->m_size == -1) {
518 if (def->m_base.m_copy) {
519 /* Somebody already imported the module,
520 likely under a different name.
521 XXX this should really not happen. */
522 Py_DECREF(def->m_base.m_copy);
523 def->m_base.m_copy = NULL;
524 }
525 dict = PyModule_GetDict(mod);
526 if (dict == NULL)
527 return -1;
528 def->m_base.m_copy = PyDict_Copy(dict);
529 if (def->m_base.m_copy == NULL)
530 return -1;
531 }
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500532 key = PyTuple_Pack(2, filename, name);
533 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200534 return -1;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500535 res = PyDict_SetItem(extensions, key, (PyObject *)def);
536 Py_DECREF(key);
537 if (res < 0)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200538 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000540}
541
Victor Stinner49d3f252010-10-17 01:24:53 +0000542int
543_PyImport_FixupBuiltin(PyObject *mod, char *name)
544{
545 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100546 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100547 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100548 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000549 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100550 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
551 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000552 return res;
553}
554
Guido van Rossum25ce5661997-08-02 03:10:38 +0000555PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100556_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000557{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500558 PyObject *mod, *mdict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 PyModuleDef* def;
560 if (extensions == NULL)
561 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500562 key = PyTuple_Pack(2, filename, name);
563 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200564 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500565 def = (PyModuleDef *)PyDict_GetItem(extensions, key);
566 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (def == NULL)
568 return NULL;
569 if (def->m_size == -1) {
570 /* Module does not support repeated initialization */
571 if (def->m_base.m_copy == NULL)
572 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100573 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (mod == NULL)
575 return NULL;
576 mdict = PyModule_GetDict(mod);
577 if (mdict == NULL)
578 return NULL;
579 if (PyDict_Update(mdict, def->m_base.m_copy))
580 return NULL;
581 }
582 else {
583 if (def->m_base.m_init == NULL)
584 return NULL;
585 mod = def->m_base.m_init();
586 if (mod == NULL)
587 return NULL;
Christian Heimes09ca7942013-07-20 14:51:53 +0200588 if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) {
589 Py_DECREF(mod);
590 return NULL;
591 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 Py_DECREF(mod);
593 }
594 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100595 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 Py_DECREF(mod);
597 return NULL;
598 }
599 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100600 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 name, filename);
602 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000603
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604}
605
Victor Stinner49d3f252010-10-17 01:24:53 +0000606PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000607_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000608{
Victor Stinner95872862011-03-07 18:20:56 +0100609 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100610 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100611 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000612 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100613 res = _PyImport_FindExtensionObject(nameobj, nameobj);
614 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000615 return res;
616}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000617
618/* Get the module object corresponding to a module name.
619 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000620 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000621 Because the former action is most common, THIS DOES NOT RETURN A
622 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623
Guido van Rossum79f25d91997-04-29 20:08:16 +0000624PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000625PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 PyObject *modules = PyImport_GetModuleDict();
628 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000629
Victor Stinner27ee0892011-03-04 12:57:09 +0000630 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 PyModule_Check(m))
632 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000633 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 if (m == NULL)
635 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000636 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 Py_DECREF(m);
638 return NULL;
639 }
640 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000643}
644
Victor Stinner27ee0892011-03-04 12:57:09 +0000645PyObject *
646PyImport_AddModule(const char *name)
647{
648 PyObject *nameobj, *module;
649 nameobj = PyUnicode_FromString(name);
650 if (nameobj == NULL)
651 return NULL;
652 module = PyImport_AddModuleObject(nameobj);
653 Py_DECREF(nameobj);
654 return module;
655}
656
657
Tim Peters1cd70172004-08-02 03:52:12 +0000658/* Remove name from sys.modules, if it's there. */
659static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000660remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000663 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000665 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 Py_FatalError("import: deleting existing key in"
667 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000668}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669
Christian Heimes3b06e532008-01-07 20:12:44 +0000670
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000671/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000672 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
673 * removed from sys.modules, to avoid leaving damaged module objects
674 * in sys.modules. The caller may wish to restore the original
675 * module object (if any) in this case; PyImport_ReloadModule is an
676 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000677 *
678 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
679 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000680 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000682PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 return PyImport_ExecCodeModuleWithPathnames(
685 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000686}
687
688PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000689PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 return PyImport_ExecCodeModuleWithPathnames(
692 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000693}
694
695PyObject *
696PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000698{
Victor Stinner27ee0892011-03-04 12:57:09 +0000699 PyObject *m = NULL;
700 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
701
702 nameobj = PyUnicode_FromString(name);
703 if (nameobj == NULL)
704 return NULL;
705
Victor Stinner27ee0892011-03-04 12:57:09 +0000706 if (cpathname != NULL) {
707 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
708 if (cpathobj == NULL)
709 goto error;
Brett Cannona6473f92012-07-13 13:57:03 -0400710 }
711 else
Victor Stinner27ee0892011-03-04 12:57:09 +0000712 cpathobj = NULL;
Brett Cannona6473f92012-07-13 13:57:03 -0400713
714 if (pathname != NULL) {
715 pathobj = PyUnicode_DecodeFSDefault(pathname);
716 if (pathobj == NULL)
717 goto error;
718 }
719 else if (cpathobj != NULL) {
720 PyInterpreterState *interp = PyThreadState_GET()->interp;
721 _Py_IDENTIFIER(_get_sourcefile);
722
723 if (interp == NULL) {
724 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
725 "no interpreter!");
726 }
727
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -0700728 pathobj = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannona6473f92012-07-13 13:57:03 -0400729 &PyId__get_sourcefile, cpathobj,
730 NULL);
731 if (pathobj == NULL)
732 PyErr_Clear();
733 }
734 else
735 pathobj = NULL;
736
Victor Stinner27ee0892011-03-04 12:57:09 +0000737 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
738error:
739 Py_DECREF(nameobj);
740 Py_XDECREF(pathobj);
741 Py_XDECREF(cpathobj);
742 return m;
743}
744
745PyObject*
746PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
747 PyObject *cpathname)
748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 PyObject *modules = PyImport_GetModuleDict();
750 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751
Victor Stinner27ee0892011-03-04 12:57:09 +0000752 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 if (m == NULL)
754 return NULL;
755 /* If the module is being reloaded, we get the old module back
756 and re-use its dict to exec the new code. */
757 d = PyModule_GetDict(m);
758 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
759 if (PyDict_SetItemString(d, "__builtins__",
760 PyEval_GetBuiltins()) != 0)
761 goto error;
762 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 if (pathname != NULL) {
Brett Cannona6473f92012-07-13 13:57:03 -0400764 v = pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 }
Brett Cannona6473f92012-07-13 13:57:03 -0400766 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 v = ((PyCodeObject *)co)->co_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 }
Brett Cannona6473f92012-07-13 13:57:03 -0400769 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (PyDict_SetItemString(d, "__file__", v) != 0)
771 PyErr_Clear(); /* Not important enough to report */
772 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000775 if (cpathname != NULL)
776 v = cpathname;
777 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 if (PyDict_SetItemString(d, "__cached__", v) != 0)
780 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000781
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000782 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 if (v == NULL)
784 goto error;
785 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000786
Victor Stinner27ee0892011-03-04 12:57:09 +0000787 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000789 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 name);
791 return NULL;
792 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000797
798 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 remove_module(name);
800 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801}
802
803
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000804static void
805update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 PyObject *constants, *tmp;
808 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 if (PyUnicode_Compare(co->co_filename, oldname))
811 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 tmp = co->co_filename;
814 co->co_filename = newname;
815 Py_INCREF(co->co_filename);
816 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 constants = co->co_consts;
819 n = PyTuple_GET_SIZE(constants);
820 for (i = 0; i < n; i++) {
821 tmp = PyTuple_GET_ITEM(constants, i);
822 if (PyCode_Check(tmp))
823 update_code_filenames((PyCodeObject *)tmp,
824 oldname, newname);
825 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000826}
827
Victor Stinner2f42ae52011-03-20 00:41:24 +0100828static void
829update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000830{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100831 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000832
Victor Stinner2f42ae52011-03-20 00:41:24 +0100833 if (PyUnicode_Compare(co->co_filename, newname) == 0)
834 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 oldname = co->co_filename;
837 Py_INCREF(oldname);
838 update_code_filenames(co, oldname, newname);
839 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000840}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000841
Brett Cannon442c9b92011-03-23 16:14:42 -0700842static PyObject *
843imp_fix_co_filename(PyObject *self, PyObject *args)
844{
845 PyObject *co;
846 PyObject *file_path;
847
848 if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
849 return NULL;
850
851 if (!PyCode_Check(co)) {
852 PyErr_SetString(PyExc_TypeError,
853 "first argument must be a code object");
854 return NULL;
855 }
856
857 if (!PyUnicode_Check(file_path)) {
858 PyErr_SetString(PyExc_TypeError,
859 "second argument must be a string");
860 return NULL;
861 }
862
863 update_compiled_module((PyCodeObject*)co, file_path);
864
865 Py_RETURN_NONE;
866}
867
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000868
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000869/* Forward */
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700870static const struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000871
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000872
873/* Helper to test for built-in module */
874
875static int
Victor Stinner95872862011-03-07 18:20:56 +0100876is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000877{
Victor Stinner95872862011-03-07 18:20:56 +0100878 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +0100880 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
881 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 if (PyImport_Inittab[i].initfunc == NULL)
883 return -1;
884 else
885 return 1;
886 }
887 }
888 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000889}
890
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000891
Just van Rossum52e14d62002-12-30 22:08:05 +0000892/* Return an importer object for a sys.path/pkg.__path__ item 'p',
893 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +0000894 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +0000895 that can handle the path item. Return None if no hook could;
896 this tells our caller it should fall back to the builtin
897 import mechanism. Cache the result in path_importer_cache.
898 Returns a borrowed reference. */
899
900static PyObject *
901get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +0000903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 PyObject *importer;
905 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +0000906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 /* These conditions are the caller's responsibility: */
908 assert(PyList_Check(path_hooks));
909 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 nhooks = PyList_Size(path_hooks);
912 if (nhooks < 0)
913 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 importer = PyDict_GetItem(path_importer_cache, p);
916 if (importer != NULL)
917 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 /* set path_importer_cache[p] to None to avoid recursion */
920 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
921 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 for (j = 0; j < nhooks; j++) {
924 PyObject *hook = PyList_GetItem(path_hooks, j);
925 if (hook == NULL)
926 return NULL;
927 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
928 if (importer != NULL)
929 break;
Just van Rossum52e14d62002-12-30 22:08:05 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
932 return NULL;
933 }
934 PyErr_Clear();
935 }
936 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -0400937 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 }
939 if (importer != NULL) {
940 int err = PyDict_SetItem(path_importer_cache, p, importer);
941 Py_DECREF(importer);
942 if (err != 0)
943 return NULL;
944 }
945 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000946}
947
Christian Heimes9cd17752007-11-18 19:35:23 +0000948PyAPI_FUNC(PyObject *)
949PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +0000951
Victor Stinner1e53bba2013-07-16 22:26:05 +0200952 path_importer_cache = PySys_GetObject("path_importer_cache");
953 path_hooks = PySys_GetObject("path_hooks");
954 if (path_importer_cache != NULL && path_hooks != NULL) {
955 importer = get_path_importer(path_importer_cache,
956 path_hooks, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 }
958 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
959 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000960}
961
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000962
Victor Stinner95872862011-03-07 18:20:56 +0100963static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000964
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000965/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000966 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000968
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000969static int
Victor Stinner95872862011-03-07 18:20:56 +0100970init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000973
Victor Stinner95872862011-03-07 18:20:56 +0100974 if (_PyImport_FindExtensionObject(name, name) != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 for (p = PyImport_Inittab; p->name != NULL; p++) {
978 PyObject *mod;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100979 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +0100980 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 if (p->initfunc == NULL) {
982 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +0100983 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 name);
985 return -1;
986 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 mod = (*p->initfunc)();
988 if (mod == 0)
989 return -1;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100990 /* Remember pointer to module init function. */
991 def = PyModule_GetDef(mod);
992 def->m_base.m_init = p->initfunc;
Victor Stinner95872862011-03-07 18:20:56 +0100993 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 return -1;
995 /* FixupExtension has put the module into sys.modules,
996 so we can release our own reference. */
997 Py_DECREF(mod);
998 return 1;
999 }
1000 }
1001 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001002}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001003
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001004
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001005/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001006
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001007static const struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +01001008find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001009{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001010 const struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001011
Victor Stinner53dc7352011-03-20 01:50:21 +01001012 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 for (p = PyImport_FrozenModules; ; p++) {
1016 if (p->name == NULL)
1017 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001018 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 break;
1020 }
1021 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001022}
1023
Guido van Rossum79f25d91997-04-29 20:08:16 +00001024static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001025get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001026{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001027 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (p == NULL) {
1031 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001032 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 name);
1034 return NULL;
1035 }
1036 if (p->code == NULL) {
1037 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001038 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 name);
1040 return NULL;
1041 }
1042 size = p->size;
1043 if (size < 0)
1044 size = -size;
1045 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001046}
1047
Brett Cannon8d110132009-03-15 02:20:16 +00001048static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001049is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001050{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001051 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (p == NULL) {
1055 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001056 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 name);
1058 return NULL;
1059 }
Brett Cannon8d110132009-03-15 02:20:16 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (size < 0)
1064 Py_RETURN_TRUE;
1065 else
1066 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001067}
1068
1069
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001070/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001071 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001072 an exception set if the initialization failed.
1073 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001074
1075int
Victor Stinner53dc7352011-03-20 01:50:21 +01001076PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001077{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001078 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001079 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 int ispackage;
1081 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001082
Victor Stinner53dc7352011-03-20 01:50:21 +01001083 p = find_frozen(name);
1084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 if (p == NULL)
1086 return 0;
1087 if (p->code == NULL) {
1088 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001089 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 name);
1091 return -1;
1092 }
1093 size = p->size;
1094 ispackage = (size < 0);
1095 if (ispackage)
1096 size = -size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1098 if (co == NULL)
1099 return -1;
1100 if (!PyCode_Check(co)) {
1101 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001102 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 name);
1104 goto err_return;
1105 }
1106 if (ispackage) {
Brett Cannon3e0651b2013-05-31 23:18:39 -04001107 /* Set __path__ to the empty list */
Victor Stinner53dc7352011-03-20 01:50:21 +01001108 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001110 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (m == NULL)
1112 goto err_return;
1113 d = PyModule_GetDict(m);
Brett Cannon3e0651b2013-05-31 23:18:39 -04001114 l = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 goto err_return;
1117 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 err = PyDict_SetItemString(d, "__path__", l);
1119 Py_DECREF(l);
1120 if (err != 0)
1121 goto err_return;
1122 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001123 path = PyUnicode_FromString("<frozen>");
1124 if (path == NULL)
1125 goto err_return;
1126 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1127 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 if (m == NULL)
1129 goto err_return;
1130 Py_DECREF(co);
1131 Py_DECREF(m);
1132 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001133err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 Py_DECREF(co);
1135 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001136}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001137
Victor Stinner53dc7352011-03-20 01:50:21 +01001138int
1139PyImport_ImportFrozenModule(char *name)
1140{
1141 PyObject *nameobj;
1142 int ret;
1143 nameobj = PyUnicode_InternFromString(name);
1144 if (nameobj == NULL)
1145 return -1;
1146 ret = PyImport_ImportFrozenModuleObject(nameobj);
1147 Py_DECREF(nameobj);
1148 return ret;
1149}
1150
Guido van Rossum74e6a111994-08-29 12:54:38 +00001151
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001152/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001153 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001154
Guido van Rossum79f25d91997-04-29 20:08:16 +00001155PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001156PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 PyObject *pname;
1159 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 pname = PyUnicode_FromString(name);
1162 if (pname == NULL)
1163 return NULL;
1164 result = PyImport_Import(pname);
1165 Py_DECREF(pname);
1166 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001167}
1168
Christian Heimes072c0f12008-01-03 23:01:04 +00001169/* Import a module without blocking
1170 *
1171 * At first it tries to fetch the module from sys.modules. If the module was
1172 * never loaded before it loads it with PyImport_ImportModule() unless another
1173 * thread holds the import lock. In the latter case the function raises an
1174 * ImportError instead of blocking.
1175 *
1176 * Returns the module object with incremented ref count.
1177 */
1178PyObject *
1179PyImport_ImportModuleNoBlock(const char *name)
1180{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001181 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001182}
1183
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001184
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001185/* Remove importlib frames from the traceback,
1186 * except in Verbose mode. */
1187static void
1188remove_importlib_frames(void)
1189{
1190 const char *importlib_filename = "<frozen importlib._bootstrap>";
Nick Coghlan42c07662012-07-31 21:14:18 +10001191 const char *remove_frames = "_call_with_frames_removed";
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001192 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001193 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001194 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001195 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001196
1197 /* Synopsis: if it's an ImportError, we trim all importlib chunks
Nick Coghlan42c07662012-07-31 21:14:18 +10001198 from the traceback. We always trim chunks
1199 which end with a call to "_call_with_frames_removed". */
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001200
1201 PyErr_Fetch(&exception, &value, &base_tb);
1202 if (!exception || Py_VerboseFlag)
1203 goto done;
1204 if (PyType_IsSubtype((PyTypeObject *) exception,
1205 (PyTypeObject *) PyExc_ImportError))
1206 always_trim = 1;
1207
1208 prev_link = &base_tb;
1209 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001210 while (tb != NULL) {
1211 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1212 PyObject *next = (PyObject *) traceback->tb_next;
1213 PyFrameObject *frame = traceback->tb_frame;
1214 PyCodeObject *code = frame->f_code;
1215 int now_in_importlib;
1216
1217 assert(PyTraceBack_Check(tb));
1218 now_in_importlib = (PyUnicode_CompareWithASCIIString(
1219 code->co_filename,
1220 importlib_filename) == 0);
1221 if (now_in_importlib && !in_importlib) {
1222 /* This is the link to this chunk of importlib tracebacks */
1223 outer_link = prev_link;
1224 }
1225 in_importlib = now_in_importlib;
1226
1227 if (in_importlib &&
1228 (always_trim ||
Nick Coghlan42c07662012-07-31 21:14:18 +10001229 PyUnicode_CompareWithASCIIString(code->co_name,
1230 remove_frames) == 0)) {
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001231 PyObject *tmp = *outer_link;
1232 *outer_link = next;
1233 Py_XINCREF(next);
1234 Py_DECREF(tmp);
1235 prev_link = outer_link;
1236 }
1237 else {
1238 prev_link = (PyObject **) &traceback->tb_next;
1239 }
1240 tb = next;
1241 }
1242done:
1243 PyErr_Restore(exception, value, base_tb);
1244}
1245
1246
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001247PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04001248PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1249 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001250 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001251{
Brett Cannonfd074152012-04-14 14:10:13 -04001252 _Py_IDENTIFIER(__import__);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001253 _Py_IDENTIFIER(__initializing__);
Brett Cannonfd074152012-04-14 14:10:13 -04001254 _Py_IDENTIFIER(__package__);
1255 _Py_IDENTIFIER(__path__);
1256 _Py_IDENTIFIER(__name__);
1257 _Py_IDENTIFIER(_find_and_load);
1258 _Py_IDENTIFIER(_handle_fromlist);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001259 _Py_IDENTIFIER(_lock_unlock_module);
Brett Cannonfd074152012-04-14 14:10:13 -04001260 _Py_static_string(single_dot, ".");
1261 PyObject *abs_name = NULL;
1262 PyObject *builtins_import = NULL;
1263 PyObject *final_mod = NULL;
1264 PyObject *mod = NULL;
1265 PyObject *package = NULL;
1266 PyObject *globals = NULL;
1267 PyObject *fromlist = NULL;
1268 PyInterpreterState *interp = PyThreadState_GET()->interp;
1269
1270 /* Make sure to use default values so as to not have
1271 PyObject_CallMethodObjArgs() truncate the parameter list because of a
1272 NULL argument. */
1273 if (given_globals == NULL) {
1274 globals = PyDict_New();
1275 if (globals == NULL) {
1276 goto error;
1277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 }
Brett Cannonfd074152012-04-14 14:10:13 -04001279 else {
1280 /* Only have to care what given_globals is if it will be used
Brett Cannonecfefb72012-08-05 19:24:57 -04001281 for something. */
Brett Cannonfd074152012-04-14 14:10:13 -04001282 if (level > 0 && !PyDict_Check(given_globals)) {
1283 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1284 goto error;
1285 }
1286 globals = given_globals;
1287 Py_INCREF(globals);
1288 }
1289
1290 if (given_fromlist == NULL) {
1291 fromlist = PyList_New(0);
1292 if (fromlist == NULL) {
1293 goto error;
1294 }
1295 }
1296 else {
1297 fromlist = given_fromlist;
1298 Py_INCREF(fromlist);
1299 }
1300 if (name == NULL) {
1301 PyErr_SetString(PyExc_ValueError, "Empty module name");
1302 goto error;
1303 }
1304
1305 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1306 for added performance. */
1307
1308 if (!PyUnicode_Check(name)) {
1309 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1310 goto error;
1311 }
1312 else if (PyUnicode_READY(name) < 0) {
1313 goto error;
1314 }
1315 if (level < 0) {
1316 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1317 goto error;
1318 }
1319 else if (level > 0) {
1320 package = _PyDict_GetItemId(globals, &PyId___package__);
1321 if (package != NULL && package != Py_None) {
1322 Py_INCREF(package);
1323 if (!PyUnicode_Check(package)) {
1324 PyErr_SetString(PyExc_TypeError, "package must be a string");
1325 goto error;
1326 }
1327 }
1328 else {
Brett Cannon273323c2012-04-17 19:05:11 -04001329 package = _PyDict_GetItemId(globals, &PyId___name__);
Brett Cannonfd074152012-04-14 14:10:13 -04001330 if (package == NULL) {
Brett Cannon273323c2012-04-17 19:05:11 -04001331 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
Brett Cannonfd074152012-04-14 14:10:13 -04001332 goto error;
1333 }
1334 else if (!PyUnicode_Check(package)) {
1335 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1336 }
1337 Py_INCREF(package);
1338
1339 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Brett Cannon740fce02012-04-14 14:23:49 -04001340 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001341 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1342 if (borrowed_dot == NULL) {
1343 goto error;
1344 }
Brett Cannon740fce02012-04-14 14:23:49 -04001345 partition = PyUnicode_RPartition(package, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001346 Py_DECREF(package);
1347 if (partition == NULL) {
1348 goto error;
1349 }
1350 package = PyTuple_GET_ITEM(partition, 0);
1351 Py_INCREF(package);
1352 Py_DECREF(partition);
1353 }
1354 }
1355
1356 if (PyDict_GetItem(interp->modules, package) == NULL) {
1357 PyErr_Format(PyExc_SystemError,
1358 "Parent module %R not loaded, cannot perform relative "
1359 "import", package);
1360 goto error;
1361 }
1362 }
1363 else { /* level == 0 */
1364 if (PyUnicode_GET_LENGTH(name) == 0) {
1365 PyErr_SetString(PyExc_ValueError, "Empty module name");
1366 goto error;
1367 }
1368 package = Py_None;
1369 Py_INCREF(package);
1370 }
1371
1372 if (level > 0) {
1373 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1374 PyObject *base = NULL;
1375 int level_up = 1;
1376
1377 for (level_up = 1; level_up < level; level_up += 1) {
1378 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1379 if (last_dot == -2) {
1380 goto error;
1381 }
1382 else if (last_dot == -1) {
1383 PyErr_SetString(PyExc_ValueError,
1384 "attempted relative import beyond top-level "
1385 "package");
1386 goto error;
1387 }
1388 }
1389 base = PyUnicode_Substring(package, 0, last_dot);
1390 if (PyUnicode_GET_LENGTH(name) > 0) {
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001391 PyObject *borrowed_dot, *seq = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001392
1393 borrowed_dot = _PyUnicode_FromId(&single_dot);
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001394 seq = PyTuple_Pack(2, base, name);
1395 Py_DECREF(base);
Brett Cannonfd074152012-04-14 14:10:13 -04001396 if (borrowed_dot == NULL || seq == NULL) {
1397 goto error;
1398 }
1399
1400 abs_name = PyUnicode_Join(borrowed_dot, seq);
1401 Py_DECREF(seq);
1402 if (abs_name == NULL) {
1403 goto error;
1404 }
1405 }
1406 else {
1407 abs_name = base;
1408 }
1409 }
1410 else {
1411 abs_name = name;
1412 Py_INCREF(abs_name);
1413 }
1414
Brian Curtine6b299f2012-04-14 14:19:33 -05001415#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001416 _PyImport_AcquireLock();
1417#endif
1418 /* From this point forward, goto error_with_unlock! */
1419 if (PyDict_Check(globals)) {
1420 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1421 }
1422 if (builtins_import == NULL) {
1423 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1424 if (builtins_import == NULL) {
Benjamin Peterson7d110042013-04-29 09:08:14 -04001425 PyErr_SetString(PyExc_ImportError, "__import__ not found");
1426 goto error_with_unlock;
Brett Cannonfd074152012-04-14 14:10:13 -04001427 }
1428 }
1429 Py_INCREF(builtins_import);
1430
1431 mod = PyDict_GetItem(interp->modules, abs_name);
1432 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001433 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1434 "None in sys.modules", abs_name);
1435 if (msg != NULL) {
Brett Cannon82da8882013-07-04 17:48:16 -04001436 PyErr_SetImportError(msg, abs_name, NULL);
Brian Curtin09b86d12012-04-17 16:57:09 -05001437 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001438 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001439 mod = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001440 goto error_with_unlock;
1441 }
1442 else if (mod != NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001443 PyObject *value;
1444 int initializing = 0;
1445
Brett Cannonfd074152012-04-14 14:10:13 -04001446 Py_INCREF(mod);
Antoine Pitrou03989852012-08-28 00:24:52 +02001447 /* Optimization: only call _bootstrap._lock_unlock_module() if
1448 __initializing__ is true.
1449 NOTE: because of this, __initializing__ must be set *before*
1450 stuffing the new module in sys.modules.
1451 */
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001452 value = _PyObject_GetAttrId(mod, &PyId___initializing__);
1453 if (value == NULL)
1454 PyErr_Clear();
1455 else {
1456 initializing = PyObject_IsTrue(value);
1457 Py_DECREF(value);
1458 if (initializing == -1)
1459 PyErr_Clear();
1460 }
1461 if (initializing > 0) {
1462 /* _bootstrap._lock_unlock_module() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001463 value = _PyObject_CallMethodIdObjArgs(interp->importlib,
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001464 &PyId__lock_unlock_module, abs_name,
1465 NULL);
1466 if (value == NULL)
1467 goto error;
1468 Py_DECREF(value);
1469 }
1470 else {
1471#ifdef WITH_THREAD
1472 if (_PyImport_ReleaseLock() < 0) {
1473 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1474 goto error;
1475 }
1476#endif
1477 }
Brett Cannonfd074152012-04-14 14:10:13 -04001478 }
1479 else {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001480 /* _bootstrap._find_and_load() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001481 mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001482 &PyId__find_and_load, abs_name,
1483 builtins_import, NULL);
1484 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001485 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001486 }
1487 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001488 /* From now on we don't hold the import lock anymore. */
Brett Cannonfd074152012-04-14 14:10:13 -04001489
1490 if (PyObject_Not(fromlist)) {
1491 if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1492 PyObject *front = NULL;
Brian Curtine6b299f2012-04-14 14:19:33 -05001493 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001494 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1495
1496 if (borrowed_dot == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001497 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001498 }
1499
Brian Curtine6b299f2012-04-14 14:19:33 -05001500 partition = PyUnicode_Partition(name, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001501 if (partition == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001502 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001503 }
1504
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001505 if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1506 /* No dot in module name, simple exit */
1507 Py_DECREF(partition);
1508 final_mod = mod;
1509 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001510 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001511 }
1512
Brett Cannonfd074152012-04-14 14:10:13 -04001513 front = PyTuple_GET_ITEM(partition, 0);
1514 Py_INCREF(front);
1515 Py_DECREF(partition);
1516
1517 if (level == 0) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001518 final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001519 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001520 }
1521 else {
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001522 Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1523 PyUnicode_GET_LENGTH(front);
1524 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001525 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001526 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001527 Py_DECREF(front);
1528 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001529 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001530 }
Brett Cannonfd074152012-04-14 14:10:13 -04001531
1532 final_mod = PyDict_GetItem(interp->modules, to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001533 if (final_mod == NULL) {
1534 PyErr_Format(PyExc_KeyError,
1535 "%R not in sys.modules as expected",
1536 to_return);
1537 }
1538 else {
1539 Py_INCREF(final_mod);
1540 }
Antoine Pitroub78174c2012-05-06 17:15:23 +02001541 Py_DECREF(to_return);
Brett Cannonfd074152012-04-14 14:10:13 -04001542 }
1543 }
1544 else {
1545 final_mod = mod;
1546 Py_INCREF(mod);
1547 }
1548 }
1549 else {
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001550 final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001551 &PyId__handle_fromlist, mod,
1552 fromlist, builtins_import,
1553 NULL);
1554 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001555 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001556
Brett Cannonfd074152012-04-14 14:10:13 -04001557 error_with_unlock:
Brian Curtine6b299f2012-04-14 14:19:33 -05001558#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001559 if (_PyImport_ReleaseLock() < 0) {
1560 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1561 }
1562#endif
1563 error:
1564 Py_XDECREF(abs_name);
1565 Py_XDECREF(builtins_import);
1566 Py_XDECREF(mod);
1567 Py_XDECREF(package);
1568 Py_XDECREF(globals);
1569 Py_XDECREF(fromlist);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001570 if (final_mod == NULL)
1571 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001572 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001573}
1574
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001575PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001576PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001577 PyObject *fromlist, int level)
1578{
1579 PyObject *nameobj, *mod;
1580 nameobj = PyUnicode_FromString(name);
1581 if (nameobj == NULL)
1582 return NULL;
1583 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1584 fromlist, level);
1585 Py_DECREF(nameobj);
1586 return mod;
1587}
1588
1589
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001590/* Re-import a module of any kind and return its module object, WITH
1591 INCREMENTED REFERENCE COUNT */
1592
Guido van Rossum79f25d91997-04-29 20:08:16 +00001593PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001594PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001595{
Brett Cannon62228db2012-04-29 14:38:11 -04001596 _Py_IDENTIFIER(reload);
1597 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001599 PyObject *imp = PyDict_GetItemString(modules, "imp");
1600 if (imp == NULL) {
1601 imp = PyImport_ImportModule("imp");
1602 if (imp == NULL) {
1603 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 }
Brett Cannon62228db2012-04-29 14:38:11 -04001606 else {
1607 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001609
Brett Cannon62228db2012-04-29 14:38:11 -04001610 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1611 Py_DECREF(imp);
1612 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001613}
1614
1615
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001616/* Higher-level import emulator which emulates the "import" statement
1617 more accurately -- it invokes the __import__() function from the
1618 builtins of the current globals. This means that the import is
1619 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001620 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001621 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001622 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001623 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001624
1625PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001626PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 static PyObject *silly_list = NULL;
1629 static PyObject *builtins_str = NULL;
1630 static PyObject *import_str = NULL;
1631 PyObject *globals = NULL;
1632 PyObject *import = NULL;
1633 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001634 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 /* Initialize constant string objects */
1638 if (silly_list == NULL) {
1639 import_str = PyUnicode_InternFromString("__import__");
1640 if (import_str == NULL)
1641 return NULL;
1642 builtins_str = PyUnicode_InternFromString("__builtins__");
1643 if (builtins_str == NULL)
1644 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001645 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 if (silly_list == NULL)
1647 return NULL;
1648 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 /* Get the builtins from current globals */
1651 globals = PyEval_GetGlobals();
1652 if (globals != NULL) {
1653 Py_INCREF(globals);
1654 builtins = PyObject_GetItem(globals, builtins_str);
1655 if (builtins == NULL)
1656 goto err;
1657 }
1658 else {
1659 /* No globals -- use standard builtins, and fake globals */
1660 builtins = PyImport_ImportModuleLevel("builtins",
1661 NULL, NULL, NULL, 0);
1662 if (builtins == NULL)
1663 return NULL;
1664 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1665 if (globals == NULL)
1666 goto err;
1667 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 /* Get the __import__ function from the builtins */
1670 if (PyDict_Check(builtins)) {
1671 import = PyObject_GetItem(builtins, import_str);
1672 if (import == NULL)
1673 PyErr_SetObject(PyExc_KeyError, import_str);
1674 }
1675 else
1676 import = PyObject_GetAttr(builtins, import_str);
1677 if (import == NULL)
1678 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001681 Always use absolute import here.
1682 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1684 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001685 if (r == NULL)
1686 goto err;
1687 Py_DECREF(r);
1688
1689 modules = PyImport_GetModuleDict();
1690 r = PyDict_GetItem(modules, module_name);
1691 if (r != NULL)
1692 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001693
1694 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 Py_XDECREF(globals);
1696 Py_XDECREF(builtins);
1697 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001700}
1701
Barry Warsaw28a691b2010-04-17 00:19:56 +00001702static PyObject *
Brett Cannon2657df42012-05-04 15:20:40 -04001703imp_extension_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001706 const char *suffix;
1707 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 list = PyList_New(0);
1710 if (list == NULL)
1711 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001712#ifdef HAVE_DYNAMIC_LOADING
1713 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1714 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (item == NULL) {
1716 Py_DECREF(list);
1717 return NULL;
1718 }
1719 if (PyList_Append(list, item) < 0) {
1720 Py_DECREF(list);
1721 Py_DECREF(item);
1722 return NULL;
1723 }
1724 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001725 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 }
Brett Cannon2657df42012-05-04 15:20:40 -04001727#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001729}
1730
Guido van Rossum79f25d91997-04-29 20:08:16 +00001731static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001732imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001733{
Victor Stinner95872862011-03-07 18:20:56 +01001734 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 int ret;
1736 PyObject *m;
Victor Stinner95872862011-03-07 18:20:56 +01001737 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 return NULL;
1739 ret = init_builtin(name);
1740 if (ret < 0)
1741 return NULL;
1742 if (ret == 0) {
1743 Py_INCREF(Py_None);
1744 return Py_None;
1745 }
Victor Stinner95872862011-03-07 18:20:56 +01001746 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 Py_XINCREF(m);
1748 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001749}
1750
Guido van Rossum79f25d91997-04-29 20:08:16 +00001751static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001752imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001753{
Victor Stinner53dc7352011-03-20 01:50:21 +01001754 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 int ret;
1756 PyObject *m;
Victor Stinner53dc7352011-03-20 01:50:21 +01001757 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001759 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 if (ret < 0)
1761 return NULL;
1762 if (ret == 0) {
1763 Py_INCREF(Py_None);
1764 return Py_None;
1765 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001766 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 Py_XINCREF(m);
1768 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001769}
1770
Guido van Rossum79f25d91997-04-29 20:08:16 +00001771static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001772imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001773{
Victor Stinner53dc7352011-03-20 01:50:21 +01001774 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00001775
Victor Stinner53dc7352011-03-20 01:50:21 +01001776 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 return NULL;
1778 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001779}
1780
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00001782imp_is_frozen_package(PyObject *self, PyObject *args)
1783{
Victor Stinner53dc7352011-03-20 01:50:21 +01001784 PyObject *name;
Brett Cannon8d110132009-03-15 02:20:16 +00001785
Victor Stinner53dc7352011-03-20 01:50:21 +01001786 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 return NULL;
1788 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00001789}
1790
1791static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001792imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001793{
Victor Stinner95872862011-03-07 18:20:56 +01001794 PyObject *name;
1795 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 return NULL;
1797 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001798}
1799
Guido van Rossum79f25d91997-04-29 20:08:16 +00001800static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001801imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001802{
Victor Stinner53dc7352011-03-20 01:50:21 +01001803 PyObject *name;
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001804 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001805 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 return NULL;
1807 p = find_frozen(name);
1808 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001809}
1810
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001811#ifdef HAVE_DYNAMIC_LOADING
1812
Guido van Rossum79f25d91997-04-29 20:08:16 +00001813static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001814imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001815{
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001816 PyObject *name, *pathname, *fob = NULL, *mod;
1817 FILE *fp;
1818
1819 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
1820 &name, PyUnicode_FSDecoder, &pathname, &fob))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 return NULL;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001822 if (fob != NULL) {
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001823 fp = _Py_fopen(pathname, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001824 if (fp == NULL) {
1825 Py_DECREF(pathname);
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001826 if (!PyErr_Occurred())
1827 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001829 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001831 else
1832 fp = NULL;
1833 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001834 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 if (fp)
1836 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001837 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001838}
1839
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001840#endif /* HAVE_DYNAMIC_LOADING */
1841
Barry Warsaw28a691b2010-04-17 00:19:56 +00001842
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001843/* Doc strings */
1844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04001846"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001847
Brett Cannon2657df42012-05-04 15:20:40 -04001848PyDoc_STRVAR(doc_extension_suffixes,
1849"extension_suffixes() -> list of strings\n\
1850Returns the list of file suffixes used to identify extension modules.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001851
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001852PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00001853"lock_held() -> boolean\n\
1854Return True if the import lock is currently held, else False.\n\
1855On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00001856
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001857PyDoc_STRVAR(doc_acquire_lock,
1858"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001859Acquires the interpreter's import lock for the current thread.\n\
1860This lock should be used by import hooks to ensure thread-safety\n\
1861when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001862On platforms without threads, this function does nothing.");
1863
1864PyDoc_STRVAR(doc_release_lock,
1865"release_lock() -> None\n\
1866Release the interpreter's import lock.\n\
1867On platforms without threads, this function does nothing.");
1868
Guido van Rossum79f25d91997-04-29 20:08:16 +00001869static PyMethodDef imp_methods[] = {
Brett Cannon2657df42012-05-04 15:20:40 -04001870 {"extension_suffixes", imp_extension_suffixes, METH_NOARGS,
1871 doc_extension_suffixes},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
1873 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
1874 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
1876 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
1877 {"init_builtin", imp_init_builtin, METH_VARARGS},
1878 {"init_frozen", imp_init_frozen, METH_VARARGS},
1879 {"is_builtin", imp_is_builtin, METH_VARARGS},
1880 {"is_frozen", imp_is_frozen, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001881#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001883#endif
Brett Cannon442c9b92011-03-23 16:14:42 -07001884 {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001886};
1887
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001888
Martin v. Löwis1a214512008-06-11 05:26:20 +00001889static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04001891 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 doc_imp,
1893 0,
1894 imp_methods,
1895 NULL,
1896 NULL,
1897 NULL,
1898 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001899};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001900
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001901PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001902PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 m = PyModule_Create(&impmodule);
1907 if (m == NULL)
1908 goto failure;
1909 d = PyModule_GetDict(m);
1910 if (d == NULL)
1911 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001914 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 Py_XDECREF(m);
1916 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001917}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001918
1919
Guido van Rossumb18618d2000-05-03 23:44:39 +00001920/* API for embedding applications that want to add their own entries
1921 to the table of built-in modules. This should normally be called
1922 *before* Py_Initialize(). When the table resize fails, -1 is
1923 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001924
1925 After a similar function by Just van Rossum. */
1926
1927int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001928PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 static struct _inittab *our_copy = NULL;
1931 struct _inittab *p;
1932 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 /* Count the number of entries in both tables */
1935 for (n = 0; newtab[n].name != NULL; n++)
1936 ;
1937 if (n == 0)
1938 return 0; /* Nothing to do */
1939 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
1940 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 /* Allocate new memory for the combined table */
1943 p = our_copy;
1944 PyMem_RESIZE(p, struct _inittab, i+n+1);
1945 if (p == NULL)
1946 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 /* Copy the tables into the new memory */
1949 if (our_copy != PyImport_Inittab)
1950 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
1951 PyImport_Inittab = our_copy = p;
1952 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001955}
1956
1957/* Shorthand to add a single entry given a name and a function */
1958
1959int
Brett Cannona826f322009-04-02 03:41:46 +00001960PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 newtab[0].name = (char *)name;
1967 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001970}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001971
1972#ifdef __cplusplus
1973}
1974#endif