blob: d0115e46bf1102323d30668dcfce32a7fbb7d5f4 [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]*/
Larry Hastings581ee362014-01-28 05:00:08 -080037/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -050038
39/*[python input]
40class fs_unicode_converter(CConverter):
41 type = 'PyObject *'
42 converter = 'PyUnicode_FSDecoder'
43
44[python start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080045/*[python end generated code: output=da39a3ee5e6b4b0d input=9d6786230166006e]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -050046
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__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800235"lock_held($module, /)\n"
236"--\n"
237"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -0500238"Return True if the import lock is currently held, else False.\n"
239"\n"
240"On platforms without threads, return False.");
241
242#define _IMP_LOCK_HELD_METHODDEF \
243 {"lock_held", (PyCFunction)_imp_lock_held, METH_NOARGS, _imp_lock_held__doc__},
244
Tim Peters69232342001-08-30 05:16:13 +0000245static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -0500246_imp_lock_held_impl(PyModuleDef *module);
247
248static PyObject *
249_imp_lock_held(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
250{
Larry Hastingsbebf7352014-01-17 17:47:17 -0800251 return _imp_lock_held_impl(module);
Brett Cannon4caa61d2014-01-09 19:03:32 -0500252}
253
254static PyObject *
255_imp_lock_held_impl(PyModuleDef *module)
Larry Hastings2623c8c2014-02-08 22:15:29 -0800256/*[clinic end generated code: output=dae65674966baa65 input=9b088f9b217d9bdf]*/
Tim Peters69232342001-08-30 05:16:13 +0000257{
Tim Peters69232342001-08-30 05:16:13 +0000258#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000260#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000262#endif
263}
264
Brett Cannon4caa61d2014-01-09 19:03:32 -0500265/*[clinic input]
266_imp.acquire_lock
267
268Acquires the interpreter's import lock for the current thread.
269
270This lock should be used by import hooks to ensure thread-safety when importing
271modules. On platforms without threads, this function does nothing.
272[clinic start generated code]*/
273
274PyDoc_STRVAR(_imp_acquire_lock__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800275"acquire_lock($module, /)\n"
276"--\n"
277"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -0500278"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{
Larry Hastingsbebf7352014-01-17 17:47:17 -0800292 return _imp_acquire_lock_impl(module);
Brett Cannon4caa61d2014-01-09 19:03:32 -0500293}
294
295static PyObject *
296_imp_acquire_lock_impl(PyModuleDef *module)
Larry Hastings2623c8c2014-02-08 22:15:29 -0800297/*[clinic end generated code: output=478f1fa089fdb9a4 input=4a2d4381866d5fdc]*/
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000298{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000299#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000301#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 Py_INCREF(Py_None);
303 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000304}
305
Brett Cannon4caa61d2014-01-09 19:03:32 -0500306/*[clinic input]
307_imp.release_lock
308
309Release the interpreter's import lock.
310
311On platforms without threads, this function does nothing.
312[clinic start generated code]*/
313
314PyDoc_STRVAR(_imp_release_lock__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800315"release_lock($module, /)\n"
316"--\n"
317"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -0500318"Release the interpreter\'s import lock.\n"
319"\n"
320"On platforms without threads, this function does nothing.");
321
322#define _IMP_RELEASE_LOCK_METHODDEF \
323 {"release_lock", (PyCFunction)_imp_release_lock, METH_NOARGS, _imp_release_lock__doc__},
324
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000325static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -0500326_imp_release_lock_impl(PyModuleDef *module);
327
328static PyObject *
329_imp_release_lock(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
330{
Larry Hastingsbebf7352014-01-17 17:47:17 -0800331 return _imp_release_lock_impl(module);
Brett Cannon4caa61d2014-01-09 19:03:32 -0500332}
333
334static PyObject *
335_imp_release_lock_impl(PyModuleDef *module)
Larry Hastings2623c8c2014-02-08 22:15:29 -0800336/*[clinic end generated code: output=36c77a6832fdafd4 input=934fb11516dd778b]*/
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000337{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000338#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (_PyImport_ReleaseLock() < 0) {
340 PyErr_SetString(PyExc_RuntimeError,
341 "not holding the import lock");
342 return NULL;
343 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000344#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 Py_INCREF(Py_None);
346 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000347}
348
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100349void
350_PyImport_Fini(void)
351{
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200352 Py_CLEAR(extensions);
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100353#ifdef WITH_THREAD
354 if (import_lock != NULL) {
355 PyThread_free_lock(import_lock);
356 import_lock = NULL;
357 }
358#endif
359}
360
Guido van Rossum25ce5661997-08-02 03:10:38 +0000361/* Helper for sys */
362
363PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000364PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 PyInterpreterState *interp = PyThreadState_GET()->interp;
367 if (interp->modules == NULL)
368 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
369 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000370}
371
Guido van Rossum3f5da241990-12-20 15:06:42 +0000372
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000373/* List of names to clear in sys */
374static char* sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 "path", "argv", "ps1", "ps2",
376 "last_type", "last_value", "last_traceback",
377 "path_hooks", "path_importer_cache", "meta_path",
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200378 "__interactivehook__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 /* misc stuff */
380 "flags", "float_info",
381 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000382};
383
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000384static char* sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 "stdin", "__stdin__",
386 "stdout", "__stdout__",
387 "stderr", "__stderr__",
388 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000389};
390
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000391/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000392
Guido van Rossum3f5da241990-12-20 15:06:42 +0000393void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000394PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000395{
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200396 Py_ssize_t pos;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 PyObject *key, *value, *dict;
398 PyInterpreterState *interp = PyThreadState_GET()->interp;
399 PyObject *modules = interp->modules;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200400 PyObject *builtins = interp->builtins;
401 PyObject *weaklist = NULL;
Guido van Rossum758eec01998-01-19 21:58:26 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (modules == NULL)
404 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 /* Delete some special variables first. These are common
407 places where user values hide and people complain when their
408 destructors fail. Since the modules containing them are
409 deleted *last* of all, they would come too late in the normal
410 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000411
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200412 /* XXX Perhaps these precautions are obsolete. Who knows? */
413
Victor Stinnerbd303c12013-11-07 23:07:29 +0100414 value = PyDict_GetItemString(modules, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (value != NULL && PyModule_Check(value)) {
416 dict = PyModule_GetDict(value);
417 if (Py_VerboseFlag)
418 PySys_WriteStderr("# clear builtins._\n");
419 PyDict_SetItemString(dict, "_", Py_None);
420 }
421 value = PyDict_GetItemString(modules, "sys");
422 if (value != NULL && PyModule_Check(value)) {
423 char **p;
424 PyObject *v;
425 dict = PyModule_GetDict(value);
426 for (p = sys_deletes; *p != NULL; p++) {
427 if (Py_VerboseFlag)
428 PySys_WriteStderr("# clear sys.%s\n", *p);
429 PyDict_SetItemString(dict, *p, Py_None);
430 }
431 for (p = sys_files; *p != NULL; p+=2) {
432 if (Py_VerboseFlag)
433 PySys_WriteStderr("# restore sys.%s\n", *p);
434 v = PyDict_GetItemString(dict, *(p+1));
435 if (v == NULL)
436 v = Py_None;
437 PyDict_SetItemString(dict, *p, v);
438 }
439 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000440
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200441 /* We prepare a list which will receive (name, weakref) tuples of
442 modules when they are removed from sys.modules. The name is used
443 for diagnosis messages (in verbose mode), while the weakref helps
444 detect those modules which have been held alive. */
445 weaklist = PyList_New(0);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200446 if (weaklist == NULL)
447 PyErr_Clear();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200448
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200449#define STORE_MODULE_WEAKREF(name, mod) \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200450 if (weaklist != NULL) { \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200451 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
452 if (name && wr) { \
453 PyObject *tup = PyTuple_Pack(2, name, wr); \
454 PyList_Append(weaklist, tup); \
455 Py_XDECREF(tup); \
456 } \
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200457 Py_XDECREF(wr); \
458 if (PyErr_Occurred()) \
459 PyErr_Clear(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000461
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200462 /* Remove all modules from sys.modules, hoping that garbage collection
463 can reclaim most of them. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 pos = 0;
465 while (PyDict_Next(modules, &pos, &key, &value)) {
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200466 if (PyModule_Check(value)) {
467 if (Py_VerboseFlag && PyUnicode_Check(key))
468 PySys_FormatStderr("# cleanup[2] removing %U\n", key, value);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200469 STORE_MODULE_WEAKREF(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyDict_SetItem(modules, key, Py_None);
471 }
472 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000473
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200474 /* Clear the modules dict. */
475 PyDict_Clear(modules);
476 /* Replace the interpreter's reference to builtins with an empty dict
477 (module globals still have a reference to the original builtins). */
478 builtins = interp->builtins;
479 interp->builtins = PyDict_New();
480 Py_DECREF(builtins);
Antoine Pitrou40322e62013-08-11 00:30:09 +0200481 /* Clear module dict copies stored in the interpreter state */
482 _PyState_ClearModules();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200483 /* Collect references */
484 _PyGC_CollectNoFail();
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200485 /* Dump GC stats before it's too late, since it uses the warnings
486 machinery. */
487 _PyGC_DumpShutdownStats();
488
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200489 /* Now, if there are any modules left alive, clear their globals to
490 minimize potential leaks. All C extension modules actually end
491 up here, since they are kept alive in the interpreter state. */
492 if (weaklist != NULL) {
493 Py_ssize_t i, n;
494 n = PyList_GET_SIZE(weaklist);
495 for (i = 0; i < n; i++) {
496 PyObject *tup = PyList_GET_ITEM(weaklist, i);
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200497 PyObject *name = PyTuple_GET_ITEM(tup, 0);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200498 PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
499 if (mod == Py_None)
500 continue;
501 Py_INCREF(mod);
502 assert(PyModule_Check(mod));
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200503 if (Py_VerboseFlag && PyUnicode_Check(name))
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200504 PySys_FormatStderr("# cleanup[3] wiping %U\n",
Antoine Pitrou79ba3882013-08-06 22:50:15 +0200505 name, mod);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200506 _PyModule_Clear(mod);
507 Py_DECREF(mod);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200508 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200509 Py_DECREF(weaklist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000511
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200512 /* Clear and delete the modules directory. Actual modules will
513 still be there only if imported during the execution of some
514 destructor. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 interp->modules = NULL;
516 Py_DECREF(modules);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200517
518 /* Once more */
519 _PyGC_CollectNoFail();
520
521#undef STORE_MODULE_WEAKREF
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000522}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000523
524
Barry Warsaw28a691b2010-04-17 00:19:56 +0000525/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000526
527long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000528PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000529{
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200530 long res;
Brett Cannon77b2abd2012-07-09 16:09:00 -0400531 PyInterpreterState *interp = PyThreadState_Get()->interp;
532 PyObject *pyc_magic = PyObject_GetAttrString(interp->importlib,
533 "_RAW_MAGIC_NUMBER");
534 if (pyc_magic == NULL)
535 return -1;
Antoine Pitrou44b4b6a2012-07-10 18:27:54 +0200536 res = PyLong_AsLong(pyc_magic);
Benjamin Peterson66f36592012-07-09 22:21:55 -0700537 Py_DECREF(pyc_magic);
538 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539}
540
541
Brett Cannon3adc7b72012-07-09 14:22:12 -0400542extern const char * _PySys_ImplCacheTag;
543
Barry Warsaw28a691b2010-04-17 00:19:56 +0000544const char *
545PyImport_GetMagicTag(void)
546{
Brett Cannon3adc7b72012-07-09 14:22:12 -0400547 return _PySys_ImplCacheTag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000548}
549
Brett Cannon98979b82012-07-02 15:13:11 -0400550
Guido van Rossum25ce5661997-08-02 03:10:38 +0000551/* Magic for extension modules (built-in as well as dynamically
552 loaded). To prevent initializing an extension module more than
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200553 once, we keep a static dictionary 'extensions' keyed by the tuple
554 (module name, module name) (for built-in modules) or by
555 (filename, module name) (for dynamically loaded modules), containing these
556 modules. A copy of the module's dictionary is stored by calling
557 _PyImport_FixupExtensionObject() immediately after the module initialization
558 function succeeds. A copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100559 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000560
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000561 Modules which do support multiple initialization set their m_size
562 field to a non-negative number (indicating the size of the
563 module-specific state). They are still recorded in the extensions
564 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000565*/
566
567int
Victor Stinner95872862011-03-07 18:20:56 +0100568_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
569 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000570{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500571 PyObject *modules, *dict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 struct PyModuleDef *def;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500573 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (extensions == NULL) {
575 extensions = PyDict_New();
576 if (extensions == NULL)
577 return -1;
578 }
579 if (mod == NULL || !PyModule_Check(mod)) {
580 PyErr_BadInternalCall();
581 return -1;
582 }
583 def = PyModule_GetDef(mod);
584 if (!def) {
585 PyErr_BadInternalCall();
586 return -1;
587 }
588 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100589 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 return -1;
591 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100592 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 return -1;
594 }
595 if (def->m_size == -1) {
596 if (def->m_base.m_copy) {
597 /* Somebody already imported the module,
598 likely under a different name.
599 XXX this should really not happen. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200600 Py_CLEAR(def->m_base.m_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 }
602 dict = PyModule_GetDict(mod);
603 if (dict == NULL)
604 return -1;
605 def->m_base.m_copy = PyDict_Copy(dict);
606 if (def->m_base.m_copy == NULL)
607 return -1;
608 }
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500609 key = PyTuple_Pack(2, filename, name);
610 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200611 return -1;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500612 res = PyDict_SetItem(extensions, key, (PyObject *)def);
613 Py_DECREF(key);
614 if (res < 0)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200615 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000617}
618
Victor Stinner49d3f252010-10-17 01:24:53 +0000619int
Serhiy Storchakac6792272013-10-19 21:03:34 +0300620_PyImport_FixupBuiltin(PyObject *mod, const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000621{
622 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100623 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100624 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100625 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000626 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100627 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
628 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000629 return res;
630}
631
Guido van Rossum25ce5661997-08-02 03:10:38 +0000632PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100633_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000634{
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500635 PyObject *mod, *mdict, *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 PyModuleDef* def;
637 if (extensions == NULL)
638 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500639 key = PyTuple_Pack(2, filename, name);
640 if (key == NULL)
Andrew Svetlov6b2cbeb2012-12-14 17:04:59 +0200641 return NULL;
Benjamin Peterson5cb8a312012-12-15 00:05:16 -0500642 def = (PyModuleDef *)PyDict_GetItem(extensions, key);
643 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 if (def == NULL)
645 return NULL;
646 if (def->m_size == -1) {
647 /* Module does not support repeated initialization */
648 if (def->m_base.m_copy == NULL)
649 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100650 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 if (mod == NULL)
652 return NULL;
653 mdict = PyModule_GetDict(mod);
654 if (mdict == NULL)
655 return NULL;
656 if (PyDict_Update(mdict, def->m_base.m_copy))
657 return NULL;
658 }
659 else {
660 if (def->m_base.m_init == NULL)
661 return NULL;
662 mod = def->m_base.m_init();
663 if (mod == NULL)
664 return NULL;
Christian Heimes09ca7942013-07-20 14:51:53 +0200665 if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) {
666 Py_DECREF(mod);
667 return NULL;
668 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 Py_DECREF(mod);
670 }
671 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100672 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 Py_DECREF(mod);
674 return NULL;
675 }
676 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100677 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 name, filename);
679 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000680
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000681}
682
Victor Stinner49d3f252010-10-17 01:24:53 +0000683PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000684_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000685{
Victor Stinner95872862011-03-07 18:20:56 +0100686 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100687 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100688 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000689 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100690 res = _PyImport_FindExtensionObject(nameobj, nameobj);
691 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000692 return res;
693}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000694
695/* Get the module object corresponding to a module name.
696 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000697 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000698 Because the former action is most common, THIS DOES NOT RETURN A
699 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000700
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000702PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 PyObject *modules = PyImport_GetModuleDict();
705 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000706
Victor Stinner27ee0892011-03-04 12:57:09 +0000707 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 PyModule_Check(m))
709 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000710 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 if (m == NULL)
712 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000713 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 Py_DECREF(m);
715 return NULL;
716 }
717 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000720}
721
Victor Stinner27ee0892011-03-04 12:57:09 +0000722PyObject *
723PyImport_AddModule(const char *name)
724{
725 PyObject *nameobj, *module;
726 nameobj = PyUnicode_FromString(name);
727 if (nameobj == NULL)
728 return NULL;
729 module = PyImport_AddModuleObject(nameobj);
730 Py_DECREF(nameobj);
731 return module;
732}
733
734
Tim Peters1cd70172004-08-02 03:52:12 +0000735/* Remove name from sys.modules, if it's there. */
736static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000737remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000740 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000742 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 Py_FatalError("import: deleting existing key in"
744 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000745}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746
Christian Heimes3b06e532008-01-07 20:12:44 +0000747
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000748/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000749 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
750 * removed from sys.modules, to avoid leaving damaged module objects
751 * in sys.modules. The caller may wish to restore the original
752 * module object (if any) in this case; PyImport_ReloadModule is an
753 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000754 *
755 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
756 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000757 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000758PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300759PyImport_ExecCodeModule(const char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 return PyImport_ExecCodeModuleWithPathnames(
762 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000763}
764
765PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300766PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 return PyImport_ExecCodeModuleWithPathnames(
769 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000770}
771
772PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300773PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
774 const char *pathname,
775 const char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000776{
Victor Stinner27ee0892011-03-04 12:57:09 +0000777 PyObject *m = NULL;
778 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
779
780 nameobj = PyUnicode_FromString(name);
781 if (nameobj == NULL)
782 return NULL;
783
Victor Stinner27ee0892011-03-04 12:57:09 +0000784 if (cpathname != NULL) {
785 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
786 if (cpathobj == NULL)
787 goto error;
Brett Cannona6473f92012-07-13 13:57:03 -0400788 }
789 else
Victor Stinner27ee0892011-03-04 12:57:09 +0000790 cpathobj = NULL;
Brett Cannona6473f92012-07-13 13:57:03 -0400791
792 if (pathname != NULL) {
793 pathobj = PyUnicode_DecodeFSDefault(pathname);
794 if (pathobj == NULL)
795 goto error;
796 }
797 else if (cpathobj != NULL) {
798 PyInterpreterState *interp = PyThreadState_GET()->interp;
799 _Py_IDENTIFIER(_get_sourcefile);
800
801 if (interp == NULL) {
802 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
803 "no interpreter!");
804 }
805
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -0700806 pathobj = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannona6473f92012-07-13 13:57:03 -0400807 &PyId__get_sourcefile, cpathobj,
808 NULL);
809 if (pathobj == NULL)
810 PyErr_Clear();
811 }
812 else
813 pathobj = NULL;
814
Victor Stinner27ee0892011-03-04 12:57:09 +0000815 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
816error:
817 Py_DECREF(nameobj);
818 Py_XDECREF(pathobj);
819 Py_XDECREF(cpathobj);
820 return m;
821}
822
823PyObject*
824PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
825 PyObject *cpathname)
826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 PyObject *modules = PyImport_GetModuleDict();
828 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000829
Victor Stinner27ee0892011-03-04 12:57:09 +0000830 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 if (m == NULL)
832 return NULL;
833 /* If the module is being reloaded, we get the old module back
834 and re-use its dict to exec the new code. */
835 d = PyModule_GetDict(m);
836 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
837 if (PyDict_SetItemString(d, "__builtins__",
838 PyEval_GetBuiltins()) != 0)
839 goto error;
840 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 if (pathname != NULL) {
Brett Cannona6473f92012-07-13 13:57:03 -0400842 v = pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 }
Brett Cannona6473f92012-07-13 13:57:03 -0400844 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 v = ((PyCodeObject *)co)->co_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 }
Brett Cannona6473f92012-07-13 13:57:03 -0400847 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 if (PyDict_SetItemString(d, "__file__", v) != 0)
849 PyErr_Clear(); /* Not important enough to report */
850 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000853 if (cpathname != NULL)
854 v = cpathname;
855 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (PyDict_SetItemString(d, "__cached__", v) != 0)
858 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000859
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000860 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 if (v == NULL)
862 goto error;
863 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000864
Victor Stinner27ee0892011-03-04 12:57:09 +0000865 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000867 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 name);
869 return NULL;
870 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000875
876 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 remove_module(name);
878 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000879}
880
881
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000882static void
883update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 PyObject *constants, *tmp;
886 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (PyUnicode_Compare(co->co_filename, oldname))
889 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 tmp = co->co_filename;
892 co->co_filename = newname;
893 Py_INCREF(co->co_filename);
894 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 constants = co->co_consts;
897 n = PyTuple_GET_SIZE(constants);
898 for (i = 0; i < n; i++) {
899 tmp = PyTuple_GET_ITEM(constants, i);
900 if (PyCode_Check(tmp))
901 update_code_filenames((PyCodeObject *)tmp,
902 oldname, newname);
903 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000904}
905
Victor Stinner2f42ae52011-03-20 00:41:24 +0100906static void
907update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000908{
Victor Stinner2f42ae52011-03-20 00:41:24 +0100909 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000910
Victor Stinner2f42ae52011-03-20 00:41:24 +0100911 if (PyUnicode_Compare(co->co_filename, newname) == 0)
912 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 oldname = co->co_filename;
915 Py_INCREF(oldname);
916 update_code_filenames(co, oldname, newname);
917 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +0000918}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000919
Brett Cannon4caa61d2014-01-09 19:03:32 -0500920/*[clinic input]
921_imp._fix_co_filename
922
923 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
924 Code object to change.
925
926 path: unicode
927 File path to use.
928 /
929
930Changes code.co_filename to specify the passed-in file path.
931[clinic start generated code]*/
932
933PyDoc_STRVAR(_imp__fix_co_filename__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800934"_fix_co_filename($module, code, path, /)\n"
935"--\n"
936"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -0500937"Changes code.co_filename to specify the passed-in file path.\n"
938"\n"
939" code\n"
940" Code object to change.\n"
941" path\n"
942" File path to use.");
943
944#define _IMP__FIX_CO_FILENAME_METHODDEF \
945 {"_fix_co_filename", (PyCFunction)_imp__fix_co_filename, METH_VARARGS, _imp__fix_co_filename__doc__},
946
Brett Cannon442c9b92011-03-23 16:14:42 -0700947static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -0500948_imp__fix_co_filename_impl(PyModuleDef *module, PyCodeObject *code, PyObject *path);
949
950static PyObject *
951_imp__fix_co_filename(PyModuleDef *module, PyObject *args)
Brett Cannon442c9b92011-03-23 16:14:42 -0700952{
Brett Cannon4caa61d2014-01-09 19:03:32 -0500953 PyObject *return_value = NULL;
954 PyCodeObject *code;
955 PyObject *path;
Brett Cannon442c9b92011-03-23 16:14:42 -0700956
Brett Cannon4caa61d2014-01-09 19:03:32 -0500957 if (!PyArg_ParseTuple(args,
958 "O!U:_fix_co_filename",
959 &PyCode_Type, &code, &path))
960 goto exit;
961 return_value = _imp__fix_co_filename_impl(module, code, path);
Brett Cannon442c9b92011-03-23 16:14:42 -0700962
Brett Cannon4caa61d2014-01-09 19:03:32 -0500963exit:
964 return return_value;
965}
Brett Cannon442c9b92011-03-23 16:14:42 -0700966
Brett Cannon4caa61d2014-01-09 19:03:32 -0500967static PyObject *
968_imp__fix_co_filename_impl(PyModuleDef *module, PyCodeObject *code, PyObject *path)
Larry Hastings2623c8c2014-02-08 22:15:29 -0800969/*[clinic end generated code: output=6b4b1edeb0d55c5d input=895ba50e78b82f05]*/
Brett Cannon442c9b92011-03-23 16:14:42 -0700970
Brett Cannon4caa61d2014-01-09 19:03:32 -0500971{
Brett Cannona6dec2e2014-01-10 07:43:55 -0500972 update_compiled_module(code, path);
Brett Cannon442c9b92011-03-23 16:14:42 -0700973
974 Py_RETURN_NONE;
975}
976
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000977
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000978/* Forward */
Benjamin Peterson6fba3db2013-03-18 23:13:31 -0700979static const struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000980
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000981
982/* Helper to test for built-in module */
983
984static int
Victor Stinner95872862011-03-07 18:20:56 +0100985is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000986{
Victor Stinner95872862011-03-07 18:20:56 +0100987 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +0100989 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
990 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 if (PyImport_Inittab[i].initfunc == NULL)
992 return -1;
993 else
994 return 1;
995 }
996 }
997 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +0000998}
999
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001000
Just van Rossum52e14d62002-12-30 22:08:05 +00001001/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1002 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001003 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001004 that can handle the path item. Return None if no hook could;
1005 this tells our caller it should fall back to the builtin
1006 import mechanism. Cache the result in path_importer_cache.
1007 Returns a borrowed reference. */
1008
1009static PyObject *
1010get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +00001012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 PyObject *importer;
1014 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* These conditions are the caller's responsibility: */
1017 assert(PyList_Check(path_hooks));
1018 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 nhooks = PyList_Size(path_hooks);
1021 if (nhooks < 0)
1022 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 importer = PyDict_GetItem(path_importer_cache, p);
1025 if (importer != NULL)
1026 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 /* set path_importer_cache[p] to None to avoid recursion */
1029 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1030 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 for (j = 0; j < nhooks; j++) {
1033 PyObject *hook = PyList_GetItem(path_hooks, j);
1034 if (hook == NULL)
1035 return NULL;
1036 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
1037 if (importer != NULL)
1038 break;
Just van Rossum52e14d62002-12-30 22:08:05 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1041 return NULL;
1042 }
1043 PyErr_Clear();
1044 }
1045 if (importer == NULL) {
Brett Cannonaa936422012-04-27 15:30:58 -04001046 return Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 }
1048 if (importer != NULL) {
1049 int err = PyDict_SetItem(path_importer_cache, p, importer);
1050 Py_DECREF(importer);
1051 if (err != 0)
1052 return NULL;
1053 }
1054 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001055}
1056
Christian Heimes9cd17752007-11-18 19:35:23 +00001057PyAPI_FUNC(PyObject *)
1058PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +00001060
Victor Stinner1e53bba2013-07-16 22:26:05 +02001061 path_importer_cache = PySys_GetObject("path_importer_cache");
1062 path_hooks = PySys_GetObject("path_hooks");
1063 if (path_importer_cache != NULL && path_hooks != NULL) {
1064 importer = get_path_importer(path_importer_cache,
1065 path_hooks, path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 }
1067 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1068 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +00001069}
1070
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001071
Victor Stinner95872862011-03-07 18:20:56 +01001072static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001073
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001074/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001075 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001076 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001077
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001078static int
Victor Stinner95872862011-03-07 18:20:56 +01001079init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 struct _inittab *p;
Victor Stinner5eb4f592013-11-14 22:38:52 +01001082 PyObject *mod;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001083
Victor Stinner5eb4f592013-11-14 22:38:52 +01001084 mod = _PyImport_FindExtensionObject(name, name);
1085 if (PyErr_Occurred())
1086 return -1;
1087 if (mod != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 for (p = PyImport_Inittab; p->name != NULL; p++) {
1091 PyObject *mod;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01001092 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +01001093 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (p->initfunc == NULL) {
1095 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +01001096 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 name);
1098 return -1;
1099 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 mod = (*p->initfunc)();
1101 if (mod == 0)
1102 return -1;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01001103 /* Remember pointer to module init function. */
1104 def = PyModule_GetDef(mod);
1105 def->m_base.m_init = p->initfunc;
Victor Stinner95872862011-03-07 18:20:56 +01001106 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 return -1;
1108 /* FixupExtension has put the module into sys.modules,
1109 so we can release our own reference. */
1110 Py_DECREF(mod);
1111 return 1;
1112 }
1113 }
1114 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00001115}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001116
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001117
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001118/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001119
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001120static const struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +01001121find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001122{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001123 const struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001124
Victor Stinner53dc7352011-03-20 01:50:21 +01001125 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 for (p = PyImport_FrozenModules; ; p++) {
1129 if (p->name == NULL)
1130 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001131 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 break;
1133 }
1134 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001135}
1136
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001138get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001139{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001140 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 if (p == NULL) {
1144 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001145 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 name);
1147 return NULL;
1148 }
1149 if (p->code == NULL) {
1150 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001151 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 name);
1153 return NULL;
1154 }
1155 size = p->size;
1156 if (size < 0)
1157 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001158 return PyMarshal_ReadObjectFromString((const char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001159}
1160
Brett Cannon8d110132009-03-15 02:20:16 +00001161static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01001162is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00001163{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001164 const struct _frozen *p = find_frozen(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (p == NULL) {
1168 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001169 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 name);
1171 return NULL;
1172 }
Brett Cannon8d110132009-03-15 02:20:16 +00001173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00001175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 if (size < 0)
1177 Py_RETURN_TRUE;
1178 else
1179 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00001180}
1181
1182
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001183/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00001184 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001185 an exception set if the initialization failed.
1186 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00001187
1188int
Victor Stinner53dc7352011-03-20 01:50:21 +01001189PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001190{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07001191 const struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01001192 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 int ispackage;
1194 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001195
Victor Stinner53dc7352011-03-20 01:50:21 +01001196 p = find_frozen(name);
1197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 if (p == NULL)
1199 return 0;
1200 if (p->code == NULL) {
1201 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001202 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 name);
1204 return -1;
1205 }
1206 size = p->size;
1207 ispackage = (size < 0);
1208 if (ispackage)
1209 size = -size;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001210 co = PyMarshal_ReadObjectFromString((const char *)p->code, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (co == NULL)
1212 return -1;
1213 if (!PyCode_Check(co)) {
1214 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01001215 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 name);
1217 goto err_return;
1218 }
1219 if (ispackage) {
Brett Cannon3e0651b2013-05-31 23:18:39 -04001220 /* Set __path__ to the empty list */
Victor Stinner53dc7352011-03-20 01:50:21 +01001221 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001223 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 if (m == NULL)
1225 goto err_return;
1226 d = PyModule_GetDict(m);
Brett Cannon3e0651b2013-05-31 23:18:39 -04001227 l = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 goto err_return;
1230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 err = PyDict_SetItemString(d, "__path__", l);
1232 Py_DECREF(l);
1233 if (err != 0)
1234 goto err_return;
1235 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001236 path = PyUnicode_FromString("<frozen>");
1237 if (path == NULL)
1238 goto err_return;
1239 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
1240 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 if (m == NULL)
1242 goto err_return;
1243 Py_DECREF(co);
1244 Py_DECREF(m);
1245 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001246err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 Py_DECREF(co);
1248 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001249}
Guido van Rossum74e6a111994-08-29 12:54:38 +00001250
Victor Stinner53dc7352011-03-20 01:50:21 +01001251int
Serhiy Storchakac6792272013-10-19 21:03:34 +03001252PyImport_ImportFrozenModule(const char *name)
Victor Stinner53dc7352011-03-20 01:50:21 +01001253{
1254 PyObject *nameobj;
1255 int ret;
1256 nameobj = PyUnicode_InternFromString(name);
1257 if (nameobj == NULL)
1258 return -1;
1259 ret = PyImport_ImportFrozenModuleObject(nameobj);
1260 Py_DECREF(nameobj);
1261 return ret;
1262}
1263
Guido van Rossum74e6a111994-08-29 12:54:38 +00001264
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001265/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001266 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00001267
Guido van Rossum79f25d91997-04-29 20:08:16 +00001268PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001269PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00001270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 PyObject *pname;
1272 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 pname = PyUnicode_FromString(name);
1275 if (pname == NULL)
1276 return NULL;
1277 result = PyImport_Import(pname);
1278 Py_DECREF(pname);
1279 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001280}
1281
Christian Heimes072c0f12008-01-03 23:01:04 +00001282/* Import a module without blocking
1283 *
1284 * At first it tries to fetch the module from sys.modules. If the module was
1285 * never loaded before it loads it with PyImport_ImportModule() unless another
1286 * thread holds the import lock. In the latter case the function raises an
1287 * ImportError instead of blocking.
1288 *
1289 * Returns the module object with incremented ref count.
1290 */
1291PyObject *
1292PyImport_ImportModuleNoBlock(const char *name)
1293{
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001294 return PyImport_ImportModule(name);
Christian Heimes072c0f12008-01-03 23:01:04 +00001295}
1296
Guido van Rossum17fc85f1997-09-06 18:52:03 +00001297
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001298/* Remove importlib frames from the traceback,
1299 * except in Verbose mode. */
1300static void
1301remove_importlib_frames(void)
1302{
1303 const char *importlib_filename = "<frozen importlib._bootstrap>";
Nick Coghlan42c07662012-07-31 21:14:18 +10001304 const char *remove_frames = "_call_with_frames_removed";
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001305 int always_trim = 0;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001306 int in_importlib = 0;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001307 PyObject *exception, *value, *base_tb, *tb;
Benjamin Petersonfa873702012-07-09 13:43:53 -07001308 PyObject **prev_link, **outer_link = NULL;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001309
1310 /* Synopsis: if it's an ImportError, we trim all importlib chunks
Nick Coghlan42c07662012-07-31 21:14:18 +10001311 from the traceback. We always trim chunks
1312 which end with a call to "_call_with_frames_removed". */
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001313
1314 PyErr_Fetch(&exception, &value, &base_tb);
1315 if (!exception || Py_VerboseFlag)
1316 goto done;
1317 if (PyType_IsSubtype((PyTypeObject *) exception,
1318 (PyTypeObject *) PyExc_ImportError))
1319 always_trim = 1;
1320
1321 prev_link = &base_tb;
1322 tb = base_tb;
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001323 while (tb != NULL) {
1324 PyTracebackObject *traceback = (PyTracebackObject *)tb;
1325 PyObject *next = (PyObject *) traceback->tb_next;
1326 PyFrameObject *frame = traceback->tb_frame;
1327 PyCodeObject *code = frame->f_code;
1328 int now_in_importlib;
1329
1330 assert(PyTraceBack_Check(tb));
1331 now_in_importlib = (PyUnicode_CompareWithASCIIString(
1332 code->co_filename,
1333 importlib_filename) == 0);
1334 if (now_in_importlib && !in_importlib) {
1335 /* This is the link to this chunk of importlib tracebacks */
1336 outer_link = prev_link;
1337 }
1338 in_importlib = now_in_importlib;
1339
1340 if (in_importlib &&
1341 (always_trim ||
Nick Coghlan42c07662012-07-31 21:14:18 +10001342 PyUnicode_CompareWithASCIIString(code->co_name,
1343 remove_frames) == 0)) {
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001344 PyObject *tmp = *outer_link;
1345 *outer_link = next;
1346 Py_XINCREF(next);
1347 Py_DECREF(tmp);
1348 prev_link = outer_link;
1349 }
1350 else {
1351 prev_link = (PyObject **) &traceback->tb_next;
1352 }
1353 tb = next;
1354 }
1355done:
1356 PyErr_Restore(exception, value, base_tb);
1357}
1358
1359
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001360PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04001361PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
1362 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001363 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001364{
Brett Cannonfd074152012-04-14 14:10:13 -04001365 _Py_IDENTIFIER(__import__);
Eric Snowb523f842013-11-22 09:05:39 -07001366 _Py_IDENTIFIER(__spec__);
1367 _Py_IDENTIFIER(_initializing);
Brett Cannonfd074152012-04-14 14:10:13 -04001368 _Py_IDENTIFIER(__package__);
1369 _Py_IDENTIFIER(__path__);
1370 _Py_IDENTIFIER(__name__);
1371 _Py_IDENTIFIER(_find_and_load);
1372 _Py_IDENTIFIER(_handle_fromlist);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001373 _Py_IDENTIFIER(_lock_unlock_module);
Brett Cannonfd074152012-04-14 14:10:13 -04001374 _Py_static_string(single_dot, ".");
1375 PyObject *abs_name = NULL;
1376 PyObject *builtins_import = NULL;
1377 PyObject *final_mod = NULL;
1378 PyObject *mod = NULL;
1379 PyObject *package = NULL;
1380 PyObject *globals = NULL;
1381 PyObject *fromlist = NULL;
1382 PyInterpreterState *interp = PyThreadState_GET()->interp;
1383
1384 /* Make sure to use default values so as to not have
1385 PyObject_CallMethodObjArgs() truncate the parameter list because of a
1386 NULL argument. */
1387 if (given_globals == NULL) {
1388 globals = PyDict_New();
1389 if (globals == NULL) {
1390 goto error;
1391 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 }
Brett Cannonfd074152012-04-14 14:10:13 -04001393 else {
1394 /* Only have to care what given_globals is if it will be used
Brett Cannonecfefb72012-08-05 19:24:57 -04001395 for something. */
Brett Cannonfd074152012-04-14 14:10:13 -04001396 if (level > 0 && !PyDict_Check(given_globals)) {
1397 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
1398 goto error;
1399 }
1400 globals = given_globals;
1401 Py_INCREF(globals);
1402 }
1403
1404 if (given_fromlist == NULL) {
1405 fromlist = PyList_New(0);
1406 if (fromlist == NULL) {
1407 goto error;
1408 }
1409 }
1410 else {
1411 fromlist = given_fromlist;
1412 Py_INCREF(fromlist);
1413 }
1414 if (name == NULL) {
1415 PyErr_SetString(PyExc_ValueError, "Empty module name");
1416 goto error;
1417 }
1418
1419 /* The below code is importlib.__import__() & _gcd_import(), ported to C
1420 for added performance. */
1421
1422 if (!PyUnicode_Check(name)) {
1423 PyErr_SetString(PyExc_TypeError, "module name must be a string");
1424 goto error;
1425 }
1426 else if (PyUnicode_READY(name) < 0) {
1427 goto error;
1428 }
1429 if (level < 0) {
1430 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
1431 goto error;
1432 }
1433 else if (level > 0) {
1434 package = _PyDict_GetItemId(globals, &PyId___package__);
1435 if (package != NULL && package != Py_None) {
1436 Py_INCREF(package);
1437 if (!PyUnicode_Check(package)) {
1438 PyErr_SetString(PyExc_TypeError, "package must be a string");
1439 goto error;
1440 }
1441 }
1442 else {
Brett Cannon273323c2012-04-17 19:05:11 -04001443 package = _PyDict_GetItemId(globals, &PyId___name__);
Brett Cannonfd074152012-04-14 14:10:13 -04001444 if (package == NULL) {
Brett Cannon273323c2012-04-17 19:05:11 -04001445 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
Brett Cannonfd074152012-04-14 14:10:13 -04001446 goto error;
1447 }
1448 else if (!PyUnicode_Check(package)) {
1449 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
1450 }
1451 Py_INCREF(package);
1452
1453 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Brett Cannon740fce02012-04-14 14:23:49 -04001454 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001455 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1456 if (borrowed_dot == NULL) {
1457 goto error;
1458 }
Brett Cannon740fce02012-04-14 14:23:49 -04001459 partition = PyUnicode_RPartition(package, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001460 Py_DECREF(package);
1461 if (partition == NULL) {
1462 goto error;
1463 }
1464 package = PyTuple_GET_ITEM(partition, 0);
1465 Py_INCREF(package);
1466 Py_DECREF(partition);
1467 }
1468 }
1469
1470 if (PyDict_GetItem(interp->modules, package) == NULL) {
1471 PyErr_Format(PyExc_SystemError,
1472 "Parent module %R not loaded, cannot perform relative "
1473 "import", package);
1474 goto error;
1475 }
1476 }
1477 else { /* level == 0 */
1478 if (PyUnicode_GET_LENGTH(name) == 0) {
1479 PyErr_SetString(PyExc_ValueError, "Empty module name");
1480 goto error;
1481 }
1482 package = Py_None;
1483 Py_INCREF(package);
1484 }
1485
1486 if (level > 0) {
1487 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
1488 PyObject *base = NULL;
1489 int level_up = 1;
1490
1491 for (level_up = 1; level_up < level; level_up += 1) {
1492 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
1493 if (last_dot == -2) {
1494 goto error;
1495 }
1496 else if (last_dot == -1) {
1497 PyErr_SetString(PyExc_ValueError,
1498 "attempted relative import beyond top-level "
1499 "package");
1500 goto error;
1501 }
1502 }
Victor Stinner22af2592013-11-13 12:11:36 +01001503
Brett Cannonfd074152012-04-14 14:10:13 -04001504 base = PyUnicode_Substring(package, 0, last_dot);
Victor Stinner22af2592013-11-13 12:11:36 +01001505 if (base == NULL)
1506 goto error;
1507
Brett Cannonfd074152012-04-14 14:10:13 -04001508 if (PyUnicode_GET_LENGTH(name) > 0) {
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001509 PyObject *borrowed_dot, *seq = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001510
1511 borrowed_dot = _PyUnicode_FromId(&single_dot);
Antoine Pitrou538ba2a2012-04-16 21:52:45 +02001512 seq = PyTuple_Pack(2, base, name);
1513 Py_DECREF(base);
Brett Cannonfd074152012-04-14 14:10:13 -04001514 if (borrowed_dot == NULL || seq == NULL) {
1515 goto error;
1516 }
1517
1518 abs_name = PyUnicode_Join(borrowed_dot, seq);
1519 Py_DECREF(seq);
1520 if (abs_name == NULL) {
1521 goto error;
1522 }
1523 }
1524 else {
1525 abs_name = base;
1526 }
1527 }
1528 else {
1529 abs_name = name;
1530 Py_INCREF(abs_name);
1531 }
1532
Brian Curtine6b299f2012-04-14 14:19:33 -05001533#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001534 _PyImport_AcquireLock();
1535#endif
1536 /* From this point forward, goto error_with_unlock! */
1537 if (PyDict_Check(globals)) {
1538 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
1539 }
1540 if (builtins_import == NULL) {
1541 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
1542 if (builtins_import == NULL) {
Benjamin Peterson7d110042013-04-29 09:08:14 -04001543 PyErr_SetString(PyExc_ImportError, "__import__ not found");
1544 goto error_with_unlock;
Brett Cannonfd074152012-04-14 14:10:13 -04001545 }
1546 }
1547 Py_INCREF(builtins_import);
1548
1549 mod = PyDict_GetItem(interp->modules, abs_name);
1550 if (mod == Py_None) {
Brett Cannon27fc5282012-04-15 14:15:31 -04001551 PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
1552 "None in sys.modules", abs_name);
1553 if (msg != NULL) {
Brett Cannon82da8882013-07-04 17:48:16 -04001554 PyErr_SetImportError(msg, abs_name, NULL);
Brian Curtin09b86d12012-04-17 16:57:09 -05001555 Py_DECREF(msg);
Brett Cannon27fc5282012-04-15 14:15:31 -04001556 }
Antoine Pitrou71382cb2012-04-16 18:48:49 +02001557 mod = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001558 goto error_with_unlock;
1559 }
1560 else if (mod != NULL) {
Eric Snowb523f842013-11-22 09:05:39 -07001561 PyObject *value = NULL;
1562 PyObject *spec;
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001563 int initializing = 0;
1564
Brett Cannonfd074152012-04-14 14:10:13 -04001565 Py_INCREF(mod);
Antoine Pitrou03989852012-08-28 00:24:52 +02001566 /* Optimization: only call _bootstrap._lock_unlock_module() if
Eric Snowb523f842013-11-22 09:05:39 -07001567 __spec__._initializing is true.
1568 NOTE: because of this, initializing must be set *before*
Antoine Pitrou03989852012-08-28 00:24:52 +02001569 stuffing the new module in sys.modules.
1570 */
Eric Snowb523f842013-11-22 09:05:39 -07001571 spec = _PyObject_GetAttrId(mod, &PyId___spec__);
1572 if (spec != NULL) {
1573 value = _PyObject_GetAttrId(spec, &PyId__initializing);
1574 Py_DECREF(spec);
1575 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001576 if (value == NULL)
1577 PyErr_Clear();
1578 else {
1579 initializing = PyObject_IsTrue(value);
1580 Py_DECREF(value);
1581 if (initializing == -1)
1582 PyErr_Clear();
1583 }
1584 if (initializing > 0) {
1585 /* _bootstrap._lock_unlock_module() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001586 value = _PyObject_CallMethodIdObjArgs(interp->importlib,
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001587 &PyId__lock_unlock_module, abs_name,
1588 NULL);
1589 if (value == NULL)
1590 goto error;
1591 Py_DECREF(value);
1592 }
1593 else {
1594#ifdef WITH_THREAD
1595 if (_PyImport_ReleaseLock() < 0) {
1596 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1597 goto error;
1598 }
1599#endif
1600 }
Brett Cannonfd074152012-04-14 14:10:13 -04001601 }
1602 else {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001603 /* _bootstrap._find_and_load() releases the import lock */
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001604 mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001605 &PyId__find_and_load, abs_name,
1606 builtins_import, NULL);
1607 if (mod == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001608 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001609 }
1610 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001611 /* From now on we don't hold the import lock anymore. */
Brett Cannonfd074152012-04-14 14:10:13 -04001612
1613 if (PyObject_Not(fromlist)) {
1614 if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1615 PyObject *front = NULL;
Brian Curtine6b299f2012-04-14 14:19:33 -05001616 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04001617 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1618
1619 if (borrowed_dot == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001620 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001621 }
1622
Brian Curtine6b299f2012-04-14 14:19:33 -05001623 partition = PyUnicode_Partition(name, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04001624 if (partition == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001625 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -04001626 }
1627
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001628 if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1629 /* No dot in module name, simple exit */
1630 Py_DECREF(partition);
1631 final_mod = mod;
1632 Py_INCREF(mod);
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001633 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001634 }
1635
Brett Cannonfd074152012-04-14 14:10:13 -04001636 front = PyTuple_GET_ITEM(partition, 0);
1637 Py_INCREF(front);
1638 Py_DECREF(partition);
1639
1640 if (level == 0) {
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001641 final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
Antoine Pitroub78174c2012-05-06 17:15:23 +02001642 Py_DECREF(front);
Brett Cannonfd074152012-04-14 14:10:13 -04001643 }
1644 else {
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001645 Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1646 PyUnicode_GET_LENGTH(front);
1647 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001648 PyObject *to_return = PyUnicode_Substring(abs_name, 0,
Brett Cannonfd074152012-04-14 14:10:13 -04001649 abs_name_len - cut_off);
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001650 Py_DECREF(front);
1651 if (to_return == NULL) {
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001652 goto error;
Antoine Pitrou22a1d172012-04-16 22:06:21 +02001653 }
Brett Cannonfd074152012-04-14 14:10:13 -04001654
1655 final_mod = PyDict_GetItem(interp->modules, to_return);
Brett Cannon49f8d8b2012-04-14 21:50:00 -04001656 if (final_mod == NULL) {
1657 PyErr_Format(PyExc_KeyError,
1658 "%R not in sys.modules as expected",
1659 to_return);
1660 }
1661 else {
1662 Py_INCREF(final_mod);
1663 }
Antoine Pitroub78174c2012-05-06 17:15:23 +02001664 Py_DECREF(to_return);
Brett Cannonfd074152012-04-14 14:10:13 -04001665 }
1666 }
1667 else {
1668 final_mod = mod;
1669 Py_INCREF(mod);
1670 }
1671 }
1672 else {
Alexandre Vassalotti865eaa12013-05-02 10:44:04 -07001673 final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
Brett Cannonfd074152012-04-14 14:10:13 -04001674 &PyId__handle_fromlist, mod,
1675 fromlist, builtins_import,
1676 NULL);
1677 }
Antoine Pitrouea3eb882012-05-17 18:55:59 +02001678 goto error;
Antoine Pitrou6efa50a2012-05-07 21:41:59 +02001679
Brett Cannonfd074152012-04-14 14:10:13 -04001680 error_with_unlock:
Brian Curtine6b299f2012-04-14 14:19:33 -05001681#ifdef WITH_THREAD
Brett Cannonfd074152012-04-14 14:10:13 -04001682 if (_PyImport_ReleaseLock() < 0) {
1683 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
1684 }
1685#endif
1686 error:
1687 Py_XDECREF(abs_name);
1688 Py_XDECREF(builtins_import);
1689 Py_XDECREF(mod);
1690 Py_XDECREF(package);
1691 Py_XDECREF(globals);
1692 Py_XDECREF(fromlist);
Antoine Pitroubc07a5c2012-07-08 12:01:27 +02001693 if (final_mod == NULL)
1694 remove_importlib_frames();
Brett Cannonfd074152012-04-14 14:10:13 -04001695 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00001696}
1697
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001698PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05001699PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04001700 PyObject *fromlist, int level)
1701{
1702 PyObject *nameobj, *mod;
1703 nameobj = PyUnicode_FromString(name);
1704 if (nameobj == NULL)
1705 return NULL;
1706 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
1707 fromlist, level);
1708 Py_DECREF(nameobj);
1709 return mod;
1710}
1711
1712
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001713/* Re-import a module of any kind and return its module object, WITH
1714 INCREMENTED REFERENCE COUNT */
1715
Guido van Rossum79f25d91997-04-29 20:08:16 +00001716PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001717PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001718{
Brett Cannon62228db2012-04-29 14:38:11 -04001719 _Py_IDENTIFIER(reload);
1720 PyObject *reloaded_module = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 PyObject *modules = PyImport_GetModuleDict();
Brett Cannon62228db2012-04-29 14:38:11 -04001722 PyObject *imp = PyDict_GetItemString(modules, "imp");
1723 if (imp == NULL) {
1724 imp = PyImport_ImportModule("imp");
1725 if (imp == NULL) {
1726 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 }
Brett Cannon62228db2012-04-29 14:38:11 -04001729 else {
1730 Py_INCREF(imp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001732
Brett Cannon62228db2012-04-29 14:38:11 -04001733 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m);
1734 Py_DECREF(imp);
1735 return reloaded_module;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001736}
1737
1738
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001739/* Higher-level import emulator which emulates the "import" statement
1740 more accurately -- it invokes the __import__() function from the
1741 builtins of the current globals. This means that the import is
1742 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00001743 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00001744 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00001745 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00001746 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001747
1748PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001749PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 static PyObject *silly_list = NULL;
1752 static PyObject *builtins_str = NULL;
1753 static PyObject *import_str = NULL;
1754 PyObject *globals = NULL;
1755 PyObject *import = NULL;
1756 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001757 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 /* Initialize constant string objects */
1761 if (silly_list == NULL) {
1762 import_str = PyUnicode_InternFromString("__import__");
1763 if (import_str == NULL)
1764 return NULL;
1765 builtins_str = PyUnicode_InternFromString("__builtins__");
1766 if (builtins_str == NULL)
1767 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00001768 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 if (silly_list == NULL)
1770 return NULL;
1771 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 /* Get the builtins from current globals */
1774 globals = PyEval_GetGlobals();
1775 if (globals != NULL) {
1776 Py_INCREF(globals);
1777 builtins = PyObject_GetItem(globals, builtins_str);
1778 if (builtins == NULL)
1779 goto err;
1780 }
1781 else {
1782 /* No globals -- use standard builtins, and fake globals */
1783 builtins = PyImport_ImportModuleLevel("builtins",
1784 NULL, NULL, NULL, 0);
1785 if (builtins == NULL)
1786 return NULL;
1787 globals = Py_BuildValue("{OO}", builtins_str, builtins);
1788 if (globals == NULL)
1789 goto err;
1790 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 /* Get the __import__ function from the builtins */
1793 if (PyDict_Check(builtins)) {
1794 import = PyObject_GetItem(builtins, import_str);
1795 if (import == NULL)
1796 PyErr_SetObject(PyExc_KeyError, import_str);
1797 }
1798 else
1799 import = PyObject_GetAttr(builtins, import_str);
1800 if (import == NULL)
1801 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00001804 Always use absolute import here.
1805 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
1807 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00001808 if (r == NULL)
1809 goto err;
1810 Py_DECREF(r);
1811
1812 modules = PyImport_GetModuleDict();
1813 r = PyDict_GetItem(modules, module_name);
1814 if (r != NULL)
1815 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001816
1817 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 Py_XDECREF(globals);
1819 Py_XDECREF(builtins);
1820 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00001823}
1824
Brett Cannon4caa61d2014-01-09 19:03:32 -05001825/*[clinic input]
1826_imp.extension_suffixes
1827
1828Returns the list of file suffixes used to identify extension modules.
1829[clinic start generated code]*/
1830
1831PyDoc_STRVAR(_imp_extension_suffixes__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001832"extension_suffixes($module, /)\n"
1833"--\n"
1834"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -05001835"Returns the list of file suffixes used to identify extension modules.");
1836
1837#define _IMP_EXTENSION_SUFFIXES_METHODDEF \
1838 {"extension_suffixes", (PyCFunction)_imp_extension_suffixes, METH_NOARGS, _imp_extension_suffixes__doc__},
1839
Barry Warsaw28a691b2010-04-17 00:19:56 +00001840static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05001841_imp_extension_suffixes_impl(PyModuleDef *module);
1842
1843static PyObject *
1844_imp_extension_suffixes(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
1845{
Larry Hastingsbebf7352014-01-17 17:47:17 -08001846 return _imp_extension_suffixes_impl(module);
Brett Cannon4caa61d2014-01-09 19:03:32 -05001847}
1848
1849static PyObject *
1850_imp_extension_suffixes_impl(PyModuleDef *module)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001851/*[clinic end generated code: output=bb30a2438167798c input=ecdeeecfcb6f839e]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 PyObject *list;
Brett Cannon2657df42012-05-04 15:20:40 -04001854 const char *suffix;
1855 unsigned int index = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 list = PyList_New(0);
1858 if (list == NULL)
1859 return NULL;
Brett Cannon2657df42012-05-04 15:20:40 -04001860#ifdef HAVE_DYNAMIC_LOADING
1861 while ((suffix = _PyImport_DynLoadFiletab[index])) {
1862 PyObject *item = PyUnicode_FromString(suffix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (item == NULL) {
1864 Py_DECREF(list);
1865 return NULL;
1866 }
1867 if (PyList_Append(list, item) < 0) {
1868 Py_DECREF(list);
1869 Py_DECREF(item);
1870 return NULL;
1871 }
1872 Py_DECREF(item);
Brett Cannon2657df42012-05-04 15:20:40 -04001873 index += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 }
Brett Cannon2657df42012-05-04 15:20:40 -04001875#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001877}
1878
Brett Cannon4caa61d2014-01-09 19:03:32 -05001879/*[clinic input]
1880_imp.init_builtin
1881
1882 name: unicode
1883 /
1884
1885Initializes a built-in module.
1886[clinic start generated code]*/
1887
1888PyDoc_STRVAR(_imp_init_builtin__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001889"init_builtin($module, name, /)\n"
1890"--\n"
1891"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -05001892"Initializes a built-in module.");
1893
1894#define _IMP_INIT_BUILTIN_METHODDEF \
1895 {"init_builtin", (PyCFunction)_imp_init_builtin, METH_VARARGS, _imp_init_builtin__doc__},
1896
Guido van Rossum79f25d91997-04-29 20:08:16 +00001897static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05001898_imp_init_builtin_impl(PyModuleDef *module, PyObject *name);
1899
1900static PyObject *
1901_imp_init_builtin(PyModuleDef *module, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001902{
Brett Cannon4caa61d2014-01-09 19:03:32 -05001903 PyObject *return_value = NULL;
Victor Stinner95872862011-03-07 18:20:56 +01001904 PyObject *name;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001905
1906 if (!PyArg_ParseTuple(args,
1907 "U:init_builtin",
1908 &name))
1909 goto exit;
1910 return_value = _imp_init_builtin_impl(module, name);
1911
1912exit:
1913 return return_value;
1914}
1915
1916static PyObject *
1917_imp_init_builtin_impl(PyModuleDef *module, PyObject *name)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001918/*[clinic end generated code: output=a0244948a43f8e26 input=f934d2231ec52a2e]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 int ret;
1921 PyObject *m;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 ret = init_builtin(name);
1924 if (ret < 0)
1925 return NULL;
1926 if (ret == 0) {
1927 Py_INCREF(Py_None);
1928 return Py_None;
1929 }
Victor Stinner95872862011-03-07 18:20:56 +01001930 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 Py_XINCREF(m);
1932 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001933}
1934
Brett Cannon4caa61d2014-01-09 19:03:32 -05001935/*[clinic input]
1936_imp.init_frozen
1937
1938 name: unicode
1939 /
1940
1941Initializes a frozen module.
1942[clinic start generated code]*/
1943
1944PyDoc_STRVAR(_imp_init_frozen__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001945"init_frozen($module, name, /)\n"
1946"--\n"
1947"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -05001948"Initializes a frozen module.");
1949
1950#define _IMP_INIT_FROZEN_METHODDEF \
1951 {"init_frozen", (PyCFunction)_imp_init_frozen, METH_VARARGS, _imp_init_frozen__doc__},
1952
Guido van Rossum79f25d91997-04-29 20:08:16 +00001953static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05001954_imp_init_frozen_impl(PyModuleDef *module, PyObject *name);
1955
1956static PyObject *
1957_imp_init_frozen(PyModuleDef *module, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001958{
Brett Cannon4caa61d2014-01-09 19:03:32 -05001959 PyObject *return_value = NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01001960 PyObject *name;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001961
1962 if (!PyArg_ParseTuple(args,
1963 "U:init_frozen",
1964 &name))
1965 goto exit;
1966 return_value = _imp_init_frozen_impl(module, name);
1967
1968exit:
1969 return return_value;
1970}
1971
1972static PyObject *
1973_imp_init_frozen_impl(PyModuleDef *module, PyObject *name)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001974/*[clinic end generated code: output=e4bc2bff296f8f22 input=13019adfc04f3fb3]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05001975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 int ret;
1977 PyObject *m;
Brett Cannon4caa61d2014-01-09 19:03:32 -05001978
Victor Stinner53dc7352011-03-20 01:50:21 +01001979 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 if (ret < 0)
1981 return NULL;
1982 if (ret == 0) {
1983 Py_INCREF(Py_None);
1984 return Py_None;
1985 }
Victor Stinner53dc7352011-03-20 01:50:21 +01001986 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 Py_XINCREF(m);
1988 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001989}
1990
Brett Cannon4caa61d2014-01-09 19:03:32 -05001991/*[clinic input]
1992_imp.get_frozen_object
1993
1994 name: unicode
1995 /
1996
1997Create a code object for a frozen module.
1998[clinic start generated code]*/
1999
2000PyDoc_STRVAR(_imp_get_frozen_object__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08002001"get_frozen_object($module, name, /)\n"
2002"--\n"
2003"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -05002004"Create a code object for a frozen module.");
2005
2006#define _IMP_GET_FROZEN_OBJECT_METHODDEF \
2007 {"get_frozen_object", (PyCFunction)_imp_get_frozen_object, METH_VARARGS, _imp_get_frozen_object__doc__},
2008
Guido van Rossum79f25d91997-04-29 20:08:16 +00002009static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05002010_imp_get_frozen_object_impl(PyModuleDef *module, PyObject *name);
2011
2012static PyObject *
2013_imp_get_frozen_object(PyModuleDef *module, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002014{
Brett Cannon4caa61d2014-01-09 19:03:32 -05002015 PyObject *return_value = NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01002016 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00002017
Brett Cannon4caa61d2014-01-09 19:03:32 -05002018 if (!PyArg_ParseTuple(args,
2019 "U:get_frozen_object",
2020 &name))
2021 goto exit;
2022 return_value = _imp_get_frozen_object_impl(module, name);
2023
2024exit:
2025 return return_value;
2026}
2027
2028static PyObject *
2029_imp_get_frozen_object_impl(PyModuleDef *module, PyObject *name)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002030/*[clinic end generated code: output=4089ec702a9d70c5 input=ed689bc05358fdbd]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05002031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002033}
2034
Brett Cannon4caa61d2014-01-09 19:03:32 -05002035/*[clinic input]
2036_imp.is_frozen_package
2037
2038 name: unicode
2039 /
2040
2041Returns True if the module name is of a frozen package.
2042[clinic start generated code]*/
2043
2044PyDoc_STRVAR(_imp_is_frozen_package__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08002045"is_frozen_package($module, name, /)\n"
2046"--\n"
2047"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -05002048"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)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002074/*[clinic end generated code: output=86aab14dcd4b959b input=81b6cdecd080fbb8]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05002075{
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__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08002089"is_builtin($module, name, /)\n"
2090"--\n"
2091"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -05002092"Returns True if the module name corresponds to a built-in module.");
2093
2094#define _IMP_IS_BUILTIN_METHODDEF \
2095 {"is_builtin", (PyCFunction)_imp_is_builtin, METH_VARARGS, _imp_is_builtin__doc__},
2096
Brett Cannon8d110132009-03-15 02:20:16 +00002097static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05002098_imp_is_builtin_impl(PyModuleDef *module, PyObject *name);
2099
2100static PyObject *
2101_imp_is_builtin(PyModuleDef *module, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002102{
Brett Cannon4caa61d2014-01-09 19:03:32 -05002103 PyObject *return_value = NULL;
Victor Stinner95872862011-03-07 18:20:56 +01002104 PyObject *name;
Brett Cannon4caa61d2014-01-09 19:03:32 -05002105
2106 if (!PyArg_ParseTuple(args,
2107 "U:is_builtin",
2108 &name))
2109 goto exit;
2110 return_value = _imp_is_builtin_impl(module, name);
2111
2112exit:
2113 return return_value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002114}
2115
Guido van Rossum79f25d91997-04-29 20:08:16 +00002116static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05002117_imp_is_builtin_impl(PyModuleDef *module, PyObject *name)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002118/*[clinic end generated code: output=d5847f8cac50946e input=86befdac021dd1c7]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002119{
Brett Cannon4caa61d2014-01-09 19:03:32 -05002120 return PyLong_FromLong(is_builtin(name));
2121}
2122
2123/*[clinic input]
2124_imp.is_frozen
2125
2126 name: unicode
2127 /
2128
2129Returns True if the module name corresponds to a frozen module.
2130[clinic start generated code]*/
2131
2132PyDoc_STRVAR(_imp_is_frozen__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08002133"is_frozen($module, name, /)\n"
2134"--\n"
2135"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -05002136"Returns True if the module name corresponds to a frozen module.");
2137
2138#define _IMP_IS_FROZEN_METHODDEF \
2139 {"is_frozen", (PyCFunction)_imp_is_frozen, METH_VARARGS, _imp_is_frozen__doc__},
2140
2141static PyObject *
2142_imp_is_frozen_impl(PyModuleDef *module, PyObject *name);
2143
2144static PyObject *
2145_imp_is_frozen(PyModuleDef *module, PyObject *args)
2146{
2147 PyObject *return_value = NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01002148 PyObject *name;
Brett Cannon4caa61d2014-01-09 19:03:32 -05002149
2150 if (!PyArg_ParseTuple(args,
2151 "U:is_frozen",
2152 &name))
2153 goto exit;
2154 return_value = _imp_is_frozen_impl(module, name);
2155
2156exit:
2157 return return_value;
2158}
2159
2160static PyObject *
2161_imp_is_frozen_impl(PyModuleDef *module, PyObject *name)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002162/*[clinic end generated code: output=6691af884ba4987d input=7301dbca1897d66b]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05002163{
Benjamin Peterson6fba3db2013-03-18 23:13:31 -07002164 const struct _frozen *p;
Brett Cannon4caa61d2014-01-09 19:03:32 -05002165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 p = find_frozen(name);
2167 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002168}
2169
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002170#ifdef HAVE_DYNAMIC_LOADING
2171
Brett Cannon4caa61d2014-01-09 19:03:32 -05002172/*[clinic input]
2173_imp.load_dynamic
2174
2175 name: unicode
2176 path: fs_unicode
2177 file: object = NULL
2178 /
2179
2180Loads an extension module.
2181[clinic start generated code]*/
2182
2183PyDoc_STRVAR(_imp_load_dynamic__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08002184"load_dynamic($module, name, path, file=None, /)\n"
2185"--\n"
2186"\n"
Brett Cannon4caa61d2014-01-09 19:03:32 -05002187"Loads an extension module.");
2188
2189#define _IMP_LOAD_DYNAMIC_METHODDEF \
2190 {"load_dynamic", (PyCFunction)_imp_load_dynamic, METH_VARARGS, _imp_load_dynamic__doc__},
2191
Guido van Rossum79f25d91997-04-29 20:08:16 +00002192static PyObject *
Brett Cannon4caa61d2014-01-09 19:03:32 -05002193_imp_load_dynamic_impl(PyModuleDef *module, PyObject *name, PyObject *path, PyObject *file);
2194
2195static PyObject *
2196_imp_load_dynamic(PyModuleDef *module, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002197{
Brett Cannon4caa61d2014-01-09 19:03:32 -05002198 PyObject *return_value = NULL;
2199 PyObject *name;
2200 PyObject *path;
2201 PyObject *file = NULL;
2202
2203 if (!PyArg_ParseTuple(args,
2204 "UO&|O:load_dynamic",
2205 &name, PyUnicode_FSDecoder, &path, &file))
2206 goto exit;
2207 return_value = _imp_load_dynamic_impl(module, name, path, file);
2208
2209exit:
2210 return return_value;
2211}
2212
2213static PyObject *
2214_imp_load_dynamic_impl(PyModuleDef *module, PyObject *name, PyObject *path, PyObject *file)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002215/*[clinic end generated code: output=81d11a1fbd1ea0a8 input=af64f06e4bad3526]*/
Brett Cannon4caa61d2014-01-09 19:03:32 -05002216{
2217 PyObject *mod;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002218 FILE *fp;
2219
Brett Cannon4caa61d2014-01-09 19:03:32 -05002220 if (file != NULL) {
2221 fp = _Py_fopen_obj(path, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01002222 if (fp == NULL) {
Brett Cannon4caa61d2014-01-09 19:03:32 -05002223 Py_DECREF(path);
Antoine Pitrouf3a42de2012-05-04 22:40:25 +02002224 if (!PyErr_Occurred())
2225 PyErr_SetFromErrno(PyExc_IOError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01002227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002229 else
2230 fp = NULL;
Brett Cannon4caa61d2014-01-09 19:03:32 -05002231 mod = _PyImport_LoadDynamicModule(name, path, fp);
2232 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 if (fp)
2234 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002235 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002236}
2237
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002238#endif /* HAVE_DYNAMIC_LOADING */
2239
Larry Hastings7726ac92014-01-31 22:03:12 -08002240/*[clinic input]
2241dump buffer
2242[clinic start generated code]*/
2243
2244#ifndef _IMP_LOAD_DYNAMIC_METHODDEF
2245 #define _IMP_LOAD_DYNAMIC_METHODDEF
2246#endif /* !defined(_IMP_LOAD_DYNAMIC_METHODDEF) */
2247/*[clinic end generated code: output=d07c1d4a343a9579 input=524ce2e021e4eba6]*/
2248
Barry Warsaw28a691b2010-04-17 00:19:56 +00002249
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002250PyDoc_STRVAR(doc_imp,
Brett Cannon6f44d662012-04-15 16:08:47 -04002251"(Extremely) low-level import machinery bits as used by importlib and imp.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00002252
Guido van Rossum79f25d91997-04-29 20:08:16 +00002253static PyMethodDef imp_methods[] = {
Brett Cannon4caa61d2014-01-09 19:03:32 -05002254 _IMP_EXTENSION_SUFFIXES_METHODDEF
2255 _IMP_LOCK_HELD_METHODDEF
2256 _IMP_ACQUIRE_LOCK_METHODDEF
2257 _IMP_RELEASE_LOCK_METHODDEF
2258 _IMP_GET_FROZEN_OBJECT_METHODDEF
2259 _IMP_IS_FROZEN_PACKAGE_METHODDEF
2260 _IMP_INIT_BUILTIN_METHODDEF
2261 _IMP_INIT_FROZEN_METHODDEF
2262 _IMP_IS_BUILTIN_METHODDEF
2263 _IMP_IS_FROZEN_METHODDEF
Brett Cannon4caa61d2014-01-09 19:03:32 -05002264 _IMP_LOAD_DYNAMIC_METHODDEF
Brett Cannon4caa61d2014-01-09 19:03:32 -05002265 _IMP__FIX_CO_FILENAME_METHODDEF
2266 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002267};
2268
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002269
Martin v. Löwis1a214512008-06-11 05:26:20 +00002270static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 PyModuleDef_HEAD_INIT,
Brett Cannon6f44d662012-04-15 16:08:47 -04002272 "_imp",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 doc_imp,
2274 0,
2275 imp_methods,
2276 NULL,
2277 NULL,
2278 NULL,
2279 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002280};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002281
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002282PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002283PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 m = PyModule_Create(&impmodule);
2288 if (m == NULL)
2289 goto failure;
2290 d = PyModule_GetDict(m);
2291 if (d == NULL)
2292 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002295 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 Py_XDECREF(m);
2297 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002298}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002299
2300
Guido van Rossumb18618d2000-05-03 23:44:39 +00002301/* API for embedding applications that want to add their own entries
2302 to the table of built-in modules. This should normally be called
2303 *before* Py_Initialize(). When the table resize fails, -1 is
2304 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002305
2306 After a similar function by Just van Rossum. */
2307
2308int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002309PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 static struct _inittab *our_copy = NULL;
2312 struct _inittab *p;
2313 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 /* Count the number of entries in both tables */
2316 for (n = 0; newtab[n].name != NULL; n++)
2317 ;
2318 if (n == 0)
2319 return 0; /* Nothing to do */
2320 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2321 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 /* Allocate new memory for the combined table */
2324 p = our_copy;
2325 PyMem_RESIZE(p, struct _inittab, i+n+1);
2326 if (p == NULL)
2327 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 /* Copy the tables into the new memory */
2330 if (our_copy != PyImport_Inittab)
2331 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2332 PyImport_Inittab = our_copy = p;
2333 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002336}
2337
2338/* Shorthand to add a single entry given a name and a function */
2339
2340int
Brett Cannona826f322009-04-02 03:41:46 +00002341PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 newtab[0].name = (char *)name;
2348 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00002351}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002352
2353#ifdef __cplusplus
2354}
2355#endif
Larry Hastings7726ac92014-01-31 22:03:12 -08002356