blob: 4ae8abfceb1a9a30ca2021fd16d6cee7e5bec431 [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{
Brett Cannon77b2abd2012-07-09 16:09:00 -0400429 PyInterpreterState *interp = PyThreadState_Get()->interp;
430 PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
431 "_RAW_MAGIC_NUMBER");
432 if (pyc_magic == NULL)
433 return -1;
434 return PyLong_AsLong(pyc_magic);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000435}
436
437
Brett Cannon3adc7b72012-07-09 14:22:12 -0400438extern const char * _PySys_ImplCacheTag;
439
Barry Warsaw28a691b2010-04-17 00:19:56 +0000440const char *
441PyImport_GetMagicTag(void)
442{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400443 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000444}
445
Brett Cannon98979b82012-07-02 15:13:11 -0400446
Guido van Rossum25ce5661997-08-02 03:10:38 +0000447/* Magic for extension modules (built-in as well as dynamically
448 loaded). To prevent initializing an extension module more than
449 once, we keep a static dictionary 'extensions' keyed by module name
450 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000451 modules), containing these modules. A copy of the module's
Victor Stinner95872862011-03-07 18:20:56 +0100452 dictionary is stored by calling _PyImport_FixupExtensionObject()
Guido van Rossum25ce5661997-08-02 03:10:38 +0000453 immediately after the module initialization function succeeds. A
454 copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100455 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000456
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000457 Modules which do support multiple initialization set their m_size
458 field to a non-negative number (indicating the size of the
459 module-specific state). They are still recorded in the extensions
460 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000461*/
462
463int
Victor Stinner95872862011-03-07 18:20:56 +0100464_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
465 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyObject *modules, *dict;
468 struct PyModuleDef *def;
469 if (extensions == NULL) {
470 extensions = PyDict_New();
471 if (extensions == NULL)
472 return -1;
473 }
474 if (mod == NULL || !PyModule_Check(mod)) {
475 PyErr_BadInternalCall();
476 return -1;
477 }
478 def = PyModule_GetDef(mod);
479 if (!def) {
480 PyErr_BadInternalCall();
481 return -1;
482 }
483 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100484 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 return -1;
486 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100487 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return -1;
489 }
490 if (def->m_size == -1) {
491 if (def->m_base.m_copy) {
492 /* Somebody already imported the module,
493 likely under a different name.
494 XXX this should really not happen. */
495 Py_DECREF(def->m_base.m_copy);
496 def->m_base.m_copy = NULL;
497 }
498 dict = PyModule_GetDict(mod);
499 if (dict == NULL)
500 return -1;
501 def->m_base.m_copy = PyDict_Copy(dict);
502 if (def->m_base.m_copy == NULL)
503 return -1;
504 }
Victor Stinner49d3f252010-10-17 01:24:53 +0000505 PyDict_SetItem(extensions, filename, (PyObject*)def);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000507}
508
Victor Stinner49d3f252010-10-17 01:24:53 +0000509int
510_PyImport_FixupBuiltin(PyObject *mod, char *name)
511{
512 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100513 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100514 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100515 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000516 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100517 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
518 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000519 return res;
520}
521
Guido van Rossum25ce5661997-08-02 03:10:38 +0000522PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100523_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 PyObject *mod, *mdict;
526 PyModuleDef* def;
527 if (extensions == NULL)
528 return NULL;
Victor Stinner49d3f252010-10-17 01:24:53 +0000529 def = (PyModuleDef*)PyDict_GetItem(extensions, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (def == NULL)
531 return NULL;
532 if (def->m_size == -1) {
533 /* Module does not support repeated initialization */
534 if (def->m_base.m_copy == NULL)
535 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100536 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (mod == NULL)
538 return NULL;
539 mdict = PyModule_GetDict(mod);
540 if (mdict == NULL)
541 return NULL;
542 if (PyDict_Update(mdict, def->m_base.m_copy))
543 return NULL;
544 }
545 else {
546 if (def->m_base.m_init == NULL)
547 return NULL;
548 mod = def->m_base.m_init();
549 if (mod == NULL)
550 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100551 PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 Py_DECREF(mod);
553 }
554 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100555 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 Py_DECREF(mod);
557 return NULL;
558 }
559 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100560 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 name, filename);
562 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000563
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000564}
565
Victor Stinner49d3f252010-10-17 01:24:53 +0000566PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000567_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000568{
Victor Stinner95872862011-03-07 18:20:56 +0100569 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100570 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100571 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000572 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100573 res = _PyImport_FindExtensionObject(nameobj, nameobj);
574 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000575 return res;
576}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000577
578/* Get the module object corresponding to a module name.
579 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000580 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000581 Because the former action is most common, THIS DOES NOT RETURN A
582 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583
Guido van Rossum79f25d91997-04-29 20:08:16 +0000584PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000585PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 PyObject *modules = PyImport_GetModuleDict();
588 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589
Victor Stinner27ee0892011-03-04 12:57:09 +0000590 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PyModule_Check(m))
592 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000593 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 if (m == NULL)
595 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000596 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_DECREF(m);
598 return NULL;
599 }
600 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603}
604
Victor Stinner27ee0892011-03-04 12:57:09 +0000605PyObject *
606PyImport_AddModule(const char *name)
607{
608 PyObject *nameobj, *module;
609 nameobj = PyUnicode_FromString(name);
610 if (nameobj == NULL)
611 return NULL;
612 module = PyImport_AddModuleObject(nameobj);
613 Py_DECREF(nameobj);
614 return module;
615}
616
617
Tim Peters1cd70172004-08-02 03:52:12 +0000618/* Remove name from sys.modules, if it's there. */
619static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000620remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000623 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000625 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 Py_FatalError("import: deleting existing key in"
627 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000628}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000629
Victor Stinnerc9abda02011-03-14 13:33:46 -0400630static PyObject * get_sourcefile(PyObject *filename);
631static PyObject *make_source_pathname(PyObject *pathname);
Christian Heimes3b06e532008-01-07 20:12:44 +0000632
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000633/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000634 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
635 * removed from sys.modules, to avoid leaving damaged module objects
636 * in sys.modules. The caller may wish to restore the original
637 * module object (if any) in this case; PyImport_ReloadModule is an
638 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000639 *
640 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
641 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000642 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000643PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000644PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 return PyImport_ExecCodeModuleWithPathnames(
647 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000648}
649
650PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 return PyImport_ExecCodeModuleWithPathnames(
654 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000655}
656
657PyObject *
658PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000660{
Victor Stinner27ee0892011-03-04 12:57:09 +0000661 PyObject *m = NULL;
662 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
663
664 nameobj = PyUnicode_FromString(name);
665 if (nameobj == NULL)
666 return NULL;
667
668 if (pathname != NULL) {
669 pathobj = PyUnicode_DecodeFSDefault(pathname);
670 if (pathobj == NULL)
671 goto error;
672 } else
673 pathobj = NULL;
674 if (cpathname != NULL) {
675 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
676 if (cpathobj == NULL)
677 goto error;
678 } else
679 cpathobj = NULL;
680 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
681error:
682 Py_DECREF(nameobj);
683 Py_XDECREF(pathobj);
684 Py_XDECREF(cpathobj);
685 return m;
686}
687
688PyObject*
689PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
690 PyObject *cpathname)
691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 PyObject *modules = PyImport_GetModuleDict();
693 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000694
Victor Stinner27ee0892011-03-04 12:57:09 +0000695 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 if (m == NULL)
697 return NULL;
698 /* If the module is being reloaded, we get the old module back
699 and re-use its dict to exec the new code. */
700 d = PyModule_GetDict(m);
701 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
702 if (PyDict_SetItemString(d, "__builtins__",
703 PyEval_GetBuiltins()) != 0)
704 goto error;
705 }
706 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 if (pathname != NULL) {
Victor Stinnerc9abda02011-03-14 13:33:46 -0400708 v = get_sourcefile(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 if (v == NULL)
710 PyErr_Clear();
711 }
Victor Stinner27ee0892011-03-04 12:57:09 +0000712 else
713 v = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 if (v == NULL) {
715 v = ((PyCodeObject *)co)->co_filename;
716 Py_INCREF(v);
717 }
718 if (PyDict_SetItemString(d, "__file__", v) != 0)
719 PyErr_Clear(); /* Not important enough to report */
720 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000723 if (cpathname != NULL)
724 v = cpathname;
725 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 if (PyDict_SetItemString(d, "__cached__", v) != 0)
728 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000729
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000730 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 if (v == NULL)
732 goto error;
733 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000734
Victor Stinner27ee0892011-03-04 12:57:09 +0000735 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000737 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 name);
739 return NULL;
740 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000745
746 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 remove_module(name);
748 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000749}
750
751
Martin v. Löwis2db72862011-10-23 17:29:08 +0200752/* Like rightmost_sep, but operate on unicode objects. */
753static Py_ssize_t
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200754rightmost_sep_obj(PyObject* o, Py_ssize_t start, Py_ssize_t end)
Martin v. Löwis2db72862011-10-23 17:29:08 +0200755{
756 Py_ssize_t found, i;
757 Py_UCS4 c;
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200758 for (found = -1, i = start; i < end; i++) {
Martin v. Löwis2db72862011-10-23 17:29:08 +0200759 c = PyUnicode_READ_CHAR(o, i);
760 if (c == SEP
761#ifdef ALTSEP
762 || c == ALTSEP
763#endif
764 )
765 {
766 found = i;
767 }
768 }
769 return found;
770}
Victor Stinnerc9abda02011-03-14 13:33:46 -0400771
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000772
Barry Warsaw28a691b2010-04-17 00:19:56 +0000773/* Given a pathname to a Python byte compiled file, return the path to the
774 source file, if the path matches the PEP 3147 format. This does not check
775 for any file existence, however, if the pyc file name does not match PEP
776 3147 style, NULL is returned. buf must be at least as big as pathname;
Victor Stinnerc9abda02011-03-14 13:33:46 -0400777 the resulting path will always be shorter.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778
Victor Stinnerc9abda02011-03-14 13:33:46 -0400779 (...)/__pycache__/foo.<tag>.pyc -> (...)/foo.py */
780
781static PyObject*
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200782make_source_pathname(PyObject *path)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000783{
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200784 Py_ssize_t left, right, dot0, dot1, len;
785 Py_ssize_t i, j;
786 PyObject *result;
787 int kind;
788 void *data;
Victor Stinnerc9abda02011-03-14 13:33:46 -0400789
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200790 len = PyUnicode_GET_LENGTH(path);
791 if (len > MAXPATHLEN)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200792 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 /* Look back two slashes from the end. In between these two slashes
795 must be the string __pycache__ or this is not a PEP 3147 style
796 path. It's possible for there to be only one slash.
797 */
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200798 right = rightmost_sep_obj(path, 0, len);
799 if (right == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 return NULL;
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200801 left = rightmost_sep_obj(path, 0, right);
802 if (left == -1)
803 left = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 else
805 left++;
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200806 if (right-left != sizeof(CACHEDIR)-1)
807 return NULL;
808 for (i = 0; i < sizeof(CACHEDIR)-1; i++)
809 if (PyUnicode_READ_CHAR(path, left+i) != CACHEDIR[i])
810 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* Now verify that the path component to the right of the last slash
813 has two dots in it.
814 */
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200815 dot0 = PyUnicode_FindChar(path, '.', right+1, len, 1);
816 if (dot0 < 0)
817 return NULL;
818 dot1 = PyUnicode_FindChar(path, '.', dot0+1, len, 1);
819 if (dot1 < 0)
820 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* Too many dots? */
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200822 if (PyUnicode_FindChar(path, '.', dot1+1, len, 1) != -1)
823 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 /* This is a PEP 3147 path. Start by copying everything from the
826 start of pathname up to and including the leftmost slash. Then
827 copy the file's basename, removing the magic tag and adding a .py
828 suffix.
829 */
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200830 result = PyUnicode_New(left + (dot0-right) + 2,
831 PyUnicode_MAX_CHAR_VALUE(path));
832 if (!result)
833 return NULL;
834 kind = PyUnicode_KIND(result);
835 data = PyUnicode_DATA(result);
836 PyUnicode_CopyCharacters(result, 0, path, 0, (i = left));
837 PyUnicode_CopyCharacters(result, left, path, right+1,
838 (j = dot0-right));
839 PyUnicode_WRITE(kind, data, i+j, 'p');
840 PyUnicode_WRITE(kind, data, i+j+1, 'y');
Victor Stinner8f825062012-04-27 13:55:39 +0200841 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200842 return result;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000843}
844
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000845
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000846static void
847update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 PyObject *constants, *tmp;
850 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 if (PyUnicode_Compare(co->co_filename, oldname))
853 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 tmp = co->co_filename;
856 co->co_filename = newname;
857 Py_INCREF(co->co_filename);
858 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 constants = co->co_consts;
861 n = PyTuple_GET_SIZE(constants);
862 for (i = 0; i < n; i++) {
863 tmp = PyTuple_GET_ITEM(constants, i);
864 if (PyCode_Check(tmp))
865 update_code_filenames((PyCodeObject *)tmp,
866 oldname, newname);
867 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000868}
869
Victor Stinner2f42ae52011-03-20 00:41:24 +0100870static void
871update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000872{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100873 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000874
Victor Stinner2f42ae52011-03-20 00:41:24 +0100875 if (PyUnicode_Compare(co->co_filename, newname) == 0)
876 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 oldname = co->co_filename;
879 Py_INCREF(oldname);
880 update_code_filenames(co, oldname, newname);
881 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000882}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883
Brett Cannon442c9b92011-03-23 16:14:42 -0700884static PyObject *
885imp_fix_co_filename(PyObject *self, PyObject *args)
886{
887 PyObject *co;
888 PyObject *file_path;
889
890 if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
891 return NULL;
892
893 if (!PyCode_Check(co)) {
894 PyErr_SetString(PyExc_TypeError,
895 "first argument must be a code object");
896 return NULL;
897 }
898
899 if (!PyUnicode_Check(file_path)) {
900 PyErr_SetString(PyExc_TypeError,
901 "second argument must be a string");
902 return NULL;
903 }
904
905 update_compiled_module((PyCodeObject*)co, file_path);
906
907 Py_RETURN_NONE;
908}
909
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910
Christian Heimes3b06e532008-01-07 20:12:44 +0000911/* Get source file -> unicode or None
912 * Returns the path to the py file if available, else the given path
913 */
914static PyObject *
Victor Stinnerc9abda02011-03-14 13:33:46 -0400915get_sourcefile(PyObject *filename)
Christian Heimes3b06e532008-01-07 20:12:44 +0000916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 Py_ssize_t len;
Victor Stinnerc9abda02011-03-14 13:33:46 -0400918 PyObject *py;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 struct stat statbuf;
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100920 int err;
Victor Stinner81c39a82012-06-16 03:22:05 +0200921 void *data;
922 unsigned int kind;
Christian Heimes3b06e532008-01-07 20:12:44 +0000923
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200924 len = PyUnicode_GET_LENGTH(filename);
Victor Stinnerc9abda02011-03-14 13:33:46 -0400925 if (len == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 Py_RETURN_NONE;
Christian Heimes3b06e532008-01-07 20:12:44 +0000927
Victor Stinnerc9abda02011-03-14 13:33:46 -0400928 /* don't match *.pyc or *.pyo? */
Victor Stinner81c39a82012-06-16 03:22:05 +0200929 data = PyUnicode_DATA(filename);
930 kind = PyUnicode_KIND(filename);
Victor Stinnerc9abda02011-03-14 13:33:46 -0400931 if (len < 5
Victor Stinner81c39a82012-06-16 03:22:05 +0200932 || PyUnicode_READ(kind, data, len-4) != '.'
933 || (PyUnicode_READ(kind, data, len-3) != 'p'
934 && PyUnicode_READ(kind, data, len-3) != 'P')
935 || (PyUnicode_READ(kind, data, len-2) != 'y'
936 && PyUnicode_READ(kind, data, len-2) != 'Y'))
Victor Stinnerc9abda02011-03-14 13:33:46 -0400937 goto unchanged;
Christian Heimes3b06e532008-01-07 20:12:44 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 /* Start by trying to turn PEP 3147 path into source path. If that
940 * fails, just chop off the trailing character, i.e. legacy pyc path
941 * to py.
942 */
Victor Stinnerc9abda02011-03-14 13:33:46 -0400943 py = make_source_pathname(filename);
944 if (py == NULL) {
945 PyErr_Clear();
Victor Stinner81c39a82012-06-16 03:22:05 +0200946 py = PyUnicode_Substring(filename, 0, len - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 }
Victor Stinnerc9abda02011-03-14 13:33:46 -0400948 if (py == NULL)
949 goto error;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000950
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100951 err = _Py_stat(py, &statbuf);
952 if (err == -2)
953 goto error;
Victor Stinner81c39a82012-06-16 03:22:05 +0200954 if (err == 0 && S_ISREG(statbuf.st_mode))
Victor Stinnerc9abda02011-03-14 13:33:46 -0400955 return py;
956 Py_DECREF(py);
957 goto unchanged;
958
959error:
960 PyErr_Clear();
961unchanged:
962 Py_INCREF(filename);
963 return filename;
Christian Heimes3b06e532008-01-07 20:12:44 +0000964}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000965
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000966/* Forward */
Victor Stinner53dc7352011-03-20 01:50:21 +0100967static struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000968
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000969
970/* Helper to test for built-in module */
971
972static int
Victor Stinner95872862011-03-07 18:20:56 +0100973is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000974{
Victor Stinner95872862011-03-07 18:20:56 +0100975 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +0100977 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
978 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 if (PyImport_Inittab[i].initfunc == NULL)
980 return -1;
981 else
982 return 1;
983 }
984 }
985 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000986}
987
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000988
Just van Rossum52e14d62002-12-30 22:08:05 +0000989/* Return an importer object for a sys.path/pkg.__path__ item 'p',
990 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +0000991 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +0000992 that can handle the path item. Return None if no hook could;
993 this tells our caller it should fall back to the builtin
994 import mechanism. Cache the result in path_importer_cache.
995 Returns a borrowed reference. */
996
997static PyObject *
998get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +00001000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 PyObject *importer;
1002 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 /* These conditions are the caller's responsibility: */
1005 assert(PyList_Check(path_hooks));
1006 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 nhooks = PyList_Size(path_hooks);
1009 if (nhooks < 0)
1010 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 importer = PyDict_GetItem(path_importer_cache, p);
1013 if (importer != NULL)
1014 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* set path_importer_cache[p] to None to avoid recursion */
1017 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1018 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 for (j = 0; j < nhooks; j++) {
1021 PyObject *hook = PyList_GetItem(path_hooks, j);
1022 if (hook == NULL)
1023 return NULL;
1024 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
1025 if (importer != NULL)
1026 break;
Just van Rossum52e14d62002-12-30 22:08:05 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1029 return NULL;
1030 }
1031 PyErr_Clear();
1032 }
1033 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -04001034 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 }
1036 if (importer != NULL) {
1037 int err = PyDict_SetItem(path_importer_cache, p, importer);
1038 Py_DECREF(importer);
1039 if (err != 0)
1040 return NULL;
1041 }
1042 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001043}
1044
Christian Heimes9cd17752007-11-18 19:35:23 +00001045PyAPI_FUNC(PyObject *)
1046PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1050 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1051 importer = get_path_importer(path_importer_cache,
1052 path_hooks, path);
1053 }
1054 }
1055 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1056 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +00001057}
1058
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001059
Victor Stinner95872862011-03-07 18:20:56 +01001060static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001061
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001062/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001063 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001064 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001065
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001066static int
Victor Stinner95872862011-03-07 18:20:56 +01001067init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001070
Victor Stinner95872862011-03-07 18:20:56 +01001071 if (_PyImport_FindExtensionObject(name, name) != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 for (p = PyImport_Inittab; p->name != NULL; p++) {
1075 PyObject *mod;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01001076 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +01001077 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (p->initfunc == NULL) {
1079 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +01001080 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 name);
1082 return -1;
1083 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 mod = (*p->initfunc)();
1085 if (mod == 0)
1086 return -1;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01001087 /* Remember pointer to module init function. */
1088 def = PyModule_GetDef(mod);
1089 def->m_base.m_init = p->initfunc;
Victor Stinner95872862011-03-07 18:20:56 +01001090 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 return -1;
1092 /* FixupExtension has put the module into sys.modules,
1093 so we can release our own reference. */
1094 Py_DECREF(mod);
1095 return 1;
1096 }
1097 }
1098 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001099}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001100
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001102/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103
Guido van Rossumcfd0a221996-06-17 17:06:34 +00001104static struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +01001105find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001108
Victor Stinner53dc7352011-03-20 01:50:21 +01001109 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00001111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 for (p = PyImport_FrozenModules; ; p++) {
1113 if (p->name == NULL)
1114 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001115 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 break;
1117 }
1118 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001119}
1120
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001122get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 struct _frozen *p = find_frozen(name);
1125 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (p == NULL) {
1128 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001129 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 name);
1131 return NULL;
1132 }
1133 if (p->code == NULL) {
1134 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001135 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 name);
1137 return NULL;
1138 }
1139 size = p->size;
1140 if (size < 0)
1141 size = -size;
1142 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001143}
1144
Brett Cannon8d110132009-03-15 02:20:16 +00001145static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001146is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 struct _frozen *p = find_frozen(name);
1149 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 if (p == NULL) {
1152 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001153 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 name);
1155 return NULL;
1156 }
Brett Cannon8d110132009-03-15 02:20:16 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 if (size < 0)
1161 Py_RETURN_TRUE;
1162 else
1163 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001164}
1165
1166
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001167/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001168 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001169 an exception set if the initialization failed.
1170 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001171
1172int
Victor Stinner53dc7352011-03-20 01:50:21 +01001173PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001174{
Victor Stinner53dc7352011-03-20 01:50:21 +01001175 struct _frozen *p;
1176 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 int ispackage;
1178 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001179
Victor Stinner53dc7352011-03-20 01:50:21 +01001180 p = find_frozen(name);
1181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 if (p == NULL)
1183 return 0;
1184 if (p->code == NULL) {
1185 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001186 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 name);
1188 return -1;
1189 }
1190 size = p->size;
1191 ispackage = (size < 0);
1192 if (ispackage)
1193 size = -size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1195 if (co == NULL)
1196 return -1;
1197 if (!PyCode_Check(co)) {
1198 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001199 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 name);
1201 goto err_return;
1202 }
1203 if (ispackage) {
1204 /* Set __path__ to the package name */
Victor Stinner53dc7352011-03-20 01:50:21 +01001205 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001207 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if (m == NULL)
1209 goto err_return;
1210 d = PyModule_GetDict(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 l = PyList_New(1);
1212 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 goto err_return;
1214 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001215 Py_INCREF(name);
1216 PyList_SET_ITEM(l, 0, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 err = PyDict_SetItemString(d, "__path__", l);
1218 Py_DECREF(l);
1219 if (err != 0)
1220 goto err_return;
1221 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001222 path = PyUnicode_FromString("<frozen>");
1223 if (path == NULL)
1224 goto err_return;
1225 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1226 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 if (m == NULL)
1228 goto err_return;
1229 Py_DECREF(co);
1230 Py_DECREF(m);
1231 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001232err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 Py_DECREF(co);
1234 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001235}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001236
Victor Stinner53dc7352011-03-20 01:50:21 +01001237int
1238PyImport_ImportFrozenModule(char *name)
1239{
1240 PyObject *nameobj;
1241 int ret;
1242 nameobj = PyUnicode_InternFromString(name);
1243 if (nameobj == NULL)
1244 return -1;
1245 ret = PyImport_ImportFrozenModuleObject(nameobj);
1246 Py_DECREF(nameobj);
1247 return ret;
1248}
1249
Guido van Rossum74e6a111994-08-29 12:54:38 +00001250
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001251/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001252 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001253
Guido van Rossum79f25d91997-04-29 20:08:16 +00001254PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001255PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 PyObject *pname;
1258 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 pname = PyUnicode_FromString(name);
1261 if (pname == NULL)
1262 return NULL;
1263 result = PyImport_Import(pname);
1264 Py_DECREF(pname);
1265 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001266}
1267
Christian Heimes072c0f12008-01-03 23:01:04 +00001268/* Import a module without blocking
1269 *
1270 * At first it tries to fetch the module from sys.modules. If the module was
1271 * never loaded before it loads it with PyImport_ImportModule() unless another
1272 * thread holds the import lock. In the latter case the function raises an
1273 * ImportError instead of blocking.
1274 *
1275 * Returns the module object with incremented ref count.
1276 */
1277PyObject *
1278PyImport_ImportModuleNoBlock(const char *name)
1279{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001280 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001281}
1282
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001283
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001284/* Remove importlib frames from the traceback,
1285 * except in Verbose mode. */
1286static void
1287remove_importlib_frames(void)
1288{
1289 const char *importlib_filename = "<frozen importlib._bootstrap>";
1290 const char *exec_funcname = "_exec_module";
1291 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001292 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001293 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001294 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001295
1296 /* Synopsis: if it's an ImportError, we trim all importlib chunks
1297 from the traceback. Otherwise, we trim only those chunks which
1298 end with a call to "_exec_module". */
1299
1300 PyErr_Fetch(&exception, &value, &base_tb);
1301 if (!exception || Py_VerboseFlag)
1302 goto done;
1303 if (PyType_IsSubtype((PyTypeObject *) exception,
1304 (PyTypeObject *) PyExc_ImportError))
1305 always_trim = 1;
1306
1307 prev_link = &base_tb;
1308 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001309 while (tb != NULL) {
1310 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1311 PyObject *next = (PyObject *) traceback->tb_next;
1312 PyFrameObject *frame = traceback->tb_frame;
1313 PyCodeObject *code = frame->f_code;
1314 int now_in_importlib;
1315
1316 assert(PyTraceBack_Check(tb));
1317 now_in_importlib = (PyUnicode_CompareWithASCIIString(
1318 code->co_filename,
1319 importlib_filename) == 0);
1320 if (now_in_importlib && !in_importlib) {
1321 /* This is the link to this chunk of importlib tracebacks */
1322 outer_link = prev_link;
1323 }
1324 in_importlib = now_in_importlib;
1325
1326 if (in_importlib &&
1327 (always_trim ||
1328 PyUnicode_CompareWithASCIIString(code->co_name,
1329 exec_funcname) == 0)) {
1330 PyObject *tmp = *outer_link;
1331 *outer_link = next;
1332 Py_XINCREF(next);
1333 Py_DECREF(tmp);
1334 prev_link = outer_link;
1335 }
1336 else {
1337 prev_link = (PyObject **) &traceback->tb_next;
1338 }
1339 tb = next;
1340 }
1341done:
1342 PyErr_Restore(exception, value, base_tb);
1343}
1344
1345
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001346PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04001347PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1348 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001349 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001350{
Brett Cannonfd074152012-04-14 14:10:13 -04001351 _Py_IDENTIFIER(__import__);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001352 _Py_IDENTIFIER(__initializing__);
Brett Cannonfd074152012-04-14 14:10:13 -04001353 _Py_IDENTIFIER(__package__);
1354 _Py_IDENTIFIER(__path__);
1355 _Py_IDENTIFIER(__name__);
1356 _Py_IDENTIFIER(_find_and_load);
1357 _Py_IDENTIFIER(_handle_fromlist);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001358 _Py_IDENTIFIER(_lock_unlock_module);
Brett Cannonfd074152012-04-14 14:10:13 -04001359 _Py_static_string(single_dot, ".");
1360 PyObject *abs_name = NULL;
1361 PyObject *builtins_import = NULL;
1362 PyObject *final_mod = NULL;
1363 PyObject *mod = NULL;
1364 PyObject *package = NULL;
1365 PyObject *globals = NULL;
1366 PyObject *fromlist = NULL;
1367 PyInterpreterState *interp = PyThreadState_GET()->interp;
1368
1369 /* Make sure to use default values so as to not have
1370 PyObject_CallMethodObjArgs() truncate the parameter list because of a
1371 NULL argument. */
1372 if (given_globals == NULL) {
1373 globals = PyDict_New();
1374 if (globals == NULL) {
1375 goto error;
1376 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 }
Brett Cannonfd074152012-04-14 14:10:13 -04001378 else {
1379 /* Only have to care what given_globals is if it will be used
1380 fortsomething. */
1381 if (level > 0 && !PyDict_Check(given_globals)) {
1382 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1383 goto error;
1384 }
1385 globals = given_globals;
1386 Py_INCREF(globals);
1387 }
1388
1389 if (given_fromlist == NULL) {
1390 fromlist = PyList_New(0);
1391 if (fromlist == NULL) {
1392 goto error;
1393 }
1394 }
1395 else {
1396 fromlist = given_fromlist;
1397 Py_INCREF(fromlist);
1398 }
1399 if (name == NULL) {
1400 PyErr_SetString(PyExc_ValueError, "Empty module name");
1401 goto error;
1402 }
1403
1404 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1405 for added performance. */
1406
1407 if (!PyUnicode_Check(name)) {
1408 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1409 goto error;
1410 }
1411 else if (PyUnicode_READY(name) < 0) {
1412 goto error;
1413 }
1414 if (level < 0) {
1415 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1416 goto error;
1417 }
1418 else if (level > 0) {
1419 package = _PyDict_GetItemId(globals, &PyId___package__);
1420 if (package != NULL && package != Py_None) {
1421 Py_INCREF(package);
1422 if (!PyUnicode_Check(package)) {
1423 PyErr_SetString(PyExc_TypeError, "package must be a string");
1424 goto error;
1425 }
1426 }
1427 else {
Brett Cannon273323c2012-04-17 19:05:11 -04001428 package = _PyDict_GetItemId(globals, &PyId___name__);
Brett Cannonfd074152012-04-14 14:10:13 -04001429 if (package == NULL) {
Brett Cannon273323c2012-04-17 19:05:11 -04001430 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
Brett Cannonfd074152012-04-14 14:10:13 -04001431 goto error;
1432 }
1433 else if (!PyUnicode_Check(package)) {
1434 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1435 }
1436 Py_INCREF(package);
1437
1438 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Brett Cannon740fce02012-04-14 14:23:49 -04001439 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001440 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1441 if (borrowed_dot == NULL) {
1442 goto error;
1443 }
Brett Cannon740fce02012-04-14 14:23:49 -04001444 partition = PyUnicode_RPartition(package, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001445 Py_DECREF(package);
1446 if (partition == NULL) {
1447 goto error;
1448 }
1449 package = PyTuple_GET_ITEM(partition, 0);
1450 Py_INCREF(package);
1451 Py_DECREF(partition);
1452 }
1453 }
1454
1455 if (PyDict_GetItem(interp->modules, package) == NULL) {
1456 PyErr_Format(PyExc_SystemError,
1457 "Parent module %R not loaded, cannot perform relative "
1458 "import", package);
1459 goto error;
1460 }
1461 }
1462 else { /* level == 0 */
1463 if (PyUnicode_GET_LENGTH(name) == 0) {
1464 PyErr_SetString(PyExc_ValueError, "Empty module name");
1465 goto error;
1466 }
1467 package = Py_None;
1468 Py_INCREF(package);
1469 }
1470
1471 if (level > 0) {
1472 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1473 PyObject *base = NULL;
1474 int level_up = 1;
1475
1476 for (level_up = 1; level_up < level; level_up += 1) {
1477 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1478 if (last_dot == -2) {
1479 goto error;
1480 }
1481 else if (last_dot == -1) {
1482 PyErr_SetString(PyExc_ValueError,
1483 "attempted relative import beyond top-level "
1484 "package");
1485 goto error;
1486 }
1487 }
1488 base = PyUnicode_Substring(package, 0, last_dot);
1489 if (PyUnicode_GET_LENGTH(name) > 0) {
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001490 PyObject *borrowed_dot, *seq = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001491
1492 borrowed_dot = _PyUnicode_FromId(&single_dot);
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001493 seq = PyTuple_Pack(2, base, name);
1494 Py_DECREF(base);
Brett Cannonfd074152012-04-14 14:10:13 -04001495 if (borrowed_dot == NULL || seq == NULL) {
1496 goto error;
1497 }
1498
1499 abs_name = PyUnicode_Join(borrowed_dot, seq);
1500 Py_DECREF(seq);
1501 if (abs_name == NULL) {
1502 goto error;
1503 }
1504 }
1505 else {
1506 abs_name = base;
1507 }
1508 }
1509 else {
1510 abs_name = name;
1511 Py_INCREF(abs_name);
1512 }
1513
Brian Curtine6b299f2012-04-14 14:19:33 -05001514#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001515 _PyImport_AcquireLock();
1516#endif
1517 /* From this point forward, goto error_with_unlock! */
1518 if (PyDict_Check(globals)) {
1519 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1520 }
1521 if (builtins_import == NULL) {
1522 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1523 if (builtins_import == NULL) {
1524 Py_FatalError("__import__ missing");
1525 }
1526 }
1527 Py_INCREF(builtins_import);
1528
1529 mod = PyDict_GetItem(interp->modules, abs_name);
1530 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001531 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1532 "None in sys.modules", abs_name);
1533 if (msg != NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -05001534 PyErr_SetImportError(msg, abs_name, NULL);
1535 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001536 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001537 mod = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001538 goto error_with_unlock;
1539 }
1540 else if (mod != NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001541 PyObject *value;
1542 int initializing = 0;
1543
Brett Cannonfd074152012-04-14 14:10:13 -04001544 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001545 /* Only call _bootstrap._lock_unlock_module() if __initializing__ is true. */
1546 value = _PyObject_GetAttrId(mod, &PyId___initializing__);
1547 if (value == NULL)
1548 PyErr_Clear();
1549 else {
1550 initializing = PyObject_IsTrue(value);
1551 Py_DECREF(value);
1552 if (initializing == -1)
1553 PyErr_Clear();
1554 }
1555 if (initializing > 0) {
1556 /* _bootstrap._lock_unlock_module() releases the import lock */
1557 value = _PyObject_CallMethodObjIdArgs(interp->importlib,
1558 &PyId__lock_unlock_module, abs_name,
1559 NULL);
1560 if (value == NULL)
1561 goto error;
1562 Py_DECREF(value);
1563 }
1564 else {
1565#ifdef WITH_THREAD
1566 if (_PyImport_ReleaseLock() < 0) {
1567 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1568 goto error;
1569 }
1570#endif
1571 }
Brett Cannonfd074152012-04-14 14:10:13 -04001572 }
1573 else {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001574 /* _bootstrap._find_and_load() releases the import lock */
Brett Cannonfd074152012-04-14 14:10:13 -04001575 mod = _PyObject_CallMethodObjIdArgs(interp->importlib,
1576 &PyId__find_and_load, abs_name,
1577 builtins_import, NULL);
1578 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001579 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001580 }
1581 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001582 /* From now on we don't hold the import lock anymore. */
Brett Cannonfd074152012-04-14 14:10:13 -04001583
1584 if (PyObject_Not(fromlist)) {
1585 if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1586 PyObject *front = NULL;
Brian Curtine6b299f2012-04-14 14:19:33 -05001587 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001588 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1589
1590 if (borrowed_dot == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001591 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001592 }
1593
Brian Curtine6b299f2012-04-14 14:19:33 -05001594 partition = PyUnicode_Partition(name, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001595 if (partition == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001596 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001597 }
1598
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001599 if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1600 /* No dot in module name, simple exit */
1601 Py_DECREF(partition);
1602 final_mod = mod;
1603 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001604 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001605 }
1606
Brett Cannonfd074152012-04-14 14:10:13 -04001607 front = PyTuple_GET_ITEM(partition, 0);
1608 Py_INCREF(front);
1609 Py_DECREF(partition);
1610
1611 if (level == 0) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001612 final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001613 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001614 }
1615 else {
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001616 Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1617 PyUnicode_GET_LENGTH(front);
1618 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001619 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001620 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001621 Py_DECREF(front);
1622 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001623 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001624 }
Brett Cannonfd074152012-04-14 14:10:13 -04001625
1626 final_mod = PyDict_GetItem(interp->modules, to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001627 if (final_mod == NULL) {
1628 PyErr_Format(PyExc_KeyError,
1629 "%R not in sys.modules as expected",
1630 to_return);
1631 }
1632 else {
1633 Py_INCREF(final_mod);
1634 }
Antoine Pitroub78174c2012-05-06 17:15:23 +02001635 Py_DECREF(to_return);
Brett Cannonfd074152012-04-14 14:10:13 -04001636 }
1637 }
1638 else {
1639 final_mod = mod;
1640 Py_INCREF(mod);
1641 }
1642 }
1643 else {
1644 final_mod = _PyObject_CallMethodObjIdArgs(interp->importlib,
1645 &PyId__handle_fromlist, mod,
1646 fromlist, builtins_import,
1647 NULL);
1648 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001649 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001650
Brett Cannonfd074152012-04-14 14:10:13 -04001651 error_with_unlock:
Brian Curtine6b299f2012-04-14 14:19:33 -05001652#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001653 if (_PyImport_ReleaseLock() < 0) {
1654 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1655 }
1656#endif
1657 error:
1658 Py_XDECREF(abs_name);
1659 Py_XDECREF(builtins_import);
1660 Py_XDECREF(mod);
1661 Py_XDECREF(package);
1662 Py_XDECREF(globals);
1663 Py_XDECREF(fromlist);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001664 if (final_mod == NULL)
1665 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001666 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001667}
1668
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001669PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001670PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001671 PyObject *fromlist, int level)
1672{
1673 PyObject *nameobj, *mod;
1674 nameobj = PyUnicode_FromString(name);
1675 if (nameobj == NULL)
1676 return NULL;
1677 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1678 fromlist, level);
1679 Py_DECREF(nameobj);
1680 return mod;
1681}
1682
1683
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001684/* Re-import a module of any kind and return its module object, WITH
1685 INCREMENTED REFERENCE COUNT */
1686
Guido van Rossum79f25d91997-04-29 20:08:16 +00001687PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001688PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001689{
Brett Cannon62228db2012-04-29 14:38:11 -04001690 _Py_IDENTIFIER(reload);
1691 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001693 PyObject *imp = PyDict_GetItemString(modules, "imp");
1694 if (imp == NULL) {
1695 imp = PyImport_ImportModule("imp");
1696 if (imp == NULL) {
1697 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 }
Brett Cannon62228db2012-04-29 14:38:11 -04001700 else {
1701 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001703
Brett Cannon62228db2012-04-29 14:38:11 -04001704 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1705 Py_DECREF(imp);
1706 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001707}
1708
1709
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001710/* Higher-level import emulator which emulates the "import" statement
1711 more accurately -- it invokes the __import__() function from the
1712 builtins of the current globals. This means that the import is
1713 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001714 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001715 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001716 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001717 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001718
1719PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001720PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 static PyObject *silly_list = NULL;
1723 static PyObject *builtins_str = NULL;
1724 static PyObject *import_str = NULL;
1725 PyObject *globals = NULL;
1726 PyObject *import = NULL;
1727 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001728 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 /* Initialize constant string objects */
1732 if (silly_list == NULL) {
1733 import_str = PyUnicode_InternFromString("__import__");
1734 if (import_str == NULL)
1735 return NULL;
1736 builtins_str = PyUnicode_InternFromString("__builtins__");
1737 if (builtins_str == NULL)
1738 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001739 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 if (silly_list == NULL)
1741 return NULL;
1742 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 /* Get the builtins from current globals */
1745 globals = PyEval_GetGlobals();
1746 if (globals != NULL) {
1747 Py_INCREF(globals);
1748 builtins = PyObject_GetItem(globals, builtins_str);
1749 if (builtins == NULL)
1750 goto err;
1751 }
1752 else {
1753 /* No globals -- use standard builtins, and fake globals */
1754 builtins = PyImport_ImportModuleLevel("builtins",
1755 NULL, NULL, NULL, 0);
1756 if (builtins == NULL)
1757 return NULL;
1758 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1759 if (globals == NULL)
1760 goto err;
1761 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 /* Get the __import__ function from the builtins */
1764 if (PyDict_Check(builtins)) {
1765 import = PyObject_GetItem(builtins, import_str);
1766 if (import == NULL)
1767 PyErr_SetObject(PyExc_KeyError, import_str);
1768 }
1769 else
1770 import = PyObject_GetAttr(builtins, import_str);
1771 if (import == NULL)
1772 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001775 Always use absolute import here.
1776 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1778 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001779 if (r == NULL)
1780 goto err;
1781 Py_DECREF(r);
1782
1783 modules = PyImport_GetModuleDict();
1784 r = PyDict_GetItem(modules, module_name);
1785 if (r != NULL)
1786 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001787
1788 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 Py_XDECREF(globals);
1790 Py_XDECREF(builtins);
1791 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001794}
1795
Barry Warsaw28a691b2010-04-17 00:19:56 +00001796static PyObject *
Brett Cannon2657df42012-05-04 15:20:40 -04001797imp_extension_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001800 const char *suffix;
1801 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 list = PyList_New(0);
1804 if (list == NULL)
1805 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001806#ifdef HAVE_DYNAMIC_LOADING
1807 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1808 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 if (item == NULL) {
1810 Py_DECREF(list);
1811 return NULL;
1812 }
1813 if (PyList_Append(list, item) < 0) {
1814 Py_DECREF(list);
1815 Py_DECREF(item);
1816 return NULL;
1817 }
1818 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001819 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 }
Brett Cannon2657df42012-05-04 15:20:40 -04001821#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001823}
1824
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001826imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001827{
Victor Stinner95872862011-03-07 18:20:56 +01001828 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 int ret;
1830 PyObject *m;
Victor Stinner95872862011-03-07 18:20:56 +01001831 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 return NULL;
1833 ret = init_builtin(name);
1834 if (ret < 0)
1835 return NULL;
1836 if (ret == 0) {
1837 Py_INCREF(Py_None);
1838 return Py_None;
1839 }
Victor Stinner95872862011-03-07 18:20:56 +01001840 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 Py_XINCREF(m);
1842 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001843}
1844
Guido van Rossum79f25d91997-04-29 20:08:16 +00001845static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001846imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001847{
Victor Stinner53dc7352011-03-20 01:50:21 +01001848 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 int ret;
1850 PyObject *m;
Victor Stinner53dc7352011-03-20 01:50:21 +01001851 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001853 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 if (ret < 0)
1855 return NULL;
1856 if (ret == 0) {
1857 Py_INCREF(Py_None);
1858 return Py_None;
1859 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001860 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 Py_XINCREF(m);
1862 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001863}
1864
Guido van Rossum79f25d91997-04-29 20:08:16 +00001865static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001866imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001867{
Victor Stinner53dc7352011-03-20 01:50:21 +01001868 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00001869
Victor Stinner53dc7352011-03-20 01:50:21 +01001870 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 return NULL;
1872 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001873}
1874
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00001876imp_is_frozen_package(PyObject *self, PyObject *args)
1877{
Victor Stinner53dc7352011-03-20 01:50:21 +01001878 PyObject *name;
Brett Cannon8d110132009-03-15 02:20:16 +00001879
Victor Stinner53dc7352011-03-20 01:50:21 +01001880 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 return NULL;
1882 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00001883}
1884
1885static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001886imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001887{
Victor Stinner95872862011-03-07 18:20:56 +01001888 PyObject *name;
1889 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 return NULL;
1891 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001892}
1893
Guido van Rossum79f25d91997-04-29 20:08:16 +00001894static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001895imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001896{
Victor Stinner53dc7352011-03-20 01:50:21 +01001897 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001899 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 return NULL;
1901 p = find_frozen(name);
1902 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001903}
1904
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001905#ifdef HAVE_DYNAMIC_LOADING
1906
Guido van Rossum79f25d91997-04-29 20:08:16 +00001907static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001908imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001909{
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001910 PyObject *name, *pathname, *fob = NULL, *mod;
1911 FILE *fp;
1912
1913 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
1914 &name, PyUnicode_FSDecoder, &pathname, &fob))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 return NULL;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001916 if (fob != NULL) {
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001917 fp = _Py_fopen(pathname, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001918 if (fp == NULL) {
1919 Py_DECREF(pathname);
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02001920 if (!PyErr_Occurred())
1921 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001923 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001925 else
1926 fp = NULL;
1927 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
Victor Stinnere9ddbf62011-03-22 10:46:35 +01001928 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 if (fp)
1930 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04001931 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001932}
1933
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001934#endif /* HAVE_DYNAMIC_LOADING */
1935
Barry Warsaw28a691b2010-04-17 00:19:56 +00001936
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001937/* Doc strings */
1938
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001939PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04001940"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001941
Brett Cannon2657df42012-05-04 15:20:40 -04001942PyDoc_STRVAR(doc_extension_suffixes,
1943"extension_suffixes() -> list of strings\n\
1944Returns the list of file suffixes used to identify extension modules.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00001945
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001946PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00001947"lock_held() -> boolean\n\
1948Return True if the import lock is currently held, else False.\n\
1949On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00001950
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001951PyDoc_STRVAR(doc_acquire_lock,
1952"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001953Acquires the interpreter's import lock for the current thread.\n\
1954This lock should be used by import hooks to ensure thread-safety\n\
1955when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00001956On platforms without threads, this function does nothing.");
1957
1958PyDoc_STRVAR(doc_release_lock,
1959"release_lock() -> None\n\
1960Release the interpreter's import lock.\n\
1961On platforms without threads, this function does nothing.");
1962
Guido van Rossum79f25d91997-04-29 20:08:16 +00001963static PyMethodDef imp_methods[] = {
Brett Cannon2657df42012-05-04 15:20:40 -04001964 {"extension_suffixes", imp_extension_suffixes, METH_NOARGS,
1965 doc_extension_suffixes},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
1967 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
1968 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
1970 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
1971 {"init_builtin", imp_init_builtin, METH_VARARGS},
1972 {"init_frozen", imp_init_frozen, METH_VARARGS},
1973 {"is_builtin", imp_is_builtin, METH_VARARGS},
1974 {"is_frozen", imp_is_frozen, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001975#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00001977#endif
Brett Cannon442c9b92011-03-23 16:14:42 -07001978 {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001980};
1981
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001982
Martin v. Löwis1a214512008-06-11 05:26:20 +00001983static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04001985 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 doc_imp,
1987 0,
1988 imp_methods,
1989 NULL,
1990 NULL,
1991 NULL,
1992 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001993};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001994
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001995PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001996PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 m = PyModule_Create(&impmodule);
2001 if (m == NULL)
2002 goto failure;
2003 d = PyModule_GetDict(m);
2004 if (d == NULL)
2005 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002008 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 Py_XDECREF(m);
2010 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002011}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002012
2013
Guido van Rossumb18618d2000-05-03 23:44:39 +00002014/* API for embedding applications that want to add their own entries
2015 to the table of built-in modules. This should normally be called
2016 *before* Py_Initialize(). When the table resize fails, -1 is
2017 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002018
2019 After a similar function by Just van Rossum. */
2020
2021int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002022PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 static struct _inittab *our_copy = NULL;
2025 struct _inittab *p;
2026 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 /* Count the number of entries in both tables */
2029 for (n = 0; newtab[n].name != NULL; n++)
2030 ;
2031 if (n == 0)
2032 return 0; /* Nothing to do */
2033 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2034 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 /* Allocate new memory for the combined table */
2037 p = our_copy;
2038 PyMem_RESIZE(p, struct _inittab, i+n+1);
2039 if (p == NULL)
2040 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 /* Copy the tables into the new memory */
2043 if (our_copy != PyImport_Inittab)
2044 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2045 PyImport_Inittab = our_copy = p;
2046 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002049}
2050
2051/* Shorthand to add a single entry given a name and a function */
2052
2053int
Brett Cannona826f322009-04-02 03:41:46 +00002054PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 newtab[0].name = (char *)name;
2061 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002064}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002065
2066#ifdef __cplusplus
2067}
2068#endif