blob: cd4fb788a10ad98ef6504f4ad846c6e017265c87 [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
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000298
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000299/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000300
Guido van Rossum3f5da241990-12-20 15:06:42 +0000301void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000302PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 Py_ssize_t pos, ndone;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 PyObject *key, *value, *dict;
306 PyInterpreterState *interp = PyThreadState_GET()->interp;
307 PyObject *modules = interp->modules;
Guido van Rossum758eec01998-01-19 21:58:26 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (modules == NULL)
310 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 /* Delete some special variables first. These are common
313 places where user values hide and people complain when their
314 destructors fail. Since the modules containing them are
315 deleted *last* of all, they would come too late in the normal
316 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 value = PyDict_GetItemString(modules, "builtins");
319 if (value != NULL && PyModule_Check(value)) {
320 dict = PyModule_GetDict(value);
321 if (Py_VerboseFlag)
322 PySys_WriteStderr("# clear builtins._\n");
323 PyDict_SetItemString(dict, "_", Py_None);
324 }
325 value = PyDict_GetItemString(modules, "sys");
326 if (value != NULL && PyModule_Check(value)) {
327 char **p;
328 PyObject *v;
329 dict = PyModule_GetDict(value);
330 for (p = sys_deletes; *p != NULL; p++) {
331 if (Py_VerboseFlag)
332 PySys_WriteStderr("# clear sys.%s\n", *p);
333 PyDict_SetItemString(dict, *p, Py_None);
334 }
335 for (p = sys_files; *p != NULL; p+=2) {
336 if (Py_VerboseFlag)
337 PySys_WriteStderr("# restore sys.%s\n", *p);
338 v = PyDict_GetItemString(dict, *(p+1));
339 if (v == NULL)
340 v = Py_None;
341 PyDict_SetItemString(dict, *p, v);
342 }
343 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 /* First, delete __main__ */
346 value = PyDict_GetItemString(modules, "__main__");
347 if (value != NULL && PyModule_Check(value)) {
348 if (Py_VerboseFlag)
349 PySys_WriteStderr("# cleanup __main__\n");
350 _PyModule_Clear(value);
351 PyDict_SetItemString(modules, "__main__", Py_None);
352 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 /* The special treatment of "builtins" here is because even
355 when it's not referenced as a module, its dictionary is
356 referenced by almost every module's __builtins__. Since
357 deleting a module clears its dictionary (even if there are
358 references left to it), we need to delete the "builtins"
359 module last. Likewise, we don't delete sys until the very
360 end because it is implicitly referenced (e.g. by print).
Guido van Rossum758eec01998-01-19 21:58:26 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 Also note that we 'delete' modules by replacing their entry
363 in the modules dict with None, rather than really deleting
364 them; this avoids a rehash of the modules dictionary and
365 also marks them as "non existent" so they won't be
366 re-imported. */
Guido van Rossum758eec01998-01-19 21:58:26 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* Next, repeatedly delete modules with a reference count of
369 one (skipping builtins and sys) and delete them */
370 do {
371 ndone = 0;
372 pos = 0;
373 while (PyDict_Next(modules, &pos, &key, &value)) {
374 if (value->ob_refcnt != 1)
375 continue;
376 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Victor Stinner9464d612011-03-07 17:08:21 +0100377 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 continue;
Victor Stinner9464d612011-03-07 17:08:21 +0100379 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 continue;
381 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100382 PySys_FormatStderr(
383 "# cleanup[1] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 _PyModule_Clear(value);
385 PyDict_SetItem(modules, key, Py_None);
386 ndone++;
387 }
388 }
389 } while (ndone > 0);
Guido van Rossum758eec01998-01-19 21:58:26 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 /* Next, delete all modules (still skipping builtins and sys) */
392 pos = 0;
393 while (PyDict_Next(modules, &pos, &key, &value)) {
394 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Victor Stinner9464d612011-03-07 17:08:21 +0100395 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 continue;
Victor Stinner9464d612011-03-07 17:08:21 +0100397 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 continue;
399 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100400 PySys_FormatStderr("# cleanup[2] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 _PyModule_Clear(value);
402 PyDict_SetItem(modules, key, Py_None);
403 }
404 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000405
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200406 /* Collect garbage remaining after deleting the modules. Mostly
407 reference cycles created by classes. */
408 PyGC_Collect();
409
410 /* Dump GC stats before it's too late, since it uses the warnings
411 machinery. */
412 _PyGC_DumpShutdownStats();
413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 /* Next, delete sys and builtins (in that order) */
415 value = PyDict_GetItemString(modules, "sys");
416 if (value != NULL && PyModule_Check(value)) {
417 if (Py_VerboseFlag)
418 PySys_WriteStderr("# cleanup sys\n");
419 _PyModule_Clear(value);
420 PyDict_SetItemString(modules, "sys", Py_None);
421 }
422 value = PyDict_GetItemString(modules, "builtins");
423 if (value != NULL && PyModule_Check(value)) {
424 if (Py_VerboseFlag)
425 PySys_WriteStderr("# cleanup builtins\n");
426 _PyModule_Clear(value);
427 PyDict_SetItemString(modules, "builtins", Py_None);
428 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* Finally, clear and delete the modules directory */
431 PyDict_Clear(modules);
432 interp->modules = NULL;
433 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000434}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000435
436
Barry Warsaw28a691b2010-04-17 00:19:56 +0000437/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000438
439long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000440PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000441{
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200442 long res;
Brett Cannon77b2abd2012-07-09 16:09:00 -0400443 PyInterpreterState *interp = PyThreadState_Get()->interp;
444 PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
445 "_RAW_MAGIC_NUMBER");
446 if (pyc_magic == NULL)
447 return -1;
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200448 res = PyLong_AsLong(pyc_magic);
Benjamin Peterson66f36592012-07-09 22:21:55 -0700449 Py_DECREF(pyc_magic);
450 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451}
452
453
Brett Cannon3adc7b72012-07-09 14:22:12 -0400454extern const char * _PySys_ImplCacheTag;
455
Barry Warsaw28a691b2010-04-17 00:19:56 +0000456const char *
457PyImport_GetMagicTag(void)
458{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400459 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000460}
461
Brett Cannon98979b82012-07-02 15:13:11 -0400462
Guido van Rossum25ce5661997-08-02 03:10:38 +0000463/* Magic for extension modules (built-in as well as dynamically
464 loaded). To prevent initializing an extension module more than
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200465 once, we keep a static dictionary 'extensions' keyed by the tuple
466 (module name, module name) (for built-in modules) or by
467 (filename, module name) (for dynamically loaded modules), containing these
468 modules. A copy of the module's dictionary is stored by calling
469 _PyImport_FixupExtensionObject() immediately after the module initialization
470 function succeeds. A copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100471 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000473 Modules which do support multiple initialization set their m_size
474 field to a non-negative number (indicating the size of the
475 module-specific state). They are still recorded in the extensions
476 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000477*/
478
479int
Victor Stinner95872862011-03-07 18:20:56 +0100480_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
481 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500483 PyObject *modules, *dict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 struct PyModuleDef *def;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500485 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if (extensions == NULL) {
487 extensions = PyDict_New();
488 if (extensions == NULL)
489 return -1;
490 }
491 if (mod == NULL || !PyModule_Check(mod)) {
492 PyErr_BadInternalCall();
493 return -1;
494 }
495 def = PyModule_GetDef(mod);
496 if (!def) {
497 PyErr_BadInternalCall();
498 return -1;
499 }
500 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100501 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 return -1;
503 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100504 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 return -1;
506 }
507 if (def->m_size == -1) {
508 if (def->m_base.m_copy) {
509 /* Somebody already imported the module,
510 likely under a different name.
511 XXX this should really not happen. */
512 Py_DECREF(def->m_base.m_copy);
513 def->m_base.m_copy = NULL;
514 }
515 dict = PyModule_GetDict(mod);
516 if (dict == NULL)
517 return -1;
518 def->m_base.m_copy = PyDict_Copy(dict);
519 if (def->m_base.m_copy == NULL)
520 return -1;
521 }
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500522 key = PyTuple_Pack(2, filename, name);
523 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200524 return -1;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500525 res = PyDict_SetItem(extensions, key, (PyObject *)def);
526 Py_DECREF(key);
527 if (res < 0)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200528 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000530}
531
Victor Stinner49d3f252010-10-17 01:24:53 +0000532int
533_PyImport_FixupBuiltin(PyObject *mod, char *name)
534{
535 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100536 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100537 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100538 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000539 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100540 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
541 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000542 return res;
543}
544
Guido van Rossum25ce5661997-08-02 03:10:38 +0000545PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100546_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500548 PyObject *mod, *mdict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyModuleDef* def;
550 if (extensions == NULL)
551 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500552 key = PyTuple_Pack(2, filename, name);
553 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200554 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500555 def = (PyModuleDef *)PyDict_GetItem(extensions, key);
556 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 if (def == NULL)
558 return NULL;
559 if (def->m_size == -1) {
560 /* Module does not support repeated initialization */
561 if (def->m_base.m_copy == NULL)
562 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100563 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 if (mod == NULL)
565 return NULL;
566 mdict = PyModule_GetDict(mod);
567 if (mdict == NULL)
568 return NULL;
569 if (PyDict_Update(mdict, def->m_base.m_copy))
570 return NULL;
571 }
572 else {
573 if (def->m_base.m_init == NULL)
574 return NULL;
575 mod = def->m_base.m_init();
576 if (mod == NULL)
577 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100578 PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 Py_DECREF(mod);
580 }
581 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100582 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 Py_DECREF(mod);
584 return NULL;
585 }
586 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100587 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 name, filename);
589 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000590
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000591}
592
Victor Stinner49d3f252010-10-17 01:24:53 +0000593PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000594_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000595{
Victor Stinner95872862011-03-07 18:20:56 +0100596 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100597 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100598 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000599 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100600 res = _PyImport_FindExtensionObject(nameobj, nameobj);
601 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000602 return res;
603}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604
605/* Get the module object corresponding to a module name.
606 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000607 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000608 Because the former action is most common, THIS DOES NOT RETURN A
609 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000612PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 PyObject *modules = PyImport_GetModuleDict();
615 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000616
Victor Stinner27ee0892011-03-04 12:57:09 +0000617 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 PyModule_Check(m))
619 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000620 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (m == NULL)
622 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000623 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 Py_DECREF(m);
625 return NULL;
626 }
627 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000630}
631
Victor Stinner27ee0892011-03-04 12:57:09 +0000632PyObject *
633PyImport_AddModule(const char *name)
634{
635 PyObject *nameobj, *module;
636 nameobj = PyUnicode_FromString(name);
637 if (nameobj == NULL)
638 return NULL;
639 module = PyImport_AddModuleObject(nameobj);
640 Py_DECREF(nameobj);
641 return module;
642}
643
644
Tim Peters1cd70172004-08-02 03:52:12 +0000645/* Remove name from sys.modules, if it's there. */
646static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000647remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000650 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000652 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 Py_FatalError("import: deleting existing key in"
654 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000655}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000656
Christian Heimes3b06e532008-01-07 20:12:44 +0000657
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000658/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000659 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
660 * removed from sys.modules, to avoid leaving damaged module objects
661 * in sys.modules. The caller may wish to restore the original
662 * module object (if any) in this case; PyImport_ReloadModule is an
663 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000664 *
665 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
666 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000667 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000668PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000669PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 return PyImport_ExecCodeModuleWithPathnames(
672 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000673}
674
675PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000676PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 return PyImport_ExecCodeModuleWithPathnames(
679 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000680}
681
682PyObject *
683PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000685{
Victor Stinner27ee0892011-03-04 12:57:09 +0000686 PyObject *m = NULL;
687 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
688
689 nameobj = PyUnicode_FromString(name);
690 if (nameobj == NULL)
691 return NULL;
692
Victor Stinner27ee0892011-03-04 12:57:09 +0000693 if (cpathname != NULL) {
694 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
695 if (cpathobj == NULL)
696 goto error;
Brett Cannona6473f92012-07-13 13:57:03 -0400697 }
698 else
Victor Stinner27ee0892011-03-04 12:57:09 +0000699 cpathobj = NULL;
Brett Cannona6473f92012-07-13 13:57:03 -0400700
701 if (pathname != NULL) {
702 pathobj = PyUnicode_DecodeFSDefault(pathname);
703 if (pathobj == NULL)
704 goto error;
705 }
706 else if (cpathobj != NULL) {
707 PyInterpreterState *interp = PyThreadState_GET()->interp;
708 _Py_IDENTIFIER(_get_sourcefile);
709
710 if (interp == NULL) {
711 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
712 "no interpreter!");
713 }
714
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -0700715 pathobj = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannona6473f92012-07-13 13:57:03 -0400716 &PyId__get_sourcefile, cpathobj,
717 NULL);
718 if (pathobj == NULL)
719 PyErr_Clear();
720 }
721 else
722 pathobj = NULL;
723
Victor Stinner27ee0892011-03-04 12:57:09 +0000724 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
725error:
726 Py_DECREF(nameobj);
727 Py_XDECREF(pathobj);
728 Py_XDECREF(cpathobj);
729 return m;
730}
731
732PyObject*
733PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
734 PyObject *cpathname)
735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 PyObject *modules = PyImport_GetModuleDict();
737 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738
Victor Stinner27ee0892011-03-04 12:57:09 +0000739 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 if (m == NULL)
741 return NULL;
742 /* If the module is being reloaded, we get the old module back
743 and re-use its dict to exec the new code. */
744 d = PyModule_GetDict(m);
745 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
746 if (PyDict_SetItemString(d, "__builtins__",
747 PyEval_GetBuiltins()) != 0)
748 goto error;
749 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 if (pathname != NULL) {
Brett Cannona6473f92012-07-13 13:57:03 -0400751 v = pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 }
Brett Cannona6473f92012-07-13 13:57:03 -0400753 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 v = ((PyCodeObject *)co)->co_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 }
Brett Cannona6473f92012-07-13 13:57:03 -0400756 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (PyDict_SetItemString(d, "__file__", v) != 0)
758 PyErr_Clear(); /* Not important enough to report */
759 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000762 if (cpathname != NULL)
763 v = cpathname;
764 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 if (PyDict_SetItemString(d, "__cached__", v) != 0)
767 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000768
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000769 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (v == NULL)
771 goto error;
772 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000773
Victor Stinner27ee0892011-03-04 12:57:09 +0000774 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000776 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 name);
778 return NULL;
779 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000784
785 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 remove_module(name);
787 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000788}
789
790
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000791static void
792update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 PyObject *constants, *tmp;
795 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 if (PyUnicode_Compare(co->co_filename, oldname))
798 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 tmp = co->co_filename;
801 co->co_filename = newname;
802 Py_INCREF(co->co_filename);
803 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 constants = co->co_consts;
806 n = PyTuple_GET_SIZE(constants);
807 for (i = 0; i < n; i++) {
808 tmp = PyTuple_GET_ITEM(constants, i);
809 if (PyCode_Check(tmp))
810 update_code_filenames((PyCodeObject *)tmp,
811 oldname, newname);
812 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000813}
814
Victor Stinner2f42ae52011-03-20 00:41:24 +0100815static void
816update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000817{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100818 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000819
Victor Stinner2f42ae52011-03-20 00:41:24 +0100820 if (PyUnicode_Compare(co->co_filename, newname) == 0)
821 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 oldname = co->co_filename;
824 Py_INCREF(oldname);
825 update_code_filenames(co, oldname, newname);
826 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000827}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000828
Brett Cannon442c9b92011-03-23 16:14:42 -0700829static PyObject *
830imp_fix_co_filename(PyObject *self, PyObject *args)
831{
832 PyObject *co;
833 PyObject *file_path;
834
835 if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
836 return NULL;
837
838 if (!PyCode_Check(co)) {
839 PyErr_SetString(PyExc_TypeError,
840 "first argument must be a code object");
841 return NULL;
842 }
843
844 if (!PyUnicode_Check(file_path)) {
845 PyErr_SetString(PyExc_TypeError,
846 "second argument must be a string");
847 return NULL;
848 }
849
850 update_compiled_module((PyCodeObject*)co, file_path);
851
852 Py_RETURN_NONE;
853}
854
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000855
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000856/* Forward */
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700857static const struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000858
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000859
860/* Helper to test for built-in module */
861
862static int
Victor Stinner95872862011-03-07 18:20:56 +0100863is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000864{
Victor Stinner95872862011-03-07 18:20:56 +0100865 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +0100867 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
868 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (PyImport_Inittab[i].initfunc == NULL)
870 return -1;
871 else
872 return 1;
873 }
874 }
875 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000876}
877
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000878
Just van Rossum52e14d62002-12-30 22:08:05 +0000879/* Return an importer object for a sys.path/pkg.__path__ item 'p',
880 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +0000881 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +0000882 that can handle the path item. Return None if no hook could;
883 this tells our caller it should fall back to the builtin
884 import mechanism. Cache the result in path_importer_cache.
885 Returns a borrowed reference. */
886
887static PyObject *
888get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +0000890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 PyObject *importer;
892 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 /* These conditions are the caller's responsibility: */
895 assert(PyList_Check(path_hooks));
896 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 nhooks = PyList_Size(path_hooks);
899 if (nhooks < 0)
900 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 importer = PyDict_GetItem(path_importer_cache, p);
903 if (importer != NULL)
904 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* set path_importer_cache[p] to None to avoid recursion */
907 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
908 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 for (j = 0; j < nhooks; j++) {
911 PyObject *hook = PyList_GetItem(path_hooks, j);
912 if (hook == NULL)
913 return NULL;
914 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
915 if (importer != NULL)
916 break;
Just van Rossum52e14d62002-12-30 22:08:05 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
919 return NULL;
920 }
921 PyErr_Clear();
922 }
923 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -0400924 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 }
926 if (importer != NULL) {
927 int err = PyDict_SetItem(path_importer_cache, p, importer);
928 Py_DECREF(importer);
929 if (err != 0)
930 return NULL;
931 }
932 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000933}
934
Christian Heimes9cd17752007-11-18 19:35:23 +0000935PyAPI_FUNC(PyObject *)
936PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
940 if ((path_hooks = PySys_GetObject("path_hooks"))) {
941 importer = get_path_importer(path_importer_cache,
942 path_hooks, path);
943 }
944 }
945 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
946 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000947}
948
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000949
Victor Stinner95872862011-03-07 18:20:56 +0100950static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000951
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000952/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000953 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000954 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000955
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000956static int
Victor Stinner95872862011-03-07 18:20:56 +0100957init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000960
Victor Stinner95872862011-03-07 18:20:56 +0100961 if (_PyImport_FindExtensionObject(name, name) != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 for (p = PyImport_Inittab; p->name != NULL; p++) {
965 PyObject *mod;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100966 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +0100967 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 if (p->initfunc == NULL) {
969 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +0100970 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 name);
972 return -1;
973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 mod = (*p->initfunc)();
975 if (mod == 0)
976 return -1;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100977 /* Remember pointer to module init function. */
978 def = PyModule_GetDef(mod);
979 def->m_base.m_init = p->initfunc;
Victor Stinner95872862011-03-07 18:20:56 +0100980 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 return -1;
982 /* FixupExtension has put the module into sys.modules,
983 so we can release our own reference. */
984 Py_DECREF(mod);
985 return 1;
986 }
987 }
988 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000989}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000990
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000991
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000992/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000993
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700994static const struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +0100995find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000996{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700997 const struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000998
Victor Stinner53dc7352011-03-20 01:50:21 +0100999 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 for (p = PyImport_FrozenModules; ; p++) {
1003 if (p->name == NULL)
1004 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001005 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 break;
1007 }
1008 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001009}
1010
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001012get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001013{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001014 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 if (p == NULL) {
1018 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001019 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 name);
1021 return NULL;
1022 }
1023 if (p->code == NULL) {
1024 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001025 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 name);
1027 return NULL;
1028 }
1029 size = p->size;
1030 if (size < 0)
1031 size = -size;
1032 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001033}
1034
Brett Cannon8d110132009-03-15 02:20:16 +00001035static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001036is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001037{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001038 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (p == NULL) {
1042 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001043 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 name);
1045 return NULL;
1046 }
Brett Cannon8d110132009-03-15 02:20:16 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (size < 0)
1051 Py_RETURN_TRUE;
1052 else
1053 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001054}
1055
1056
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001057/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001058 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001059 an exception set if the initialization failed.
1060 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001061
1062int
Victor Stinner53dc7352011-03-20 01:50:21 +01001063PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001064{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001065 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001066 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 int ispackage;
1068 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001069
Victor Stinner53dc7352011-03-20 01:50:21 +01001070 p = find_frozen(name);
1071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (p == NULL)
1073 return 0;
1074 if (p->code == NULL) {
1075 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001076 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 name);
1078 return -1;
1079 }
1080 size = p->size;
1081 ispackage = (size < 0);
1082 if (ispackage)
1083 size = -size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1085 if (co == NULL)
1086 return -1;
1087 if (!PyCode_Check(co)) {
1088 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001089 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 name);
1091 goto err_return;
1092 }
1093 if (ispackage) {
1094 /* Set __path__ to the package name */
Victor Stinner53dc7352011-03-20 01:50:21 +01001095 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001097 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (m == NULL)
1099 goto err_return;
1100 d = PyModule_GetDict(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 l = PyList_New(1);
1102 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 goto err_return;
1104 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001105 Py_INCREF(name);
1106 PyList_SET_ITEM(l, 0, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 err = PyDict_SetItemString(d, "__path__", l);
1108 Py_DECREF(l);
1109 if (err != 0)
1110 goto err_return;
1111 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001112 path = PyUnicode_FromString("<frozen>");
1113 if (path == NULL)
1114 goto err_return;
1115 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1116 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 if (m == NULL)
1118 goto err_return;
1119 Py_DECREF(co);
1120 Py_DECREF(m);
1121 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001122err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 Py_DECREF(co);
1124 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001125}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001126
Victor Stinner53dc7352011-03-20 01:50:21 +01001127int
1128PyImport_ImportFrozenModule(char *name)
1129{
1130 PyObject *nameobj;
1131 int ret;
1132 nameobj = PyUnicode_InternFromString(name);
1133 if (nameobj == NULL)
1134 return -1;
1135 ret = PyImport_ImportFrozenModuleObject(nameobj);
1136 Py_DECREF(nameobj);
1137 return ret;
1138}
1139
Guido van Rossum74e6a111994-08-29 12:54:38 +00001140
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001141/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001142 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001143
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001145PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 PyObject *pname;
1148 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 pname = PyUnicode_FromString(name);
1151 if (pname == NULL)
1152 return NULL;
1153 result = PyImport_Import(pname);
1154 Py_DECREF(pname);
1155 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001156}
1157
Christian Heimes072c0f12008-01-03 23:01:04 +00001158/* Import a module without blocking
1159 *
1160 * At first it tries to fetch the module from sys.modules. If the module was
1161 * never loaded before it loads it with PyImport_ImportModule() unless another
1162 * thread holds the import lock. In the latter case the function raises an
1163 * ImportError instead of blocking.
1164 *
1165 * Returns the module object with incremented ref count.
1166 */
1167PyObject *
1168PyImport_ImportModuleNoBlock(const char *name)
1169{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001170 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001171}
1172
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001173
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001174/* Remove importlib frames from the traceback,
1175 * except in Verbose mode. */
1176static void
1177remove_importlib_frames(void)
1178{
1179 const char *importlib_filename = "<frozen importlib._bootstrap>";
Nick Coghlan42c07662012-07-31 21:14:18 +10001180 const char *remove_frames = "_call_with_frames_removed";
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001181 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001182 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001183 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001184 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001185
1186 /* Synopsis: if it's an ImportError, we trim all importlib chunks
Nick Coghlan42c07662012-07-31 21:14:18 +10001187 from the traceback. We always trim chunks
1188 which end with a call to "_call_with_frames_removed". */
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001189
1190 PyErr_Fetch(&exception, &value, &base_tb);
1191 if (!exception || Py_VerboseFlag)
1192 goto done;
1193 if (PyType_IsSubtype((PyTypeObject *) exception,
1194 (PyTypeObject *) PyExc_ImportError))
1195 always_trim = 1;
1196
1197 prev_link = &base_tb;
1198 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001199 while (tb != NULL) {
1200 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1201 PyObject *next = (PyObject *) traceback->tb_next;
1202 PyFrameObject *frame = traceback->tb_frame;
1203 PyCodeObject *code = frame->f_code;
1204 int now_in_importlib;
1205
1206 assert(PyTraceBack_Check(tb));
1207 now_in_importlib = (PyUnicode_CompareWithASCIIString(
1208 code->co_filename,
1209 importlib_filename) == 0);
1210 if (now_in_importlib && !in_importlib) {
1211 /* This is the link to this chunk of importlib tracebacks */
1212 outer_link = prev_link;
1213 }
1214 in_importlib = now_in_importlib;
1215
1216 if (in_importlib &&
1217 (always_trim ||
Nick Coghlan42c07662012-07-31 21:14:18 +10001218 PyUnicode_CompareWithASCIIString(code->co_name,
1219 remove_frames) == 0)) {
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001220 PyObject *tmp = *outer_link;
1221 *outer_link = next;
1222 Py_XINCREF(next);
1223 Py_DECREF(tmp);
1224 prev_link = outer_link;
1225 }
1226 else {
1227 prev_link = (PyObject **) &traceback->tb_next;
1228 }
1229 tb = next;
1230 }
1231done:
1232 PyErr_Restore(exception, value, base_tb);
1233}
1234
1235
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001236PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04001237PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1238 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001239 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001240{
Brett Cannonfd074152012-04-14 14:10:13 -04001241 _Py_IDENTIFIER(__import__);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001242 _Py_IDENTIFIER(__initializing__);
Brett Cannonfd074152012-04-14 14:10:13 -04001243 _Py_IDENTIFIER(__package__);
1244 _Py_IDENTIFIER(__path__);
1245 _Py_IDENTIFIER(__name__);
1246 _Py_IDENTIFIER(_find_and_load);
1247 _Py_IDENTIFIER(_handle_fromlist);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001248 _Py_IDENTIFIER(_lock_unlock_module);
Brett Cannonfd074152012-04-14 14:10:13 -04001249 _Py_static_string(single_dot, ".");
1250 PyObject *abs_name = NULL;
1251 PyObject *builtins_import = NULL;
1252 PyObject *final_mod = NULL;
1253 PyObject *mod = NULL;
1254 PyObject *package = NULL;
1255 PyObject *globals = NULL;
1256 PyObject *fromlist = NULL;
1257 PyInterpreterState *interp = PyThreadState_GET()->interp;
1258
1259 /* Make sure to use default values so as to not have
1260 PyObject_CallMethodObjArgs() truncate the parameter list because of a
1261 NULL argument. */
1262 if (given_globals == NULL) {
1263 globals = PyDict_New();
1264 if (globals == NULL) {
1265 goto error;
1266 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 }
Brett Cannonfd074152012-04-14 14:10:13 -04001268 else {
1269 /* Only have to care what given_globals is if it will be used
Brett Cannonecfefb72012-08-05 19:24:57 -04001270 for something. */
Brett Cannonfd074152012-04-14 14:10:13 -04001271 if (level > 0 && !PyDict_Check(given_globals)) {
1272 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1273 goto error;
1274 }
1275 globals = given_globals;
1276 Py_INCREF(globals);
1277 }
1278
1279 if (given_fromlist == NULL) {
1280 fromlist = PyList_New(0);
1281 if (fromlist == NULL) {
1282 goto error;
1283 }
1284 }
1285 else {
1286 fromlist = given_fromlist;
1287 Py_INCREF(fromlist);
1288 }
1289 if (name == NULL) {
1290 PyErr_SetString(PyExc_ValueError, "Empty module name");
1291 goto error;
1292 }
1293
1294 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1295 for added performance. */
1296
1297 if (!PyUnicode_Check(name)) {
1298 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1299 goto error;
1300 }
1301 else if (PyUnicode_READY(name) < 0) {
1302 goto error;
1303 }
1304 if (level < 0) {
1305 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1306 goto error;
1307 }
1308 else if (level > 0) {
1309 package = _PyDict_GetItemId(globals, &PyId___package__);
1310 if (package != NULL && package != Py_None) {
1311 Py_INCREF(package);
1312 if (!PyUnicode_Check(package)) {
1313 PyErr_SetString(PyExc_TypeError, "package must be a string");
1314 goto error;
1315 }
1316 }
1317 else {
Brett Cannon273323c2012-04-17 19:05:11 -04001318 package = _PyDict_GetItemId(globals, &PyId___name__);
Brett Cannonfd074152012-04-14 14:10:13 -04001319 if (package == NULL) {
Brett Cannon273323c2012-04-17 19:05:11 -04001320 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
Brett Cannonfd074152012-04-14 14:10:13 -04001321 goto error;
1322 }
1323 else if (!PyUnicode_Check(package)) {
1324 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1325 }
1326 Py_INCREF(package);
1327
1328 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Brett Cannon740fce02012-04-14 14:23:49 -04001329 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001330 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1331 if (borrowed_dot == NULL) {
1332 goto error;
1333 }
Brett Cannon740fce02012-04-14 14:23:49 -04001334 partition = PyUnicode_RPartition(package, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001335 Py_DECREF(package);
1336 if (partition == NULL) {
1337 goto error;
1338 }
1339 package = PyTuple_GET_ITEM(partition, 0);
1340 Py_INCREF(package);
1341 Py_DECREF(partition);
1342 }
1343 }
1344
1345 if (PyDict_GetItem(interp->modules, package) == NULL) {
1346 PyErr_Format(PyExc_SystemError,
1347 "Parent module %R not loaded, cannot perform relative "
1348 "import", package);
1349 goto error;
1350 }
1351 }
1352 else { /* level == 0 */
1353 if (PyUnicode_GET_LENGTH(name) == 0) {
1354 PyErr_SetString(PyExc_ValueError, "Empty module name");
1355 goto error;
1356 }
1357 package = Py_None;
1358 Py_INCREF(package);
1359 }
1360
1361 if (level > 0) {
1362 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1363 PyObject *base = NULL;
1364 int level_up = 1;
1365
1366 for (level_up = 1; level_up < level; level_up += 1) {
1367 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1368 if (last_dot == -2) {
1369 goto error;
1370 }
1371 else if (last_dot == -1) {
1372 PyErr_SetString(PyExc_ValueError,
1373 "attempted relative import beyond top-level "
1374 "package");
1375 goto error;
1376 }
1377 }
1378 base = PyUnicode_Substring(package, 0, last_dot);
1379 if (PyUnicode_GET_LENGTH(name) > 0) {
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001380 PyObject *borrowed_dot, *seq = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001381
1382 borrowed_dot = _PyUnicode_FromId(&single_dot);
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001383 seq = PyTuple_Pack(2, base, name);
1384 Py_DECREF(base);
Brett Cannonfd074152012-04-14 14:10:13 -04001385 if (borrowed_dot == NULL || seq == NULL) {
1386 goto error;
1387 }
1388
1389 abs_name = PyUnicode_Join(borrowed_dot, seq);
1390 Py_DECREF(seq);
1391 if (abs_name == NULL) {
1392 goto error;
1393 }
1394 }
1395 else {
1396 abs_name = base;
1397 }
1398 }
1399 else {
1400 abs_name = name;
1401 Py_INCREF(abs_name);
1402 }
1403
Brian Curtine6b299f2012-04-14 14:19:33 -05001404#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001405 _PyImport_AcquireLock();
1406#endif
1407 /* From this point forward, goto error_with_unlock! */
1408 if (PyDict_Check(globals)) {
1409 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1410 }
1411 if (builtins_import == NULL) {
1412 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1413 if (builtins_import == NULL) {
Benjamin Peterson7d110042013-04-29 09:08:14 -04001414 PyErr_SetString(PyExc_ImportError, "__import__ not found");
1415 goto error_with_unlock;
Brett Cannonfd074152012-04-14 14:10:13 -04001416 }
1417 }
1418 Py_INCREF(builtins_import);
1419
1420 mod = PyDict_GetItem(interp->modules, abs_name);
1421 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001422 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1423 "None in sys.modules", abs_name);
1424 if (msg != NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -05001425 PyErr_SetImportError(msg, abs_name, NULL);
1426 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001427 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001428 mod = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001429 goto error_with_unlock;
1430 }
1431 else if (mod != NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001432 PyObject *value;
1433 int initializing = 0;
1434
Brett Cannonfd074152012-04-14 14:10:13 -04001435 Py_INCREF(mod);
Antoine Pitrou03989852012-08-28 00:24:52 +02001436 /* Optimization: only call _bootstrap._lock_unlock_module() if
1437 __initializing__ is true.
1438 NOTE: because of this, __initializing__ must be set *before*
1439 stuffing the new module in sys.modules.
1440 */
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001441 value = _PyObject_GetAttrId(mod, &PyId___initializing__);
1442 if (value == NULL)
1443 PyErr_Clear();
1444 else {
1445 initializing = PyObject_IsTrue(value);
1446 Py_DECREF(value);
1447 if (initializing == -1)
1448 PyErr_Clear();
1449 }
1450 if (initializing > 0) {
1451 /* _bootstrap._lock_unlock_module() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001452 value = _PyObject_CallMethodIdObjArgs(interp->importlib,
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001453 &PyId__lock_unlock_module, abs_name,
1454 NULL);
1455 if (value == NULL)
1456 goto error;
1457 Py_DECREF(value);
1458 }
1459 else {
1460#ifdef WITH_THREAD
1461 if (_PyImport_ReleaseLock() < 0) {
1462 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1463 goto error;
1464 }
1465#endif
1466 }
Brett Cannonfd074152012-04-14 14:10:13 -04001467 }
1468 else {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001469 /* _bootstrap._find_and_load() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001470 mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001471 &PyId__find_and_load, abs_name,
1472 builtins_import, NULL);
1473 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001474 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001475 }
1476 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001477 /* From now on we don't hold the import lock anymore. */
Brett Cannonfd074152012-04-14 14:10:13 -04001478
1479 if (PyObject_Not(fromlist)) {
1480 if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1481 PyObject *front = NULL;
Brian Curtine6b299f2012-04-14 14:19:33 -05001482 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001483 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1484
1485 if (borrowed_dot == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001486 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001487 }
1488
Brian Curtine6b299f2012-04-14 14:19:33 -05001489 partition = PyUnicode_Partition(name, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001490 if (partition == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001491 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001492 }
1493
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001494 if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1495 /* No dot in module name, simple exit */
1496 Py_DECREF(partition);
1497 final_mod = mod;
1498 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001499 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001500 }
1501
Brett Cannonfd074152012-04-14 14:10:13 -04001502 front = PyTuple_GET_ITEM(partition, 0);
1503 Py_INCREF(front);
1504 Py_DECREF(partition);
1505
1506 if (level == 0) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001507 final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001508 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001509 }
1510 else {
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001511 Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1512 PyUnicode_GET_LENGTH(front);
1513 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001514 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001515 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001516 Py_DECREF(front);
1517 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001518 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001519 }
Brett Cannonfd074152012-04-14 14:10:13 -04001520
1521 final_mod = PyDict_GetItem(interp->modules, to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001522 if (final_mod == NULL) {
1523 PyErr_Format(PyExc_KeyError,
1524 "%R not in sys.modules as expected",
1525 to_return);
1526 }
1527 else {
1528 Py_INCREF(final_mod);
1529 }
Antoine Pitroub78174c2012-05-06 17:15:23 +02001530 Py_DECREF(to_return);
Brett Cannonfd074152012-04-14 14:10:13 -04001531 }
1532 }
1533 else {
1534 final_mod = mod;
1535 Py_INCREF(mod);
1536 }
1537 }
1538 else {
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001539 final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001540 &PyId__handle_fromlist, mod,
1541 fromlist, builtins_import,
1542 NULL);
1543 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001544 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001545
Brett Cannonfd074152012-04-14 14:10:13 -04001546 error_with_unlock:
Brian Curtine6b299f2012-04-14 14:19:33 -05001547#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001548 if (_PyImport_ReleaseLock() < 0) {
1549 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1550 }
1551#endif
1552 error:
1553 Py_XDECREF(abs_name);
1554 Py_XDECREF(builtins_import);
1555 Py_XDECREF(mod);
1556 Py_XDECREF(package);
1557 Py_XDECREF(globals);
1558 Py_XDECREF(fromlist);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001559 if (final_mod == NULL)
1560 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001561 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001562}
1563
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001564PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001565PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001566 PyObject *fromlist, int level)
1567{
1568 PyObject *nameobj, *mod;
1569 nameobj = PyUnicode_FromString(name);
1570 if (nameobj == NULL)
1571 return NULL;
1572 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1573 fromlist, level);
1574 Py_DECREF(nameobj);
1575 return mod;
1576}
1577
1578
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001579/* Re-import a module of any kind and return its module object, WITH
1580 INCREMENTED REFERENCE COUNT */
1581
Guido van Rossum79f25d91997-04-29 20:08:16 +00001582PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001583PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001584{
Brett Cannon62228db2012-04-29 14:38:11 -04001585 _Py_IDENTIFIER(reload);
1586 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001588 PyObject *imp = PyDict_GetItemString(modules, "imp");
1589 if (imp == NULL) {
1590 imp = PyImport_ImportModule("imp");
1591 if (imp == NULL) {
1592 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 }
Brett Cannon62228db2012-04-29 14:38:11 -04001595 else {
1596 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001598
Brett Cannon62228db2012-04-29 14:38:11 -04001599 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1600 Py_DECREF(imp);
1601 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001602}
1603
1604
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001605/* Higher-level import emulator which emulates the "import" statement
1606 more accurately -- it invokes the __import__() function from the
1607 builtins of the current globals. This means that the import is
1608 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001609 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001610 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001611 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001612 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001613
1614PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001615PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 static PyObject *silly_list = NULL;
1618 static PyObject *builtins_str = NULL;
1619 static PyObject *import_str = NULL;
1620 PyObject *globals = NULL;
1621 PyObject *import = NULL;
1622 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001623 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 /* Initialize constant string objects */
1627 if (silly_list == NULL) {
1628 import_str = PyUnicode_InternFromString("__import__");
1629 if (import_str == NULL)
1630 return NULL;
1631 builtins_str = PyUnicode_InternFromString("__builtins__");
1632 if (builtins_str == NULL)
1633 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001634 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 if (silly_list == NULL)
1636 return NULL;
1637 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 /* Get the builtins from current globals */
1640 globals = PyEval_GetGlobals();
1641 if (globals != NULL) {
1642 Py_INCREF(globals);
1643 builtins = PyObject_GetItem(globals, builtins_str);
1644 if (builtins == NULL)
1645 goto err;
1646 }
1647 else {
1648 /* No globals -- use standard builtins, and fake globals */
1649 builtins = PyImport_ImportModuleLevel("builtins",
1650 NULL, NULL, NULL, 0);
1651 if (builtins == NULL)
1652 return NULL;
1653 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1654 if (globals == NULL)
1655 goto err;
1656 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 /* Get the __import__ function from the builtins */
1659 if (PyDict_Check(builtins)) {
1660 import = PyObject_GetItem(builtins, import_str);
1661 if (import == NULL)
1662 PyErr_SetObject(PyExc_KeyError, import_str);
1663 }
1664 else
1665 import = PyObject_GetAttr(builtins, import_str);
1666 if (import == NULL)
1667 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001670 Always use absolute import here.
1671 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1673 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001674 if (r == NULL)
1675 goto err;
1676 Py_DECREF(r);
1677
1678 modules = PyImport_GetModuleDict();
1679 r = PyDict_GetItem(modules, module_name);
1680 if (r != NULL)
1681 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001682
1683 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 Py_XDECREF(globals);
1685 Py_XDECREF(builtins);
1686 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001689}
1690
Barry Warsaw28a691b2010-04-17 00:19:56 +00001691static PyObject *
Brett Cannon2657df42012-05-04 15:20:40 -04001692imp_extension_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001695 const char *suffix;
1696 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 list = PyList_New(0);
1699 if (list == NULL)
1700 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001701#ifdef HAVE_DYNAMIC_LOADING
1702 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1703 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 if (item == NULL) {
1705 Py_DECREF(list);
1706 return NULL;
1707 }
1708 if (PyList_Append(list, item) < 0) {
1709 Py_DECREF(list);
1710 Py_DECREF(item);
1711 return NULL;
1712 }
1713 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001714 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 }
Brett Cannon2657df42012-05-04 15:20:40 -04001716#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001718}
1719
Guido van Rossum79f25d91997-04-29 20:08:16 +00001720static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001721imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001722{
Victor Stinner95872862011-03-07 18:20:56 +01001723 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 int ret;
1725 PyObject *m;
Victor Stinner95872862011-03-07 18:20:56 +01001726 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 return NULL;
1728 ret = init_builtin(name);
1729 if (ret < 0)
1730 return NULL;
1731 if (ret == 0) {
1732 Py_INCREF(Py_None);
1733 return Py_None;
1734 }
Victor Stinner95872862011-03-07 18:20:56 +01001735 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 Py_XINCREF(m);
1737 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001738}
1739
Guido van Rossum79f25d91997-04-29 20:08:16 +00001740static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001741imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001742{
Victor Stinner53dc7352011-03-20 01:50:21 +01001743 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 int ret;
1745 PyObject *m;
Victor Stinner53dc7352011-03-20 01:50:21 +01001746 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001748 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 if (ret < 0)
1750 return NULL;
1751 if (ret == 0) {
1752 Py_INCREF(Py_None);
1753 return Py_None;
1754 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001755 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 Py_XINCREF(m);
1757 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001758}
1759
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001761imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001762{
Victor Stinner53dc7352011-03-20 01:50:21 +01001763 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00001764
Victor Stinner53dc7352011-03-20 01:50:21 +01001765 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 return NULL;
1767 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001768}
1769
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00001771imp_is_frozen_package(PyObject *self, PyObject *args)
1772{
Victor Stinner53dc7352011-03-20 01:50:21 +01001773 PyObject *name;
Brett Cannon8d110132009-03-15 02:20:16 +00001774
Victor Stinner53dc7352011-03-20 01:50:21 +01001775 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 return NULL;
1777 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00001778}
1779
1780static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001781imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001782{
Victor Stinner95872862011-03-07 18:20:56 +01001783 PyObject *name;
1784 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 return NULL;
1786 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001787}
1788
Guido van Rossum79f25d91997-04-29 20:08:16 +00001789static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001790imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001791{
Victor Stinner53dc7352011-03-20 01:50:21 +01001792 PyObject *name;
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001793 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001794 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 return NULL;
1796 p = find_frozen(name);
1797 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001798}
1799
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001800#ifdef HAVE_DYNAMIC_LOADING
1801
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001803imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001804{
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001805 PyObject *name, *pathname, *fob = NULL, *mod;
1806 FILE *fp;
1807
1808 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
1809 &name, PyUnicode_FSDecoder, &pathname, &fob))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 return NULL;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001811 if (fob != NULL) {
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001812 fp = _Py_fopen(pathname, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001813 if (fp == NULL) {
1814 Py_DECREF(pathname);
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001815 if (!PyErr_Occurred())
1816 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001818 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001820 else
1821 fp = NULL;
1822 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001823 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 if (fp)
1825 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001826 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001827}
1828
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001829#endif /* HAVE_DYNAMIC_LOADING */
1830
Barry Warsaw28a691b2010-04-17 00:19:56 +00001831
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001832/* Doc strings */
1833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001834PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04001835"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001836
Brett Cannon2657df42012-05-04 15:20:40 -04001837PyDoc_STRVAR(doc_extension_suffixes,
1838"extension_suffixes() -> list of strings\n\
1839Returns the list of file suffixes used to identify extension modules.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001840
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00001842"lock_held() -> boolean\n\
1843Return True if the import lock is currently held, else False.\n\
1844On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00001845
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001846PyDoc_STRVAR(doc_acquire_lock,
1847"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001848Acquires the interpreter's import lock for the current thread.\n\
1849This lock should be used by import hooks to ensure thread-safety\n\
1850when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001851On platforms without threads, this function does nothing.");
1852
1853PyDoc_STRVAR(doc_release_lock,
1854"release_lock() -> None\n\
1855Release the interpreter's import lock.\n\
1856On platforms without threads, this function does nothing.");
1857
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858static PyMethodDef imp_methods[] = {
Brett Cannon2657df42012-05-04 15:20:40 -04001859 {"extension_suffixes", imp_extension_suffixes, METH_NOARGS,
1860 doc_extension_suffixes},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
1862 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
1863 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
1865 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
1866 {"init_builtin", imp_init_builtin, METH_VARARGS},
1867 {"init_frozen", imp_init_frozen, METH_VARARGS},
1868 {"is_builtin", imp_is_builtin, METH_VARARGS},
1869 {"is_frozen", imp_is_frozen, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001870#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001872#endif
Brett Cannon442c9b92011-03-23 16:14:42 -07001873 {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001875};
1876
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001877
Martin v. Löwis1a214512008-06-11 05:26:20 +00001878static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04001880 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 doc_imp,
1882 0,
1883 imp_methods,
1884 NULL,
1885 NULL,
1886 NULL,
1887 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001888};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001889
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001890PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001891PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 m = PyModule_Create(&impmodule);
1896 if (m == NULL)
1897 goto failure;
1898 d = PyModule_GetDict(m);
1899 if (d == NULL)
1900 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001903 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 Py_XDECREF(m);
1905 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001906}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001907
1908
Guido van Rossumb18618d2000-05-03 23:44:39 +00001909/* API for embedding applications that want to add their own entries
1910 to the table of built-in modules. This should normally be called
1911 *before* Py_Initialize(). When the table resize fails, -1 is
1912 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001913
1914 After a similar function by Just van Rossum. */
1915
1916int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001917PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 static struct _inittab *our_copy = NULL;
1920 struct _inittab *p;
1921 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 /* Count the number of entries in both tables */
1924 for (n = 0; newtab[n].name != NULL; n++)
1925 ;
1926 if (n == 0)
1927 return 0; /* Nothing to do */
1928 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
1929 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 /* Allocate new memory for the combined table */
1932 p = our_copy;
1933 PyMem_RESIZE(p, struct _inittab, i+n+1);
1934 if (p == NULL)
1935 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 /* Copy the tables into the new memory */
1938 if (our_copy != PyImport_Inittab)
1939 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
1940 PyImport_Inittab = our_copy = p;
1941 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001944}
1945
1946/* Shorthand to add a single entry given a name and a function */
1947
1948int
Brett Cannona826f322009-04-02 03:41:46 +00001949PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 newtab[0].name = (char *)name;
1956 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001959}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001960
1961#ifdef __cplusplus
1962}
1963#endif