blob: 2f71b97bc8b099d56e9ab071da1610d3345355be [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 }
172 import_lock_thread = me;
173 import_lock_level = 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000174}
175
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000176int
177_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 long me = PyThread_get_thread_ident();
180 if (me == -1 || import_lock == NULL)
181 return 0; /* Too bad */
182 if (import_lock_thread != me)
183 return -1;
184 import_lock_level--;
185 if (import_lock_level == 0) {
186 import_lock_thread = -1;
187 PyThread_release_lock(import_lock);
188 }
189 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000190}
191
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000192/* This function is called from PyOS_AfterFork to ensure that newly
193 created child processes do not share locks with the parent.
194 We now acquire the import lock around fork() calls but on some platforms
195 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000196
197void
198_PyImport_ReInitLock(void)
199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (import_lock != NULL)
201 import_lock = PyThread_allocate_lock();
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000202 if (import_lock_level > 1) {
203 /* Forked as a side effect of import */
204 long me = PyThread_get_thread_ident();
205 PyThread_acquire_lock(import_lock, 0);
Victor Stinner942003c2011-03-07 16:57:48 +0100206 /* XXX: can the previous line fail? */
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000207 import_lock_thread = me;
208 import_lock_level--;
209 } else {
210 import_lock_thread = -1;
211 import_lock_level = 0;
212 }
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000213}
214
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000215#endif
216
Tim Peters69232342001-08-30 05:16:13 +0000217static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000218imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000219{
Tim Peters69232342001-08-30 05:16:13 +0000220#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000222#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000224#endif
225}
226
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000227static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000228imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000229{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000230#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000232#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 Py_INCREF(Py_None);
234 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000235}
236
237static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000238imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000239{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000240#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 if (_PyImport_ReleaseLock() < 0) {
242 PyErr_SetString(PyExc_RuntimeError,
243 "not holding the import lock");
244 return NULL;
245 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000246#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 Py_INCREF(Py_None);
248 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000249}
250
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100251void
252_PyImport_Fini(void)
253{
254 Py_XDECREF(extensions);
255 extensions = NULL;
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100256#ifdef WITH_THREAD
257 if (import_lock != NULL) {
258 PyThread_free_lock(import_lock);
259 import_lock = NULL;
260 }
261#endif
262}
263
Guido van Rossum25ce5661997-08-02 03:10:38 +0000264/* Helper for sys */
265
266PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000267PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 PyInterpreterState *interp = PyThreadState_GET()->interp;
270 if (interp->modules == NULL)
271 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
272 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000273}
274
Guido van Rossum3f5da241990-12-20 15:06:42 +0000275
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000276/* List of names to clear in sys */
277static char* sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 "path", "argv", "ps1", "ps2",
279 "last_type", "last_value", "last_traceback",
280 "path_hooks", "path_importer_cache", "meta_path",
281 /* misc stuff */
282 "flags", "float_info",
283 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000284};
285
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000286static char* sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 "stdin", "__stdin__",
288 "stdout", "__stdout__",
289 "stderr", "__stderr__",
290 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000291};
292
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000293
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000294/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000295
Guido van Rossum3f5da241990-12-20 15:06:42 +0000296void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000297PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_ssize_t pos, ndone;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 PyObject *key, *value, *dict;
301 PyInterpreterState *interp = PyThreadState_GET()->interp;
302 PyObject *modules = interp->modules;
Guido van Rossum758eec01998-01-19 21:58:26 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 if (modules == NULL)
305 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 /* Delete some special variables first. These are common
308 places where user values hide and people complain when their
309 destructors fail. Since the modules containing them are
310 deleted *last* of all, they would come too late in the normal
311 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 value = PyDict_GetItemString(modules, "builtins");
314 if (value != NULL && PyModule_Check(value)) {
315 dict = PyModule_GetDict(value);
316 if (Py_VerboseFlag)
317 PySys_WriteStderr("# clear builtins._\n");
318 PyDict_SetItemString(dict, "_", Py_None);
319 }
320 value = PyDict_GetItemString(modules, "sys");
321 if (value != NULL && PyModule_Check(value)) {
322 char **p;
323 PyObject *v;
324 dict = PyModule_GetDict(value);
325 for (p = sys_deletes; *p != NULL; p++) {
326 if (Py_VerboseFlag)
327 PySys_WriteStderr("# clear sys.%s\n", *p);
328 PyDict_SetItemString(dict, *p, Py_None);
329 }
330 for (p = sys_files; *p != NULL; p+=2) {
331 if (Py_VerboseFlag)
332 PySys_WriteStderr("# restore sys.%s\n", *p);
333 v = PyDict_GetItemString(dict, *(p+1));
334 if (v == NULL)
335 v = Py_None;
336 PyDict_SetItemString(dict, *p, v);
337 }
338 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 /* First, delete __main__ */
341 value = PyDict_GetItemString(modules, "__main__");
342 if (value != NULL && PyModule_Check(value)) {
343 if (Py_VerboseFlag)
344 PySys_WriteStderr("# cleanup __main__\n");
345 _PyModule_Clear(value);
346 PyDict_SetItemString(modules, "__main__", Py_None);
347 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 /* The special treatment of "builtins" here is because even
350 when it's not referenced as a module, its dictionary is
351 referenced by almost every module's __builtins__. Since
352 deleting a module clears its dictionary (even if there are
353 references left to it), we need to delete the "builtins"
354 module last. Likewise, we don't delete sys until the very
355 end because it is implicitly referenced (e.g. by print).
Guido van Rossum758eec01998-01-19 21:58:26 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 Also note that we 'delete' modules by replacing their entry
358 in the modules dict with None, rather than really deleting
359 them; this avoids a rehash of the modules dictionary and
360 also marks them as "non existent" so they won't be
361 re-imported. */
Guido van Rossum758eec01998-01-19 21:58:26 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 /* Next, repeatedly delete modules with a reference count of
364 one (skipping builtins and sys) and delete them */
365 do {
366 ndone = 0;
367 pos = 0;
368 while (PyDict_Next(modules, &pos, &key, &value)) {
369 if (value->ob_refcnt != 1)
370 continue;
371 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Victor Stinner9464d612011-03-07 17:08:21 +0100372 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 continue;
Victor Stinner9464d612011-03-07 17:08:21 +0100374 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 continue;
376 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100377 PySys_FormatStderr(
378 "# cleanup[1] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 _PyModule_Clear(value);
380 PyDict_SetItem(modules, key, Py_None);
381 ndone++;
382 }
383 }
384 } while (ndone > 0);
Guido van Rossum758eec01998-01-19 21:58:26 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 /* Next, delete all modules (still skipping builtins and sys) */
387 pos = 0;
388 while (PyDict_Next(modules, &pos, &key, &value)) {
389 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Victor Stinner9464d612011-03-07 17:08:21 +0100390 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 continue;
Victor Stinner9464d612011-03-07 17:08:21 +0100392 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 continue;
394 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100395 PySys_FormatStderr("# cleanup[2] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 _PyModule_Clear(value);
397 PyDict_SetItem(modules, key, Py_None);
398 }
399 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 /* Next, delete sys and builtins (in that order) */
402 value = PyDict_GetItemString(modules, "sys");
403 if (value != NULL && PyModule_Check(value)) {
404 if (Py_VerboseFlag)
405 PySys_WriteStderr("# cleanup sys\n");
406 _PyModule_Clear(value);
407 PyDict_SetItemString(modules, "sys", Py_None);
408 }
409 value = PyDict_GetItemString(modules, "builtins");
410 if (value != NULL && PyModule_Check(value)) {
411 if (Py_VerboseFlag)
412 PySys_WriteStderr("# cleanup builtins\n");
413 _PyModule_Clear(value);
414 PyDict_SetItemString(modules, "builtins", Py_None);
415 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* Finally, clear and delete the modules directory */
418 PyDict_Clear(modules);
419 interp->modules = NULL;
420 Py_DECREF(modules);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000421}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000422
423
Barry Warsaw28a691b2010-04-17 00:19:56 +0000424/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000425
426long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000427PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000428{
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200429 long res;
Brett Cannon77b2abd2012-07-09 16:09:00 -0400430 PyInterpreterState *interp = PyThreadState_Get()->interp;
431 PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
432 "_RAW_MAGIC_NUMBER");
433 if (pyc_magic == NULL)
434 return -1;
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200435 res = PyLong_AsLong(pyc_magic);
Benjamin Peterson66f36592012-07-09 22:21:55 -0700436 Py_DECREF(pyc_magic);
437 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000438}
439
440
Brett Cannon3adc7b72012-07-09 14:22:12 -0400441extern const char * _PySys_ImplCacheTag;
442
Barry Warsaw28a691b2010-04-17 00:19:56 +0000443const char *
444PyImport_GetMagicTag(void)
445{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400446 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000447}
448
Brett Cannon98979b82012-07-02 15:13:11 -0400449
Guido van Rossum25ce5661997-08-02 03:10:38 +0000450/* Magic for extension modules (built-in as well as dynamically
451 loaded). To prevent initializing an extension module more than
452 once, we keep a static dictionary 'extensions' keyed by module name
453 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000454 modules), containing these modules. A copy of the module's
Victor Stinner95872862011-03-07 18:20:56 +0100455 dictionary is stored by calling _PyImport_FixupExtensionObject()
Guido van Rossum25ce5661997-08-02 03:10:38 +0000456 immediately after the module initialization function succeeds. A
457 copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100458 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000459
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000460 Modules which do support multiple initialization set their m_size
461 field to a non-negative number (indicating the size of the
462 module-specific state). They are still recorded in the extensions
463 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000464*/
465
466int
Victor Stinner95872862011-03-07 18:20:56 +0100467_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
468 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyObject *modules, *dict;
471 struct PyModuleDef *def;
472 if (extensions == NULL) {
473 extensions = PyDict_New();
474 if (extensions == NULL)
475 return -1;
476 }
477 if (mod == NULL || !PyModule_Check(mod)) {
478 PyErr_BadInternalCall();
479 return -1;
480 }
481 def = PyModule_GetDef(mod);
482 if (!def) {
483 PyErr_BadInternalCall();
484 return -1;
485 }
486 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100487 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return -1;
489 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100490 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return -1;
492 }
493 if (def->m_size == -1) {
494 if (def->m_base.m_copy) {
495 /* Somebody already imported the module,
496 likely under a different name.
497 XXX this should really not happen. */
498 Py_DECREF(def->m_base.m_copy);
499 def->m_base.m_copy = NULL;
500 }
501 dict = PyModule_GetDict(mod);
502 if (dict == NULL)
503 return -1;
504 def->m_base.m_copy = PyDict_Copy(dict);
505 if (def->m_base.m_copy == NULL)
506 return -1;
507 }
Victor Stinner49d3f252010-10-17 01:24:53 +0000508 PyDict_SetItem(extensions, filename, (PyObject*)def);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000510}
511
Victor Stinner49d3f252010-10-17 01:24:53 +0000512int
513_PyImport_FixupBuiltin(PyObject *mod, char *name)
514{
515 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100516 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100517 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100518 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000519 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100520 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
521 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000522 return res;
523}
524
Guido van Rossum25ce5661997-08-02 03:10:38 +0000525PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100526_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 PyObject *mod, *mdict;
529 PyModuleDef* def;
530 if (extensions == NULL)
531 return NULL;
Victor Stinner49d3f252010-10-17 01:24:53 +0000532 def = (PyModuleDef*)PyDict_GetItem(extensions, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 if (def == NULL)
534 return NULL;
535 if (def->m_size == -1) {
536 /* Module does not support repeated initialization */
537 if (def->m_base.m_copy == NULL)
538 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100539 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (mod == NULL)
541 return NULL;
542 mdict = PyModule_GetDict(mod);
543 if (mdict == NULL)
544 return NULL;
545 if (PyDict_Update(mdict, def->m_base.m_copy))
546 return NULL;
547 }
548 else {
549 if (def->m_base.m_init == NULL)
550 return NULL;
551 mod = def->m_base.m_init();
552 if (mod == NULL)
553 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100554 PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 Py_DECREF(mod);
556 }
557 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100558 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 Py_DECREF(mod);
560 return NULL;
561 }
562 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100563 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 name, filename);
565 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000566
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567}
568
Victor Stinner49d3f252010-10-17 01:24:53 +0000569PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000570_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000571{
Victor Stinner95872862011-03-07 18:20:56 +0100572 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100573 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100574 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000575 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100576 res = _PyImport_FindExtensionObject(nameobj, nameobj);
577 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000578 return res;
579}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000580
581/* Get the module object corresponding to a module name.
582 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000583 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000584 Because the former action is most common, THIS DOES NOT RETURN A
585 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586
Guido van Rossum79f25d91997-04-29 20:08:16 +0000587PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000588PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 PyObject *modules = PyImport_GetModuleDict();
591 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592
Victor Stinner27ee0892011-03-04 12:57:09 +0000593 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyModule_Check(m))
595 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000596 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 if (m == NULL)
598 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000599 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 Py_DECREF(m);
601 return NULL;
602 }
603 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606}
607
Victor Stinner27ee0892011-03-04 12:57:09 +0000608PyObject *
609PyImport_AddModule(const char *name)
610{
611 PyObject *nameobj, *module;
612 nameobj = PyUnicode_FromString(name);
613 if (nameobj == NULL)
614 return NULL;
615 module = PyImport_AddModuleObject(nameobj);
616 Py_DECREF(nameobj);
617 return module;
618}
619
620
Tim Peters1cd70172004-08-02 03:52:12 +0000621/* Remove name from sys.modules, if it's there. */
622static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000623remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000626 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000628 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 Py_FatalError("import: deleting existing key in"
630 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000631}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632
Christian Heimes3b06e532008-01-07 20:12:44 +0000633
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000634/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000635 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
636 * removed from sys.modules, to avoid leaving damaged module objects
637 * in sys.modules. The caller may wish to restore the original
638 * module object (if any) in this case; PyImport_ReloadModule is an
639 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000640 *
641 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
642 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000643 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000645PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 return PyImport_ExecCodeModuleWithPathnames(
648 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000649}
650
651PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000652PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 return PyImport_ExecCodeModuleWithPathnames(
655 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000656}
657
658PyObject *
659PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000661{
Victor Stinner27ee0892011-03-04 12:57:09 +0000662 PyObject *m = NULL;
663 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
664
665 nameobj = PyUnicode_FromString(name);
666 if (nameobj == NULL)
667 return NULL;
668
Victor Stinner27ee0892011-03-04 12:57:09 +0000669 if (cpathname != NULL) {
670 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
671 if (cpathobj == NULL)
672 goto error;
Brett Cannona6473f92012-07-13 13:57:03 -0400673 }
674 else
Victor Stinner27ee0892011-03-04 12:57:09 +0000675 cpathobj = NULL;
Brett Cannona6473f92012-07-13 13:57:03 -0400676
677 if (pathname != NULL) {
678 pathobj = PyUnicode_DecodeFSDefault(pathname);
679 if (pathobj == NULL)
680 goto error;
681 }
682 else if (cpathobj != NULL) {
683 PyInterpreterState *interp = PyThreadState_GET()->interp;
684 _Py_IDENTIFIER(_get_sourcefile);
685
686 if (interp == NULL) {
687 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
688 "no interpreter!");
689 }
690
691 pathobj = _PyObject_CallMethodObjIdArgs(interp->importlib,
692 &PyId__get_sourcefile, cpathobj,
693 NULL);
694 if (pathobj == NULL)
695 PyErr_Clear();
696 }
697 else
698 pathobj = NULL;
699
Victor Stinner27ee0892011-03-04 12:57:09 +0000700 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
701error:
702 Py_DECREF(nameobj);
703 Py_XDECREF(pathobj);
704 Py_XDECREF(cpathobj);
705 return m;
706}
707
708PyObject*
709PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
710 PyObject *cpathname)
711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 PyObject *modules = PyImport_GetModuleDict();
713 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714
Victor Stinner27ee0892011-03-04 12:57:09 +0000715 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (m == NULL)
717 return NULL;
718 /* If the module is being reloaded, we get the old module back
719 and re-use its dict to exec the new code. */
720 d = PyModule_GetDict(m);
721 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
722 if (PyDict_SetItemString(d, "__builtins__",
723 PyEval_GetBuiltins()) != 0)
724 goto error;
725 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 if (pathname != NULL) {
Brett Cannona6473f92012-07-13 13:57:03 -0400727 v = pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 }
Brett Cannona6473f92012-07-13 13:57:03 -0400729 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 v = ((PyCodeObject *)co)->co_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 }
Brett Cannona6473f92012-07-13 13:57:03 -0400732 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 if (PyDict_SetItemString(d, "__file__", v) != 0)
734 PyErr_Clear(); /* Not important enough to report */
735 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000738 if (cpathname != NULL)
739 v = cpathname;
740 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 if (PyDict_SetItemString(d, "__cached__", v) != 0)
743 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000744
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000745 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 if (v == NULL)
747 goto error;
748 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000749
Victor Stinner27ee0892011-03-04 12:57:09 +0000750 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000752 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 name);
754 return NULL;
755 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000760
761 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 remove_module(name);
763 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000764}
765
766
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000767static void
768update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 PyObject *constants, *tmp;
771 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 if (PyUnicode_Compare(co->co_filename, oldname))
774 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 tmp = co->co_filename;
777 co->co_filename = newname;
778 Py_INCREF(co->co_filename);
779 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 constants = co->co_consts;
782 n = PyTuple_GET_SIZE(constants);
783 for (i = 0; i < n; i++) {
784 tmp = PyTuple_GET_ITEM(constants, i);
785 if (PyCode_Check(tmp))
786 update_code_filenames((PyCodeObject *)tmp,
787 oldname, newname);
788 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000789}
790
Victor Stinner2f42ae52011-03-20 00:41:24 +0100791static void
792update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000793{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100794 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000795
Victor Stinner2f42ae52011-03-20 00:41:24 +0100796 if (PyUnicode_Compare(co->co_filename, newname) == 0)
797 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 oldname = co->co_filename;
800 Py_INCREF(oldname);
801 update_code_filenames(co, oldname, newname);
802 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000803}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000804
Brett Cannon442c9b92011-03-23 16:14:42 -0700805static PyObject *
806imp_fix_co_filename(PyObject *self, PyObject *args)
807{
808 PyObject *co;
809 PyObject *file_path;
810
811 if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
812 return NULL;
813
814 if (!PyCode_Check(co)) {
815 PyErr_SetString(PyExc_TypeError,
816 "first argument must be a code object");
817 return NULL;
818 }
819
820 if (!PyUnicode_Check(file_path)) {
821 PyErr_SetString(PyExc_TypeError,
822 "second argument must be a string");
823 return NULL;
824 }
825
826 update_compiled_module((PyCodeObject*)co, file_path);
827
828 Py_RETURN_NONE;
829}
830
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000832/* Forward */
Victor Stinner53dc7352011-03-20 01:50:21 +0100833static struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000834
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000835
836/* Helper to test for built-in module */
837
838static int
Victor Stinner95872862011-03-07 18:20:56 +0100839is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000840{
Victor Stinner95872862011-03-07 18:20:56 +0100841 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +0100843 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
844 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 if (PyImport_Inittab[i].initfunc == NULL)
846 return -1;
847 else
848 return 1;
849 }
850 }
851 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000852}
853
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000854
Just van Rossum52e14d62002-12-30 22:08:05 +0000855/* Return an importer object for a sys.path/pkg.__path__ item 'p',
856 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +0000857 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +0000858 that can handle the path item. Return None if no hook could;
859 this tells our caller it should fall back to the builtin
860 import mechanism. Cache the result in path_importer_cache.
861 Returns a borrowed reference. */
862
863static PyObject *
864get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +0000866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 PyObject *importer;
868 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 /* These conditions are the caller's responsibility: */
871 assert(PyList_Check(path_hooks));
872 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 nhooks = PyList_Size(path_hooks);
875 if (nhooks < 0)
876 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 importer = PyDict_GetItem(path_importer_cache, p);
879 if (importer != NULL)
880 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* set path_importer_cache[p] to None to avoid recursion */
883 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
884 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 for (j = 0; j < nhooks; j++) {
887 PyObject *hook = PyList_GetItem(path_hooks, j);
888 if (hook == NULL)
889 return NULL;
890 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
891 if (importer != NULL)
892 break;
Just van Rossum52e14d62002-12-30 22:08:05 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
895 return NULL;
896 }
897 PyErr_Clear();
898 }
899 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -0400900 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 }
902 if (importer != NULL) {
903 int err = PyDict_SetItem(path_importer_cache, p, importer);
904 Py_DECREF(importer);
905 if (err != 0)
906 return NULL;
907 }
908 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +0000909}
910
Christian Heimes9cd17752007-11-18 19:35:23 +0000911PyAPI_FUNC(PyObject *)
912PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
916 if ((path_hooks = PySys_GetObject("path_hooks"))) {
917 importer = get_path_importer(path_importer_cache,
918 path_hooks, path);
919 }
920 }
921 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
922 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +0000923}
924
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000925
Victor Stinner95872862011-03-07 18:20:56 +0100926static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000927
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000928/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000929 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000930 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000931
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000932static int
Victor Stinner95872862011-03-07 18:20:56 +0100933init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000936
Victor Stinner95872862011-03-07 18:20:56 +0100937 if (_PyImport_FindExtensionObject(name, name) != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 for (p = PyImport_Inittab; p->name != NULL; p++) {
941 PyObject *mod;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100942 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +0100943 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 if (p->initfunc == NULL) {
945 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +0100946 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 name);
948 return -1;
949 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 mod = (*p->initfunc)();
951 if (mod == 0)
952 return -1;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +0100953 /* Remember pointer to module init function. */
954 def = PyModule_GetDef(mod);
955 def->m_base.m_init = p->initfunc;
Victor Stinner95872862011-03-07 18:20:56 +0100956 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 return -1;
958 /* FixupExtension has put the module into sys.modules,
959 so we can release our own reference. */
960 Py_DECREF(mod);
961 return 1;
962 }
963 }
964 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000965}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000966
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000968/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000969
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000970static struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +0100971find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000974
Victor Stinner53dc7352011-03-20 01:50:21 +0100975 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 for (p = PyImport_FrozenModules; ; p++) {
979 if (p->name == NULL)
980 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +0100981 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 break;
983 }
984 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000985}
986
Guido van Rossum79f25d91997-04-29 20:08:16 +0000987static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +0100988get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 struct _frozen *p = find_frozen(name);
991 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (p == NULL) {
994 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +0100995 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 name);
997 return NULL;
998 }
999 if (p->code == NULL) {
1000 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001001 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 name);
1003 return NULL;
1004 }
1005 size = p->size;
1006 if (size < 0)
1007 size = -size;
1008 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001009}
1010
Brett Cannon8d110132009-03-15 02:20:16 +00001011static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001012is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 struct _frozen *p = find_frozen(name);
1015 int size;
Brett Cannon8d110132009-03-15 02:20:16 +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 }
Brett Cannon8d110132009-03-15 02:20:16 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (size < 0)
1027 Py_RETURN_TRUE;
1028 else
1029 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001030}
1031
1032
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001033/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001034 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001035 an exception set if the initialization failed.
1036 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001037
1038int
Victor Stinner53dc7352011-03-20 01:50:21 +01001039PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001040{
Victor Stinner53dc7352011-03-20 01:50:21 +01001041 struct _frozen *p;
1042 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 int ispackage;
1044 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001045
Victor Stinner53dc7352011-03-20 01:50:21 +01001046 p = find_frozen(name);
1047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 if (p == NULL)
1049 return 0;
1050 if (p->code == NULL) {
1051 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001052 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 name);
1054 return -1;
1055 }
1056 size = p->size;
1057 ispackage = (size < 0);
1058 if (ispackage)
1059 size = -size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1061 if (co == NULL)
1062 return -1;
1063 if (!PyCode_Check(co)) {
1064 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001065 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 name);
1067 goto err_return;
1068 }
1069 if (ispackage) {
1070 /* Set __path__ to the package name */
Victor Stinner53dc7352011-03-20 01:50:21 +01001071 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001073 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (m == NULL)
1075 goto err_return;
1076 d = PyModule_GetDict(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 l = PyList_New(1);
1078 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 goto err_return;
1080 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001081 Py_INCREF(name);
1082 PyList_SET_ITEM(l, 0, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 err = PyDict_SetItemString(d, "__path__", l);
1084 Py_DECREF(l);
1085 if (err != 0)
1086 goto err_return;
1087 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001088 path = PyUnicode_FromString("<frozen>");
1089 if (path == NULL)
1090 goto err_return;
1091 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1092 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (m == NULL)
1094 goto err_return;
1095 Py_DECREF(co);
1096 Py_DECREF(m);
1097 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001098err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 Py_DECREF(co);
1100 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001101}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001102
Victor Stinner53dc7352011-03-20 01:50:21 +01001103int
1104PyImport_ImportFrozenModule(char *name)
1105{
1106 PyObject *nameobj;
1107 int ret;
1108 nameobj = PyUnicode_InternFromString(name);
1109 if (nameobj == NULL)
1110 return -1;
1111 ret = PyImport_ImportFrozenModuleObject(nameobj);
1112 Py_DECREF(nameobj);
1113 return ret;
1114}
1115
Guido van Rossum74e6a111994-08-29 12:54:38 +00001116
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001117/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001118 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001119
Guido van Rossum79f25d91997-04-29 20:08:16 +00001120PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001121PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 PyObject *pname;
1124 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 pname = PyUnicode_FromString(name);
1127 if (pname == NULL)
1128 return NULL;
1129 result = PyImport_Import(pname);
1130 Py_DECREF(pname);
1131 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001132}
1133
Christian Heimes072c0f12008-01-03 23:01:04 +00001134/* Import a module without blocking
1135 *
1136 * At first it tries to fetch the module from sys.modules. If the module was
1137 * never loaded before it loads it with PyImport_ImportModule() unless another
1138 * thread holds the import lock. In the latter case the function raises an
1139 * ImportError instead of blocking.
1140 *
1141 * Returns the module object with incremented ref count.
1142 */
1143PyObject *
1144PyImport_ImportModuleNoBlock(const char *name)
1145{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001146 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001147}
1148
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001149
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001150/* Remove importlib frames from the traceback,
1151 * except in Verbose mode. */
1152static void
1153remove_importlib_frames(void)
1154{
1155 const char *importlib_filename = "<frozen importlib._bootstrap>";
Nick Coghlan42c07662012-07-31 21:14:18 +10001156 const char *remove_frames = "_call_with_frames_removed";
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001157 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001158 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001159 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001160 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001161
1162 /* Synopsis: if it's an ImportError, we trim all importlib chunks
Nick Coghlan42c07662012-07-31 21:14:18 +10001163 from the traceback. We always trim chunks
1164 which end with a call to "_call_with_frames_removed". */
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001165
1166 PyErr_Fetch(&exception, &value, &base_tb);
1167 if (!exception || Py_VerboseFlag)
1168 goto done;
1169 if (PyType_IsSubtype((PyTypeObject *) exception,
1170 (PyTypeObject *) PyExc_ImportError))
1171 always_trim = 1;
1172
1173 prev_link = &base_tb;
1174 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001175 while (tb != NULL) {
1176 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1177 PyObject *next = (PyObject *) traceback->tb_next;
1178 PyFrameObject *frame = traceback->tb_frame;
1179 PyCodeObject *code = frame->f_code;
1180 int now_in_importlib;
1181
1182 assert(PyTraceBack_Check(tb));
1183 now_in_importlib = (PyUnicode_CompareWithASCIIString(
1184 code->co_filename,
1185 importlib_filename) == 0);
1186 if (now_in_importlib && !in_importlib) {
1187 /* This is the link to this chunk of importlib tracebacks */
1188 outer_link = prev_link;
1189 }
1190 in_importlib = now_in_importlib;
1191
1192 if (in_importlib &&
1193 (always_trim ||
Nick Coghlan42c07662012-07-31 21:14:18 +10001194 PyUnicode_CompareWithASCIIString(code->co_name,
1195 remove_frames) == 0)) {
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001196 PyObject *tmp = *outer_link;
1197 *outer_link = next;
1198 Py_XINCREF(next);
1199 Py_DECREF(tmp);
1200 prev_link = outer_link;
1201 }
1202 else {
1203 prev_link = (PyObject **) &traceback->tb_next;
1204 }
1205 tb = next;
1206 }
1207done:
1208 PyErr_Restore(exception, value, base_tb);
1209}
1210
1211
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001212PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04001213PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1214 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001215 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001216{
Brett Cannonfd074152012-04-14 14:10:13 -04001217 _Py_IDENTIFIER(__import__);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001218 _Py_IDENTIFIER(__initializing__);
Brett Cannonfd074152012-04-14 14:10:13 -04001219 _Py_IDENTIFIER(__package__);
1220 _Py_IDENTIFIER(__path__);
1221 _Py_IDENTIFIER(__name__);
1222 _Py_IDENTIFIER(_find_and_load);
1223 _Py_IDENTIFIER(_handle_fromlist);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001224 _Py_IDENTIFIER(_lock_unlock_module);
Brett Cannonfd074152012-04-14 14:10:13 -04001225 _Py_static_string(single_dot, ".");
1226 PyObject *abs_name = NULL;
1227 PyObject *builtins_import = NULL;
1228 PyObject *final_mod = NULL;
1229 PyObject *mod = NULL;
1230 PyObject *package = NULL;
1231 PyObject *globals = NULL;
1232 PyObject *fromlist = NULL;
1233 PyInterpreterState *interp = PyThreadState_GET()->interp;
1234
1235 /* Make sure to use default values so as to not have
1236 PyObject_CallMethodObjArgs() truncate the parameter list because of a
1237 NULL argument. */
1238 if (given_globals == NULL) {
1239 globals = PyDict_New();
1240 if (globals == NULL) {
1241 goto error;
1242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 }
Brett Cannonfd074152012-04-14 14:10:13 -04001244 else {
1245 /* Only have to care what given_globals is if it will be used
Brett Cannonecfefb72012-08-05 19:24:57 -04001246 for something. */
Brett Cannonfd074152012-04-14 14:10:13 -04001247 if (level > 0 && !PyDict_Check(given_globals)) {
1248 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1249 goto error;
1250 }
1251 globals = given_globals;
1252 Py_INCREF(globals);
1253 }
1254
1255 if (given_fromlist == NULL) {
1256 fromlist = PyList_New(0);
1257 if (fromlist == NULL) {
1258 goto error;
1259 }
1260 }
1261 else {
1262 fromlist = given_fromlist;
1263 Py_INCREF(fromlist);
1264 }
1265 if (name == NULL) {
1266 PyErr_SetString(PyExc_ValueError, "Empty module name");
1267 goto error;
1268 }
1269
1270 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1271 for added performance. */
1272
1273 if (!PyUnicode_Check(name)) {
1274 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1275 goto error;
1276 }
1277 else if (PyUnicode_READY(name) < 0) {
1278 goto error;
1279 }
1280 if (level < 0) {
1281 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1282 goto error;
1283 }
1284 else if (level > 0) {
1285 package = _PyDict_GetItemId(globals, &PyId___package__);
1286 if (package != NULL && package != Py_None) {
1287 Py_INCREF(package);
1288 if (!PyUnicode_Check(package)) {
1289 PyErr_SetString(PyExc_TypeError, "package must be a string");
1290 goto error;
1291 }
1292 }
1293 else {
Brett Cannon273323c2012-04-17 19:05:11 -04001294 package = _PyDict_GetItemId(globals, &PyId___name__);
Brett Cannonfd074152012-04-14 14:10:13 -04001295 if (package == NULL) {
Brett Cannon273323c2012-04-17 19:05:11 -04001296 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
Brett Cannonfd074152012-04-14 14:10:13 -04001297 goto error;
1298 }
1299 else if (!PyUnicode_Check(package)) {
1300 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1301 }
1302 Py_INCREF(package);
1303
1304 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Brett Cannon740fce02012-04-14 14:23:49 -04001305 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001306 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1307 if (borrowed_dot == NULL) {
1308 goto error;
1309 }
Brett Cannon740fce02012-04-14 14:23:49 -04001310 partition = PyUnicode_RPartition(package, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001311 Py_DECREF(package);
1312 if (partition == NULL) {
1313 goto error;
1314 }
1315 package = PyTuple_GET_ITEM(partition, 0);
1316 Py_INCREF(package);
1317 Py_DECREF(partition);
1318 }
1319 }
1320
1321 if (PyDict_GetItem(interp->modules, package) == NULL) {
1322 PyErr_Format(PyExc_SystemError,
1323 "Parent module %R not loaded, cannot perform relative "
1324 "import", package);
1325 goto error;
1326 }
1327 }
1328 else { /* level == 0 */
1329 if (PyUnicode_GET_LENGTH(name) == 0) {
1330 PyErr_SetString(PyExc_ValueError, "Empty module name");
1331 goto error;
1332 }
1333 package = Py_None;
1334 Py_INCREF(package);
1335 }
1336
1337 if (level > 0) {
1338 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1339 PyObject *base = NULL;
1340 int level_up = 1;
1341
1342 for (level_up = 1; level_up < level; level_up += 1) {
1343 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1344 if (last_dot == -2) {
1345 goto error;
1346 }
1347 else if (last_dot == -1) {
1348 PyErr_SetString(PyExc_ValueError,
1349 "attempted relative import beyond top-level "
1350 "package");
1351 goto error;
1352 }
1353 }
1354 base = PyUnicode_Substring(package, 0, last_dot);
1355 if (PyUnicode_GET_LENGTH(name) > 0) {
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001356 PyObject *borrowed_dot, *seq = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001357
1358 borrowed_dot = _PyUnicode_FromId(&single_dot);
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001359 seq = PyTuple_Pack(2, base, name);
1360 Py_DECREF(base);
Brett Cannonfd074152012-04-14 14:10:13 -04001361 if (borrowed_dot == NULL || seq == NULL) {
1362 goto error;
1363 }
1364
1365 abs_name = PyUnicode_Join(borrowed_dot, seq);
1366 Py_DECREF(seq);
1367 if (abs_name == NULL) {
1368 goto error;
1369 }
1370 }
1371 else {
1372 abs_name = base;
1373 }
1374 }
1375 else {
1376 abs_name = name;
1377 Py_INCREF(abs_name);
1378 }
1379
Brian Curtine6b299f2012-04-14 14:19:33 -05001380#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001381 _PyImport_AcquireLock();
1382#endif
1383 /* From this point forward, goto error_with_unlock! */
1384 if (PyDict_Check(globals)) {
1385 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1386 }
1387 if (builtins_import == NULL) {
1388 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1389 if (builtins_import == NULL) {
1390 Py_FatalError("__import__ missing");
1391 }
1392 }
1393 Py_INCREF(builtins_import);
1394
1395 mod = PyDict_GetItem(interp->modules, abs_name);
1396 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001397 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1398 "None in sys.modules", abs_name);
1399 if (msg != NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -05001400 PyErr_SetImportError(msg, abs_name, NULL);
1401 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001402 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001403 mod = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001404 goto error_with_unlock;
1405 }
1406 else if (mod != NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001407 PyObject *value;
1408 int initializing = 0;
1409
Brett Cannonfd074152012-04-14 14:10:13 -04001410 Py_INCREF(mod);
Antoine Pitrou03989852012-08-28 00:24:52 +02001411 /* Optimization: only call _bootstrap._lock_unlock_module() if
1412 __initializing__ is true.
1413 NOTE: because of this, __initializing__ must be set *before*
1414 stuffing the new module in sys.modules.
1415 */
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001416 value = _PyObject_GetAttrId(mod, &PyId___initializing__);
1417 if (value == NULL)
1418 PyErr_Clear();
1419 else {
1420 initializing = PyObject_IsTrue(value);
1421 Py_DECREF(value);
1422 if (initializing == -1)
1423 PyErr_Clear();
1424 }
1425 if (initializing > 0) {
1426 /* _bootstrap._lock_unlock_module() releases the import lock */
1427 value = _PyObject_CallMethodObjIdArgs(interp->importlib,
1428 &PyId__lock_unlock_module, abs_name,
1429 NULL);
1430 if (value == NULL)
1431 goto error;
1432 Py_DECREF(value);
1433 }
1434 else {
1435#ifdef WITH_THREAD
1436 if (_PyImport_ReleaseLock() < 0) {
1437 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1438 goto error;
1439 }
1440#endif
1441 }
Brett Cannonfd074152012-04-14 14:10:13 -04001442 }
1443 else {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001444 /* _bootstrap._find_and_load() releases the import lock */
Brett Cannonfd074152012-04-14 14:10:13 -04001445 mod = _PyObject_CallMethodObjIdArgs(interp->importlib,
1446 &PyId__find_and_load, abs_name,
1447 builtins_import, NULL);
1448 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001449 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001450 }
1451 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001452 /* From now on we don't hold the import lock anymore. */
Brett Cannonfd074152012-04-14 14:10:13 -04001453
1454 if (PyObject_Not(fromlist)) {
1455 if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1456 PyObject *front = NULL;
Brian Curtine6b299f2012-04-14 14:19:33 -05001457 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001458 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1459
1460 if (borrowed_dot == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001461 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001462 }
1463
Brian Curtine6b299f2012-04-14 14:19:33 -05001464 partition = PyUnicode_Partition(name, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001465 if (partition == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001466 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001467 }
1468
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001469 if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1470 /* No dot in module name, simple exit */
1471 Py_DECREF(partition);
1472 final_mod = mod;
1473 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001474 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001475 }
1476
Brett Cannonfd074152012-04-14 14:10:13 -04001477 front = PyTuple_GET_ITEM(partition, 0);
1478 Py_INCREF(front);
1479 Py_DECREF(partition);
1480
1481 if (level == 0) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001482 final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001483 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001484 }
1485 else {
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001486 Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1487 PyUnicode_GET_LENGTH(front);
1488 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001489 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001490 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001491 Py_DECREF(front);
1492 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001493 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001494 }
Brett Cannonfd074152012-04-14 14:10:13 -04001495
1496 final_mod = PyDict_GetItem(interp->modules, to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001497 if (final_mod == NULL) {
1498 PyErr_Format(PyExc_KeyError,
1499 "%R not in sys.modules as expected",
1500 to_return);
1501 }
1502 else {
1503 Py_INCREF(final_mod);
1504 }
Antoine Pitroub78174c2012-05-06 17:15:23 +02001505 Py_DECREF(to_return);
Brett Cannonfd074152012-04-14 14:10:13 -04001506 }
1507 }
1508 else {
1509 final_mod = mod;
1510 Py_INCREF(mod);
1511 }
1512 }
1513 else {
1514 final_mod = _PyObject_CallMethodObjIdArgs(interp->importlib,
1515 &PyId__handle_fromlist, mod,
1516 fromlist, builtins_import,
1517 NULL);
1518 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001519 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001520
Brett Cannonfd074152012-04-14 14:10:13 -04001521 error_with_unlock:
Brian Curtine6b299f2012-04-14 14:19:33 -05001522#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001523 if (_PyImport_ReleaseLock() < 0) {
1524 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1525 }
1526#endif
1527 error:
1528 Py_XDECREF(abs_name);
1529 Py_XDECREF(builtins_import);
1530 Py_XDECREF(mod);
1531 Py_XDECREF(package);
1532 Py_XDECREF(globals);
1533 Py_XDECREF(fromlist);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001534 if (final_mod == NULL)
1535 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001536 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001537}
1538
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001539PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001540PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001541 PyObject *fromlist, int level)
1542{
1543 PyObject *nameobj, *mod;
1544 nameobj = PyUnicode_FromString(name);
1545 if (nameobj == NULL)
1546 return NULL;
1547 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1548 fromlist, level);
1549 Py_DECREF(nameobj);
1550 return mod;
1551}
1552
1553
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001554/* Re-import a module of any kind and return its module object, WITH
1555 INCREMENTED REFERENCE COUNT */
1556
Guido van Rossum79f25d91997-04-29 20:08:16 +00001557PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001558PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001559{
Brett Cannon62228db2012-04-29 14:38:11 -04001560 _Py_IDENTIFIER(reload);
1561 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001563 PyObject *imp = PyDict_GetItemString(modules, "imp");
1564 if (imp == NULL) {
1565 imp = PyImport_ImportModule("imp");
1566 if (imp == NULL) {
1567 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 }
Brett Cannon62228db2012-04-29 14:38:11 -04001570 else {
1571 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001573
Brett Cannon62228db2012-04-29 14:38:11 -04001574 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1575 Py_DECREF(imp);
1576 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001577}
1578
1579
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001580/* Higher-level import emulator which emulates the "import" statement
1581 more accurately -- it invokes the __import__() function from the
1582 builtins of the current globals. This means that the import is
1583 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001584 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001585 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001586 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001587 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001588
1589PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001590PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 static PyObject *silly_list = NULL;
1593 static PyObject *builtins_str = NULL;
1594 static PyObject *import_str = NULL;
1595 PyObject *globals = NULL;
1596 PyObject *import = NULL;
1597 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001598 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 /* Initialize constant string objects */
1602 if (silly_list == NULL) {
1603 import_str = PyUnicode_InternFromString("__import__");
1604 if (import_str == NULL)
1605 return NULL;
1606 builtins_str = PyUnicode_InternFromString("__builtins__");
1607 if (builtins_str == NULL)
1608 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001609 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if (silly_list == NULL)
1611 return NULL;
1612 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 /* Get the builtins from current globals */
1615 globals = PyEval_GetGlobals();
1616 if (globals != NULL) {
1617 Py_INCREF(globals);
1618 builtins = PyObject_GetItem(globals, builtins_str);
1619 if (builtins == NULL)
1620 goto err;
1621 }
1622 else {
1623 /* No globals -- use standard builtins, and fake globals */
1624 builtins = PyImport_ImportModuleLevel("builtins",
1625 NULL, NULL, NULL, 0);
1626 if (builtins == NULL)
1627 return NULL;
1628 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1629 if (globals == NULL)
1630 goto err;
1631 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 /* Get the __import__ function from the builtins */
1634 if (PyDict_Check(builtins)) {
1635 import = PyObject_GetItem(builtins, import_str);
1636 if (import == NULL)
1637 PyErr_SetObject(PyExc_KeyError, import_str);
1638 }
1639 else
1640 import = PyObject_GetAttr(builtins, import_str);
1641 if (import == NULL)
1642 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001645 Always use absolute import here.
1646 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1648 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001649 if (r == NULL)
1650 goto err;
1651 Py_DECREF(r);
1652
1653 modules = PyImport_GetModuleDict();
1654 r = PyDict_GetItem(modules, module_name);
1655 if (r != NULL)
1656 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001657
1658 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 Py_XDECREF(globals);
1660 Py_XDECREF(builtins);
1661 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001664}
1665
Barry Warsaw28a691b2010-04-17 00:19:56 +00001666static PyObject *
Brett Cannon2657df42012-05-04 15:20:40 -04001667imp_extension_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001670 const char *suffix;
1671 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 list = PyList_New(0);
1674 if (list == NULL)
1675 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001676#ifdef HAVE_DYNAMIC_LOADING
1677 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1678 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 if (item == NULL) {
1680 Py_DECREF(list);
1681 return NULL;
1682 }
1683 if (PyList_Append(list, item) < 0) {
1684 Py_DECREF(list);
1685 Py_DECREF(item);
1686 return NULL;
1687 }
1688 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001689 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 }
Brett Cannon2657df42012-05-04 15:20:40 -04001691#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001693}
1694
Guido van Rossum79f25d91997-04-29 20:08:16 +00001695static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001696imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001697{
Victor Stinner95872862011-03-07 18:20:56 +01001698 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 int ret;
1700 PyObject *m;
Victor Stinner95872862011-03-07 18:20:56 +01001701 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 return NULL;
1703 ret = init_builtin(name);
1704 if (ret < 0)
1705 return NULL;
1706 if (ret == 0) {
1707 Py_INCREF(Py_None);
1708 return Py_None;
1709 }
Victor Stinner95872862011-03-07 18:20:56 +01001710 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 Py_XINCREF(m);
1712 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001713}
1714
Guido van Rossum79f25d91997-04-29 20:08:16 +00001715static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001716imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001717{
Victor Stinner53dc7352011-03-20 01:50:21 +01001718 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 int ret;
1720 PyObject *m;
Victor Stinner53dc7352011-03-20 01:50:21 +01001721 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001723 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (ret < 0)
1725 return NULL;
1726 if (ret == 0) {
1727 Py_INCREF(Py_None);
1728 return Py_None;
1729 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001730 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 Py_XINCREF(m);
1732 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001733}
1734
Guido van Rossum79f25d91997-04-29 20:08:16 +00001735static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001736imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001737{
Victor Stinner53dc7352011-03-20 01:50:21 +01001738 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00001739
Victor Stinner53dc7352011-03-20 01:50:21 +01001740 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 return NULL;
1742 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001743}
1744
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00001746imp_is_frozen_package(PyObject *self, PyObject *args)
1747{
Victor Stinner53dc7352011-03-20 01:50:21 +01001748 PyObject *name;
Brett Cannon8d110132009-03-15 02:20:16 +00001749
Victor Stinner53dc7352011-03-20 01:50:21 +01001750 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 return NULL;
1752 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00001753}
1754
1755static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001756imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001757{
Victor Stinner95872862011-03-07 18:20:56 +01001758 PyObject *name;
1759 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 return NULL;
1761 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001762}
1763
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001765imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001766{
Victor Stinner53dc7352011-03-20 01:50:21 +01001767 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001769 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 return NULL;
1771 p = find_frozen(name);
1772 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001773}
1774
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001775#ifdef HAVE_DYNAMIC_LOADING
1776
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001778imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001779{
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001780 PyObject *name, *pathname, *fob = NULL, *mod;
1781 FILE *fp;
1782
1783 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
1784 &name, PyUnicode_FSDecoder, &pathname, &fob))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 return NULL;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001786 if (fob != NULL) {
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001787 fp = _Py_fopen(pathname, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001788 if (fp == NULL) {
1789 Py_DECREF(pathname);
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001790 if (!PyErr_Occurred())
1791 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001793 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001795 else
1796 fp = NULL;
1797 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001798 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (fp)
1800 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001801 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001802}
1803
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001804#endif /* HAVE_DYNAMIC_LOADING */
1805
Barry Warsaw28a691b2010-04-17 00:19:56 +00001806
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001807/* Doc strings */
1808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001809PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04001810"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001811
Brett Cannon2657df42012-05-04 15:20:40 -04001812PyDoc_STRVAR(doc_extension_suffixes,
1813"extension_suffixes() -> list of strings\n\
1814Returns the list of file suffixes used to identify extension modules.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001815
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001816PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00001817"lock_held() -> boolean\n\
1818Return True if the import lock is currently held, else False.\n\
1819On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00001820
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001821PyDoc_STRVAR(doc_acquire_lock,
1822"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001823Acquires the interpreter's import lock for the current thread.\n\
1824This lock should be used by import hooks to ensure thread-safety\n\
1825when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001826On platforms without threads, this function does nothing.");
1827
1828PyDoc_STRVAR(doc_release_lock,
1829"release_lock() -> None\n\
1830Release the interpreter's import lock.\n\
1831On platforms without threads, this function does nothing.");
1832
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833static PyMethodDef imp_methods[] = {
Brett Cannon2657df42012-05-04 15:20:40 -04001834 {"extension_suffixes", imp_extension_suffixes, METH_NOARGS,
1835 doc_extension_suffixes},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
1837 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
1838 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
1840 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
1841 {"init_builtin", imp_init_builtin, METH_VARARGS},
1842 {"init_frozen", imp_init_frozen, METH_VARARGS},
1843 {"is_builtin", imp_is_builtin, METH_VARARGS},
1844 {"is_frozen", imp_is_frozen, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001845#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001847#endif
Brett Cannon442c9b92011-03-23 16:14:42 -07001848 {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001850};
1851
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001852
Martin v. Löwis1a214512008-06-11 05:26:20 +00001853static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04001855 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 doc_imp,
1857 0,
1858 imp_methods,
1859 NULL,
1860 NULL,
1861 NULL,
1862 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001863};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001864
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001865PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001866PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 m = PyModule_Create(&impmodule);
1871 if (m == NULL)
1872 goto failure;
1873 d = PyModule_GetDict(m);
1874 if (d == NULL)
1875 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001878 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 Py_XDECREF(m);
1880 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001881}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001882
1883
Guido van Rossumb18618d2000-05-03 23:44:39 +00001884/* API for embedding applications that want to add their own entries
1885 to the table of built-in modules. This should normally be called
1886 *before* Py_Initialize(). When the table resize fails, -1 is
1887 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001888
1889 After a similar function by Just van Rossum. */
1890
1891int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001892PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 static struct _inittab *our_copy = NULL;
1895 struct _inittab *p;
1896 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 /* Count the number of entries in both tables */
1899 for (n = 0; newtab[n].name != NULL; n++)
1900 ;
1901 if (n == 0)
1902 return 0; /* Nothing to do */
1903 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
1904 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 /* Allocate new memory for the combined table */
1907 p = our_copy;
1908 PyMem_RESIZE(p, struct _inittab, i+n+1);
1909 if (p == NULL)
1910 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 /* Copy the tables into the new memory */
1913 if (our_copy != PyImport_Inittab)
1914 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
1915 PyImport_Inittab = our_copy = p;
1916 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001919}
1920
1921/* Shorthand to add a single entry given a name and a function */
1922
1923int
Brett Cannona826f322009-04-02 03:41:46 +00001924PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 newtab[0].name = (char *)name;
1931 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00001934}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001935
1936#ifdef __cplusplus
1937}
1938#endif