blob: be0995dac690a85952e0d9e5be48bacbe95ae13b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module definition and import implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008#include "errcode.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +00009#include "marshal.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010#include "code.h"
Antoine Pitroubc07a5c2012-07-08 12:01:27 +020011#include "frameobject.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000012#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000013#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000014
Guido van Rossum55a83382000-09-20 20:31:38 +000015#ifdef HAVE_FCNTL_H
16#include <fcntl.h>
17#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000018#ifdef __cplusplus
Brett Cannon9a5b25a2010-03-01 02:09:17 +000019extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000020#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000021
Barry Warsaw28a691b2010-04-17 00:19:56 +000022#define CACHEDIR "__pycache__"
Guido van Rossum96774c12000-05-01 20:19:08 +000023
Victor Stinner95872862011-03-07 18:20:56 +010024/* See _PyImport_FixupExtensionObject() below */
Guido van Rossum25ce5661997-08-02 03:10:38 +000025static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000026
Guido van Rossum771c6c81997-10-31 18:37:24 +000027/* This table is defined in config.c: */
28extern struct _inittab _PyImport_Inittab[];
29
30struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +000031
Victor Stinnerd0296212011-03-14 14:04:10 -040032static PyObject *initstr = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000033
Brett Cannon4caa61d2014-01-09 19:03:32 -050034/*[clinic input]
35module _imp
36[clinic start generated code]*/
37/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
38
39/*[python input]
40class fs_unicode_converter(CConverter):
41 type = 'PyObject *'
42 converter = 'PyUnicode_FSDecoder'
43
44[python start generated code]*/
45/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
46
Guido van Rossum1ae940a1995-01-02 19:04:15 +000047/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048
49void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000050_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
Victor Stinnerd0296212011-03-14 14:04:10 -040052 initstr = PyUnicode_InternFromString("__init__");
53 if (initstr == NULL)
54 Py_FatalError("Can't initialize import variables");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055}
56
Guido van Rossum25ce5661997-08-02 03:10:38 +000057void
Just van Rossum52e14d62002-12-30 22:08:05 +000058_PyImportHooks_Init(void)
59{
Brett Cannonfd074152012-04-14 14:10:13 -040060 PyObject *v, *path_hooks = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 int err = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +000062
Brett Cannonfd074152012-04-14 14:10:13 -040063 /* adding sys.path_hooks and sys.path_importer_cache */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 v = PyList_New(0);
65 if (v == NULL)
66 goto error;
67 err = PySys_SetObject("meta_path", v);
68 Py_DECREF(v);
69 if (err)
70 goto error;
71 v = PyDict_New();
72 if (v == NULL)
73 goto error;
74 err = PySys_SetObject("path_importer_cache", v);
75 Py_DECREF(v);
76 if (err)
77 goto error;
78 path_hooks = PyList_New(0);
79 if (path_hooks == NULL)
80 goto error;
81 err = PySys_SetObject("path_hooks", path_hooks);
82 if (err) {
Just van Rossum52e14d62002-12-30 22:08:05 +000083 error:
Brett Cannonfd074152012-04-14 14:10:13 -040084 PyErr_Print();
85 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
Nadeem Vawda8f46d652012-05-05 12:27:30 +020086 "or path_importer_cache failed");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 }
Brett Cannonfd074152012-04-14 14:10:13 -040088 Py_DECREF(path_hooks);
89}
90
91void
92_PyImportZip_Init(void)
93{
94 PyObject *path_hooks, *zimpimport;
95 int err = 0;
96
97 path_hooks = PySys_GetObject("path_hooks");
Victor Stinner1e53bba2013-07-16 22:26:05 +020098 if (path_hooks == NULL) {
99 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks");
Brett Cannonfd074152012-04-14 14:10:13 -0400100 goto error;
Victor Stinner1e53bba2013-07-16 22:26:05 +0200101 }
Brett Cannonfd074152012-04-14 14:10:13 -0400102
103 if (Py_VerboseFlag)
104 PySys_WriteStderr("# installing zipimport hook\n");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 zimpimport = PyImport_ImportModule("zipimport");
107 if (zimpimport == NULL) {
108 PyErr_Clear(); /* No zip import module -- okay */
109 if (Py_VerboseFlag)
110 PySys_WriteStderr("# can't import zipimport\n");
111 }
112 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200113 _Py_IDENTIFIER(zipimporter);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200114 PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
115 &PyId_zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 Py_DECREF(zimpimport);
117 if (zipimporter == NULL) {
118 PyErr_Clear(); /* No zipimporter object -- okay */
119 if (Py_VerboseFlag)
120 PySys_WriteStderr(
121 "# can't import zipimport.zipimporter\n");
122 }
123 else {
Brett Cannon8923a4d2012-04-24 22:03:46 -0400124 /* sys.path_hooks.insert(0, zipimporter) */
125 err = PyList_Insert(path_hooks, 0, zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 Py_DECREF(zipimporter);
Brett Cannonfd074152012-04-14 14:10:13 -0400127 if (err < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -0400129 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 if (Py_VerboseFlag)
131 PySys_WriteStderr(
132 "# installed zipimport hook\n");
133 }
134 }
Brett Cannonfd074152012-04-14 14:10:13 -0400135
136 return;
137
138 error:
139 PyErr_Print();
Brett Cannonacf85cd2012-04-29 12:50:03 -0400140 Py_FatalError("initializing zipimport failed");
Just van Rossum52e14d62002-12-30 22:08:05 +0000141}
142
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000143/* Locking primitives to prevent parallel imports of the same module
144 in different threads to return with a partially loaded module.
145 These calls are serialized by the global interpreter lock. */
146
147#ifdef WITH_THREAD
148
Guido van Rossum49b56061998-10-01 20:42:43 +0000149#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000150
Guido van Rossum65d5b571998-12-21 19:32:43 +0000151static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000152static long import_lock_thread = -1;
153static int import_lock_level = 0;
154
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000155void
156_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 long me = PyThread_get_thread_ident();
159 if (me == -1)
160 return; /* Too bad */
161 if (import_lock == NULL) {
162 import_lock = PyThread_allocate_lock();
163 if (import_lock == NULL)
164 return; /* Nothing much we can do. */
165 }
166 if (import_lock_thread == me) {
167 import_lock_level++;
168 return;
169 }
170 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
171 {
172 PyThreadState *tstate = PyEval_SaveThread();
173 PyThread_acquire_lock(import_lock, 1);
174 PyEval_RestoreThread(tstate);
175 }
Antoine Pitrou202b6062012-12-18 22:18:17 +0100176 assert(import_lock_level == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 import_lock_thread = me;
178 import_lock_level = 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000179}
180
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000181int
182_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 long me = PyThread_get_thread_ident();
185 if (me == -1 || import_lock == NULL)
186 return 0; /* Too bad */
187 if (import_lock_thread != me)
188 return -1;
189 import_lock_level--;
Antoine Pitrou202b6062012-12-18 22:18:17 +0100190 assert(import_lock_level >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if (import_lock_level == 0) {
192 import_lock_thread = -1;
193 PyThread_release_lock(import_lock);
194 }
195 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000196}
197
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000198/* This function is called from PyOS_AfterFork to ensure that newly
199 created child processes do not share locks with the parent.
200 We now acquire the import lock around fork() calls but on some platforms
201 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000202
203void
204_PyImport_ReInitLock(void)
205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 if (import_lock != NULL)
207 import_lock = PyThread_allocate_lock();
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000208 if (import_lock_level > 1) {
209 /* Forked as a side effect of import */
210 long me = PyThread_get_thread_ident();
Brett Cannone4710cf2012-11-15 21:39:36 -0500211 /* The following could fail if the lock is already held, but forking as
212 a side-effect of an import is a) rare, b) nuts, and c) difficult to
213 do thanks to the lock only being held when doing individual module
214 locks per import. */
215 PyThread_acquire_lock(import_lock, NOWAIT_LOCK);
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000216 import_lock_thread = me;
217 import_lock_level--;
218 } else {
219 import_lock_thread = -1;
220 import_lock_level = 0;
221 }
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000222}
223
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000224#endif
225
Brett Cannon4caa61d2014-01-09 19:03:32 -0500226/*[clinic input]
227_imp.lock_held
228
229Return True if the import lock is currently held, else False.
230
231On platforms without threads, return False.
232[clinic start generated code]*/
233
234PyDoc_STRVAR(_imp_lock_held__doc__,
235"lock_held()\n"
236"Return True if the import lock is currently held, else False.\n"
237"\n"
238"On platforms without threads, return False.");
239
240#define _IMP_LOCK_HELD_METHODDEF \
241 {"lock_held", (PyCFunction)_imp_lock_held, METH_NOARGS, _imp_lock_held__doc__},
242
Tim Peters69232342001-08-30 05:16:13 +0000243static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -0500244_imp_lock_held_impl(PyModuleDef *module);
245
246static PyObject *
247_imp_lock_held(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
248{
249 PyObject *return_value = NULL;
250
251 return_value = _imp_lock_held_impl(module);
252
253 return return_value;
254}
255
256static PyObject *
257_imp_lock_held_impl(PyModuleDef *module)
258/*[clinic end generated code: checksum=c5858b257881f94dee95526229a8d1a57ccff158]*/
Tim Peters69232342001-08-30 05:16:13 +0000259{
Tim Peters69232342001-08-30 05:16:13 +0000260#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000262#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000264#endif
265}
266
Brett Cannon4caa61d2014-01-09 19:03:32 -0500267/*[clinic input]
268_imp.acquire_lock
269
270Acquires the interpreter's import lock for the current thread.
271
272This lock should be used by import hooks to ensure thread-safety when importing
273modules. On platforms without threads, this function does nothing.
274[clinic start generated code]*/
275
276PyDoc_STRVAR(_imp_acquire_lock__doc__,
277"acquire_lock()\n"
278"Acquires the interpreter\'s import lock for the current thread.\n"
279"\n"
280"This lock should be used by import hooks to ensure thread-safety when importing\n"
281"modules. On platforms without threads, this function does nothing.");
282
283#define _IMP_ACQUIRE_LOCK_METHODDEF \
284 {"acquire_lock", (PyCFunction)_imp_acquire_lock, METH_NOARGS, _imp_acquire_lock__doc__},
285
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000286static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -0500287_imp_acquire_lock_impl(PyModuleDef *module);
288
289static PyObject *
290_imp_acquire_lock(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
291{
292 PyObject *return_value = NULL;
293
294 return_value = _imp_acquire_lock_impl(module);
295
296 return return_value;
297}
298
299static PyObject *
300_imp_acquire_lock_impl(PyModuleDef *module)
301/*[clinic end generated code: checksum=badb56ed0079a6b902c9616fe068d572765b1863]*/
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000302{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000303#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000305#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_INCREF(Py_None);
307 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000308}
309
Brett Cannon4caa61d2014-01-09 19:03:32 -0500310/*[clinic input]
311_imp.release_lock
312
313Release the interpreter's import lock.
314
315On platforms without threads, this function does nothing.
316[clinic start generated code]*/
317
318PyDoc_STRVAR(_imp_release_lock__doc__,
319"release_lock()\n"
320"Release the interpreter\'s import lock.\n"
321"\n"
322"On platforms without threads, this function does nothing.");
323
324#define _IMP_RELEASE_LOCK_METHODDEF \
325 {"release_lock", (PyCFunction)_imp_release_lock, METH_NOARGS, _imp_release_lock__doc__},
326
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000327static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -0500328_imp_release_lock_impl(PyModuleDef *module);
329
330static PyObject *
331_imp_release_lock(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
332{
333 PyObject *return_value = NULL;
334
335 return_value = _imp_release_lock_impl(module);
336
337 return return_value;
338}
339
340static PyObject *
341_imp_release_lock_impl(PyModuleDef *module)
342/*[clinic end generated code: checksum=f1c2a75e3136a113184e0af2a676d5f0b5b685b4]*/
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000343{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000344#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 if (_PyImport_ReleaseLock() < 0) {
346 PyErr_SetString(PyExc_RuntimeError,
347 "not holding the import lock");
348 return NULL;
349 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000350#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 Py_INCREF(Py_None);
352 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000353}
354
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100355void
356_PyImport_Fini(void)
357{
358 Py_XDECREF(extensions);
359 extensions = NULL;
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100360#ifdef WITH_THREAD
361 if (import_lock != NULL) {
362 PyThread_free_lock(import_lock);
363 import_lock = NULL;
364 }
365#endif
366}
367
Guido van Rossum25ce5661997-08-02 03:10:38 +0000368/* Helper for sys */
369
370PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000371PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 PyInterpreterState *interp = PyThreadState_GET()->interp;
374 if (interp->modules == NULL)
375 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
376 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000377}
378
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000380/* List of names to clear in sys */
381static char* sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 "path", "argv", "ps1", "ps2",
383 "last_type", "last_value", "last_traceback",
384 "path_hooks", "path_importer_cache", "meta_path",
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200385 "__interactivehook__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 /* misc stuff */
387 "flags", "float_info",
388 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000389};
390
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000391static char* sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 "stdin", "__stdin__",
393 "stdout", "__stdout__",
394 "stderr", "__stderr__",
395 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000396};
397
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000398/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000399
Guido van Rossum3f5da241990-12-20 15:06:42 +0000400void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000401PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000402{
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200403 Py_ssize_t pos;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 PyObject *key, *value, *dict;
405 PyInterpreterState *interp = PyThreadState_GET()->interp;
406 PyObject *modules = interp->modules;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200407 PyObject *builtins = interp->builtins;
408 PyObject *weaklist = NULL;
Guido van Rossum758eec01998-01-19 21:58:26 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 if (modules == NULL)
411 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 /* Delete some special variables first. These are common
414 places where user values hide and people complain when their
415 destructors fail. Since the modules containing them are
416 deleted *last* of all, they would come too late in the normal
417 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000418
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200419 /* XXX Perhaps these precautions are obsolete. Who knows? */
420
Victor Stinnerbd303c12013-11-07 23:07:29 +0100421 value = PyDict_GetItemString(modules, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 if (value != NULL && PyModule_Check(value)) {
423 dict = PyModule_GetDict(value);
424 if (Py_VerboseFlag)
425 PySys_WriteStderr("# clear builtins._\n");
426 PyDict_SetItemString(dict, "_", Py_None);
427 }
428 value = PyDict_GetItemString(modules, "sys");
429 if (value != NULL && PyModule_Check(value)) {
430 char **p;
431 PyObject *v;
432 dict = PyModule_GetDict(value);
433 for (p = sys_deletes; *p != NULL; p++) {
434 if (Py_VerboseFlag)
435 PySys_WriteStderr("# clear sys.%s\n", *p);
436 PyDict_SetItemString(dict, *p, Py_None);
437 }
438 for (p = sys_files; *p != NULL; p+=2) {
439 if (Py_VerboseFlag)
440 PySys_WriteStderr("# restore sys.%s\n", *p);
441 v = PyDict_GetItemString(dict, *(p+1));
442 if (v == NULL)
443 v = Py_None;
444 PyDict_SetItemString(dict, *p, v);
445 }
446 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000447
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200448 /* We prepare a list which will receive (name, weakref) tuples of
449 modules when they are removed from sys.modules. The name is used
450 for diagnosis messages (in verbose mode), while the weakref helps
451 detect those modules which have been held alive. */
452 weaklist = PyList_New(0);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200453 if (weaklist == NULL)
454 PyErr_Clear();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200455
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200456#define STORE_MODULE_WEAKREF(name, mod) \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200457 if (weaklist != NULL) { \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200458 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
459 if (name && wr) { \
460 PyObject *tup = PyTuple_Pack(2, name, wr); \
461 PyList_Append(weaklist, tup); \
462 Py_XDECREF(tup); \
463 } \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200464 Py_XDECREF(wr); \
465 if (PyErr_Occurred()) \
466 PyErr_Clear(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000468
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200469 /* Remove all modules from sys.modules, hoping that garbage collection
470 can reclaim most of them. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 pos = 0;
472 while (PyDict_Next(modules, &pos, &key, &value)) {
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200473 if (PyModule_Check(value)) {
474 if (Py_VerboseFlag && PyUnicode_Check(key))
475 PySys_FormatStderr("# cleanup[2] removing %U\n", key, value);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200476 STORE_MODULE_WEAKREF(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 PyDict_SetItem(modules, key, Py_None);
478 }
479 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000480
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200481 /* Clear the modules dict. */
482 PyDict_Clear(modules);
483 /* Replace the interpreter's reference to builtins with an empty dict
484 (module globals still have a reference to the original builtins). */
485 builtins = interp->builtins;
486 interp->builtins = PyDict_New();
487 Py_DECREF(builtins);
Antoine Pitrou40322e62013-08-11 00:30:09 +0200488 /* Clear module dict copies stored in the interpreter state */
489 _PyState_ClearModules();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200490 /* Collect references */
491 _PyGC_CollectNoFail();
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200492 /* Dump GC stats before it's too late, since it uses the warnings
493 machinery. */
494 _PyGC_DumpShutdownStats();
495
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200496 /* Now, if there are any modules left alive, clear their globals to
497 minimize potential leaks. All C extension modules actually end
498 up here, since they are kept alive in the interpreter state. */
499 if (weaklist != NULL) {
500 Py_ssize_t i, n;
501 n = PyList_GET_SIZE(weaklist);
502 for (i = 0; i < n; i++) {
503 PyObject *tup = PyList_GET_ITEM(weaklist, i);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200504 PyObject *name = PyTuple_GET_ITEM(tup, 0);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200505 PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
506 if (mod == Py_None)
507 continue;
508 Py_INCREF(mod);
509 assert(PyModule_Check(mod));
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200510 if (Py_VerboseFlag && PyUnicode_Check(name))
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200511 PySys_FormatStderr("# cleanup[3] wiping %U\n",
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200512 name, mod);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200513 _PyModule_Clear(mod);
514 Py_DECREF(mod);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200515 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200516 Py_DECREF(weaklist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000518
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200519 /* Clear and delete the modules directory. Actual modules will
520 still be there only if imported during the execution of some
521 destructor. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 interp->modules = NULL;
523 Py_DECREF(modules);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200524
525 /* Once more */
526 _PyGC_CollectNoFail();
527
528#undef STORE_MODULE_WEAKREF
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000529}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000530
531
Barry Warsaw28a691b2010-04-17 00:19:56 +0000532/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533
534long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000535PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000536{
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200537 long res;
Brett Cannon77b2abd2012-07-09 16:09:00 -0400538 PyInterpreterState *interp = PyThreadState_Get()->interp;
539 PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
540 "_RAW_MAGIC_NUMBER");
541 if (pyc_magic == NULL)
542 return -1;
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200543 res = PyLong_AsLong(pyc_magic);
Benjamin Peterson66f36592012-07-09 22:21:55 -0700544 Py_DECREF(pyc_magic);
545 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546}
547
548
Brett Cannon3adc7b72012-07-09 14:22:12 -0400549extern const char * _PySys_ImplCacheTag;
550
Barry Warsaw28a691b2010-04-17 00:19:56 +0000551const char *
552PyImport_GetMagicTag(void)
553{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400554 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000555}
556
Brett Cannon98979b82012-07-02 15:13:11 -0400557
Guido van Rossum25ce5661997-08-02 03:10:38 +0000558/* Magic for extension modules (built-in as well as dynamically
559 loaded). To prevent initializing an extension module more than
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200560 once, we keep a static dictionary 'extensions' keyed by the tuple
561 (module name, module name) (for built-in modules) or by
562 (filename, module name) (for dynamically loaded modules), containing these
563 modules. A copy of the module's dictionary is stored by calling
564 _PyImport_FixupExtensionObject() immediately after the module initialization
565 function succeeds. A copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100566 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000568 Modules which do support multiple initialization set their m_size
569 field to a non-negative number (indicating the size of the
570 module-specific state). They are still recorded in the extensions
571 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000572*/
573
574int
Victor Stinner95872862011-03-07 18:20:56 +0100575_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
576 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000577{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500578 PyObject *modules, *dict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 struct PyModuleDef *def;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500580 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (extensions == NULL) {
582 extensions = PyDict_New();
583 if (extensions == NULL)
584 return -1;
585 }
586 if (mod == NULL || !PyModule_Check(mod)) {
587 PyErr_BadInternalCall();
588 return -1;
589 }
590 def = PyModule_GetDef(mod);
591 if (!def) {
592 PyErr_BadInternalCall();
593 return -1;
594 }
595 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100596 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return -1;
598 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100599 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 return -1;
601 }
602 if (def->m_size == -1) {
603 if (def->m_base.m_copy) {
604 /* Somebody already imported the module,
605 likely under a different name.
606 XXX this should really not happen. */
607 Py_DECREF(def->m_base.m_copy);
608 def->m_base.m_copy = NULL;
609 }
610 dict = PyModule_GetDict(mod);
611 if (dict == NULL)
612 return -1;
613 def->m_base.m_copy = PyDict_Copy(dict);
614 if (def->m_base.m_copy == NULL)
615 return -1;
616 }
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500617 key = PyTuple_Pack(2, filename, name);
618 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200619 return -1;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500620 res = PyDict_SetItem(extensions, key, (PyObject *)def);
621 Py_DECREF(key);
622 if (res < 0)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200623 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000625}
626
Victor Stinner49d3f252010-10-17 01:24:53 +0000627int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300628_PyImport_FixupBuiltin(PyObject *mod, const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000629{
630 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100631 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100632 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100633 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000634 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100635 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
636 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000637 return res;
638}
639
Guido van Rossum25ce5661997-08-02 03:10:38 +0000640PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100641_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000642{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500643 PyObject *mod, *mdict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 PyModuleDef* def;
645 if (extensions == NULL)
646 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500647 key = PyTuple_Pack(2, filename, name);
648 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200649 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500650 def = (PyModuleDef *)PyDict_GetItem(extensions, key);
651 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 if (def == NULL)
653 return NULL;
654 if (def->m_size == -1) {
655 /* Module does not support repeated initialization */
656 if (def->m_base.m_copy == NULL)
657 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100658 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 if (mod == NULL)
660 return NULL;
661 mdict = PyModule_GetDict(mod);
662 if (mdict == NULL)
663 return NULL;
664 if (PyDict_Update(mdict, def->m_base.m_copy))
665 return NULL;
666 }
667 else {
668 if (def->m_base.m_init == NULL)
669 return NULL;
670 mod = def->m_base.m_init();
671 if (mod == NULL)
672 return NULL;
Christian Heimes09ca7942013-07-20 14:51:53 +0200673 if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) {
674 Py_DECREF(mod);
675 return NULL;
676 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 Py_DECREF(mod);
678 }
679 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100680 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 Py_DECREF(mod);
682 return NULL;
683 }
684 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100685 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 name, filename);
687 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000688
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000689}
690
Victor Stinner49d3f252010-10-17 01:24:53 +0000691PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000692_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000693{
Victor Stinner95872862011-03-07 18:20:56 +0100694 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100695 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100696 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000697 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100698 res = _PyImport_FindExtensionObject(nameobj, nameobj);
699 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000700 return res;
701}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000702
703/* Get the module object corresponding to a module name.
704 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000705 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000706 Because the former action is most common, THIS DOES NOT RETURN A
707 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000710PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 PyObject *modules = PyImport_GetModuleDict();
713 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714
Victor Stinner27ee0892011-03-04 12:57:09 +0000715 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 PyModule_Check(m))
717 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000718 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 if (m == NULL)
720 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000721 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 Py_DECREF(m);
723 return NULL;
724 }
725 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000728}
729
Victor Stinner27ee0892011-03-04 12:57:09 +0000730PyObject *
731PyImport_AddModule(const char *name)
732{
733 PyObject *nameobj, *module;
734 nameobj = PyUnicode_FromString(name);
735 if (nameobj == NULL)
736 return NULL;
737 module = PyImport_AddModuleObject(nameobj);
738 Py_DECREF(nameobj);
739 return module;
740}
741
742
Tim Peters1cd70172004-08-02 03:52:12 +0000743/* Remove name from sys.modules, if it's there. */
744static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000745remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000748 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000750 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 Py_FatalError("import: deleting existing key in"
752 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000753}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754
Christian Heimes3b06e532008-01-07 20:12:44 +0000755
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000756/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000757 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
758 * removed from sys.modules, to avoid leaving damaged module objects
759 * in sys.modules. The caller may wish to restore the original
760 * module object (if any) in this case; PyImport_ReloadModule is an
761 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000762 *
763 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
764 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000765 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000766PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300767PyImport_ExecCodeModule(const char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 return PyImport_ExecCodeModuleWithPathnames(
770 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000771}
772
773PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300774PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 return PyImport_ExecCodeModuleWithPathnames(
777 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000778}
779
780PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300781PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
782 const char *pathname,
783 const char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000784{
Victor Stinner27ee0892011-03-04 12:57:09 +0000785 PyObject *m = NULL;
786 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
787
788 nameobj = PyUnicode_FromString(name);
789 if (nameobj == NULL)
790 return NULL;
791
Victor Stinner27ee0892011-03-04 12:57:09 +0000792 if (cpathname != NULL) {
793 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
794 if (cpathobj == NULL)
795 goto error;
Brett Cannona6473f92012-07-13 13:57:03 -0400796 }
797 else
Victor Stinner27ee0892011-03-04 12:57:09 +0000798 cpathobj = NULL;
Brett Cannona6473f92012-07-13 13:57:03 -0400799
800 if (pathname != NULL) {
801 pathobj = PyUnicode_DecodeFSDefault(pathname);
802 if (pathobj == NULL)
803 goto error;
804 }
805 else if (cpathobj != NULL) {
806 PyInterpreterState *interp = PyThreadState_GET()->interp;
807 _Py_IDENTIFIER(_get_sourcefile);
808
809 if (interp == NULL) {
810 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
811 "no interpreter!");
812 }
813
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -0700814 pathobj = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannona6473f92012-07-13 13:57:03 -0400815 &PyId__get_sourcefile, cpathobj,
816 NULL);
817 if (pathobj == NULL)
818 PyErr_Clear();
819 }
820 else
821 pathobj = NULL;
822
Victor Stinner27ee0892011-03-04 12:57:09 +0000823 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
824error:
825 Py_DECREF(nameobj);
826 Py_XDECREF(pathobj);
827 Py_XDECREF(cpathobj);
828 return m;
829}
830
831PyObject*
832PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
833 PyObject *cpathname)
834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 PyObject *modules = PyImport_GetModuleDict();
836 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000837
Victor Stinner27ee0892011-03-04 12:57:09 +0000838 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 if (m == NULL)
840 return NULL;
841 /* If the module is being reloaded, we get the old module back
842 and re-use its dict to exec the new code. */
843 d = PyModule_GetDict(m);
844 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
845 if (PyDict_SetItemString(d, "__builtins__",
846 PyEval_GetBuiltins()) != 0)
847 goto error;
848 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 if (pathname != NULL) {
Brett Cannona6473f92012-07-13 13:57:03 -0400850 v = pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 }
Brett Cannona6473f92012-07-13 13:57:03 -0400852 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 v = ((PyCodeObject *)co)->co_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 }
Brett Cannona6473f92012-07-13 13:57:03 -0400855 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if (PyDict_SetItemString(d, "__file__", v) != 0)
857 PyErr_Clear(); /* Not important enough to report */
858 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000861 if (cpathname != NULL)
862 v = cpathname;
863 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 if (PyDict_SetItemString(d, "__cached__", v) != 0)
866 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000867
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000868 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (v == NULL)
870 goto error;
871 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000872
Victor Stinner27ee0892011-03-04 12:57:09 +0000873 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000875 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 name);
877 return NULL;
878 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000883
884 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 remove_module(name);
886 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000887}
888
889
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000890static void
891update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 PyObject *constants, *tmp;
894 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 if (PyUnicode_Compare(co->co_filename, oldname))
897 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 tmp = co->co_filename;
900 co->co_filename = newname;
901 Py_INCREF(co->co_filename);
902 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 constants = co->co_consts;
905 n = PyTuple_GET_SIZE(constants);
906 for (i = 0; i < n; i++) {
907 tmp = PyTuple_GET_ITEM(constants, i);
908 if (PyCode_Check(tmp))
909 update_code_filenames((PyCodeObject *)tmp,
910 oldname, newname);
911 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000912}
913
Victor Stinner2f42ae52011-03-20 00:41:24 +0100914static void
915update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000916{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100917 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000918
Victor Stinner2f42ae52011-03-20 00:41:24 +0100919 if (PyUnicode_Compare(co->co_filename, newname) == 0)
920 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 oldname = co->co_filename;
923 Py_INCREF(oldname);
924 update_code_filenames(co, oldname, newname);
925 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000926}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000927
Brett Cannon4caa61d2014-01-09 19:03:32 -0500928/*[clinic input]
929_imp._fix_co_filename
930
931 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
932 Code object to change.
933
934 path: unicode
935 File path to use.
936 /
937
938Changes code.co_filename to specify the passed-in file path.
939[clinic start generated code]*/
940
941PyDoc_STRVAR(_imp__fix_co_filename__doc__,
942"_fix_co_filename(code, path)\n"
943"Changes code.co_filename to specify the passed-in file path.\n"
944"\n"
945" code\n"
946" Code object to change.\n"
947" path\n"
948" File path to use.");
949
950#define _IMP__FIX_CO_FILENAME_METHODDEF \
951 {"_fix_co_filename", (PyCFunction)_imp__fix_co_filename, METH_VARARGS, _imp__fix_co_filename__doc__},
952
Brett Cannon442c9b92011-03-23 16:14:42 -0700953static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -0500954_imp__fix_co_filename_impl(PyModuleDef *module, PyCodeObject *code, PyObject *path);
955
956static PyObject *
957_imp__fix_co_filename(PyModuleDef *module, PyObject *args)
Brett Cannon442c9b92011-03-23 16:14:42 -0700958{
Brett Cannon4caa61d2014-01-09 19:03:32 -0500959 PyObject *return_value = NULL;
960 PyCodeObject *code;
961 PyObject *path;
Brett Cannon442c9b92011-03-23 16:14:42 -0700962
Brett Cannon4caa61d2014-01-09 19:03:32 -0500963 if (!PyArg_ParseTuple(args,
964 "O!U:_fix_co_filename",
965 &PyCode_Type, &code, &path))
966 goto exit;
967 return_value = _imp__fix_co_filename_impl(module, code, path);
Brett Cannon442c9b92011-03-23 16:14:42 -0700968
Brett Cannon4caa61d2014-01-09 19:03:32 -0500969exit:
970 return return_value;
971}
Brett Cannon442c9b92011-03-23 16:14:42 -0700972
Brett Cannon4caa61d2014-01-09 19:03:32 -0500973static PyObject *
974_imp__fix_co_filename_impl(PyModuleDef *module, PyCodeObject *code, PyObject *path)
975/*[clinic end generated code: checksum=4f55bad308072b30ad1921068fc4ce85bd2b39bf]*/
Brett Cannon442c9b92011-03-23 16:14:42 -0700976
Brett Cannon4caa61d2014-01-09 19:03:32 -0500977{
Brett Cannona6dec2e2014-01-10 07:43:55 -0500978 update_compiled_module(code, path);
Brett Cannon442c9b92011-03-23 16:14:42 -0700979
980 Py_RETURN_NONE;
981}
982
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000983
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000984/* Forward */
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700985static const struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000986
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000987
988/* Helper to test for built-in module */
989
990static int
Victor Stinner95872862011-03-07 18:20:56 +0100991is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000992{
Victor Stinner95872862011-03-07 18:20:56 +0100993 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +0100995 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
996 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (PyImport_Inittab[i].initfunc == NULL)
998 return -1;
999 else
1000 return 1;
1001 }
1002 }
1003 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001004}
1005
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001006
Just van Rossum52e14d62002-12-30 22:08:05 +00001007/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1008 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001009 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001010 that can handle the path item. Return None if no hook could;
1011 this tells our caller it should fall back to the builtin
1012 import mechanism. Cache the result in path_importer_cache.
1013 Returns a borrowed reference. */
1014
1015static PyObject *
1016get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +00001018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyObject *importer;
1020 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 /* These conditions are the caller's responsibility: */
1023 assert(PyList_Check(path_hooks));
1024 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 nhooks = PyList_Size(path_hooks);
1027 if (nhooks < 0)
1028 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 importer = PyDict_GetItem(path_importer_cache, p);
1031 if (importer != NULL)
1032 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 /* set path_importer_cache[p] to None to avoid recursion */
1035 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1036 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 for (j = 0; j < nhooks; j++) {
1039 PyObject *hook = PyList_GetItem(path_hooks, j);
1040 if (hook == NULL)
1041 return NULL;
1042 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
1043 if (importer != NULL)
1044 break;
Just van Rossum52e14d62002-12-30 22:08:05 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1047 return NULL;
1048 }
1049 PyErr_Clear();
1050 }
1051 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -04001052 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 }
1054 if (importer != NULL) {
1055 int err = PyDict_SetItem(path_importer_cache, p, importer);
1056 Py_DECREF(importer);
1057 if (err != 0)
1058 return NULL;
1059 }
1060 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001061}
1062
Christian Heimes9cd17752007-11-18 19:35:23 +00001063PyAPI_FUNC(PyObject *)
1064PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +00001066
Victor Stinner1e53bba2013-07-16 22:26:05 +02001067 path_importer_cache = PySys_GetObject("path_importer_cache");
1068 path_hooks = PySys_GetObject("path_hooks");
1069 if (path_importer_cache != NULL && path_hooks != NULL) {
1070 importer = get_path_importer(path_importer_cache,
1071 path_hooks, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 }
1073 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1074 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +00001075}
1076
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001077
Victor Stinner95872862011-03-07 18:20:56 +01001078static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001079
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001080/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001081 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001082 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001083
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001084static int
Victor Stinner95872862011-03-07 18:20:56 +01001085init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 struct _inittab *p;
Victor Stinner5eb4f592013-11-14 22:38:52 +01001088 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001089
Victor Stinner5eb4f592013-11-14 22:38:52 +01001090 mod = _PyImport_FindExtensionObject(name, name);
1091 if (PyErr_Occurred())
1092 return -1;
1093 if (mod != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 for (p = PyImport_Inittab; p->name != NULL; p++) {
1097 PyObject *mod;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01001098 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +01001099 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (p->initfunc == NULL) {
1101 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +01001102 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 name);
1104 return -1;
1105 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 mod = (*p->initfunc)();
1107 if (mod == 0)
1108 return -1;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01001109 /* Remember pointer to module init function. */
1110 def = PyModule_GetDef(mod);
1111 def->m_base.m_init = p->initfunc;
Victor Stinner95872862011-03-07 18:20:56 +01001112 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 return -1;
1114 /* FixupExtension has put the module into sys.modules,
1115 so we can release our own reference. */
1116 Py_DECREF(mod);
1117 return 1;
1118 }
1119 }
1120 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001121}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001122
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001123
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001124/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001125
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001126static const struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +01001127find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001128{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001129 const struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001130
Victor Stinner53dc7352011-03-20 01:50:21 +01001131 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 for (p = PyImport_FrozenModules; ; p++) {
1135 if (p->name == NULL)
1136 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001137 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 break;
1139 }
1140 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001141}
1142
Guido van Rossum79f25d91997-04-29 20:08:16 +00001143static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001144get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001145{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001146 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (p == NULL) {
1150 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001151 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 name);
1153 return NULL;
1154 }
1155 if (p->code == NULL) {
1156 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001157 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 name);
1159 return NULL;
1160 }
1161 size = p->size;
1162 if (size < 0)
1163 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001164 return PyMarshal_ReadObjectFromString((const char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001165}
1166
Brett Cannon8d110132009-03-15 02:20:16 +00001167static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001168is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001169{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001170 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 if (p == NULL) {
1174 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001175 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 name);
1177 return NULL;
1178 }
Brett Cannon8d110132009-03-15 02:20:16 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 if (size < 0)
1183 Py_RETURN_TRUE;
1184 else
1185 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001186}
1187
1188
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001189/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001190 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001191 an exception set if the initialization failed.
1192 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001193
1194int
Victor Stinner53dc7352011-03-20 01:50:21 +01001195PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001196{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001197 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001198 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 int ispackage;
1200 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001201
Victor Stinner53dc7352011-03-20 01:50:21 +01001202 p = find_frozen(name);
1203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 if (p == NULL)
1205 return 0;
1206 if (p->code == NULL) {
1207 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001208 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 name);
1210 return -1;
1211 }
1212 size = p->size;
1213 ispackage = (size < 0);
1214 if (ispackage)
1215 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001216 co = PyMarshal_ReadObjectFromString((const char *)p->code, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 if (co == NULL)
1218 return -1;
1219 if (!PyCode_Check(co)) {
1220 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001221 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 name);
1223 goto err_return;
1224 }
1225 if (ispackage) {
Brett Cannon3e0651b2013-05-31 23:18:39 -04001226 /* Set __path__ to the empty list */
Victor Stinner53dc7352011-03-20 01:50:21 +01001227 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001229 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 if (m == NULL)
1231 goto err_return;
1232 d = PyModule_GetDict(m);
Brett Cannon3e0651b2013-05-31 23:18:39 -04001233 l = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 goto err_return;
1236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 err = PyDict_SetItemString(d, "__path__", l);
1238 Py_DECREF(l);
1239 if (err != 0)
1240 goto err_return;
1241 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001242 path = PyUnicode_FromString("<frozen>");
1243 if (path == NULL)
1244 goto err_return;
1245 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1246 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (m == NULL)
1248 goto err_return;
1249 Py_DECREF(co);
1250 Py_DECREF(m);
1251 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001252err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 Py_DECREF(co);
1254 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001255}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001256
Victor Stinner53dc7352011-03-20 01:50:21 +01001257int
Serhiy Storchakac6792272013-10-19 21:03:34 +03001258PyImport_ImportFrozenModule(const char *name)
Victor Stinner53dc7352011-03-20 01:50:21 +01001259{
1260 PyObject *nameobj;
1261 int ret;
1262 nameobj = PyUnicode_InternFromString(name);
1263 if (nameobj == NULL)
1264 return -1;
1265 ret = PyImport_ImportFrozenModuleObject(nameobj);
1266 Py_DECREF(nameobj);
1267 return ret;
1268}
1269
Guido van Rossum74e6a111994-08-29 12:54:38 +00001270
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001271/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001272 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001273
Guido van Rossum79f25d91997-04-29 20:08:16 +00001274PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001275PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 PyObject *pname;
1278 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 pname = PyUnicode_FromString(name);
1281 if (pname == NULL)
1282 return NULL;
1283 result = PyImport_Import(pname);
1284 Py_DECREF(pname);
1285 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001286}
1287
Christian Heimes072c0f12008-01-03 23:01:04 +00001288/* Import a module without blocking
1289 *
1290 * At first it tries to fetch the module from sys.modules. If the module was
1291 * never loaded before it loads it with PyImport_ImportModule() unless another
1292 * thread holds the import lock. In the latter case the function raises an
1293 * ImportError instead of blocking.
1294 *
1295 * Returns the module object with incremented ref count.
1296 */
1297PyObject *
1298PyImport_ImportModuleNoBlock(const char *name)
1299{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001300 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001301}
1302
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001303
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001304/* Remove importlib frames from the traceback,
1305 * except in Verbose mode. */
1306static void
1307remove_importlib_frames(void)
1308{
1309 const char *importlib_filename = "<frozen importlib._bootstrap>";
Nick Coghlan42c07662012-07-31 21:14:18 +10001310 const char *remove_frames = "_call_with_frames_removed";
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001311 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001312 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001313 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001314 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001315
1316 /* Synopsis: if it's an ImportError, we trim all importlib chunks
Nick Coghlan42c07662012-07-31 21:14:18 +10001317 from the traceback. We always trim chunks
1318 which end with a call to "_call_with_frames_removed". */
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001319
1320 PyErr_Fetch(&exception, &value, &base_tb);
1321 if (!exception || Py_VerboseFlag)
1322 goto done;
1323 if (PyType_IsSubtype((PyTypeObject *) exception,
1324 (PyTypeObject *) PyExc_ImportError))
1325 always_trim = 1;
1326
1327 prev_link = &base_tb;
1328 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001329 while (tb != NULL) {
1330 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1331 PyObject *next = (PyObject *) traceback->tb_next;
1332 PyFrameObject *frame = traceback->tb_frame;
1333 PyCodeObject *code = frame->f_code;
1334 int now_in_importlib;
1335
1336 assert(PyTraceBack_Check(tb));
1337 now_in_importlib = (PyUnicode_CompareWithASCIIString(
1338 code->co_filename,
1339 importlib_filename) == 0);
1340 if (now_in_importlib && !in_importlib) {
1341 /* This is the link to this chunk of importlib tracebacks */
1342 outer_link = prev_link;
1343 }
1344 in_importlib = now_in_importlib;
1345
1346 if (in_importlib &&
1347 (always_trim ||
Nick Coghlan42c07662012-07-31 21:14:18 +10001348 PyUnicode_CompareWithASCIIString(code->co_name,
1349 remove_frames) == 0)) {
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001350 PyObject *tmp = *outer_link;
1351 *outer_link = next;
1352 Py_XINCREF(next);
1353 Py_DECREF(tmp);
1354 prev_link = outer_link;
1355 }
1356 else {
1357 prev_link = (PyObject **) &traceback->tb_next;
1358 }
1359 tb = next;
1360 }
1361done:
1362 PyErr_Restore(exception, value, base_tb);
1363}
1364
1365
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001366PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04001367PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1368 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001369 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001370{
Brett Cannonfd074152012-04-14 14:10:13 -04001371 _Py_IDENTIFIER(__import__);
Eric Snowb523f842013-11-22 09:05:39 -07001372 _Py_IDENTIFIER(__spec__);
1373 _Py_IDENTIFIER(_initializing);
Brett Cannonfd074152012-04-14 14:10:13 -04001374 _Py_IDENTIFIER(__package__);
1375 _Py_IDENTIFIER(__path__);
1376 _Py_IDENTIFIER(__name__);
1377 _Py_IDENTIFIER(_find_and_load);
1378 _Py_IDENTIFIER(_handle_fromlist);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001379 _Py_IDENTIFIER(_lock_unlock_module);
Brett Cannonfd074152012-04-14 14:10:13 -04001380 _Py_static_string(single_dot, ".");
1381 PyObject *abs_name = NULL;
1382 PyObject *builtins_import = NULL;
1383 PyObject *final_mod = NULL;
1384 PyObject *mod = NULL;
1385 PyObject *package = NULL;
1386 PyObject *globals = NULL;
1387 PyObject *fromlist = NULL;
1388 PyInterpreterState *interp = PyThreadState_GET()->interp;
1389
1390 /* Make sure to use default values so as to not have
1391 PyObject_CallMethodObjArgs() truncate the parameter list because of a
1392 NULL argument. */
1393 if (given_globals == NULL) {
1394 globals = PyDict_New();
1395 if (globals == NULL) {
1396 goto error;
1397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 }
Brett Cannonfd074152012-04-14 14:10:13 -04001399 else {
1400 /* Only have to care what given_globals is if it will be used
Brett Cannonecfefb72012-08-05 19:24:57 -04001401 for something. */
Brett Cannonfd074152012-04-14 14:10:13 -04001402 if (level > 0 && !PyDict_Check(given_globals)) {
1403 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1404 goto error;
1405 }
1406 globals = given_globals;
1407 Py_INCREF(globals);
1408 }
1409
1410 if (given_fromlist == NULL) {
1411 fromlist = PyList_New(0);
1412 if (fromlist == NULL) {
1413 goto error;
1414 }
1415 }
1416 else {
1417 fromlist = given_fromlist;
1418 Py_INCREF(fromlist);
1419 }
1420 if (name == NULL) {
1421 PyErr_SetString(PyExc_ValueError, "Empty module name");
1422 goto error;
1423 }
1424
1425 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1426 for added performance. */
1427
1428 if (!PyUnicode_Check(name)) {
1429 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1430 goto error;
1431 }
1432 else if (PyUnicode_READY(name) < 0) {
1433 goto error;
1434 }
1435 if (level < 0) {
1436 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1437 goto error;
1438 }
1439 else if (level > 0) {
1440 package = _PyDict_GetItemId(globals, &PyId___package__);
1441 if (package != NULL && package != Py_None) {
1442 Py_INCREF(package);
1443 if (!PyUnicode_Check(package)) {
1444 PyErr_SetString(PyExc_TypeError, "package must be a string");
1445 goto error;
1446 }
1447 }
1448 else {
Brett Cannon273323c2012-04-17 19:05:11 -04001449 package = _PyDict_GetItemId(globals, &PyId___name__);
Brett Cannonfd074152012-04-14 14:10:13 -04001450 if (package == NULL) {
Brett Cannon273323c2012-04-17 19:05:11 -04001451 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
Brett Cannonfd074152012-04-14 14:10:13 -04001452 goto error;
1453 }
1454 else if (!PyUnicode_Check(package)) {
1455 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1456 }
1457 Py_INCREF(package);
1458
1459 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Brett Cannon740fce02012-04-14 14:23:49 -04001460 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001461 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1462 if (borrowed_dot == NULL) {
1463 goto error;
1464 }
Brett Cannon740fce02012-04-14 14:23:49 -04001465 partition = PyUnicode_RPartition(package, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001466 Py_DECREF(package);
1467 if (partition == NULL) {
1468 goto error;
1469 }
1470 package = PyTuple_GET_ITEM(partition, 0);
1471 Py_INCREF(package);
1472 Py_DECREF(partition);
1473 }
1474 }
1475
1476 if (PyDict_GetItem(interp->modules, package) == NULL) {
1477 PyErr_Format(PyExc_SystemError,
1478 "Parent module %R not loaded, cannot perform relative "
1479 "import", package);
1480 goto error;
1481 }
1482 }
1483 else { /* level == 0 */
1484 if (PyUnicode_GET_LENGTH(name) == 0) {
1485 PyErr_SetString(PyExc_ValueError, "Empty module name");
1486 goto error;
1487 }
1488 package = Py_None;
1489 Py_INCREF(package);
1490 }
1491
1492 if (level > 0) {
1493 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1494 PyObject *base = NULL;
1495 int level_up = 1;
1496
1497 for (level_up = 1; level_up < level; level_up += 1) {
1498 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1499 if (last_dot == -2) {
1500 goto error;
1501 }
1502 else if (last_dot == -1) {
1503 PyErr_SetString(PyExc_ValueError,
1504 "attempted relative import beyond top-level "
1505 "package");
1506 goto error;
1507 }
1508 }
Victor Stinner22af2592013-11-13 12:11:36 +01001509
Brett Cannonfd074152012-04-14 14:10:13 -04001510 base = PyUnicode_Substring(package, 0, last_dot);
Victor Stinner22af2592013-11-13 12:11:36 +01001511 if (base == NULL)
1512 goto error;
1513
Brett Cannonfd074152012-04-14 14:10:13 -04001514 if (PyUnicode_GET_LENGTH(name) > 0) {
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001515 PyObject *borrowed_dot, *seq = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001516
1517 borrowed_dot = _PyUnicode_FromId(&single_dot);
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001518 seq = PyTuple_Pack(2, base, name);
1519 Py_DECREF(base);
Brett Cannonfd074152012-04-14 14:10:13 -04001520 if (borrowed_dot == NULL || seq == NULL) {
1521 goto error;
1522 }
1523
1524 abs_name = PyUnicode_Join(borrowed_dot, seq);
1525 Py_DECREF(seq);
1526 if (abs_name == NULL) {
1527 goto error;
1528 }
1529 }
1530 else {
1531 abs_name = base;
1532 }
1533 }
1534 else {
1535 abs_name = name;
1536 Py_INCREF(abs_name);
1537 }
1538
Brian Curtine6b299f2012-04-14 14:19:33 -05001539#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001540 _PyImport_AcquireLock();
1541#endif
1542 /* From this point forward, goto error_with_unlock! */
1543 if (PyDict_Check(globals)) {
1544 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1545 }
1546 if (builtins_import == NULL) {
1547 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1548 if (builtins_import == NULL) {
Benjamin Peterson7d110042013-04-29 09:08:14 -04001549 PyErr_SetString(PyExc_ImportError, "__import__ not found");
1550 goto error_with_unlock;
Brett Cannonfd074152012-04-14 14:10:13 -04001551 }
1552 }
1553 Py_INCREF(builtins_import);
1554
1555 mod = PyDict_GetItem(interp->modules, abs_name);
1556 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001557 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1558 "None in sys.modules", abs_name);
1559 if (msg != NULL) {
Brett Cannon82da8882013-07-04 17:48:16 -04001560 PyErr_SetImportError(msg, abs_name, NULL);
Brian Curtin09b86d12012-04-17 16:57:09 -05001561 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001562 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001563 mod = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001564 goto error_with_unlock;
1565 }
1566 else if (mod != NULL) {
Eric Snowb523f842013-11-22 09:05:39 -07001567 PyObject *value = NULL;
1568 PyObject *spec;
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001569 int initializing = 0;
1570
Brett Cannonfd074152012-04-14 14:10:13 -04001571 Py_INCREF(mod);
Antoine Pitrou03989852012-08-28 00:24:52 +02001572 /* Optimization: only call _bootstrap._lock_unlock_module() if
Eric Snowb523f842013-11-22 09:05:39 -07001573 __spec__._initializing is true.
1574 NOTE: because of this, initializing must be set *before*
Antoine Pitrou03989852012-08-28 00:24:52 +02001575 stuffing the new module in sys.modules.
1576 */
Eric Snowb523f842013-11-22 09:05:39 -07001577 spec = _PyObject_GetAttrId(mod, &PyId___spec__);
1578 if (spec != NULL) {
1579 value = _PyObject_GetAttrId(spec, &PyId__initializing);
1580 Py_DECREF(spec);
1581 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001582 if (value == NULL)
1583 PyErr_Clear();
1584 else {
1585 initializing = PyObject_IsTrue(value);
1586 Py_DECREF(value);
1587 if (initializing == -1)
1588 PyErr_Clear();
1589 }
1590 if (initializing > 0) {
1591 /* _bootstrap._lock_unlock_module() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001592 value = _PyObject_CallMethodIdObjArgs(interp->importlib,
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001593 &PyId__lock_unlock_module, abs_name,
1594 NULL);
1595 if (value == NULL)
1596 goto error;
1597 Py_DECREF(value);
1598 }
1599 else {
1600#ifdef WITH_THREAD
1601 if (_PyImport_ReleaseLock() < 0) {
1602 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1603 goto error;
1604 }
1605#endif
1606 }
Brett Cannonfd074152012-04-14 14:10:13 -04001607 }
1608 else {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001609 /* _bootstrap._find_and_load() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001610 mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001611 &PyId__find_and_load, abs_name,
1612 builtins_import, NULL);
1613 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001614 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001615 }
1616 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001617 /* From now on we don't hold the import lock anymore. */
Brett Cannonfd074152012-04-14 14:10:13 -04001618
1619 if (PyObject_Not(fromlist)) {
1620 if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1621 PyObject *front = NULL;
Brian Curtine6b299f2012-04-14 14:19:33 -05001622 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001623 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1624
1625 if (borrowed_dot == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001626 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001627 }
1628
Brian Curtine6b299f2012-04-14 14:19:33 -05001629 partition = PyUnicode_Partition(name, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001630 if (partition == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001631 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001632 }
1633
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001634 if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1635 /* No dot in module name, simple exit */
1636 Py_DECREF(partition);
1637 final_mod = mod;
1638 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001639 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001640 }
1641
Brett Cannonfd074152012-04-14 14:10:13 -04001642 front = PyTuple_GET_ITEM(partition, 0);
1643 Py_INCREF(front);
1644 Py_DECREF(partition);
1645
1646 if (level == 0) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001647 final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001648 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001649 }
1650 else {
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001651 Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1652 PyUnicode_GET_LENGTH(front);
1653 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001654 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001655 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001656 Py_DECREF(front);
1657 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001658 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001659 }
Brett Cannonfd074152012-04-14 14:10:13 -04001660
1661 final_mod = PyDict_GetItem(interp->modules, to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001662 if (final_mod == NULL) {
1663 PyErr_Format(PyExc_KeyError,
1664 "%R not in sys.modules as expected",
1665 to_return);
1666 }
1667 else {
1668 Py_INCREF(final_mod);
1669 }
Antoine Pitroub78174c2012-05-06 17:15:23 +02001670 Py_DECREF(to_return);
Brett Cannonfd074152012-04-14 14:10:13 -04001671 }
1672 }
1673 else {
1674 final_mod = mod;
1675 Py_INCREF(mod);
1676 }
1677 }
1678 else {
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001679 final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001680 &PyId__handle_fromlist, mod,
1681 fromlist, builtins_import,
1682 NULL);
1683 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001684 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001685
Brett Cannonfd074152012-04-14 14:10:13 -04001686 error_with_unlock:
Brian Curtine6b299f2012-04-14 14:19:33 -05001687#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001688 if (_PyImport_ReleaseLock() < 0) {
1689 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1690 }
1691#endif
1692 error:
1693 Py_XDECREF(abs_name);
1694 Py_XDECREF(builtins_import);
1695 Py_XDECREF(mod);
1696 Py_XDECREF(package);
1697 Py_XDECREF(globals);
1698 Py_XDECREF(fromlist);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001699 if (final_mod == NULL)
1700 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001701 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001702}
1703
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001704PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001705PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001706 PyObject *fromlist, int level)
1707{
1708 PyObject *nameobj, *mod;
1709 nameobj = PyUnicode_FromString(name);
1710 if (nameobj == NULL)
1711 return NULL;
1712 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1713 fromlist, level);
1714 Py_DECREF(nameobj);
1715 return mod;
1716}
1717
1718
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001719/* Re-import a module of any kind and return its module object, WITH
1720 INCREMENTED REFERENCE COUNT */
1721
Guido van Rossum79f25d91997-04-29 20:08:16 +00001722PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001723PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001724{
Brett Cannon62228db2012-04-29 14:38:11 -04001725 _Py_IDENTIFIER(reload);
1726 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001728 PyObject *imp = PyDict_GetItemString(modules, "imp");
1729 if (imp == NULL) {
1730 imp = PyImport_ImportModule("imp");
1731 if (imp == NULL) {
1732 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 }
Brett Cannon62228db2012-04-29 14:38:11 -04001735 else {
1736 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001738
Brett Cannon62228db2012-04-29 14:38:11 -04001739 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1740 Py_DECREF(imp);
1741 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001742}
1743
1744
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001745/* Higher-level import emulator which emulates the "import" statement
1746 more accurately -- it invokes the __import__() function from the
1747 builtins of the current globals. This means that the import is
1748 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001749 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001750 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001751 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001752 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001753
1754PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001755PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 static PyObject *silly_list = NULL;
1758 static PyObject *builtins_str = NULL;
1759 static PyObject *import_str = NULL;
1760 PyObject *globals = NULL;
1761 PyObject *import = NULL;
1762 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001763 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 /* Initialize constant string objects */
1767 if (silly_list == NULL) {
1768 import_str = PyUnicode_InternFromString("__import__");
1769 if (import_str == NULL)
1770 return NULL;
1771 builtins_str = PyUnicode_InternFromString("__builtins__");
1772 if (builtins_str == NULL)
1773 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001774 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 if (silly_list == NULL)
1776 return NULL;
1777 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 /* Get the builtins from current globals */
1780 globals = PyEval_GetGlobals();
1781 if (globals != NULL) {
1782 Py_INCREF(globals);
1783 builtins = PyObject_GetItem(globals, builtins_str);
1784 if (builtins == NULL)
1785 goto err;
1786 }
1787 else {
1788 /* No globals -- use standard builtins, and fake globals */
1789 builtins = PyImport_ImportModuleLevel("builtins",
1790 NULL, NULL, NULL, 0);
1791 if (builtins == NULL)
1792 return NULL;
1793 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1794 if (globals == NULL)
1795 goto err;
1796 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 /* Get the __import__ function from the builtins */
1799 if (PyDict_Check(builtins)) {
1800 import = PyObject_GetItem(builtins, import_str);
1801 if (import == NULL)
1802 PyErr_SetObject(PyExc_KeyError, import_str);
1803 }
1804 else
1805 import = PyObject_GetAttr(builtins, import_str);
1806 if (import == NULL)
1807 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001810 Always use absolute import here.
1811 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1813 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001814 if (r == NULL)
1815 goto err;
1816 Py_DECREF(r);
1817
1818 modules = PyImport_GetModuleDict();
1819 r = PyDict_GetItem(modules, module_name);
1820 if (r != NULL)
1821 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001822
1823 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 Py_XDECREF(globals);
1825 Py_XDECREF(builtins);
1826 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001829}
1830
Brett Cannon4caa61d2014-01-09 19:03:32 -05001831/*[clinic input]
1832_imp.extension_suffixes
1833
1834Returns the list of file suffixes used to identify extension modules.
1835[clinic start generated code]*/
1836
1837PyDoc_STRVAR(_imp_extension_suffixes__doc__,
1838"extension_suffixes()\n"
1839"Returns the list of file suffixes used to identify extension modules.");
1840
1841#define _IMP_EXTENSION_SUFFIXES_METHODDEF \
1842 {"extension_suffixes", (PyCFunction)_imp_extension_suffixes, METH_NOARGS, _imp_extension_suffixes__doc__},
1843
Barry Warsaw28a691b2010-04-17 00:19:56 +00001844static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05001845_imp_extension_suffixes_impl(PyModuleDef *module);
1846
1847static PyObject *
1848_imp_extension_suffixes(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
1849{
1850 PyObject *return_value = NULL;
1851
1852 return_value = _imp_extension_suffixes_impl(module);
1853
1854 return return_value;
1855}
1856
1857static PyObject *
1858_imp_extension_suffixes_impl(PyModuleDef *module)
1859/*[clinic end generated code: checksum=835921e67fd698e22e101eea64839d1ad62b6451]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001862 const char *suffix;
1863 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 list = PyList_New(0);
1866 if (list == NULL)
1867 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001868#ifdef HAVE_DYNAMIC_LOADING
1869 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1870 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 if (item == NULL) {
1872 Py_DECREF(list);
1873 return NULL;
1874 }
1875 if (PyList_Append(list, item) < 0) {
1876 Py_DECREF(list);
1877 Py_DECREF(item);
1878 return NULL;
1879 }
1880 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001881 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 }
Brett Cannon2657df42012-05-04 15:20:40 -04001883#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001885}
1886
Brett Cannon4caa61d2014-01-09 19:03:32 -05001887/*[clinic input]
1888_imp.init_builtin
1889
1890 name: unicode
1891 /
1892
1893Initializes a built-in module.
1894[clinic start generated code]*/
1895
1896PyDoc_STRVAR(_imp_init_builtin__doc__,
1897"init_builtin(name)\n"
1898"Initializes a built-in module.");
1899
1900#define _IMP_INIT_BUILTIN_METHODDEF \
1901 {"init_builtin", (PyCFunction)_imp_init_builtin, METH_VARARGS, _imp_init_builtin__doc__},
1902
Guido van Rossum79f25d91997-04-29 20:08:16 +00001903static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05001904_imp_init_builtin_impl(PyModuleDef *module, PyObject *name);
1905
1906static PyObject *
1907_imp_init_builtin(PyModuleDef *module, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001908{
Brett Cannon4caa61d2014-01-09 19:03:32 -05001909 PyObject *return_value = NULL;
Victor Stinner95872862011-03-07 18:20:56 +01001910 PyObject *name;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001911
1912 if (!PyArg_ParseTuple(args,
1913 "U:init_builtin",
1914 &name))
1915 goto exit;
1916 return_value = _imp_init_builtin_impl(module, name);
1917
1918exit:
1919 return return_value;
1920}
1921
1922static PyObject *
1923_imp_init_builtin_impl(PyModuleDef *module, PyObject *name)
1924/*[clinic end generated code: checksum=59239206e5b2fb59358066e72fd0e72e55a7baf5]*/
1925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 int ret;
1927 PyObject *m;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 ret = init_builtin(name);
1930 if (ret < 0)
1931 return NULL;
1932 if (ret == 0) {
1933 Py_INCREF(Py_None);
1934 return Py_None;
1935 }
Victor Stinner95872862011-03-07 18:20:56 +01001936 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 Py_XINCREF(m);
1938 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001939}
1940
Brett Cannon4caa61d2014-01-09 19:03:32 -05001941/*[clinic input]
1942_imp.init_frozen
1943
1944 name: unicode
1945 /
1946
1947Initializes a frozen module.
1948[clinic start generated code]*/
1949
1950PyDoc_STRVAR(_imp_init_frozen__doc__,
1951"init_frozen(name)\n"
1952"Initializes a frozen module.");
1953
1954#define _IMP_INIT_FROZEN_METHODDEF \
1955 {"init_frozen", (PyCFunction)_imp_init_frozen, METH_VARARGS, _imp_init_frozen__doc__},
1956
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05001958_imp_init_frozen_impl(PyModuleDef *module, PyObject *name);
1959
1960static PyObject *
1961_imp_init_frozen(PyModuleDef *module, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001962{
Brett Cannon4caa61d2014-01-09 19:03:32 -05001963 PyObject *return_value = NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001964 PyObject *name;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001965
1966 if (!PyArg_ParseTuple(args,
1967 "U:init_frozen",
1968 &name))
1969 goto exit;
1970 return_value = _imp_init_frozen_impl(module, name);
1971
1972exit:
1973 return return_value;
1974}
1975
1976static PyObject *
1977_imp_init_frozen_impl(PyModuleDef *module, PyObject *name)
1978/*[clinic end generated code: checksum=503fcc3de9961263e4d9484259af357a7d287a0b]*/
1979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 int ret;
1981 PyObject *m;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001982
Victor Stinner53dc7352011-03-20 01:50:21 +01001983 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 if (ret < 0)
1985 return NULL;
1986 if (ret == 0) {
1987 Py_INCREF(Py_None);
1988 return Py_None;
1989 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001990 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 Py_XINCREF(m);
1992 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001993}
1994
Brett Cannon4caa61d2014-01-09 19:03:32 -05001995/*[clinic input]
1996_imp.get_frozen_object
1997
1998 name: unicode
1999 /
2000
2001Create a code object for a frozen module.
2002[clinic start generated code]*/
2003
2004PyDoc_STRVAR(_imp_get_frozen_object__doc__,
2005"get_frozen_object(name)\n"
2006"Create a code object for a frozen module.");
2007
2008#define _IMP_GET_FROZEN_OBJECT_METHODDEF \
2009 {"get_frozen_object", (PyCFunction)_imp_get_frozen_object, METH_VARARGS, _imp_get_frozen_object__doc__},
2010
Guido van Rossum79f25d91997-04-29 20:08:16 +00002011static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05002012_imp_get_frozen_object_impl(PyModuleDef *module, PyObject *name);
2013
2014static PyObject *
2015_imp_get_frozen_object(PyModuleDef *module, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002016{
Brett Cannon4caa61d2014-01-09 19:03:32 -05002017 PyObject *return_value = NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01002018 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002019
Brett Cannon4caa61d2014-01-09 19:03:32 -05002020 if (!PyArg_ParseTuple(args,
2021 "U:get_frozen_object",
2022 &name))
2023 goto exit;
2024 return_value = _imp_get_frozen_object_impl(module, name);
2025
2026exit:
2027 return return_value;
2028}
2029
2030static PyObject *
2031_imp_get_frozen_object_impl(PyModuleDef *module, PyObject *name)
2032/*[clinic end generated code: checksum=7a6423a4daf139496b9a394ff3ac6130089d1cba]*/
2033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002035}
2036
Brett Cannon4caa61d2014-01-09 19:03:32 -05002037/*[clinic input]
2038_imp.is_frozen_package
2039
2040 name: unicode
2041 /
2042
2043Returns True if the module name is of a frozen package.
2044[clinic start generated code]*/
2045
2046PyDoc_STRVAR(_imp_is_frozen_package__doc__,
2047"is_frozen_package(name)\n"
2048"Returns True if the module name is of a frozen package.");
2049
2050#define _IMP_IS_FROZEN_PACKAGE_METHODDEF \
2051 {"is_frozen_package", (PyCFunction)_imp_is_frozen_package, METH_VARARGS, _imp_is_frozen_package__doc__},
2052
Guido van Rossum79f25d91997-04-29 20:08:16 +00002053static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05002054_imp_is_frozen_package_impl(PyModuleDef *module, PyObject *name);
2055
2056static PyObject *
2057_imp_is_frozen_package(PyModuleDef *module, PyObject *args)
Brett Cannon8d110132009-03-15 02:20:16 +00002058{
Brett Cannon4caa61d2014-01-09 19:03:32 -05002059 PyObject *return_value = NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01002060 PyObject *name;
Brett Cannon8d110132009-03-15 02:20:16 +00002061
Brett Cannon4caa61d2014-01-09 19:03:32 -05002062 if (!PyArg_ParseTuple(args,
2063 "U:is_frozen_package",
2064 &name))
2065 goto exit;
2066 return_value = _imp_is_frozen_package_impl(module, name);
2067
2068exit:
2069 return return_value;
2070}
2071
2072static PyObject *
2073_imp_is_frozen_package_impl(PyModuleDef *module, PyObject *name)
2074/*[clinic end generated code: checksum=dc7e361ea30b6945b8bbe7266d7b9a5ea433b510]*/
2075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00002077}
2078
Brett Cannon4caa61d2014-01-09 19:03:32 -05002079/*[clinic input]
2080_imp.is_builtin
2081
2082 name: unicode
2083 /
2084
2085Returns True if the module name corresponds to a built-in module.
2086[clinic start generated code]*/
2087
2088PyDoc_STRVAR(_imp_is_builtin__doc__,
2089"is_builtin(name)\n"
2090"Returns True if the module name corresponds to a built-in module.");
2091
2092#define _IMP_IS_BUILTIN_METHODDEF \
2093 {"is_builtin", (PyCFunction)_imp_is_builtin, METH_VARARGS, _imp_is_builtin__doc__},
2094
Brett Cannon8d110132009-03-15 02:20:16 +00002095static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05002096_imp_is_builtin_impl(PyModuleDef *module, PyObject *name);
2097
2098static PyObject *
2099_imp_is_builtin(PyModuleDef *module, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002100{
Brett Cannon4caa61d2014-01-09 19:03:32 -05002101 PyObject *return_value = NULL;
Victor Stinner95872862011-03-07 18:20:56 +01002102 PyObject *name;
Brett Cannon4caa61d2014-01-09 19:03:32 -05002103
2104 if (!PyArg_ParseTuple(args,
2105 "U:is_builtin",
2106 &name))
2107 goto exit;
2108 return_value = _imp_is_builtin_impl(module, name);
2109
2110exit:
2111 return return_value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002112}
2113
Guido van Rossum79f25d91997-04-29 20:08:16 +00002114static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05002115_imp_is_builtin_impl(PyModuleDef *module, PyObject *name)
2116/*[clinic end generated code: checksum=353938c1d55210a1e3850d3ccba7539d02165cac]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002117{
Brett Cannon4caa61d2014-01-09 19:03:32 -05002118 return PyLong_FromLong(is_builtin(name));
2119}
2120
2121/*[clinic input]
2122_imp.is_frozen
2123
2124 name: unicode
2125 /
2126
2127Returns True if the module name corresponds to a frozen module.
2128[clinic start generated code]*/
2129
2130PyDoc_STRVAR(_imp_is_frozen__doc__,
2131"is_frozen(name)\n"
2132"Returns True if the module name corresponds to a frozen module.");
2133
2134#define _IMP_IS_FROZEN_METHODDEF \
2135 {"is_frozen", (PyCFunction)_imp_is_frozen, METH_VARARGS, _imp_is_frozen__doc__},
2136
2137static PyObject *
2138_imp_is_frozen_impl(PyModuleDef *module, PyObject *name);
2139
2140static PyObject *
2141_imp_is_frozen(PyModuleDef *module, PyObject *args)
2142{
2143 PyObject *return_value = NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01002144 PyObject *name;
Brett Cannon4caa61d2014-01-09 19:03:32 -05002145
2146 if (!PyArg_ParseTuple(args,
2147 "U:is_frozen",
2148 &name))
2149 goto exit;
2150 return_value = _imp_is_frozen_impl(module, name);
2151
2152exit:
2153 return return_value;
2154}
2155
2156static PyObject *
2157_imp_is_frozen_impl(PyModuleDef *module, PyObject *name)
2158/*[clinic end generated code: checksum=978b547ddcb76fa6c4a181ad53569c9acf382c7b]*/
2159{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07002160 const struct _frozen *p;
Brett Cannon4caa61d2014-01-09 19:03:32 -05002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 p = find_frozen(name);
2163 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002164}
2165
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002166#ifdef HAVE_DYNAMIC_LOADING
2167
Brett Cannon4caa61d2014-01-09 19:03:32 -05002168/*[clinic input]
2169_imp.load_dynamic
2170
2171 name: unicode
2172 path: fs_unicode
2173 file: object = NULL
2174 /
2175
2176Loads an extension module.
2177[clinic start generated code]*/
2178
2179PyDoc_STRVAR(_imp_load_dynamic__doc__,
2180"load_dynamic(name, path, file=None)\n"
2181"Loads an extension module.");
2182
2183#define _IMP_LOAD_DYNAMIC_METHODDEF \
2184 {"load_dynamic", (PyCFunction)_imp_load_dynamic, METH_VARARGS, _imp_load_dynamic__doc__},
2185
Guido van Rossum79f25d91997-04-29 20:08:16 +00002186static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05002187_imp_load_dynamic_impl(PyModuleDef *module, PyObject *name, PyObject *path, PyObject *file);
2188
2189static PyObject *
2190_imp_load_dynamic(PyModuleDef *module, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002191{
Brett Cannon4caa61d2014-01-09 19:03:32 -05002192 PyObject *return_value = NULL;
2193 PyObject *name;
2194 PyObject *path;
2195 PyObject *file = NULL;
2196
2197 if (!PyArg_ParseTuple(args,
2198 "UO&|O:load_dynamic",
2199 &name, PyUnicode_FSDecoder, &path, &file))
2200 goto exit;
2201 return_value = _imp_load_dynamic_impl(module, name, path, file);
2202
2203exit:
2204 return return_value;
2205}
2206
2207static PyObject *
2208_imp_load_dynamic_impl(PyModuleDef *module, PyObject *name, PyObject *path, PyObject *file)
2209/*[clinic end generated code: checksum=6795f65d9ce003ccaf08e4e8eef484dc52e262d0]*/
2210{
2211 PyObject *mod;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002212 FILE *fp;
2213
Brett Cannon4caa61d2014-01-09 19:03:32 -05002214 if (file != NULL) {
2215 fp = _Py_fopen_obj(path, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01002216 if (fp == NULL) {
Brett Cannon4caa61d2014-01-09 19:03:32 -05002217 Py_DECREF(path);
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02002218 if (!PyErr_Occurred())
2219 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01002221 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002223 else
2224 fp = NULL;
Brett Cannon4caa61d2014-01-09 19:03:32 -05002225 mod = _PyImport_LoadDynamicModule(name, path, fp);
2226 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 if (fp)
2228 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002229 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002230}
2231
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002232#endif /* HAVE_DYNAMIC_LOADING */
2233
Barry Warsaw28a691b2010-04-17 00:19:56 +00002234
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002235PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04002236"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002237
Guido van Rossum79f25d91997-04-29 20:08:16 +00002238static PyMethodDef imp_methods[] = {
Brett Cannon4caa61d2014-01-09 19:03:32 -05002239 _IMP_EXTENSION_SUFFIXES_METHODDEF
2240 _IMP_LOCK_HELD_METHODDEF
2241 _IMP_ACQUIRE_LOCK_METHODDEF
2242 _IMP_RELEASE_LOCK_METHODDEF
2243 _IMP_GET_FROZEN_OBJECT_METHODDEF
2244 _IMP_IS_FROZEN_PACKAGE_METHODDEF
2245 _IMP_INIT_BUILTIN_METHODDEF
2246 _IMP_INIT_FROZEN_METHODDEF
2247 _IMP_IS_BUILTIN_METHODDEF
2248 _IMP_IS_FROZEN_METHODDEF
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002249#ifdef HAVE_DYNAMIC_LOADING
Brett Cannon4caa61d2014-01-09 19:03:32 -05002250 _IMP_LOAD_DYNAMIC_METHODDEF
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002251#endif
Brett Cannon4caa61d2014-01-09 19:03:32 -05002252 _IMP__FIX_CO_FILENAME_METHODDEF
2253 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002254};
2255
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002256
Martin v. Löwis1a214512008-06-11 05:26:20 +00002257static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04002259 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 doc_imp,
2261 0,
2262 imp_methods,
2263 NULL,
2264 NULL,
2265 NULL,
2266 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002267};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002268
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002269PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002270PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 m = PyModule_Create(&impmodule);
2275 if (m == NULL)
2276 goto failure;
2277 d = PyModule_GetDict(m);
2278 if (d == NULL)
2279 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002282 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 Py_XDECREF(m);
2284 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002285}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002286
2287
Guido van Rossumb18618d2000-05-03 23:44:39 +00002288/* API for embedding applications that want to add their own entries
2289 to the table of built-in modules. This should normally be called
2290 *before* Py_Initialize(). When the table resize fails, -1 is
2291 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002292
2293 After a similar function by Just van Rossum. */
2294
2295int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002296PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 static struct _inittab *our_copy = NULL;
2299 struct _inittab *p;
2300 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 /* Count the number of entries in both tables */
2303 for (n = 0; newtab[n].name != NULL; n++)
2304 ;
2305 if (n == 0)
2306 return 0; /* Nothing to do */
2307 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2308 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 /* Allocate new memory for the combined table */
2311 p = our_copy;
2312 PyMem_RESIZE(p, struct _inittab, i+n+1);
2313 if (p == NULL)
2314 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 /* Copy the tables into the new memory */
2317 if (our_copy != PyImport_Inittab)
2318 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2319 PyImport_Inittab = our_copy = p;
2320 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002323}
2324
2325/* Shorthand to add a single entry given a name and a function */
2326
2327int
Brett Cannona826f322009-04-02 03:41:46 +00002328PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 newtab[0].name = (char *)name;
2335 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002338}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002339
2340#ifdef __cplusplus
2341}
2342#endif