blob: 083146b15584a71e062c155f851512c2a8c96740 [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"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000011#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000012#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000013
Guido van Rossum55a83382000-09-20 20:31:38 +000014#ifdef HAVE_FCNTL_H
15#include <fcntl.h>
16#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000017#ifdef __cplusplus
Brett Cannon9a5b25a2010-03-01 02:09:17 +000018extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019#endif
Guido van Rossum55a83382000-09-20 20:31:38 +000020
Christian Heimesd3eb5a152008-02-24 00:38:49 +000021#ifdef MS_WINDOWS
22/* for stat.st_mode */
23typedef unsigned short mode_t;
Daniel Stutzbachc7937792010-09-09 21:18:04 +000024/* for _mkdir */
25#include <direct.h>
Christian Heimesd3eb5a152008-02-24 00:38:49 +000026#endif
27
Guido van Rossum21d335e1993-10-15 13:01:11 +000028
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000029/* Magic word to reject .pyc files generated by other Python versions.
30 It should change for each incompatible change to the bytecode.
31
32 The value of CR and LF is incorporated so if you ever read or write
Guido van Rossum7faeab31995-07-07 22:50:36 +000033 a .pyc file in text mode the magic number will be wrong; also, the
Tim Peters36515e22001-11-18 04:06:29 +000034 Apple MPW compiler swaps their values, botching string constants.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000035
Guido van Rossum45aecf42006-03-15 04:58:47 +000036 The magic numbers must be spaced apart at least 2 values, as the
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000037 -U interpeter flag will cause MAGIC+1 being used. They have been
38 odd numbers for some time now.
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000039
Jeremy Hyltond4ceb312004-04-01 02:45:22 +000040 There were a variety of old schemes for setting the magic number.
41 The current working scheme is to increment the previous value by
42 10.
Guido van Rossumf6894922002-08-31 15:16:14 +000043
Barry Warsaw28a691b2010-04-17 00:19:56 +000044 Starting with the adoption of PEP 3147 in Python 3.2, every bump in magic
45 number also includes a new "magic tag", i.e. a human readable string used
46 to represent the magic number in __pycache__ directories. When you change
47 the magic number, you must also set a new unique magic tag. Generally this
48 can be named after the Python major version of the magic number bump, but
49 it can really be anything, as long as it's different than anything else
50 that's come before. The tags are included in the following table, starting
51 with Python 3.2a0.
52
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000053 Known values:
54 Python 1.5: 20121
55 Python 1.5.1: 20121
56 Python 1.5.2: 20121
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057 Python 1.6: 50428
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +000058 Python 2.0: 50823
59 Python 2.0.1: 50823
60 Python 2.1: 60202
61 Python 2.1.1: 60202
62 Python 2.1.2: 60202
63 Python 2.2: 60717
Neal Norwitz7fdcb412002-06-14 01:07:39 +000064 Python 2.3a0: 62011
Michael W. Hudsondd32a912002-08-15 14:59:02 +000065 Python 2.3a0: 62021
Guido van Rossumf6894922002-08-31 15:16:14 +000066 Python 2.3a0: 62011 (!)
Martin v. Löwisef82d2f2004-06-27 16:51:46 +000067 Python 2.4a0: 62041
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +000068 Python 2.4a3: 62051
Raymond Hettinger2c31a052004-09-22 18:44:21 +000069 Python 2.4b1: 62061
Michael W. Hudsondf888462005-06-03 14:41:55 +000070 Python 2.5a0: 62071
Michael W. Hudsonaee2e282005-10-21 11:32:20 +000071 Python 2.5a0: 62081 (ast-branch)
Guido van Rossumc2e20742006-02-27 22:32:47 +000072 Python 2.5a0: 62091 (with)
Guido van Rossumf6694362006-03-10 02:28:35 +000073 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
75 Python 2.5b3: 62111 (fix wrong code: x += yield)
76 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 storing constants that should have been removed)
Thomas Wouters89f507f2006-12-13 04:49:30 +000078 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Christian Heimes99170a52007-12-19 02:07:34 +000079 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000080 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
Guido van Rossum45aecf42006-03-15 04:58:47 +000081 Python 3000: 3000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 3010 (removed UNARY_CONVERT)
83 3020 (added BUILD_SET)
84 3030 (added keyword-only parameters)
85 3040 (added signature annotations)
86 3050 (print becomes a function)
87 3060 (PEP 3115 metaclass syntax)
88 3061 (string literals become unicode)
89 3071 (PEP 3109 raise changes)
90 3081 (PEP 3137 make __file__ and __name__ unicode)
91 3091 (kill str8 interning)
92 3101 (merge from 2.6a0, see 62151)
93 3103 (__file__ points to source file)
Benjamin Peterson0f6cae02009-12-10 02:09:08 +000094 Python 3.0a4: 3111 (WITH_CLEANUP optimization).
95 Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT)
96 Python 3.1a0: 3141 (optimize list, set and dict comprehensions:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 change LIST_APPEND and SET_ADD, add MAP_ADD)
Benjamin Peterson0f6cae02009-12-10 02:09:08 +000098 Python 3.1a0: 3151 (optimize conditional branches:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000100 Python 3.2a0: 3160 (add SETUP_WITH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 tag: cpython-32
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000102 Python 3.2a1: 3170 (add DUP_TOP_TWO, remove DUP_TOPX and ROT_FOUR)
103 tag: cpython-32
Benjamin Peterson6246d6d2010-09-10 21:51:44 +0000104 Python 3.2a2 3180 (add DELETE_DEREF)
Benjamin Petersone7c15fa2011-06-19 19:54:45 -0500105 Python 3.3a0 3190 __class__ super closure changed
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100106 Python 3.3a0 3200 (__qualname__ added)
Benjamin Petersonabdb5522012-03-15 15:40:37 -0500107 3210 (added size modulo 2**32 to the pyc header)
108 Python 3.3a1 3220 (changed PEP 380 implementation)
Tim Peters36515e22001-11-18 04:06:29 +0000109*/
Guido van Rossum3ddee711991-12-16 13:06:34 +0000110
Nick Coghlancd419ab2010-09-11 00:39:25 +0000111/* MAGIC must change whenever the bytecode emitted by the compiler may no
112 longer be understood by older implementations of the eval loop (usually
113 due to the addition of new opcodes)
Martin v. Löwisfadcd312011-10-23 18:08:20 +0200114 TAG must change for each major Python release. The magic number will take
115 care of any bytecode changes that occur during development.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000116*/
Benjamin Peterson48deae12011-06-03 17:50:16 -0500117#define QUOTE(arg) #arg
118#define STRIFY(name) QUOTE(name)
119#define MAJOR STRIFY(PY_MAJOR_VERSION)
120#define MINOR STRIFY(PY_MINOR_VERSION)
Benjamin Petersonf53d20f2012-03-16 09:39:12 -0500121#define MAGIC (3220 | ((long)'\r'<<16) | ((long)'\n'<<24))
Benjamin Peterson48deae12011-06-03 17:50:16 -0500122#define TAG "cpython-" MAJOR MINOR;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000123#define CACHEDIR "__pycache__"
124/* Current magic word and string tag as globals. */
Guido van Rossum96774c12000-05-01 20:19:08 +0000125static long pyc_magic = MAGIC;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000126static const char *pyc_tag = TAG;
Benjamin Peterson48deae12011-06-03 17:50:16 -0500127#undef QUOTE
128#undef STRIFY
129#undef MAJOR
130#undef MINOR
Guido van Rossum96774c12000-05-01 20:19:08 +0000131
Victor Stinner95872862011-03-07 18:20:56 +0100132/* See _PyImport_FixupExtensionObject() below */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134
Victor Stinnerfe7c5b52011-04-05 01:48:03 +0200135/* Function from Parser/tokenizer.c */
136extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
137
Guido van Rossum771c6c81997-10-31 18:37:24 +0000138/* This table is defined in config.c: */
139extern struct _inittab _PyImport_Inittab[];
140
141struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +0000142
Guido van Rossumed1170e1999-12-20 21:23:41 +0000143/* these tables define the module suffixes that Python recognizes */
144struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000145
Guido van Rossumed1170e1999-12-20 21:23:41 +0000146static const struct filedescr _PyImport_StandardFiletab[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000148#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000150#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 {".pyc", "rb", PY_COMPILED},
152 {0, 0}
Guido van Rossumed1170e1999-12-20 21:23:41 +0000153};
154
Victor Stinnerd0296212011-03-14 14:04:10 -0400155static PyObject *initstr = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200156_Py_IDENTIFIER(__path__);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000158/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159
160void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000161_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 const struct filedescr *scan;
164 struct filedescr *filetab;
165 int countD = 0;
166 int countS = 0;
Guido van Rossumed1170e1999-12-20 21:23:41 +0000167
Victor Stinnerd0296212011-03-14 14:04:10 -0400168 initstr = PyUnicode_InternFromString("__init__");
169 if (initstr == NULL)
170 Py_FatalError("Can't initialize import variables");
171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 /* prepare _PyImport_Filetab: copy entries from
173 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
174 */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000175#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
177 ++countD;
Guido van Rossum04110fb2007-08-24 16:32:05 +0000178#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
180 ++countS;
181 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
182 if (filetab == NULL)
183 Py_FatalError("Can't initialize import file table.");
Guido van Rossum04110fb2007-08-24 16:32:05 +0000184#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 memcpy(filetab, _PyImport_DynLoadFiletab,
186 countD * sizeof(struct filedescr));
Guido van Rossum04110fb2007-08-24 16:32:05 +0000187#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 memcpy(filetab + countD, _PyImport_StandardFiletab,
189 countS * sizeof(struct filedescr));
190 filetab[countD + countS].suffix = NULL;
Guido van Rossumed1170e1999-12-20 21:23:41 +0000191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 _PyImport_Filetab = filetab;
Guido van Rossumed1170e1999-12-20 21:23:41 +0000193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 if (Py_OptimizeFlag) {
195 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
196 for (; filetab->suffix != NULL; filetab++) {
197 if (strcmp(filetab->suffix, ".pyc") == 0)
198 filetab->suffix = ".pyo";
199 }
200 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201}
202
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203void
Just van Rossum52e14d62002-12-30 22:08:05 +0000204_PyImportHooks_Init(void)
205{
Brett Cannonfd074152012-04-14 14:10:13 -0400206 PyObject *v, *path_hooks = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 int err = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if (PyType_Ready(&PyNullImporter_Type) < 0)
210 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000211
Brett Cannonfd074152012-04-14 14:10:13 -0400212 /* adding sys.path_hooks and sys.path_importer_cache */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 v = PyList_New(0);
214 if (v == NULL)
215 goto error;
216 err = PySys_SetObject("meta_path", v);
217 Py_DECREF(v);
218 if (err)
219 goto error;
220 v = PyDict_New();
221 if (v == NULL)
222 goto error;
223 err = PySys_SetObject("path_importer_cache", v);
224 Py_DECREF(v);
225 if (err)
226 goto error;
227 path_hooks = PyList_New(0);
228 if (path_hooks == NULL)
229 goto error;
230 err = PySys_SetObject("path_hooks", path_hooks);
231 if (err) {
Just van Rossum52e14d62002-12-30 22:08:05 +0000232 error:
Brett Cannonfd074152012-04-14 14:10:13 -0400233 PyErr_Print();
234 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
235 "path_importer_cache, or NullImporter failed"
236 );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 }
Brett Cannonfd074152012-04-14 14:10:13 -0400238 Py_DECREF(path_hooks);
239}
240
241void
242_PyImportZip_Init(void)
243{
244 PyObject *path_hooks, *zimpimport;
245 int err = 0;
246
247 path_hooks = PySys_GetObject("path_hooks");
248 if (path_hooks == NULL)
249 goto error;
250
251 if (Py_VerboseFlag)
252 PySys_WriteStderr("# installing zipimport hook\n");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 zimpimport = PyImport_ImportModule("zipimport");
255 if (zimpimport == NULL) {
256 PyErr_Clear(); /* No zip import module -- okay */
257 if (Py_VerboseFlag)
258 PySys_WriteStderr("# can't import zipimport\n");
259 }
260 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200261 _Py_IDENTIFIER(zipimporter);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200262 PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
263 &PyId_zipimporter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_DECREF(zimpimport);
265 if (zipimporter == NULL) {
266 PyErr_Clear(); /* No zipimporter object -- okay */
267 if (Py_VerboseFlag)
268 PySys_WriteStderr(
269 "# can't import zipimport.zipimporter\n");
270 }
271 else {
272 /* sys.path_hooks.append(zipimporter) */
273 err = PyList_Append(path_hooks, zipimporter);
274 Py_DECREF(zipimporter);
Brett Cannonfd074152012-04-14 14:10:13 -0400275 if (err < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 goto error;
Brett Cannonfd074152012-04-14 14:10:13 -0400277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (Py_VerboseFlag)
279 PySys_WriteStderr(
280 "# installed zipimport hook\n");
281 }
282 }
Brett Cannonfd074152012-04-14 14:10:13 -0400283
284 return;
285
286 error:
287 PyErr_Print();
288 Py_FatalError("initializing zipimport or NullImporter failed");
Just van Rossum52e14d62002-12-30 22:08:05 +0000289}
290
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000291/* Locking primitives to prevent parallel imports of the same module
292 in different threads to return with a partially loaded module.
293 These calls are serialized by the global interpreter lock. */
294
295#ifdef WITH_THREAD
296
Guido van Rossum49b56061998-10-01 20:42:43 +0000297#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000298
Guido van Rossum65d5b571998-12-21 19:32:43 +0000299static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000300static long import_lock_thread = -1;
301static int import_lock_level = 0;
302
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000303void
304_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 long me = PyThread_get_thread_ident();
307 if (me == -1)
308 return; /* Too bad */
309 if (import_lock == NULL) {
310 import_lock = PyThread_allocate_lock();
311 if (import_lock == NULL)
312 return; /* Nothing much we can do. */
313 }
314 if (import_lock_thread == me) {
315 import_lock_level++;
316 return;
317 }
318 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
319 {
320 PyThreadState *tstate = PyEval_SaveThread();
321 PyThread_acquire_lock(import_lock, 1);
322 PyEval_RestoreThread(tstate);
323 }
324 import_lock_thread = me;
325 import_lock_level = 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000326}
327
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000328int
329_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 long me = PyThread_get_thread_ident();
332 if (me == -1 || import_lock == NULL)
333 return 0; /* Too bad */
334 if (import_lock_thread != me)
335 return -1;
336 import_lock_level--;
337 if (import_lock_level == 0) {
338 import_lock_thread = -1;
339 PyThread_release_lock(import_lock);
340 }
341 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000342}
343
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000344/* This function is called from PyOS_AfterFork to ensure that newly
345 created child processes do not share locks with the parent.
346 We now acquire the import lock around fork() calls but on some platforms
347 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000348
349void
350_PyImport_ReInitLock(void)
351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (import_lock != NULL)
353 import_lock = PyThread_allocate_lock();
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000354 if (import_lock_level > 1) {
355 /* Forked as a side effect of import */
356 long me = PyThread_get_thread_ident();
357 PyThread_acquire_lock(import_lock, 0);
Victor Stinner942003c2011-03-07 16:57:48 +0100358 /* XXX: can the previous line fail? */
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000359 import_lock_thread = me;
360 import_lock_level--;
361 } else {
362 import_lock_thread = -1;
363 import_lock_level = 0;
364 }
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000365}
366
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000367#endif
368
Tim Peters69232342001-08-30 05:16:13 +0000369static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000370imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000371{
Tim Peters69232342001-08-30 05:16:13 +0000372#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000374#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000376#endif
377}
378
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000379static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000380imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000381{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000382#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000384#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 Py_INCREF(Py_None);
386 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000387}
388
389static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000390imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000391{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000392#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (_PyImport_ReleaseLock() < 0) {
394 PyErr_SetString(PyExc_RuntimeError,
395 "not holding the import lock");
396 return NULL;
397 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000398#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 Py_INCREF(Py_None);
400 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000401}
402
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100403void
404_PyImport_Fini(void)
405{
406 Py_XDECREF(extensions);
407 extensions = NULL;
408 PyMem_DEL(_PyImport_Filetab);
409 _PyImport_Filetab = NULL;
410#ifdef WITH_THREAD
411 if (import_lock != NULL) {
412 PyThread_free_lock(import_lock);
413 import_lock = NULL;
414 }
415#endif
416}
417
Guido van Rossumd8faa362007-04-27 19:54:29 +0000418static void
419imp_modules_reloading_clear(void)
420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 PyInterpreterState *interp = PyThreadState_Get()->interp;
422 if (interp->modules_reloading != NULL)
423 PyDict_Clear(interp->modules_reloading);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000424}
425
Guido van Rossum25ce5661997-08-02 03:10:38 +0000426/* Helper for sys */
427
428PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000429PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyInterpreterState *interp = PyThreadState_GET()->interp;
432 if (interp->modules == NULL)
433 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
434 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000435}
436
Guido van Rossum3f5da241990-12-20 15:06:42 +0000437
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000438/* List of names to clear in sys */
439static char* sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 "path", "argv", "ps1", "ps2",
441 "last_type", "last_value", "last_traceback",
442 "path_hooks", "path_importer_cache", "meta_path",
443 /* misc stuff */
444 "flags", "float_info",
445 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000446};
447
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000448static char* sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 "stdin", "__stdin__",
450 "stdout", "__stdout__",
451 "stderr", "__stderr__",
452 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000453};
454
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000455
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000456/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000457
Guido van Rossum3f5da241990-12-20 15:06:42 +0000458void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000459PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 Py_ssize_t pos, ndone;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 PyObject *key, *value, *dict;
463 PyInterpreterState *interp = PyThreadState_GET()->interp;
464 PyObject *modules = interp->modules;
Guido van Rossum758eec01998-01-19 21:58:26 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (modules == NULL)
467 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 /* Delete some special variables first. These are common
470 places where user values hide and people complain when their
471 destructors fail. Since the modules containing them are
472 deleted *last* of all, they would come too late in the normal
473 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 value = PyDict_GetItemString(modules, "builtins");
476 if (value != NULL && PyModule_Check(value)) {
477 dict = PyModule_GetDict(value);
478 if (Py_VerboseFlag)
479 PySys_WriteStderr("# clear builtins._\n");
480 PyDict_SetItemString(dict, "_", Py_None);
481 }
482 value = PyDict_GetItemString(modules, "sys");
483 if (value != NULL && PyModule_Check(value)) {
484 char **p;
485 PyObject *v;
486 dict = PyModule_GetDict(value);
487 for (p = sys_deletes; *p != NULL; p++) {
488 if (Py_VerboseFlag)
489 PySys_WriteStderr("# clear sys.%s\n", *p);
490 PyDict_SetItemString(dict, *p, Py_None);
491 }
492 for (p = sys_files; *p != NULL; p+=2) {
493 if (Py_VerboseFlag)
494 PySys_WriteStderr("# restore sys.%s\n", *p);
495 v = PyDict_GetItemString(dict, *(p+1));
496 if (v == NULL)
497 v = Py_None;
498 PyDict_SetItemString(dict, *p, v);
499 }
500 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* First, delete __main__ */
503 value = PyDict_GetItemString(modules, "__main__");
504 if (value != NULL && PyModule_Check(value)) {
505 if (Py_VerboseFlag)
506 PySys_WriteStderr("# cleanup __main__\n");
507 _PyModule_Clear(value);
508 PyDict_SetItemString(modules, "__main__", Py_None);
509 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 /* The special treatment of "builtins" here is because even
512 when it's not referenced as a module, its dictionary is
513 referenced by almost every module's __builtins__. Since
514 deleting a module clears its dictionary (even if there are
515 references left to it), we need to delete the "builtins"
516 module last. Likewise, we don't delete sys until the very
517 end because it is implicitly referenced (e.g. by print).
Guido van Rossum758eec01998-01-19 21:58:26 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 Also note that we 'delete' modules by replacing their entry
520 in the modules dict with None, rather than really deleting
521 them; this avoids a rehash of the modules dictionary and
522 also marks them as "non existent" so they won't be
523 re-imported. */
Guido van Rossum758eec01998-01-19 21:58:26 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 /* Next, repeatedly delete modules with a reference count of
526 one (skipping builtins and sys) and delete them */
527 do {
528 ndone = 0;
529 pos = 0;
530 while (PyDict_Next(modules, &pos, &key, &value)) {
531 if (value->ob_refcnt != 1)
532 continue;
533 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Victor Stinner9464d612011-03-07 17:08:21 +0100534 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 continue;
Victor Stinner9464d612011-03-07 17:08:21 +0100536 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 continue;
538 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100539 PySys_FormatStderr(
540 "# cleanup[1] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 _PyModule_Clear(value);
542 PyDict_SetItem(modules, key, Py_None);
543 ndone++;
544 }
545 }
546 } while (ndone > 0);
Guido van Rossum758eec01998-01-19 21:58:26 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 /* Next, delete all modules (still skipping builtins and sys) */
549 pos = 0;
550 while (PyDict_Next(modules, &pos, &key, &value)) {
551 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Victor Stinner9464d612011-03-07 17:08:21 +0100552 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 continue;
Victor Stinner9464d612011-03-07 17:08:21 +0100554 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 continue;
556 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100557 PySys_FormatStderr("# cleanup[2] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 _PyModule_Clear(value);
559 PyDict_SetItem(modules, key, Py_None);
560 }
561 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* Next, delete sys and builtins (in that order) */
564 value = PyDict_GetItemString(modules, "sys");
565 if (value != NULL && PyModule_Check(value)) {
566 if (Py_VerboseFlag)
567 PySys_WriteStderr("# cleanup sys\n");
568 _PyModule_Clear(value);
569 PyDict_SetItemString(modules, "sys", Py_None);
570 }
571 value = PyDict_GetItemString(modules, "builtins");
572 if (value != NULL && PyModule_Check(value)) {
573 if (Py_VerboseFlag)
574 PySys_WriteStderr("# cleanup builtins\n");
575 _PyModule_Clear(value);
576 PyDict_SetItemString(modules, "builtins", Py_None);
577 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 /* Finally, clear and delete the modules directory */
580 PyDict_Clear(modules);
581 interp->modules = NULL;
582 Py_DECREF(modules);
583 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000584}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000585
586
Barry Warsaw28a691b2010-04-17 00:19:56 +0000587/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000588
589long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000590PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593}
594
595
Barry Warsaw28a691b2010-04-17 00:19:56 +0000596const char *
597PyImport_GetMagicTag(void)
598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 return pyc_tag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000600}
601
Guido van Rossum25ce5661997-08-02 03:10:38 +0000602/* Magic for extension modules (built-in as well as dynamically
603 loaded). To prevent initializing an extension module more than
604 once, we keep a static dictionary 'extensions' keyed by module name
605 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000606 modules), containing these modules. A copy of the module's
Victor Stinner95872862011-03-07 18:20:56 +0100607 dictionary is stored by calling _PyImport_FixupExtensionObject()
Guido van Rossum25ce5661997-08-02 03:10:38 +0000608 immediately after the module initialization function succeeds. A
609 copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100610 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000612 Modules which do support multiple initialization set their m_size
613 field to a non-negative number (indicating the size of the
614 module-specific state). They are still recorded in the extensions
615 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000616*/
617
618int
Victor Stinner95872862011-03-07 18:20:56 +0100619_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
620 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 PyObject *modules, *dict;
623 struct PyModuleDef *def;
624 if (extensions == NULL) {
625 extensions = PyDict_New();
626 if (extensions == NULL)
627 return -1;
628 }
629 if (mod == NULL || !PyModule_Check(mod)) {
630 PyErr_BadInternalCall();
631 return -1;
632 }
633 def = PyModule_GetDef(mod);
634 if (!def) {
635 PyErr_BadInternalCall();
636 return -1;
637 }
638 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100639 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 return -1;
641 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100642 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 return -1;
644 }
645 if (def->m_size == -1) {
646 if (def->m_base.m_copy) {
647 /* Somebody already imported the module,
648 likely under a different name.
649 XXX this should really not happen. */
650 Py_DECREF(def->m_base.m_copy);
651 def->m_base.m_copy = NULL;
652 }
653 dict = PyModule_GetDict(mod);
654 if (dict == NULL)
655 return -1;
656 def->m_base.m_copy = PyDict_Copy(dict);
657 if (def->m_base.m_copy == NULL)
658 return -1;
659 }
Victor Stinner49d3f252010-10-17 01:24:53 +0000660 PyDict_SetItem(extensions, filename, (PyObject*)def);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000662}
663
Victor Stinner49d3f252010-10-17 01:24:53 +0000664int
665_PyImport_FixupBuiltin(PyObject *mod, char *name)
666{
667 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100668 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100669 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100670 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000671 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100672 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
673 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000674 return res;
675}
676
Guido van Rossum25ce5661997-08-02 03:10:38 +0000677PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100678_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 PyObject *mod, *mdict;
681 PyModuleDef* def;
682 if (extensions == NULL)
683 return NULL;
Victor Stinner49d3f252010-10-17 01:24:53 +0000684 def = (PyModuleDef*)PyDict_GetItem(extensions, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 if (def == NULL)
686 return NULL;
687 if (def->m_size == -1) {
688 /* Module does not support repeated initialization */
689 if (def->m_base.m_copy == NULL)
690 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100691 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (mod == NULL)
693 return NULL;
694 mdict = PyModule_GetDict(mod);
695 if (mdict == NULL)
696 return NULL;
697 if (PyDict_Update(mdict, def->m_base.m_copy))
698 return NULL;
699 }
700 else {
701 if (def->m_base.m_init == NULL)
702 return NULL;
703 mod = def->m_base.m_init();
704 if (mod == NULL)
705 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100706 PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 Py_DECREF(mod);
708 }
709 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100710 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 Py_DECREF(mod);
712 return NULL;
713 }
714 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100715 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 name, filename);
717 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000718
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719}
720
Victor Stinner49d3f252010-10-17 01:24:53 +0000721PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000722_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000723{
Victor Stinner95872862011-03-07 18:20:56 +0100724 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100725 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100726 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000727 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100728 res = _PyImport_FindExtensionObject(nameobj, nameobj);
729 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000730 return res;
731}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000732
733/* Get the module object corresponding to a module name.
734 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000735 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000736 Because the former action is most common, THIS DOES NOT RETURN A
737 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738
Guido van Rossum79f25d91997-04-29 20:08:16 +0000739PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000740PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 PyObject *modules = PyImport_GetModuleDict();
743 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000744
Victor Stinner27ee0892011-03-04 12:57:09 +0000745 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 PyModule_Check(m))
747 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000748 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 if (m == NULL)
750 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000751 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 Py_DECREF(m);
753 return NULL;
754 }
755 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758}
759
Victor Stinner27ee0892011-03-04 12:57:09 +0000760PyObject *
761PyImport_AddModule(const char *name)
762{
763 PyObject *nameobj, *module;
764 nameobj = PyUnicode_FromString(name);
765 if (nameobj == NULL)
766 return NULL;
767 module = PyImport_AddModuleObject(nameobj);
768 Py_DECREF(nameobj);
769 return module;
770}
771
772
Tim Peters1cd70172004-08-02 03:52:12 +0000773/* Remove name from sys.modules, if it's there. */
774static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000775remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000778 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000780 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 Py_FatalError("import: deleting existing key in"
782 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000783}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000784
Victor Stinnerc9abda02011-03-14 13:33:46 -0400785static PyObject * get_sourcefile(PyObject *filename);
786static PyObject *make_source_pathname(PyObject *pathname);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200787static PyObject* make_compiled_pathname(PyObject *pathname, int debug);
Christian Heimes3b06e532008-01-07 20:12:44 +0000788
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000789/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000790 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
791 * removed from sys.modules, to avoid leaving damaged module objects
792 * in sys.modules. The caller may wish to restore the original
793 * module object (if any) in this case; PyImport_ReloadModule is an
794 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000795 *
796 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
797 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000798 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000799PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000800PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 return PyImport_ExecCodeModuleWithPathnames(
803 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000804}
805
806PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000807PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 return PyImport_ExecCodeModuleWithPathnames(
810 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000811}
812
813PyObject *
814PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000816{
Victor Stinner27ee0892011-03-04 12:57:09 +0000817 PyObject *m = NULL;
818 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
819
820 nameobj = PyUnicode_FromString(name);
821 if (nameobj == NULL)
822 return NULL;
823
824 if (pathname != NULL) {
825 pathobj = PyUnicode_DecodeFSDefault(pathname);
826 if (pathobj == NULL)
827 goto error;
828 } else
829 pathobj = NULL;
830 if (cpathname != NULL) {
831 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
832 if (cpathobj == NULL)
833 goto error;
834 } else
835 cpathobj = NULL;
836 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
837error:
838 Py_DECREF(nameobj);
839 Py_XDECREF(pathobj);
840 Py_XDECREF(cpathobj);
841 return m;
842}
843
844PyObject*
845PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
846 PyObject *cpathname)
847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 PyObject *modules = PyImport_GetModuleDict();
849 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000850
Victor Stinner27ee0892011-03-04 12:57:09 +0000851 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 if (m == NULL)
853 return NULL;
854 /* If the module is being reloaded, we get the old module back
855 and re-use its dict to exec the new code. */
856 d = PyModule_GetDict(m);
857 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
858 if (PyDict_SetItemString(d, "__builtins__",
859 PyEval_GetBuiltins()) != 0)
860 goto error;
861 }
862 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 if (pathname != NULL) {
Victor Stinnerc9abda02011-03-14 13:33:46 -0400864 v = get_sourcefile(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 if (v == NULL)
866 PyErr_Clear();
867 }
Victor Stinner27ee0892011-03-04 12:57:09 +0000868 else
869 v = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 if (v == NULL) {
871 v = ((PyCodeObject *)co)->co_filename;
872 Py_INCREF(v);
873 }
874 if (PyDict_SetItemString(d, "__file__", v) != 0)
875 PyErr_Clear(); /* Not important enough to report */
876 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000879 if (cpathname != NULL)
880 v = cpathname;
881 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 if (PyDict_SetItemString(d, "__cached__", v) != 0)
884 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000885
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000886 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 if (v == NULL)
888 goto error;
889 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000890
Victor Stinner27ee0892011-03-04 12:57:09 +0000891 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000893 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 name);
895 return NULL;
896 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000901
902 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 remove_module(name);
904 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000905}
906
907
Barry Warsaw28a691b2010-04-17 00:19:56 +0000908/* Like strrchr(string, '/') but searches for the rightmost of either SEP
909 or ALTSEP, if the latter is defined.
910*/
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200911static Py_UCS4*
912rightmost_sep(Py_UCS4 *s)
Victor Stinnerc9abda02011-03-14 13:33:46 -0400913{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200914 Py_UCS4 *found, c;
Victor Stinnerc9abda02011-03-14 13:33:46 -0400915 for (found = NULL; (c = *s); s++) {
916 if (c == SEP
917#ifdef ALTSEP
918 || c == ALTSEP
919#endif
920 )
921 {
922 found = s;
923 }
924 }
925 return found;
926}
927
Martin v. Löwis2db72862011-10-23 17:29:08 +0200928/* Like rightmost_sep, but operate on unicode objects. */
929static Py_ssize_t
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200930rightmost_sep_obj(PyObject* o, Py_ssize_t start, Py_ssize_t end)
Martin v. Löwis2db72862011-10-23 17:29:08 +0200931{
932 Py_ssize_t found, i;
933 Py_UCS4 c;
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200934 for (found = -1, i = start; i < end; i++) {
Martin v. Löwis2db72862011-10-23 17:29:08 +0200935 c = PyUnicode_READ_CHAR(o, i);
936 if (c == SEP
937#ifdef ALTSEP
938 || c == ALTSEP
939#endif
940 )
941 {
942 found = i;
943 }
944 }
945 return found;
946}
Victor Stinnerc9abda02011-03-14 13:33:46 -0400947
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000948/* Given a pathname for a Python source file, fill a buffer with the
949 pathname for the corresponding compiled file. Return the pathname
950 for the compiled file, or NULL if there's no space in the buffer.
Victor Stinner2f42ae52011-03-20 00:41:24 +0100951 Doesn't set an exception.
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000952
Martin v. Löwis30260a72011-10-23 17:35:46 +0200953 foo.py -> __pycache__/foo.<tag>.pyc
954
955 pathstr is assumed to be "ready".
956*/
Victor Stinner2f42ae52011-03-20 00:41:24 +0100957
958static PyObject*
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200959make_compiled_pathname(PyObject *pathstr, int debug)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000960{
Martin v. Löwis2db72862011-10-23 17:29:08 +0200961 PyObject *result;
962 Py_ssize_t fname, ext, len, i, pos, taglen;
Martin v. Löwisfadcd312011-10-23 18:08:20 +0200963 Py_ssize_t pycache_len = sizeof(CACHEDIR) - 1;
Martin v. Löwis2db72862011-10-23 17:29:08 +0200964 int kind;
965 void *data;
Antoine Pitroub528fcf2011-10-25 00:21:02 +0200966 Py_UCS4 lastsep;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000967
Martin v. Löwis2db72862011-10-23 17:29:08 +0200968 /* Compute the output string size. */
969 len = PyUnicode_GET_LENGTH(pathstr);
970 /* If there is no separator, this returns -1, so
Antoine Pitroub528fcf2011-10-25 00:21:02 +0200971 fname will be 0. */
Martin v. Löwis8a0ef782011-10-23 18:05:06 +0200972 fname = rightmost_sep_obj(pathstr, 0, len) + 1;
Antoine Pitroub528fcf2011-10-25 00:21:02 +0200973 /* Windows: re-use the last separator character (/ or \\) when
974 appending the __pycache__ path. */
975 if (fname > 0)
976 lastsep = PyUnicode_READ_CHAR(pathstr, fname -1);
977 else
978 lastsep = SEP;
Martin v. Löwis2db72862011-10-23 17:29:08 +0200979 ext = fname - 1;
980 for(i = fname; i < len; i++)
981 if (PyUnicode_READ_CHAR(pathstr, i) == '.')
982 ext = i + 1;
983 if (ext < fname)
984 /* No dot in filename; use entire filename */
985 ext = len;
986
987 /* result = pathstr[:fname] + "__pycache__" + SEP +
988 pathstr[fname:ext] + tag + ".py[co]" */
989 taglen = strlen(pyc_tag);
Martin v. Löwis2cc0cc52011-10-23 18:41:56 +0200990 result = PyUnicode_New(ext + pycache_len + 1 + taglen + 4,
Martin v. Löwis2db72862011-10-23 17:29:08 +0200991 PyUnicode_MAX_CHAR_VALUE(pathstr));
992 if (!result)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200993 return NULL;
Martin v. Löwis2db72862011-10-23 17:29:08 +0200994 kind = PyUnicode_KIND(result);
995 data = PyUnicode_DATA(result);
996 PyUnicode_CopyCharacters(result, 0, pathstr, 0, fname);
997 pos = fname;
Martin v. Löwis2cc0cc52011-10-23 18:41:56 +0200998 for (i = 0; i < pycache_len; i++)
Martin v. Löwisfadcd312011-10-23 18:08:20 +0200999 PyUnicode_WRITE(kind, data, pos++, CACHEDIR[i]);
Antoine Pitroub528fcf2011-10-25 00:21:02 +02001000 PyUnicode_WRITE(kind, data, pos++, lastsep);
Martin v. Löwis2db72862011-10-23 17:29:08 +02001001 PyUnicode_CopyCharacters(result, pos, pathstr,
1002 fname, ext - fname);
1003 pos += ext - fname;
1004 for (i = 0; pyc_tag[i]; i++)
1005 PyUnicode_WRITE(kind, data, pos++, pyc_tag[i]);
1006 PyUnicode_WRITE(kind, data, pos++, '.');
1007 PyUnicode_WRITE(kind, data, pos++, 'p');
1008 PyUnicode_WRITE(kind, data, pos++, 'y');
1009 PyUnicode_WRITE(kind, data, pos++, debug ? 'c' : 'o');
1010 return result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001011}
1012
1013
Barry Warsaw28a691b2010-04-17 00:19:56 +00001014/* Given a pathname to a Python byte compiled file, return the path to the
1015 source file, if the path matches the PEP 3147 format. This does not check
1016 for any file existence, however, if the pyc file name does not match PEP
1017 3147 style, NULL is returned. buf must be at least as big as pathname;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001018 the resulting path will always be shorter.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019
Victor Stinnerc9abda02011-03-14 13:33:46 -04001020 (...)/__pycache__/foo.<tag>.pyc -> (...)/foo.py */
1021
1022static PyObject*
Martin v. Löwis8a0ef782011-10-23 18:05:06 +02001023make_source_pathname(PyObject *path)
Barry Warsaw28a691b2010-04-17 00:19:56 +00001024{
Martin v. Löwis8a0ef782011-10-23 18:05:06 +02001025 Py_ssize_t left, right, dot0, dot1, len;
1026 Py_ssize_t i, j;
1027 PyObject *result;
1028 int kind;
1029 void *data;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001030
Martin v. Löwis8a0ef782011-10-23 18:05:06 +02001031 len = PyUnicode_GET_LENGTH(path);
1032 if (len > MAXPATHLEN)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001033 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 /* Look back two slashes from the end. In between these two slashes
1036 must be the string __pycache__ or this is not a PEP 3147 style
1037 path. It's possible for there to be only one slash.
1038 */
Martin v. Löwis8a0ef782011-10-23 18:05:06 +02001039 right = rightmost_sep_obj(path, 0, len);
1040 if (right == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 return NULL;
Martin v. Löwis8a0ef782011-10-23 18:05:06 +02001042 left = rightmost_sep_obj(path, 0, right);
1043 if (left == -1)
1044 left = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 else
1046 left++;
Martin v. Löwis8a0ef782011-10-23 18:05:06 +02001047 if (right-left != sizeof(CACHEDIR)-1)
1048 return NULL;
1049 for (i = 0; i < sizeof(CACHEDIR)-1; i++)
1050 if (PyUnicode_READ_CHAR(path, left+i) != CACHEDIR[i])
1051 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 /* Now verify that the path component to the right of the last slash
1054 has two dots in it.
1055 */
Martin v. Löwis8a0ef782011-10-23 18:05:06 +02001056 dot0 = PyUnicode_FindChar(path, '.', right+1, len, 1);
1057 if (dot0 < 0)
1058 return NULL;
1059 dot1 = PyUnicode_FindChar(path, '.', dot0+1, len, 1);
1060 if (dot1 < 0)
1061 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 /* Too many dots? */
Martin v. Löwis8a0ef782011-10-23 18:05:06 +02001063 if (PyUnicode_FindChar(path, '.', dot1+1, len, 1) != -1)
1064 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 /* This is a PEP 3147 path. Start by copying everything from the
1067 start of pathname up to and including the leftmost slash. Then
1068 copy the file's basename, removing the magic tag and adding a .py
1069 suffix.
1070 */
Martin v. Löwis8a0ef782011-10-23 18:05:06 +02001071 result = PyUnicode_New(left + (dot0-right) + 2,
1072 PyUnicode_MAX_CHAR_VALUE(path));
1073 if (!result)
1074 return NULL;
1075 kind = PyUnicode_KIND(result);
1076 data = PyUnicode_DATA(result);
1077 PyUnicode_CopyCharacters(result, 0, path, 0, (i = left));
1078 PyUnicode_CopyCharacters(result, left, path, right+1,
1079 (j = dot0-right));
1080 PyUnicode_WRITE(kind, data, i+j, 'p');
1081 PyUnicode_WRITE(kind, data, i+j+1, 'y');
1082 return result;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001083}
1084
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001085/* Given a pathname for a Python source file, its time of last
1086 modification, and a pathname for a compiled file, check whether the
1087 compiled file represents the same version of the source. If so,
1088 return a FILE pointer for the compiled file, positioned just after
1089 the header; if not, return NULL.
1090 Doesn't set an exception. */
1091
1092static FILE *
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001093check_compiled_module(PyObject *pathname, struct stat *srcstat, PyObject *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 FILE *fp;
1096 long magic;
1097 long pyc_mtime;
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001098 long pyc_size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001099
Victor Stinner2f42ae52011-03-20 00:41:24 +01001100 fp = _Py_fopen(cpathname, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 if (fp == NULL)
1102 return NULL;
1103 magic = PyMarshal_ReadLongFromFile(fp);
1104 if (magic != pyc_magic) {
1105 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001106 PySys_FormatStderr("# %R has bad magic\n", cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 fclose(fp);
1108 return NULL;
1109 }
1110 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001111 if (pyc_mtime != srcstat->st_mtime) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001113 PySys_FormatStderr("# %R has bad mtime\n", cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 fclose(fp);
1115 return NULL;
1116 }
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001117 pyc_size = PyMarshal_ReadLongFromFile(fp);
1118 if (pyc_size != (srcstat->st_size & 0xFFFFFFFF)) {
1119 if (Py_VerboseFlag)
1120 PySys_FormatStderr("# %R has bad size\n", cpathname);
1121 fclose(fp);
1122 return NULL;
1123 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001125 PySys_FormatStderr("# %R matches %R\n", cpathname, pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 return fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001127}
1128
1129
1130/* Read a code object from a file and check it for validity */
1131
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132static PyCodeObject *
Victor Stinner2f42ae52011-03-20 00:41:24 +01001133read_compiled_module(PyObject *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 co = PyMarshal_ReadLastObjectFromFile(fp);
1138 if (co == NULL)
1139 return NULL;
1140 if (!PyCode_Check(co)) {
1141 PyErr_Format(PyExc_ImportError,
Victor Stinner2f42ae52011-03-20 00:41:24 +01001142 "Non-code object in %R", cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 Py_DECREF(co);
1144 return NULL;
1145 }
1146 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001147}
1148
1149
1150/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001151 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001152
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153static PyObject *
Victor Stinner2f42ae52011-03-20 00:41:24 +01001154load_compiled_module(PyObject *name, PyObject *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 long magic;
1157 PyCodeObject *co;
1158 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 magic = PyMarshal_ReadLongFromFile(fp);
1161 if (magic != pyc_magic) {
1162 PyErr_Format(PyExc_ImportError,
Victor Stinner2f42ae52011-03-20 00:41:24 +01001163 "Bad magic number in %R", cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 return NULL;
1165 }
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001166 /* Skip mtime and size */
1167 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 (void) PyMarshal_ReadLongFromFile(fp);
1169 co = read_compiled_module(cpathname, fp);
1170 if (co == NULL)
1171 return NULL;
1172 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001173 PySys_FormatStderr("import %U # precompiled from %R\n",
1174 name, cpathname);
1175 m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1176 cpathname, cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001180}
1181
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001182/* Parse a source file and return the corresponding code object */
1183
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184static PyCodeObject *
Victor Stinner2f42ae52011-03-20 00:41:24 +01001185parse_source_module(PyObject *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001186{
Victor Stinner2f42ae52011-03-20 00:41:24 +01001187 PyCodeObject *co;
1188 PyObject *pathbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 mod_ty mod;
1190 PyCompilerFlags flags;
Victor Stinner2f42ae52011-03-20 00:41:24 +01001191 PyArena *arena;
1192
1193 pathbytes = PyUnicode_EncodeFSDefault(pathname);
1194 if (pathbytes == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001196
Victor Stinner2f42ae52011-03-20 00:41:24 +01001197 arena = PyArena_New();
1198 if (arena == NULL) {
1199 Py_DECREF(pathbytes);
1200 return NULL;
1201 }
1202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 flags.cf_flags = 0;
Victor Stinner2f42ae52011-03-20 00:41:24 +01001204 mod = PyParser_ASTFromFile(fp, PyBytes_AS_STRING(pathbytes), NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 Py_file_input, 0, 0, &flags,
1206 NULL, arena);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001207 if (mod != NULL)
1208 co = PyAST_Compile(mod, PyBytes_AS_STRING(pathbytes), NULL, arena);
1209 else
1210 co = NULL;
1211 Py_DECREF(pathbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 PyArena_Free(arena);
1213 return co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001214}
1215
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001216/* Write a compiled module to a file, placing the time of last
1217 modification of its source into the header.
1218 Errors are ignored, if a write error occurs an attempt is made to
1219 remove the file. */
1220
1221static void
Victor Stinner2f42ae52011-03-20 00:41:24 +01001222write_compiled_module(PyCodeObject *co, PyObject *cpathname,
1223 struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001224{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001225 Py_UCS4 *cpathname_ucs4;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 FILE *fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 time_t mtime = srcstat->st_mtime;
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001228 long size = srcstat->st_size & 0xFFFFFFFF;
Antoine Pitrou8ad982c2011-11-15 22:27:32 +01001229 PyObject *cpathname_tmp;
Alexandre Vassalotti9d58e3e2009-07-17 10:55:50 +00001230#ifdef MS_WINDOWS /* since Windows uses different permissions */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 mode_t mode = srcstat->st_mode & ~S_IEXEC;
Victor Stinner1f795172011-11-17 00:45:54 +01001232 wchar_t *wdirname, *wpathname, *wpathname_tmp;
Alexandre Vassalotti9d58e3e2009-07-17 10:55:50 +00001233#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 mode_t dirmode = (srcstat->st_mode |
1235 S_IXUSR | S_IXGRP | S_IXOTH |
1236 S_IWUSR | S_IWGRP | S_IWOTH);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001237 PyObject *dirbytes;
Antoine Pitrou707033a2011-10-17 19:28:44 +02001238 PyObject *cpathbytes, *cpathbytes_tmp;
Victor Stinner783c82c2011-04-20 03:27:51 +02001239#endif
Charles-François Natali0c929d92011-11-10 19:12:29 +01001240 int fd;
Victor Stinner783c82c2011-04-20 03:27:51 +02001241 PyObject *dirname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001242 Py_UCS4 *dirsep;
Victor Stinner2f42ae52011-03-20 00:41:24 +01001243 int res, ok;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 /* Ensure that the __pycache__ directory exists. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001246 cpathname_ucs4 = PyUnicode_AsUCS4Copy(cpathname);
1247 if (!cpathname_ucs4)
1248 return;
1249 dirsep = rightmost_sep(cpathname_ucs4);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001250 if (dirsep == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001252 PySys_FormatStderr("# no %s path found %R\n", CACHEDIR, cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 return;
1254 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001255 dirname = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
1256 cpathname_ucs4,
1257 dirsep - cpathname_ucs4);
1258 PyMem_Free(cpathname_ucs4);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001259 if (dirname == NULL) {
1260 PyErr_Clear();
1261 return;
1262 }
Daniel Stutzbachc7937792010-09-09 21:18:04 +00001263
1264#ifdef MS_WINDOWS
Victor Stinner1f795172011-11-17 00:45:54 +01001265 wdirname = PyUnicode_AsUnicode(dirname);
1266 if (wdirname == NULL) {
1267 PyErr_Clear();
1268 return;
1269 }
1270 res = CreateDirectoryW(wdirname, NULL);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001271 ok = (res != 0);
1272 if (!ok && GetLastError() == ERROR_ALREADY_EXISTS)
1273 ok = 1;
Daniel Stutzbachc7937792010-09-09 21:18:04 +00001274#else
Victor Stinner2f42ae52011-03-20 00:41:24 +01001275 dirbytes = PyUnicode_EncodeFSDefault(dirname);
1276 if (dirbytes == NULL) {
1277 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 return;
1279 }
Victor Stinner2f42ae52011-03-20 00:41:24 +01001280 res = mkdir(PyBytes_AS_STRING(dirbytes), dirmode);
1281 Py_DECREF(dirbytes);
1282 if (0 <= res)
1283 ok = 1;
1284 else
1285 ok = (errno == EEXIST);
1286#endif
1287 if (!ok) {
1288 if (Py_VerboseFlag)
1289 PySys_FormatStderr("# cannot create cache dir %R\n", dirname);
1290 Py_DECREF(dirname);
1291 return;
1292 }
1293 Py_DECREF(dirname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001294
Antoine Pitrou28e401e2011-11-15 19:15:19 +01001295 /* We first write to a tmp file and then take advantage
Antoine Pitrou8ad982c2011-11-15 22:27:32 +01001296 of atomic renaming (which *should* be true even under Windows).
1297 As in importlib, we use id(something) to generate a pseudo-random
1298 filename. mkstemp() can't be used since it doesn't allow specifying
1299 the file access permissions.
1300 */
1301 cpathname_tmp = PyUnicode_FromFormat("%U.%zd",
1302 cpathname, (Py_ssize_t) co);
Antoine Pitrou28e401e2011-11-15 19:15:19 +01001303 if (cpathname_tmp == NULL) {
1304 PyErr_Clear();
1305 return;
1306 }
Antoine Pitrou8ad982c2011-11-15 22:27:32 +01001307#ifdef MS_WINDOWS
Victor Stinner1f795172011-11-17 00:45:54 +01001308 wpathname = PyUnicode_AsUnicode(cpathname);
1309 if (wpathname == NULL) {
1310 PyErr_Clear();
1311 return;
1312 }
1313 wpathname_tmp = PyUnicode_AsUnicode(cpathname_tmp);
1314 if (wpathname_tmp == NULL) {
1315 PyErr_Clear();
1316 return;
1317 }
1318
1319 (void)DeleteFileW(wpathname_tmp);
1320 fd = _wopen(wpathname_tmp,
Antoine Pitrou28e401e2011-11-15 19:15:19 +01001321 O_EXCL | O_CREAT | O_WRONLY | O_BINARY,
1322 mode);
Victor Stinner783c82c2011-04-20 03:27:51 +02001323 if (0 <= fd)
1324 fp = fdopen(fd, "wb");
1325 else
1326 fp = NULL;
1327#else
Antoine Pitrou8ad982c2011-11-15 22:27:32 +01001328 cpathbytes_tmp = PyUnicode_EncodeFSDefault(cpathname_tmp);
1329 Py_DECREF(cpathname_tmp);
1330 if (cpathbytes_tmp == NULL) {
1331 PyErr_Clear();
1332 return;
1333 }
Victor Stinner2f42ae52011-03-20 00:41:24 +01001334 cpathbytes = PyUnicode_EncodeFSDefault(cpathname);
1335 if (cpathbytes == NULL) {
1336 PyErr_Clear();
1337 return;
1338 }
Charles-François Natali0c929d92011-11-10 19:12:29 +01001339 fd = open(PyBytes_AS_STRING(cpathbytes_tmp),
1340 O_CREAT | O_EXCL | O_WRONLY, 0666);
Charles-François Natalie695eec2011-10-31 20:47:31 +01001341 if (0 <= fd)
1342 fp = fdopen(fd, "wb");
1343 else
1344 fp = NULL;
Victor Stinner783c82c2011-04-20 03:27:51 +02001345#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (fp == NULL) {
1347 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001348 PySys_FormatStderr(
1349 "# can't create %R\n", cpathname);
Antoine Pitrou28e401e2011-11-15 19:15:19 +01001350#ifdef MS_WINDOWS
1351 Py_DECREF(cpathname_tmp);
1352#else
Victor Stinner2f42ae52011-03-20 00:41:24 +01001353 Py_DECREF(cpathbytes);
Antoine Pitrou707033a2011-10-17 19:28:44 +02001354 Py_DECREF(cpathbytes_tmp);
Victor Stinner783c82c2011-04-20 03:27:51 +02001355#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 return;
1357 }
1358 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001359 /* First write a 0 for mtime and size */
1360 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
1362 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
Antoine Pitrou707033a2011-10-17 19:28:44 +02001363 fflush(fp);
Antoine Pitrou58161662012-01-25 18:06:07 +01001364 /* Now write the true mtime and size (as 32-bit fields) */
Antoine Pitrou707033a2011-10-17 19:28:44 +02001365 fseek(fp, 4L, 0);
Antoine Pitrou33d15f72012-01-25 18:01:45 +01001366 assert(mtime <= 0xFFFFFFFF);
Antoine Pitrou707033a2011-10-17 19:28:44 +02001367 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001368 PyMarshal_WriteLongToFile(size, fp, Py_MARSHAL_VERSION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if (fflush(fp) != 0 || ferror(fp)) {
1370 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001371 PySys_FormatStderr("# can't write %R\n", cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 /* Don't keep partial file */
1373 fclose(fp);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001374#ifdef MS_WINDOWS
Victor Stinner1f795172011-11-17 00:45:54 +01001375 (void)DeleteFileW(wpathname_tmp);
Antoine Pitrou28e401e2011-11-15 19:15:19 +01001376 Py_DECREF(cpathname_tmp);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001377#else
Antoine Pitrou707033a2011-10-17 19:28:44 +02001378 (void) unlink(PyBytes_AS_STRING(cpathbytes_tmp));
Victor Stinner2f42ae52011-03-20 00:41:24 +01001379 Py_DECREF(cpathbytes);
Antoine Pitrou707033a2011-10-17 19:28:44 +02001380 Py_DECREF(cpathbytes_tmp);
Victor Stinner783c82c2011-04-20 03:27:51 +02001381#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 return;
1383 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 fclose(fp);
Antoine Pitrou28e401e2011-11-15 19:15:19 +01001385 /* Do a (hopefully) atomic rename */
1386#ifdef MS_WINDOWS
Victor Stinner1f795172011-11-17 00:45:54 +01001387 if (!MoveFileExW(wpathname_tmp, wpathname, MOVEFILE_REPLACE_EXISTING)) {
Antoine Pitrou28e401e2011-11-15 19:15:19 +01001388 if (Py_VerboseFlag)
1389 PySys_FormatStderr("# can't write %R\n", cpathname);
1390 /* Don't keep tmp file */
Victor Stinner1f795172011-11-17 00:45:54 +01001391 (void) DeleteFileW(wpathname_tmp);
Antoine Pitrou28e401e2011-11-15 19:15:19 +01001392 Py_DECREF(cpathname_tmp);
1393 return;
1394 }
1395 Py_DECREF(cpathname_tmp);
1396#else
Antoine Pitrou707033a2011-10-17 19:28:44 +02001397 if (rename(PyBytes_AS_STRING(cpathbytes_tmp),
1398 PyBytes_AS_STRING(cpathbytes))) {
1399 if (Py_VerboseFlag)
1400 PySys_FormatStderr("# can't write %R\n", cpathname);
1401 /* Don't keep tmp file */
1402 unlink(PyBytes_AS_STRING(cpathbytes_tmp));
1403 Py_DECREF(cpathbytes);
1404 Py_DECREF(cpathbytes_tmp);
1405 return;
1406 }
1407 Py_DECREF(cpathbytes);
1408 Py_DECREF(cpathbytes_tmp);
1409#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001411 PySys_FormatStderr("# wrote %R\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001412}
1413
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001414static void
1415update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
1416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 PyObject *constants, *tmp;
1418 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (PyUnicode_Compare(co->co_filename, oldname))
1421 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 tmp = co->co_filename;
1424 co->co_filename = newname;
1425 Py_INCREF(co->co_filename);
1426 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 constants = co->co_consts;
1429 n = PyTuple_GET_SIZE(constants);
1430 for (i = 0; i < n; i++) {
1431 tmp = PyTuple_GET_ITEM(constants, i);
1432 if (PyCode_Check(tmp))
1433 update_code_filenames((PyCodeObject *)tmp,
1434 oldname, newname);
1435 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001436}
1437
Victor Stinner2f42ae52011-03-20 00:41:24 +01001438static void
1439update_compiled_module(PyCodeObject *co, PyObject *newname)
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001440{
Victor Stinner2f42ae52011-03-20 00:41:24 +01001441 PyObject *oldname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001442
Victor Stinner2f42ae52011-03-20 00:41:24 +01001443 if (PyUnicode_Compare(co->co_filename, newname) == 0)
1444 return;
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +00001445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 oldname = co->co_filename;
1447 Py_INCREF(oldname);
1448 update_code_filenames(co, oldname, newname);
1449 Py_DECREF(oldname);
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001450}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001451
Brett Cannon442c9b92011-03-23 16:14:42 -07001452static PyObject *
1453imp_fix_co_filename(PyObject *self, PyObject *args)
1454{
1455 PyObject *co;
1456 PyObject *file_path;
1457
1458 if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
1459 return NULL;
1460
1461 if (!PyCode_Check(co)) {
1462 PyErr_SetString(PyExc_TypeError,
1463 "first argument must be a code object");
1464 return NULL;
1465 }
1466
1467 if (!PyUnicode_Check(file_path)) {
1468 PyErr_SetString(PyExc_TypeError,
1469 "second argument must be a string");
1470 return NULL;
1471 }
1472
1473 update_compiled_module((PyCodeObject*)co, file_path);
1474
1475 Py_RETURN_NONE;
1476}
1477
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001478/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001479 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
1480 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001481
Guido van Rossum79f25d91997-04-29 20:08:16 +00001482static PyObject *
Victor Stinner2f42ae52011-03-20 00:41:24 +01001483load_source_module(PyObject *name, PyObject *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 struct stat st;
1486 FILE *fpc;
Victor Stinner2f42ae52011-03-20 00:41:24 +01001487 PyObject *cpathname = NULL, *cpathbytes = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 PyCodeObject *co;
Victor Stinner2f42ae52011-03-20 00:41:24 +01001489 PyObject *m = NULL;
Brett Cannon9a5b25a2010-03-01 02:09:17 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 if (fstat(fileno(fp), &st) != 0) {
1492 PyErr_Format(PyExc_RuntimeError,
Victor Stinner2f42ae52011-03-20 00:41:24 +01001493 "unable to get file status from %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 pathname);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001495 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 }
Antoine Pitrou33d15f72012-01-25 18:01:45 +01001497 if (sizeof st.st_mtime > 4) {
1498 /* Python's .pyc timestamp handling presumes that the timestamp fits
1499 in 4 bytes. Since the code only does an equality comparison,
1500 ordering is not important and we can safely ignore the higher bits
1501 (collisions are extremely unlikely).
1502 */
1503 st.st_mtime &= 0xFFFFFFFF;
1504 }
Martin v. Löwis30260a72011-10-23 17:35:46 +02001505 if (PyUnicode_READY(pathname) < 0)
1506 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001507 cpathname = make_compiled_pathname(pathname, !Py_OptimizeFlag);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001508
1509 if (cpathname != NULL)
Antoine Pitrou5136ac02012-01-13 18:52:16 +01001510 fpc = check_compiled_module(pathname, &st, cpathname);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001511 else
1512 fpc = NULL;
1513
1514 if (fpc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 co = read_compiled_module(cpathname, fpc);
1516 fclose(fpc);
1517 if (co == NULL)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001518 goto error;
1519 update_compiled_module(co, pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001521 PySys_FormatStderr("import %U # precompiled from %R\n",
1522 name, cpathname);
1523 m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1524 cpathname, cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 }
1526 else {
1527 co = parse_source_module(pathname, fp);
1528 if (co == NULL)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001529 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 if (Py_VerboseFlag)
Victor Stinner2f42ae52011-03-20 00:41:24 +01001531 PySys_FormatStderr("import %U # from %R\n",
Victor Stinner98dbba52011-03-14 15:15:47 -04001532 name, pathname);
Victor Stinner2f42ae52011-03-20 00:41:24 +01001533 if (cpathname != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1535 if (ro == NULL || !PyObject_IsTrue(ro))
1536 write_compiled_module(co, cpathname, &st);
1537 }
Victor Stinner2f42ae52011-03-20 00:41:24 +01001538 m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1539 pathname, cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001542
Victor Stinner2f42ae52011-03-20 00:41:24 +01001543error:
1544 Py_XDECREF(cpathbytes);
1545 Py_XDECREF(cpathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001547}
1548
Christian Heimes3b06e532008-01-07 20:12:44 +00001549/* Get source file -> unicode or None
1550 * Returns the path to the py file if available, else the given path
1551 */
1552static PyObject *
Victor Stinnerc9abda02011-03-14 13:33:46 -04001553get_sourcefile(PyObject *filename)
Christian Heimes3b06e532008-01-07 20:12:44 +00001554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 Py_ssize_t len;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001556 Py_UCS4 *fileuni;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001557 PyObject *py;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 struct stat statbuf;
Victor Stinnerbd0850b2011-12-18 20:47:30 +01001559 int err;
Christian Heimes3b06e532008-01-07 20:12:44 +00001560
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001561 len = PyUnicode_GET_LENGTH(filename);
Victor Stinnerc9abda02011-03-14 13:33:46 -04001562 if (len == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 Py_RETURN_NONE;
Christian Heimes3b06e532008-01-07 20:12:44 +00001564
Victor Stinnerc9abda02011-03-14 13:33:46 -04001565 /* don't match *.pyc or *.pyo? */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001566 fileuni = PyUnicode_AsUCS4Copy(filename);
1567 if (!fileuni)
1568 return NULL;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001569 if (len < 5
1570 || fileuni[len-4] != '.'
1571 || (fileuni[len-3] != 'p' && fileuni[len-3] != 'P')
1572 || (fileuni[len-2] != 'y' && fileuni[len-2] != 'Y'))
1573 goto unchanged;
Christian Heimes3b06e532008-01-07 20:12:44 +00001574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 /* Start by trying to turn PEP 3147 path into source path. If that
1576 * fails, just chop off the trailing character, i.e. legacy pyc path
1577 * to py.
1578 */
Victor Stinnerc9abda02011-03-14 13:33:46 -04001579 py = make_source_pathname(filename);
1580 if (py == NULL) {
1581 PyErr_Clear();
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001582 py = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, fileuni, len - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 }
Victor Stinnerc9abda02011-03-14 13:33:46 -04001584 if (py == NULL)
1585 goto error;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001586
Victor Stinnerbd0850b2011-12-18 20:47:30 +01001587 err = _Py_stat(py, &statbuf);
1588 if (err == -2)
1589 goto error;
1590 if (err == 0 && S_ISREG(statbuf.st_mode)) {
Martin v. Löwis0b1d3482011-10-01 16:35:40 +02001591 PyMem_Free(fileuni);
Victor Stinnerc9abda02011-03-14 13:33:46 -04001592 return py;
Martin v. Löwis0b1d3482011-10-01 16:35:40 +02001593 }
Victor Stinnerc9abda02011-03-14 13:33:46 -04001594 Py_DECREF(py);
1595 goto unchanged;
1596
1597error:
1598 PyErr_Clear();
1599unchanged:
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001600 PyMem_Free(fileuni);
Victor Stinnerc9abda02011-03-14 13:33:46 -04001601 Py_INCREF(filename);
1602 return filename;
Christian Heimes3b06e532008-01-07 20:12:44 +00001603}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001604
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001605/* Forward */
Victor Stinner41c5fec2011-03-13 21:46:30 -04001606static PyObject *load_module(PyObject *, FILE *, PyObject *, int, PyObject *);
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001607static struct filedescr *find_module(PyObject *, PyObject *, PyObject *,
Victor Stinner2fd76e42011-03-14 15:19:39 -04001608 PyObject **, FILE **, PyObject **);
Victor Stinner53dc7352011-03-20 01:50:21 +01001609static struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001610
1611/* Load a package and return its module object WITH INCREMENTED
1612 REFERENCE COUNT */
1613
1614static PyObject *
Victor Stinnerc9abda02011-03-14 13:33:46 -04001615load_package(PyObject *name, PyObject *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001616{
Victor Stinner41c5fec2011-03-13 21:46:30 -04001617 PyObject *m, *d, *bufobj;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001618 PyObject *file = NULL, *path_list = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 int err;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001620 FILE *fp = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 struct filedescr *fdp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001622
Victor Stinnerc9abda02011-03-14 13:33:46 -04001623 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (m == NULL)
1625 return NULL;
1626 if (Py_VerboseFlag)
Victor Stinner98dbba52011-03-14 15:15:47 -04001627 PySys_FormatStderr("import %U # directory %R\n",
1628 name, pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 file = get_sourcefile(pathname);
1630 if (file == NULL)
Victor Stinnerc9abda02011-03-14 13:33:46 -04001631 return NULL;
1632 path_list = Py_BuildValue("[O]", file);
1633 if (path_list == NULL) {
1634 Py_DECREF(file);
1635 return NULL;
1636 }
1637 d = PyModule_GetDict(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 err = PyDict_SetItemString(d, "__file__", file);
Victor Stinnerc9abda02011-03-14 13:33:46 -04001639 Py_DECREF(file);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (err == 0)
Victor Stinnerc9abda02011-03-14 13:33:46 -04001641 err = PyDict_SetItemString(d, "__path__", path_list);
1642 if (err != 0) {
1643 Py_DECREF(path_list);
1644 return NULL;
1645 }
Victor Stinner533d7832011-03-14 13:22:54 -04001646 fdp = find_module(name, initstr, path_list,
Victor Stinner2fd76e42011-03-14 15:19:39 -04001647 &bufobj, &fp, NULL);
Victor Stinnerc9abda02011-03-14 13:33:46 -04001648 Py_DECREF(path_list);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (fdp == NULL) {
1650 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1651 PyErr_Clear();
1652 Py_INCREF(m);
Victor Stinnerc9abda02011-03-14 13:33:46 -04001653 return m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 }
1655 else
Victor Stinnerc9abda02011-03-14 13:33:46 -04001656 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 }
Victor Stinner2fd76e42011-03-14 15:19:39 -04001658 m = load_module(name, fp, bufobj, fdp->type, NULL);
1659 Py_XDECREF(bufobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (fp != NULL)
1661 fclose(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001663}
1664
1665
1666/* Helper to test for built-in module */
1667
1668static int
Victor Stinner95872862011-03-07 18:20:56 +01001669is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001670{
Victor Stinner95872862011-03-07 18:20:56 +01001671 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +01001673 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
1674 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 if (PyImport_Inittab[i].initfunc == NULL)
1676 return -1;
1677 else
1678 return 1;
1679 }
1680 }
1681 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001682}
1683
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001684
Just van Rossum52e14d62002-12-30 22:08:05 +00001685/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1686 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001687 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001688 that can handle the path item. Return None if no hook could;
1689 this tells our caller it should fall back to the builtin
1690 import mechanism. Cache the result in path_importer_cache.
1691 Returns a borrowed reference. */
1692
1693static PyObject *
1694get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +00001696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 PyObject *importer;
1698 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 /* These conditions are the caller's responsibility: */
1701 assert(PyList_Check(path_hooks));
1702 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 nhooks = PyList_Size(path_hooks);
1705 if (nhooks < 0)
1706 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 importer = PyDict_GetItem(path_importer_cache, p);
1709 if (importer != NULL)
1710 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 /* set path_importer_cache[p] to None to avoid recursion */
1713 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1714 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 for (j = 0; j < nhooks; j++) {
1717 PyObject *hook = PyList_GetItem(path_hooks, j);
1718 if (hook == NULL)
1719 return NULL;
1720 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
1721 if (importer != NULL)
1722 break;
Just van Rossum52e14d62002-12-30 22:08:05 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1725 return NULL;
1726 }
1727 PyErr_Clear();
1728 }
1729 if (importer == NULL) {
1730 importer = PyObject_CallFunctionObjArgs(
1731 (PyObject *)&PyNullImporter_Type, p, NULL
1732 );
1733 if (importer == NULL) {
1734 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1735 PyErr_Clear();
1736 return Py_None;
1737 }
1738 }
1739 }
1740 if (importer != NULL) {
1741 int err = PyDict_SetItem(path_importer_cache, p, importer);
1742 Py_DECREF(importer);
1743 if (err != 0)
1744 return NULL;
1745 }
1746 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001747}
1748
Christian Heimes9cd17752007-11-18 19:35:23 +00001749PyAPI_FUNC(PyObject *)
1750PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1754 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1755 importer = get_path_importer(path_importer_cache,
1756 path_hooks, path);
1757 }
1758 }
1759 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1760 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +00001761}
1762
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001763/* Search the path (default sys.path) for a module. Return the
1764 corresponding filedescr struct, and (via return arguments) the
1765 pathname and an open file. Return NULL if the module is not found. */
1766
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001767#ifdef MS_COREDLL
Victor Stinner4d6c1c42011-03-08 23:49:04 +01001768extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **,
1769 PyObject **p_path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001770#endif
1771
Victor Stinner547a2a62011-03-20 03:07:28 +01001772/* Forward */
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001773static int case_ok(PyObject *, Py_ssize_t, PyObject *);
Victor Stinner547a2a62011-03-20 03:07:28 +01001774static int find_init_module(PyObject *);
Just van Rossum52e14d62002-12-30 22:08:05 +00001775static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001776
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001777/* Get the path of a module: get its importer and call importer.find_module()
1778 hook, or check if the module if a package (if path/__init__.py exists).
1779
1780 -1: error: a Python error occurred
1781 0: ignore: an error occurred because of invalid data, but the error is not
1782 important enough to be reported.
1783 1: get path: module not found, but *buf contains its path
1784 2: found: *p_fd is the file descriptor (IMP_HOOK or PKG_DIRECTORY)
1785 and *buf is the path */
1786
1787static int
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001788find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001789 PyObject *path_hooks, PyObject *path_importer_cache,
Victor Stinner2fd76e42011-03-14 15:19:39 -04001790 PyObject **p_path, PyObject **p_loader, struct filedescr **p_fd)
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001791{
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001792 PyObject *path_unicode, *filename = NULL;
1793 Py_ssize_t len, pos;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001794 struct stat statbuf;
1795 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
Victor Stinnerbd0850b2011-12-18 20:47:30 +01001796 int err, result, addsep;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001797
1798 if (PyUnicode_Check(path)) {
Victor Stinner2fd76e42011-03-14 15:19:39 -04001799 Py_INCREF(path);
1800 path_unicode = path;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001801 }
1802 else if (PyBytes_Check(path)) {
Victor Stinner2fd76e42011-03-14 15:19:39 -04001803 path_unicode = PyUnicode_DecodeFSDefaultAndSize(
1804 PyBytes_AS_STRING(path), PyBytes_GET_SIZE(path));
1805 if (path_unicode == NULL)
1806 return -1;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001807 }
1808 else
1809 return 0;
1810
Victor Stinner46084ba2011-10-06 02:39:42 +02001811 if (PyUnicode_READY(path_unicode))
1812 return -1;
1813
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001814 len = PyUnicode_GET_LENGTH(path_unicode);
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001815 if (PyUnicode_FindChar(path_unicode, 0, 0, len, 1) != -1) {
1816 result = 0;
1817 goto out; /* path contains '\0' */
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001818 }
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001819
1820 /* sys.path_hooks import hook */
1821 if (p_loader != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001822 _Py_IDENTIFIER(find_module);
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001823 PyObject *importer;
1824
1825 importer = get_path_importer(path_importer_cache,
1826 path_hooks, path);
1827 if (importer == NULL) {
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001828 result = -1;
1829 goto out;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001830 }
1831 /* Note: importer is a borrowed reference */
1832 if (importer != Py_None) {
1833 PyObject *loader;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001834 loader = _PyObject_CallMethodId(importer,
1835 &PyId_find_module, "O", fullname);
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001836 if (loader == NULL) {
1837 result = -1; /* error */
1838 goto out;
1839 }
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001840 if (loader != Py_None) {
1841 /* a loader was found */
1842 *p_loader = loader;
1843 *p_fd = &importhookdescr;
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001844 result = 2;
1845 goto out;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001846 }
1847 Py_DECREF(loader);
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001848 result = 0;
1849 goto out;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001850 }
1851 }
1852 /* no hook was found, use builtin import */
1853
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001854 addsep = 0;
1855 if (len > 0 && PyUnicode_READ_CHAR(path_unicode, len-1) != SEP
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001856#ifdef ALTSEP
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001857 && PyUnicode_READ_CHAR(path_unicode, len-1) != ALTSEP
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001858#endif
1859 )
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001860 addsep = 1;
1861 filename = PyUnicode_New(len + PyUnicode_GET_LENGTH(name) + addsep,
1862 Py_MAX(PyUnicode_MAX_CHAR_VALUE(path_unicode),
1863 PyUnicode_MAX_CHAR_VALUE(name)));
1864 if (filename == NULL) {
1865 result = -1;
1866 goto out;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001867 }
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001868 PyUnicode_CopyCharacters(filename, 0, path_unicode, 0, len);
1869 pos = len;
1870 if (addsep)
Victor Stinner1f795172011-11-17 00:45:54 +01001871 PyUnicode_WRITE(PyUnicode_KIND(filename),
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001872 PyUnicode_DATA(filename),
1873 pos++, SEP);
Victor Stinner1f795172011-11-17 00:45:54 +01001874 PyUnicode_CopyCharacters(filename, pos, name, 0,
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001875 PyUnicode_GET_LENGTH(name));
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001876
1877 /* Check for package import (buf holds a directory name,
1878 and there's an __init__ module in that directory */
1879#ifdef HAVE_STAT
Victor Stinnerbd0850b2011-12-18 20:47:30 +01001880 err = _Py_stat(filename, &statbuf);
1881 if (err == -2) {
1882 result = -1;
1883 goto out;
1884 }
1885 if (err == 0 && /* it exists */
Victor Stinnerd0296212011-03-14 14:04:10 -04001886 S_ISDIR(statbuf.st_mode)) /* it's a directory */
1887 {
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001888 int match;
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001889
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001890 match = case_ok(filename, 0, name);
1891 if (match < 0) {
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001892 result = -1;
1893 goto out;
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001894 }
1895 if (match) { /* case matches */
1896 if (find_init_module(filename)) { /* and has __init__.py */
Victor Stinner2fd76e42011-03-14 15:19:39 -04001897 *p_path = filename;
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001898 filename = NULL;
Victor Stinnerd0296212011-03-14 14:04:10 -04001899 *p_fd = &fd_package;
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001900 result = 2;
1901 goto out;
Victor Stinnerd0296212011-03-14 14:04:10 -04001902 }
1903 else {
1904 int err;
Victor Stinnerd0296212011-03-14 14:04:10 -04001905 err = PyErr_WarnFormat(PyExc_ImportWarning, 1,
Victor Stinner547a2a62011-03-20 03:07:28 +01001906 "Not importing directory %R: missing __init__.py",
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001907 filename);
Victor Stinner547a2a62011-03-20 03:07:28 +01001908 if (err) {
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001909 result = -1;
1910 goto out;
Victor Stinner547a2a62011-03-20 03:07:28 +01001911 }
Victor Stinnerd0296212011-03-14 14:04:10 -04001912 }
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001913 }
1914 }
1915#endif
Victor Stinner2fd76e42011-03-14 15:19:39 -04001916 *p_path = filename;
Martin v. Löwis9af29d32011-10-23 18:45:16 +02001917 filename = NULL;
1918 result = 1;
1919 out:
1920 Py_DECREF(path_unicode);
1921 Py_XDECREF(filename);
1922 return result;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001923}
1924
1925/* Find a module in search_path_list. For each path, try
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001926 find_module_path() or try each _PyImport_Filetab suffix.
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001927
1928 If the module is found, return a file descriptor, write the path in
1929 *p_filename, write the pointer to the file object into *p_fp, and (if
1930 p_loader is not NULL) the loader into *p_loader.
1931
1932 Otherwise, raise an exception and return NULL. */
1933
Victor Stinner37580282011-03-20 01:34:43 +01001934static struct filedescr*
Victor Stinnerad3c03b2011-03-14 09:21:33 -04001935find_module_path_list(PyObject *fullname, PyObject *name,
Victor Stinner37580282011-03-20 01:34:43 +01001936 PyObject *search_path_list, PyObject *path_hooks,
1937 PyObject *path_importer_cache,
Victor Stinner2fd76e42011-03-14 15:19:39 -04001938 PyObject **p_path, FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 Py_ssize_t i, npath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 struct filedescr *fdp = NULL;
1942 char *filemode;
1943 FILE *fp = NULL;
Victor Stinner2fd76e42011-03-14 15:19:39 -04001944 PyObject *prefix, *filename;
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001945 int match;
Victor Stinnerbd0850b2011-12-18 20:47:30 +01001946 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01001947
Victor Stinner37580282011-03-20 01:34:43 +01001948 npath = PyList_Size(search_path_list);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 for (i = 0; i < npath; i++) {
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001950 PyObject *path;
1951 int ok;
1952
1953 path = PyList_GetItem(search_path_list, i);
1954 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 return NULL;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001956
Victor Stinner2fd76e42011-03-14 15:19:39 -04001957 prefix = NULL;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001958 ok = find_module_path(fullname, name, path,
1959 path_hooks, path_importer_cache,
Victor Stinner2fd76e42011-03-14 15:19:39 -04001960 &prefix, p_loader, &fdp);
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001961 if (ok < 0)
1962 return NULL;
1963 if (ok == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 continue;
Victor Stinner2fd76e42011-03-14 15:19:39 -04001965 if (ok == 2) {
1966 *p_path = prefix;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001967 return fdp;
Victor Stinner2fd76e42011-03-14 15:19:39 -04001968 }
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Victor Stinnera1fe1f82011-09-23 18:59:08 +02001971 struct stat statbuf;
1972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 filemode = fdp->mode;
1974 if (filemode[0] == 'U')
1975 filemode = "r" PY_STDIOTEXTMODE;
Victor Stinnerd0296212011-03-14 14:04:10 -04001976
Victor Stinner2fd76e42011-03-14 15:19:39 -04001977 filename = PyUnicode_FromFormat("%U%s", prefix, fdp->suffix);
1978 if (filename == NULL) {
1979 Py_DECREF(prefix);
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001980 return NULL;
Victor Stinner2fd76e42011-03-14 15:19:39 -04001981 }
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001982
1983 if (Py_VerboseFlag > 1)
1984 PySys_FormatStderr("# trying %R\n", filename);
1985
Victor Stinnerbd0850b2011-12-18 20:47:30 +01001986 err = _Py_stat(filename, &statbuf);
1987 if (err == -2) {
1988 Py_DECREF(prefix);
1989 Py_DECREF(filename);
1990 return NULL;
1991 }
1992 if (err != 0 || S_ISDIR(statbuf.st_mode)) {
Charles-François Natali1659b832011-12-07 23:17:58 +01001993 /* it doesn't exist, or it's a directory */
Victor Stinnera1fe1f82011-09-23 18:59:08 +02001994 Py_DECREF(filename);
1995 continue;
1996 }
1997
Victor Stinnerc9a271c2011-03-14 14:34:13 -04001998 fp = _Py_fopen(filename, filemode);
1999 if (fp == NULL) {
2000 Py_DECREF(filename);
Victor Stinner925ef392011-06-20 15:01:10 +02002001 if (PyErr_Occurred()) {
2002 Py_DECREF(prefix);
2003 return NULL;
2004 }
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002005 continue;
2006 }
2007 match = case_ok(filename, -(Py_ssize_t)strlen(fdp->suffix), name);
2008 if (match < 0) {
Victor Stinner2fd76e42011-03-14 15:19:39 -04002009 Py_DECREF(prefix);
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002010 Py_DECREF(filename);
2011 return NULL;
2012 }
2013 if (match) {
Victor Stinner2fd76e42011-03-14 15:19:39 -04002014 Py_DECREF(prefix);
2015 *p_path = filename;
Victor Stinnerd0296212011-03-14 14:04:10 -04002016 *p_fp = fp;
2017 return fdp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 }
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002019 Py_DECREF(filename);
Victor Stinnerd0296212011-03-14 14:04:10 -04002020
2021 fclose(fp);
2022 fp = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 }
Victor Stinner2fd76e42011-03-14 15:19:39 -04002024 Py_DECREF(prefix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
Victor Stinnerd0296212011-03-14 14:04:10 -04002026 PyErr_Format(PyExc_ImportError,
Victor Stinner98dbba52011-03-14 15:15:47 -04002027 "No module named %R", name);
Victor Stinnerd0296212011-03-14 14:04:10 -04002028 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002029}
2030
Victor Stinner37580282011-03-20 01:34:43 +01002031/* Find a module:
2032
2033 - try find_module() of each sys.meta_path hook
2034 - try find_frozen()
2035 - try is_builtin()
2036 - try _PyWin_FindRegisteredModule() (Windows only)
2037 - otherwise, call find_module_path_list() with search_path_list (if not
2038 NULL) or sys.path
2039
Victor Stinner2fd76e42011-03-14 15:19:39 -04002040 fullname can be NULL, but only if p_loader is NULL.
2041
Victor Stinner37580282011-03-20 01:34:43 +01002042 Return:
2043
2044 - &fd_builtin (C_BUILTIN) if it is a builtin
2045 - &fd_frozen (PY_FROZEN) if it is frozen
Victor Stinner2fd76e42011-03-14 15:19:39 -04002046 - &fd_package (PKG_DIRECTORY) and write the filename into *p_path
Victor Stinner37580282011-03-20 01:34:43 +01002047 if it is a package
2048 - &importhookdescr (IMP_HOOK) and write the loader into *p_loader if a
2049 importer loader was found
2050 - a file descriptor (PY_SOURCE, PY_COMPILED, C_EXTENSION, PY_RESOURCE or
2051 PY_CODERESOURCE: see _PyImport_Filetab), write the filename into
Victor Stinner2fd76e42011-03-14 15:19:39 -04002052 *p_path and the pointer to the open file into *p_fp
Victor Stinner37580282011-03-20 01:34:43 +01002053 - NULL on error
2054
Victor Stinner2fd76e42011-03-14 15:19:39 -04002055 By default, *p_path, *p_fp and *p_loader (if set) are set to NULL.
2056 Eg. *p_path is set to NULL for a builtin package.
2057*/
Victor Stinner37580282011-03-20 01:34:43 +01002058
2059static struct filedescr *
Victor Stinnerad3c03b2011-03-14 09:21:33 -04002060find_module(PyObject *fullname, PyObject *name, PyObject *search_path_list,
Victor Stinner2fd76e42011-03-14 15:19:39 -04002061 PyObject **p_path, FILE **p_fp, PyObject **p_loader)
Victor Stinner37580282011-03-20 01:34:43 +01002062{
2063 Py_ssize_t i, npath;
2064 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
2065 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
2066 PyObject *path_hooks, *path_importer_cache;
Victor Stinner37580282011-03-20 01:34:43 +01002067
Victor Stinner2fd76e42011-03-14 15:19:39 -04002068 *p_path = NULL;
Victor Stinner37580282011-03-20 01:34:43 +01002069 *p_fp = NULL;
2070 if (p_loader != NULL)
2071 *p_loader = NULL;
2072
Victor Stinner37580282011-03-20 01:34:43 +01002073 /* sys.meta_path import hook */
2074 if (p_loader != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002075 _Py_IDENTIFIER(find_module);
Victor Stinner37580282011-03-20 01:34:43 +01002076 PyObject *meta_path;
2077
2078 meta_path = PySys_GetObject("meta_path");
2079 if (meta_path == NULL || !PyList_Check(meta_path)) {
Victor Stinner16191322011-09-15 19:28:05 +02002080 PyErr_SetString(PyExc_RuntimeError,
Victor Stinner37580282011-03-20 01:34:43 +01002081 "sys.meta_path must be a list of "
2082 "import hooks");
2083 return NULL;
2084 }
2085 Py_INCREF(meta_path); /* zap guard */
2086 npath = PyList_Size(meta_path);
2087 for (i = 0; i < npath; i++) {
2088 PyObject *loader;
2089 PyObject *hook = PyList_GetItem(meta_path, i);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002090 loader = _PyObject_CallMethodId(hook, &PyId_find_module,
Victor Stinnerad3c03b2011-03-14 09:21:33 -04002091 "OO", fullname,
Victor Stinner37580282011-03-20 01:34:43 +01002092 search_path_list != NULL ?
2093 search_path_list : Py_None);
2094 if (loader == NULL) {
2095 Py_DECREF(meta_path);
2096 return NULL; /* true error */
2097 }
2098 if (loader != Py_None) {
2099 /* a loader was found */
2100 *p_loader = loader;
2101 Py_DECREF(meta_path);
2102 return &importhookdescr;
2103 }
2104 Py_DECREF(loader);
2105 }
2106 Py_DECREF(meta_path);
2107 }
2108
Victor Stinnerdf75a022011-03-14 13:40:04 -04002109 if (find_frozen(fullname) != NULL)
Victor Stinnerad3c03b2011-03-14 09:21:33 -04002110 return &fd_frozen;
Victor Stinner37580282011-03-20 01:34:43 +01002111
2112 if (search_path_list == NULL) {
2113#ifdef MS_COREDLL
2114 FILE *fp;
2115 struct filedescr *fdp;
Victor Stinner37580282011-03-20 01:34:43 +01002116#endif
Victor Stinnerdf75a022011-03-14 13:40:04 -04002117 if (is_builtin(name))
Victor Stinner37580282011-03-20 01:34:43 +01002118 return &fd_builtin;
Victor Stinner37580282011-03-20 01:34:43 +01002119#ifdef MS_COREDLL
Victor Stinner2fd76e42011-03-14 15:19:39 -04002120 fp = _PyWin_FindRegisteredModule(name, &fdp, p_path);
Victor Stinner37580282011-03-20 01:34:43 +01002121 if (fp != NULL) {
Victor Stinner37580282011-03-20 01:34:43 +01002122 *p_fp = fp;
2123 return fdp;
2124 }
2125 else if (PyErr_Occurred())
2126 return NULL;
2127#endif
Victor Stinner37580282011-03-20 01:34:43 +01002128 search_path_list = PySys_GetObject("path");
2129 }
2130
2131 if (search_path_list == NULL || !PyList_Check(search_path_list)) {
Victor Stinner16191322011-09-15 19:28:05 +02002132 PyErr_SetString(PyExc_RuntimeError,
Victor Stinner37580282011-03-20 01:34:43 +01002133 "sys.path must be a list of directory names");
2134 return NULL;
2135 }
2136
2137 path_hooks = PySys_GetObject("path_hooks");
2138 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
Victor Stinner16191322011-09-15 19:28:05 +02002139 PyErr_SetString(PyExc_RuntimeError,
Victor Stinner37580282011-03-20 01:34:43 +01002140 "sys.path_hooks must be a list of "
2141 "import hooks");
2142 return NULL;
2143 }
2144 path_importer_cache = PySys_GetObject("path_importer_cache");
2145 if (path_importer_cache == NULL ||
2146 !PyDict_Check(path_importer_cache)) {
Victor Stinner16191322011-09-15 19:28:05 +02002147 PyErr_SetString(PyExc_RuntimeError,
Victor Stinner37580282011-03-20 01:34:43 +01002148 "sys.path_importer_cache must be a dict");
2149 return NULL;
2150 }
2151
2152 return find_module_path_list(fullname, name,
2153 search_path_list, path_hooks,
2154 path_importer_cache,
Victor Stinner2fd76e42011-03-14 15:19:39 -04002155 p_path, p_fp, p_loader);
Victor Stinner37580282011-03-20 01:34:43 +01002156}
2157
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002158/* case_bytes(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00002159 * The arguments here are tricky, best shown by example:
2160 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
2161 * ^ ^ ^ ^
2162 * |--------------------- buf ---------------------|
2163 * |------------------- len ------------------|
2164 * |------ name -------|
2165 * |----- namelen -----|
2166 * buf is the full path, but len only counts up to (& exclusive of) the
2167 * extension. name is the module name, also exclusive of extension.
2168 *
2169 * We've already done a successful stat() or fopen() on buf, so know that
2170 * there's some match, possibly case-insensitive.
2171 *
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002172 * case_bytes() is to return 1 if there's a case-sensitive match for
2173 * name, else 0. case_bytes() is also to return 1 if envar PYTHONCASEOK
Tim Peters50d8d372001-02-28 05:34:27 +00002174 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00002175 *
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002176 * case_bytes() is used to implement case-sensitive import semantics even
Tim Peters50d8d372001-02-28 05:34:27 +00002177 * on platforms with case-insensitive filesystems. It's trivial to implement
2178 * for case-sensitive filesystems. It's pretty much a cross-platform
2179 * nightmare for systems with case-insensitive filesystems.
2180 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00002181
Tim Peters50d8d372001-02-28 05:34:27 +00002182/* First we may need a pile of platform-specific header files; the sequence
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002183 * of #if's here should match the sequence in the body of case_bytes().
Tim Peters50d8d372001-02-28 05:34:27 +00002184 */
Jason Tishler7961aa62005-05-20 00:56:54 +00002185#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00002186#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00002187
Tim Peters50d8d372001-02-28 05:34:27 +00002188#elif defined(DJGPP)
2189#include <dir.h>
2190
Jason Tishler7961aa62005-05-20 00:56:54 +00002191#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00002192#include <sys/types.h>
2193#include <dirent.h>
2194
Andrew MacIntyred9400542002-02-26 11:41:34 +00002195#elif defined(PYOS_OS2)
2196#define INCL_DOS
2197#define INCL_DOSERRORS
2198#define INCL_NOPMAPI
2199#include <os2.h>
Tim Peters50d8d372001-02-28 05:34:27 +00002200#endif
2201
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002202#if defined(DJGPP) \
2203 || ((defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) \
2204 && defined(HAVE_DIRENT_H)) \
2205 || defined(PYOS_OS2)
2206# define USE_CASE_OK_BYTES
2207#endif
2208
2209
2210#ifdef USE_CASE_OK_BYTES
Guido van Rossum0980bd91998-02-13 17:18:36 +00002211static int
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002212case_bytes(char *buf, Py_ssize_t len, Py_ssize_t namelen, const char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00002213{
Tim Peters50d8d372001-02-28 05:34:27 +00002214/* Pick a platform-specific implementation; the sequence of #if's here should
2215 * match the sequence just above.
2216 */
2217
Tim Peters50d8d372001-02-28 05:34:27 +00002218/* DJGPP */
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002219#if defined(DJGPP)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 struct ffblk ffblk;
2221 int done;
Tim Peters50d8d372001-02-28 05:34:27 +00002222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 if (Py_GETENV("PYTHONCASEOK") != NULL)
2224 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00002225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
2227 if (done) {
2228 PyErr_Format(PyExc_NameError,
2229 "Can't find file for module %.100s\n(filename %.300s)",
2230 name, buf);
Victor Stinner9c61e242011-03-22 01:22:27 +01002231 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 }
2233 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00002234
Jason Tishler7961aa62005-05-20 00:56:54 +00002235/* new-fangled macintosh (macosx) or Cygwin */
2236#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 DIR *dirp;
2238 struct dirent *dp;
2239 char dirname[MAXPATHLEN + 1];
2240 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00002241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 if (Py_GETENV("PYTHONCASEOK") != NULL)
2243 return 1;
Tim Petersdbe6ebb2001-03-01 08:47:29 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 /* Copy the dir component into dirname; substitute "." if empty */
2246 if (dirlen <= 0) {
2247 dirname[0] = '.';
2248 dirname[1] = '\0';
2249 }
2250 else {
2251 assert(dirlen <= MAXPATHLEN);
2252 memcpy(dirname, buf, dirlen);
2253 dirname[dirlen] = '\0';
2254 }
2255 /* Open the directory and search the entries for an exact match. */
2256 dirp = opendir(dirname);
2257 if (dirp) {
2258 char *nameWithExt = buf + len - namelen;
2259 while ((dp = readdir(dirp)) != NULL) {
2260 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00002261#ifdef _DIRENT_HAVE_D_NAMELEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00002263#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00002265#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 if (thislen >= namelen &&
2267 strcmp(dp->d_name, nameWithExt) == 0) {
2268 (void)closedir(dirp);
2269 return 1; /* Found */
2270 }
2271 }
2272 (void)closedir(dirp);
2273 }
2274 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00002275
Andrew MacIntyred9400542002-02-26 11:41:34 +00002276/* OS/2 */
2277#elif defined(PYOS_OS2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 HDIR hdir = 1;
2279 ULONG srchcnt = 1;
2280 FILEFINDBUF3 ffbuf;
2281 APIRET rc;
Andrew MacIntyred9400542002-02-26 11:41:34 +00002282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 if (Py_GETENV("PYTHONCASEOK") != NULL)
2284 return 1;
Andrew MacIntyred9400542002-02-26 11:41:34 +00002285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 rc = DosFindFirst(buf,
2287 &hdir,
2288 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
2289 &ffbuf, sizeof(ffbuf),
2290 &srchcnt,
2291 FIL_STANDARD);
2292 if (rc != NO_ERROR)
2293 return 0;
2294 return strncmp(ffbuf.achName, name, namelen) == 0;
Andrew MacIntyred9400542002-02-26 11:41:34 +00002295
Tim Peters50d8d372001-02-28 05:34:27 +00002296/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
2297#else
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002298# error "USE_CASE_OK_BYTES is not correctly defined"
2299#endif
2300}
2301#endif
2302
2303/*
2304 * Check if a filename case matchs the name case. We've already done a
2305 * successful stat() or fopen() on buf, so know that there's some match,
2306 * possibly case-insensitive.
2307 *
2308 * case_ok() is to return 1 if there's a case-sensitive match for name, 0 if it
2309 * the filename doesn't match, or -1 on error. case_ok() is also to return 1
2310 * if envar PYTHONCASEOK exists.
2311 *
2312 * case_ok() is used to implement case-sensitive import semantics even
2313 * on platforms with case-insensitive filesystems. It's trivial to implement
2314 * for case-sensitive filesystems. It's pretty much a cross-platform
2315 * nightmare for systems with case-insensitive filesystems.
2316 */
2317
2318static int
2319case_ok(PyObject *filename, Py_ssize_t prefix_delta, PyObject *name)
2320{
2321#ifdef MS_WINDOWS
2322 WIN32_FIND_DATAW data;
2323 HANDLE h;
2324 int cmp;
Victor Stinner1f795172011-11-17 00:45:54 +01002325 wchar_t *wfilename, *wname;
Victor Stinner8c981892011-10-11 22:27:13 +02002326 Py_ssize_t wname_len;
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002327
2328 if (Py_GETENV("PYTHONCASEOK") != NULL)
2329 return 1;
2330
Victor Stinner1f795172011-11-17 00:45:54 +01002331 wfilename = PyUnicode_AsUnicode(filename);
2332 if (wfilename == NULL)
2333 return -1;
2334
2335 h = FindFirstFileW(wfilename, &data);
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002336 if (h == INVALID_HANDLE_VALUE) {
2337 PyErr_Format(PyExc_NameError,
2338 "Can't find file for module %R\n(filename %R)",
2339 name, filename);
Victor Stinner1f795172011-11-17 00:45:54 +01002340 return -1;
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002341 }
2342 FindClose(h);
Victor Stinnerbeac78b2011-10-11 21:55:01 +02002343
2344 wname = PyUnicode_AsUnicodeAndSize(name, &wname_len);
2345 if (wname == NULL)
2346 return -1;
2347
2348 cmp = wcsncmp(data.cFileName, wname, wname_len);
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002349 return cmp == 0;
2350#elif defined(USE_CASE_OK_BYTES)
2351 int match;
2352 PyObject *filebytes, *namebytes;
2353 filebytes = PyUnicode_EncodeFSDefault(filename);
2354 if (filebytes == NULL)
2355 return -1;
2356 namebytes = PyUnicode_EncodeFSDefault(name);
2357 if (namebytes == NULL) {
2358 Py_DECREF(filebytes);
2359 return -1;
2360 }
2361 match = case_bytes(
2362 PyBytes_AS_STRING(filebytes),
2363 PyBytes_GET_SIZE(filebytes) + prefix_delta,
Victor Stinner1304f2d2011-03-20 04:28:55 +01002364 PyBytes_GET_SIZE(namebytes),
2365 PyBytes_AS_STRING(namebytes));
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002366 Py_DECREF(filebytes);
2367 Py_DECREF(namebytes);
2368 return match;
2369#else
2370 /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00002372
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00002373#endif
Tim Peters50d8d372001-02-28 05:34:27 +00002374}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00002375
Guido van Rossum197346f1997-10-31 18:38:52 +00002376#ifdef HAVE_STAT
Victor Stinner4f4402c2010-08-14 14:50:26 +00002377
Victor Stinner547a2a62011-03-20 03:07:28 +01002378/* Helper to look for __init__.py or __init__.py[co] in potential package.
2379 Return 1 if __init__ was found, 0 if not, or -1 on error. */
Guido van Rossum197346f1997-10-31 18:38:52 +00002380static int
Victor Stinner547a2a62011-03-20 03:07:28 +01002381find_init_module(PyObject *directory)
Guido van Rossum197346f1997-10-31 18:38:52 +00002382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 struct stat statbuf;
Victor Stinner547a2a62011-03-20 03:07:28 +01002384 PyObject *filename;
2385 int match;
Victor Stinnerbd0850b2011-12-18 20:47:30 +01002386 int err;
Guido van Rossum197346f1997-10-31 18:38:52 +00002387
Victor Stinner547a2a62011-03-20 03:07:28 +01002388 filename = PyUnicode_FromFormat("%U%c__init__.py", directory, SEP);
2389 if (filename == NULL)
2390 return -1;
Victor Stinnerbd0850b2011-12-18 20:47:30 +01002391 err = _Py_stat(filename, &statbuf);
2392 if (err == -2)
2393 return -1;
2394 if (err == 0) {
Victor Stinnercc9564e2011-03-20 04:58:29 +01002395 /* 3=len(".py") */
2396 match = case_ok(filename, -3, initstr);
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002397 if (match < 0) {
2398 Py_DECREF(filename);
2399 return -1;
2400 }
2401 if (match) {
Victor Stinner547a2a62011-03-20 03:07:28 +01002402 Py_DECREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 return 1;
2404 }
2405 }
Victor Stinner547a2a62011-03-20 03:07:28 +01002406 Py_DECREF(filename);
2407
2408 filename = PyUnicode_FromFormat("%U%c__init__.py%c",
2409 directory, SEP, Py_OptimizeFlag ? 'o' : 'c');
2410 if (filename == NULL)
2411 return -1;
Victor Stinnerbd0850b2011-12-18 20:47:30 +01002412 err = _Py_stat(filename, &statbuf);
2413 if (err == -2) {
2414 Py_DECREF(filename);
2415 return -1;
2416 }
2417 if (err == 0) {
Victor Stinnercc9564e2011-03-20 04:58:29 +01002418 /* 4=len(".pyc") */
2419 match = case_ok(filename, -4, initstr);
Victor Stinnerc9a271c2011-03-14 14:34:13 -04002420 if (match < 0) {
2421 Py_DECREF(filename);
2422 return -1;
2423 }
2424 if (match) {
Victor Stinner547a2a62011-03-20 03:07:28 +01002425 Py_DECREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 return 1;
2427 }
2428 }
Victor Stinner547a2a62011-03-20 03:07:28 +01002429 Py_DECREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 return 0;
Guido van Rossum197346f1997-10-31 18:38:52 +00002431}
Guido van Rossum48a680c2001-03-02 06:34:14 +00002432
Guido van Rossum197346f1997-10-31 18:38:52 +00002433#endif /* HAVE_STAT */
2434
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002435
Victor Stinner95872862011-03-07 18:20:56 +01002436static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002437
Victor Stinner44c6c152010-08-09 00:59:10 +00002438static PyObject*
Victor Stinner95872862011-03-07 18:20:56 +01002439load_builtin(PyObject *name, int type)
Victor Stinner44c6c152010-08-09 00:59:10 +00002440{
2441 PyObject *m, *modules;
2442 int err;
2443
Victor Stinner44c6c152010-08-09 00:59:10 +00002444 if (type == C_BUILTIN)
2445 err = init_builtin(name);
2446 else
Victor Stinner95872862011-03-07 18:20:56 +01002447 err = PyImport_ImportFrozenModuleObject(name);
Victor Stinner44c6c152010-08-09 00:59:10 +00002448 if (err < 0)
2449 return NULL;
2450 if (err == 0) {
2451 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +01002452 "Purported %s module %R not found",
2453 type == C_BUILTIN ? "builtin" : "frozen",
Victor Stinner44c6c152010-08-09 00:59:10 +00002454 name);
2455 return NULL;
2456 }
2457
2458 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +01002459 m = PyDict_GetItem(modules, name);
Victor Stinner44c6c152010-08-09 00:59:10 +00002460 if (m == NULL) {
2461 PyErr_Format(
2462 PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +01002463 "%s module %R not properly initialized",
2464 type == C_BUILTIN ? "builtin" : "frozen",
Victor Stinner44c6c152010-08-09 00:59:10 +00002465 name);
2466 return NULL;
2467 }
2468 Py_INCREF(m);
2469 return m;
2470}
2471
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002472/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002473 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002474
Guido van Rossum79f25d91997-04-29 20:08:16 +00002475static PyObject *
Victor Stinner41c5fec2011-03-13 21:46:30 -04002476load_module(PyObject *name, FILE *fp, PyObject *pathname, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 /* First check that there's an open file (if we need one) */
2481 switch (type) {
2482 case PY_SOURCE:
2483 case PY_COMPILED:
2484 if (fp == NULL) {
2485 PyErr_Format(PyExc_ValueError,
Victor Stinner41c5fec2011-03-13 21:46:30 -04002486 "file object required for import (type code %d)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 type);
2488 return NULL;
2489 }
2490 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002493
Victor Stinner41c5fec2011-03-13 21:46:30 -04002494 case PY_SOURCE:
2495 m = load_source_module(name, pathname, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002497
Victor Stinner41c5fec2011-03-13 21:46:30 -04002498 case PY_COMPILED:
2499 m = load_compiled_module(name, pathname, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002501
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002502#ifdef HAVE_DYNAMIC_LOADING
Victor Stinner41c5fec2011-03-13 21:46:30 -04002503 case C_EXTENSION:
2504 m = _PyImport_LoadDynamicModule(name, pathname, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 break;
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002506#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002507
Victor Stinner41c5fec2011-03-13 21:46:30 -04002508 case PKG_DIRECTORY:
2509 m = load_package(name, pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 break;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 case C_BUILTIN:
Victor Stinner41c5fec2011-03-13 21:46:30 -04002513 case PY_FROZEN:
2514 m = load_builtin(name, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 break;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 case IMP_HOOK: {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002518 _Py_IDENTIFIER(load_module);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 if (loader == NULL) {
2520 PyErr_SetString(PyExc_ImportError,
2521 "import hook without loader");
2522 return NULL;
2523 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002524 m = _PyObject_CallMethodId(loader, &PyId_load_module, "O", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 break;
2526 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 default:
2529 PyErr_Format(PyExc_ImportError,
Victor Stinner98dbba52011-03-14 15:15:47 -04002530 "Don't know how to import %R (type code %d)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 name, type);
2532 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002537}
2538
2539
2540/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00002541 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002542 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00002543
Guido van Rossum7f133ed1991-02-19 12:23:57 +00002544static int
Victor Stinner95872862011-03-07 18:20:56 +01002545init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00002546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002548
Victor Stinner95872862011-03-07 18:20:56 +01002549 if (_PyImport_FindExtensionObject(name, name) != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 for (p = PyImport_Inittab; p->name != NULL; p++) {
2553 PyObject *mod;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01002554 PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +01002555 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 if (p->initfunc == NULL) {
2557 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +01002558 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 name);
2560 return -1;
2561 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 mod = (*p->initfunc)();
2563 if (mod == 0)
2564 return -1;
Antoine Pitrou6c40eb72012-01-18 20:16:09 +01002565 /* Remember pointer to module init function. */
2566 def = PyModule_GetDef(mod);
2567 def->m_base.m_init = p->initfunc;
Victor Stinner95872862011-03-07 18:20:56 +01002568 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 return -1;
2570 /* FixupExtension has put the module into sys.modules,
2571 so we can release our own reference. */
2572 Py_DECREF(mod);
2573 return 1;
2574 }
2575 }
2576 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00002577}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002578
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002579
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002580/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002581
Guido van Rossumcfd0a221996-06-17 17:06:34 +00002582static struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +01002583find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002586
Victor Stinner53dc7352011-03-20 01:50:21 +01002587 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00002589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 for (p = PyImport_FrozenModules; ; p++) {
2591 if (p->name == NULL)
2592 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01002593 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 break;
2595 }
2596 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002597}
2598
Guido van Rossum79f25d91997-04-29 20:08:16 +00002599static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01002600get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 struct _frozen *p = find_frozen(name);
2603 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 if (p == NULL) {
2606 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002607 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 name);
2609 return NULL;
2610 }
2611 if (p->code == NULL) {
2612 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002613 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 name);
2615 return NULL;
2616 }
2617 size = p->size;
2618 if (size < 0)
2619 size = -size;
2620 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002621}
2622
Brett Cannon8d110132009-03-15 02:20:16 +00002623static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01002624is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00002625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 struct _frozen *p = find_frozen(name);
2627 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00002628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 if (p == NULL) {
2630 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002631 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 name);
2633 return NULL;
2634 }
Brett Cannon8d110132009-03-15 02:20:16 +00002635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00002637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 if (size < 0)
2639 Py_RETURN_TRUE;
2640 else
2641 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00002642}
2643
2644
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002645/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00002646 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002647 an exception set if the initialization failed.
2648 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00002649
2650int
Victor Stinner53dc7352011-03-20 01:50:21 +01002651PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002652{
Victor Stinner53dc7352011-03-20 01:50:21 +01002653 struct _frozen *p;
2654 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 int ispackage;
2656 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002657
Victor Stinner53dc7352011-03-20 01:50:21 +01002658 p = find_frozen(name);
2659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 if (p == NULL)
2661 return 0;
2662 if (p->code == NULL) {
2663 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002664 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 name);
2666 return -1;
2667 }
2668 size = p->size;
2669 ispackage = (size < 0);
2670 if (ispackage)
2671 size = -size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
2673 if (co == NULL)
2674 return -1;
2675 if (!PyCode_Check(co)) {
2676 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002677 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 name);
2679 goto err_return;
2680 }
2681 if (ispackage) {
2682 /* Set __path__ to the package name */
Victor Stinner53dc7352011-03-20 01:50:21 +01002683 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01002685 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 if (m == NULL)
2687 goto err_return;
2688 d = PyModule_GetDict(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 l = PyList_New(1);
2690 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 goto err_return;
2692 }
Victor Stinner53dc7352011-03-20 01:50:21 +01002693 Py_INCREF(name);
2694 PyList_SET_ITEM(l, 0, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 err = PyDict_SetItemString(d, "__path__", l);
2696 Py_DECREF(l);
2697 if (err != 0)
2698 goto err_return;
2699 }
Victor Stinner53dc7352011-03-20 01:50:21 +01002700 path = PyUnicode_FromString("<frozen>");
2701 if (path == NULL)
2702 goto err_return;
2703 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
2704 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 if (m == NULL)
2706 goto err_return;
2707 Py_DECREF(co);
2708 Py_DECREF(m);
2709 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002710err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 Py_DECREF(co);
2712 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002713}
Guido van Rossum74e6a111994-08-29 12:54:38 +00002714
Victor Stinner53dc7352011-03-20 01:50:21 +01002715int
2716PyImport_ImportFrozenModule(char *name)
2717{
2718 PyObject *nameobj;
2719 int ret;
2720 nameobj = PyUnicode_InternFromString(name);
2721 if (nameobj == NULL)
2722 return -1;
2723 ret = PyImport_ImportFrozenModuleObject(nameobj);
2724 Py_DECREF(nameobj);
2725 return ret;
2726}
2727
Guido van Rossum74e6a111994-08-29 12:54:38 +00002728
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002729/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002730 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00002731
Guido van Rossum79f25d91997-04-29 20:08:16 +00002732PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002733PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00002734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 PyObject *pname;
2736 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 pname = PyUnicode_FromString(name);
2739 if (pname == NULL)
2740 return NULL;
2741 result = PyImport_Import(pname);
2742 Py_DECREF(pname);
2743 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002744}
2745
Christian Heimes072c0f12008-01-03 23:01:04 +00002746/* Import a module without blocking
2747 *
2748 * At first it tries to fetch the module from sys.modules. If the module was
2749 * never loaded before it loads it with PyImport_ImportModule() unless another
2750 * thread holds the import lock. In the latter case the function raises an
2751 * ImportError instead of blocking.
2752 *
2753 * Returns the module object with incremented ref count.
2754 */
2755PyObject *
2756PyImport_ImportModuleNoBlock(const char *name)
2757{
Victor Stinner2e5f11a2011-03-13 21:57:27 -04002758 PyObject *nameobj, *modules, *result;
Victor Stinner0af03062011-09-02 00:11:43 +02002759#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 long me;
Victor Stinner0af03062011-09-02 00:11:43 +02002761#endif
Christian Heimes072c0f12008-01-03 23:01:04 +00002762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 /* Try to get the module from sys.modules[name] */
2764 modules = PyImport_GetModuleDict();
2765 if (modules == NULL)
2766 return NULL;
Christian Heimes072c0f12008-01-03 23:01:04 +00002767
Victor Stinner2e5f11a2011-03-13 21:57:27 -04002768 nameobj = PyUnicode_FromString(name);
2769 if (nameobj == NULL)
2770 return NULL;
2771 result = PyDict_GetItem(modules, nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 if (result != NULL) {
Victor Stinner2e5f11a2011-03-13 21:57:27 -04002773 Py_DECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 Py_INCREF(result);
2775 return result;
2776 }
Victor Stinner2e5f11a2011-03-13 21:57:27 -04002777 PyErr_Clear();
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002778#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 /* check the import lock
2780 * me might be -1 but I ignore the error here, the lock function
2781 * takes care of the problem */
2782 me = PyThread_get_thread_ident();
2783 if (import_lock_thread == -1 || import_lock_thread == me) {
2784 /* no thread or me is holding the lock */
Victor Stinner2e5f11a2011-03-13 21:57:27 -04002785 result = PyImport_Import(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 }
2787 else {
2788 PyErr_Format(PyExc_ImportError,
Victor Stinnerc24c8102011-03-13 22:38:06 -04002789 "Failed to import %R because the import lock"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 "is held by another thread.",
Victor Stinner2e5f11a2011-03-13 21:57:27 -04002791 nameobj);
2792 result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002794#else
Victor Stinner2e5f11a2011-03-13 21:57:27 -04002795 result = PyImport_Import(nameobj);
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002796#endif
Victor Stinner2e5f11a2011-03-13 21:57:27 -04002797 Py_DECREF(nameobj);
2798 return result;
Christian Heimes072c0f12008-01-03 23:01:04 +00002799}
2800
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002801
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002802PyObject *
Brett Cannonfd074152012-04-14 14:10:13 -04002803PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
2804 PyObject *locals, PyObject *given_fromlist,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04002805 int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002806{
Brett Cannonfd074152012-04-14 14:10:13 -04002807 _Py_IDENTIFIER(__import__);
2808 _Py_IDENTIFIER(__package__);
2809 _Py_IDENTIFIER(__path__);
2810 _Py_IDENTIFIER(__name__);
2811 _Py_IDENTIFIER(_find_and_load);
2812 _Py_IDENTIFIER(_handle_fromlist);
2813 _Py_static_string(single_dot, ".");
2814 PyObject *abs_name = NULL;
2815 PyObject *builtins_import = NULL;
2816 PyObject *final_mod = NULL;
2817 PyObject *mod = NULL;
2818 PyObject *package = NULL;
2819 PyObject *globals = NULL;
2820 PyObject *fromlist = NULL;
2821 PyInterpreterState *interp = PyThreadState_GET()->interp;
2822
2823 /* Make sure to use default values so as to not have
2824 PyObject_CallMethodObjArgs() truncate the parameter list because of a
2825 NULL argument. */
2826 if (given_globals == NULL) {
2827 globals = PyDict_New();
2828 if (globals == NULL) {
2829 goto error;
2830 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 }
Brett Cannonfd074152012-04-14 14:10:13 -04002832 else {
2833 /* Only have to care what given_globals is if it will be used
2834 fortsomething. */
2835 if (level > 0 && !PyDict_Check(given_globals)) {
2836 PyErr_SetString(PyExc_TypeError, "globals must be a dict");
2837 goto error;
2838 }
2839 globals = given_globals;
2840 Py_INCREF(globals);
2841 }
2842
2843 if (given_fromlist == NULL) {
2844 fromlist = PyList_New(0);
2845 if (fromlist == NULL) {
2846 goto error;
2847 }
2848 }
2849 else {
2850 fromlist = given_fromlist;
2851 Py_INCREF(fromlist);
2852 }
2853 if (name == NULL) {
2854 PyErr_SetString(PyExc_ValueError, "Empty module name");
2855 goto error;
2856 }
2857
2858 /* The below code is importlib.__import__() & _gcd_import(), ported to C
2859 for added performance. */
2860
2861 if (!PyUnicode_Check(name)) {
2862 PyErr_SetString(PyExc_TypeError, "module name must be a string");
2863 goto error;
2864 }
2865 else if (PyUnicode_READY(name) < 0) {
2866 goto error;
2867 }
2868 if (level < 0) {
2869 PyErr_SetString(PyExc_ValueError, "level must be >= 0");
2870 goto error;
2871 }
2872 else if (level > 0) {
2873 package = _PyDict_GetItemId(globals, &PyId___package__);
2874 if (package != NULL && package != Py_None) {
2875 Py_INCREF(package);
2876 if (!PyUnicode_Check(package)) {
2877 PyErr_SetString(PyExc_TypeError, "package must be a string");
2878 goto error;
2879 }
2880 }
2881 else {
2882 package = _PyDict_GetItemIdWithError(globals, &PyId___name__);
2883 if (package == NULL) {
2884 goto error;
2885 }
2886 else if (!PyUnicode_Check(package)) {
2887 PyErr_SetString(PyExc_TypeError, "__name__ must be a string");
2888 }
2889 Py_INCREF(package);
2890
2891 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
Brett Cannon740fce02012-04-14 14:23:49 -04002892 PyObject *partition = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -04002893 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
2894 if (borrowed_dot == NULL) {
2895 goto error;
2896 }
Brett Cannon740fce02012-04-14 14:23:49 -04002897 partition = PyUnicode_RPartition(package, borrowed_dot);
Brett Cannonfd074152012-04-14 14:10:13 -04002898 Py_DECREF(package);
2899 if (partition == NULL) {
2900 goto error;
2901 }
2902 package = PyTuple_GET_ITEM(partition, 0);
2903 Py_INCREF(package);
2904 Py_DECREF(partition);
2905 }
2906 }
2907
2908 if (PyDict_GetItem(interp->modules, package) == NULL) {
2909 PyErr_Format(PyExc_SystemError,
2910 "Parent module %R not loaded, cannot perform relative "
2911 "import", package);
2912 goto error;
2913 }
2914 }
2915 else { /* level == 0 */
2916 if (PyUnicode_GET_LENGTH(name) == 0) {
2917 PyErr_SetString(PyExc_ValueError, "Empty module name");
2918 goto error;
2919 }
2920 package = Py_None;
2921 Py_INCREF(package);
2922 }
2923
2924 if (level > 0) {
2925 Py_ssize_t last_dot = PyUnicode_GET_LENGTH(package);
2926 PyObject *base = NULL;
2927 int level_up = 1;
2928
2929 for (level_up = 1; level_up < level; level_up += 1) {
2930 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
2931 if (last_dot == -2) {
2932 goto error;
2933 }
2934 else if (last_dot == -1) {
2935 PyErr_SetString(PyExc_ValueError,
2936 "attempted relative import beyond top-level "
2937 "package");
2938 goto error;
2939 }
2940 }
2941 base = PyUnicode_Substring(package, 0, last_dot);
2942 if (PyUnicode_GET_LENGTH(name) > 0) {
2943 PyObject *borrowed_dot = NULL;
2944 PyObject *seq = PyTuple_Pack(2, base, name);
2945
2946 borrowed_dot = _PyUnicode_FromId(&single_dot);
2947 if (borrowed_dot == NULL || seq == NULL) {
2948 goto error;
2949 }
2950
2951 abs_name = PyUnicode_Join(borrowed_dot, seq);
2952 Py_DECREF(seq);
2953 if (abs_name == NULL) {
2954 goto error;
2955 }
2956 }
2957 else {
2958 abs_name = base;
2959 }
2960 }
2961 else {
2962 abs_name = name;
2963 Py_INCREF(abs_name);
2964 }
2965
2966#if WITH_THREAD
2967 _PyImport_AcquireLock();
2968#endif
2969 /* From this point forward, goto error_with_unlock! */
2970 if (PyDict_Check(globals)) {
2971 builtins_import = _PyDict_GetItemId(globals, &PyId___import__);
2972 }
2973 if (builtins_import == NULL) {
2974 builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
2975 if (builtins_import == NULL) {
2976 Py_FatalError("__import__ missing");
2977 }
2978 }
2979 Py_INCREF(builtins_import);
2980
2981 mod = PyDict_GetItem(interp->modules, abs_name);
2982 if (mod == Py_None) {
2983 PyErr_Format(PyExc_ImportError,
2984 "import of %R halted; None in sys.modules", abs_name);
2985 goto error_with_unlock;
2986 }
2987 else if (mod != NULL) {
2988 Py_INCREF(mod);
2989 }
2990 else {
2991 mod = _PyObject_CallMethodObjIdArgs(interp->importlib,
2992 &PyId__find_and_load, abs_name,
2993 builtins_import, NULL);
2994 if (mod == NULL) {
2995 goto error_with_unlock;
2996 }
2997 }
2998
2999 if (PyObject_Not(fromlist)) {
3000 if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
3001 PyObject *front = NULL;
3002 PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
3003
3004 if (borrowed_dot == NULL) {
3005 goto error_with_unlock;
3006 }
3007
3008 PyObject *partition = PyUnicode_Partition(name, borrowed_dot);
3009 if (partition == NULL) {
3010 goto error_with_unlock;
3011 }
3012
3013 front = PyTuple_GET_ITEM(partition, 0);
3014 Py_INCREF(front);
3015 Py_DECREF(partition);
3016
3017 if (level == 0) {
3018 final_mod = PyDict_GetItemWithError(interp->modules, front);
3019 Py_DECREF(front);
3020 Py_XINCREF(final_mod);
3021 }
3022 else {
3023 Py_ssize_t cut_off = PyUnicode_GetLength(name) -
3024 PyUnicode_GetLength(front);
3025 Py_ssize_t abs_name_len = PyUnicode_GetLength(abs_name);
3026 PyObject *to_return = PyUnicode_Substring(name, 0,
3027 abs_name_len - cut_off);
3028
3029 final_mod = PyDict_GetItem(interp->modules, to_return);
3030 Py_INCREF(final_mod);
3031 Py_DECREF(to_return);
3032 }
3033 }
3034 else {
3035 final_mod = mod;
3036 Py_INCREF(mod);
3037 }
3038 }
3039 else {
3040 final_mod = _PyObject_CallMethodObjIdArgs(interp->importlib,
3041 &PyId__handle_fromlist, mod,
3042 fromlist, builtins_import,
3043 NULL);
3044 }
3045 error_with_unlock:
3046#if WITH_THREAD
3047 if (_PyImport_ReleaseLock() < 0) {
3048 PyErr_SetString(PyExc_RuntimeError, "not holding the import lock");
3049 }
3050#endif
3051 error:
3052 Py_XDECREF(abs_name);
3053 Py_XDECREF(builtins_import);
3054 Py_XDECREF(mod);
3055 Py_XDECREF(package);
3056 Py_XDECREF(globals);
3057 Py_XDECREF(fromlist);
3058 return final_mod;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00003059}
3060
Victor Stinnerfe93faf2011-03-14 15:54:52 -04003061PyObject *
Benjamin Peterson04778a82011-05-25 09:29:00 -05003062PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
Victor Stinnerfe93faf2011-03-14 15:54:52 -04003063 PyObject *fromlist, int level)
3064{
3065 PyObject *nameobj, *mod;
3066 nameobj = PyUnicode_FromString(name);
3067 if (nameobj == NULL)
3068 return NULL;
3069 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
3070 fromlist, level);
3071 Py_DECREF(nameobj);
3072 return mod;
3073}
3074
3075
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003076/* Re-import a module of any kind and return its module object, WITH
3077 INCREMENTED REFERENCE COUNT */
3078
Guido van Rossum79f25d91997-04-29 20:08:16 +00003079PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003080PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 PyInterpreterState *interp = PyThreadState_Get()->interp;
3083 PyObject *modules_reloading = interp->modules_reloading;
3084 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner533d7832011-03-14 13:22:54 -04003085 PyObject *path_list = NULL, *loader = NULL, *existing_m = NULL;
Martin v. Löwis796ea532011-10-30 09:07:07 +01003086 PyObject *name, *bufobj, *subname;
3087 Py_ssize_t subname_start;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 struct filedescr *fdp;
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003089 FILE *fp = NULL;
3090 PyObject *newm = NULL;
Brett Cannon9a5b25a2010-03-01 02:09:17 +00003091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 if (modules_reloading == NULL) {
3093 Py_FatalError("PyImport_ReloadModule: "
3094 "no modules_reloading dictionary!");
3095 return NULL;
3096 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 if (m == NULL || !PyModule_Check(m)) {
3099 PyErr_SetString(PyExc_TypeError,
3100 "reload() argument must be module");
3101 return NULL;
3102 }
Martin v. Löwis796ea532011-10-30 09:07:07 +01003103 name = PyModule_GetNameObject(m);
3104 if (name == NULL || PyUnicode_READY(name) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 return NULL;
Martin v. Löwis796ea532011-10-30 09:07:07 +01003106 if (m != PyDict_GetItem(modules, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 PyErr_Format(PyExc_ImportError,
Victor Stinner98dbba52011-03-14 15:15:47 -04003108 "reload(): module %R not in sys.modules",
Martin v. Löwis796ea532011-10-30 09:07:07 +01003109 name);
3110 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 return NULL;
3112 }
Martin v. Löwis796ea532011-10-30 09:07:07 +01003113 existing_m = PyDict_GetItem(modules_reloading, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 if (existing_m != NULL) {
3115 /* Due to a recursive reload, this module is already
3116 being reloaded. */
Martin v. Löwis796ea532011-10-30 09:07:07 +01003117 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 Py_INCREF(existing_m);
3119 return existing_m;
3120 }
Martin v. Löwis796ea532011-10-30 09:07:07 +01003121 if (PyDict_SetItem(modules_reloading, name, m) < 0) {
3122 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 return NULL;
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003124 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003125
Martin v. Löwis796ea532011-10-30 09:07:07 +01003126 subname_start = PyUnicode_FindChar(name, '.', 0,
3127 PyUnicode_GET_LENGTH(name), -1);
3128 if (subname_start == -1) {
3129 Py_INCREF(name);
3130 subname = name;
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003131 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 else {
3133 PyObject *parentname, *parent;
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003134 Py_ssize_t len;
Martin v. Löwis796ea532011-10-30 09:07:07 +01003135 parentname = PyUnicode_Substring(name, 0, subname_start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 if (parentname == NULL) {
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 }
3139 parent = PyDict_GetItem(modules, parentname);
3140 if (parent == NULL) {
3141 PyErr_Format(PyExc_ImportError,
Victor Stinner98dbba52011-03-14 15:15:47 -04003142 "reload(): parent %R not in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 parentname);
3144 Py_DECREF(parentname);
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003145 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 }
3147 Py_DECREF(parentname);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02003148 path_list = _PyObject_GetAttrId(parent, &PyId___path__);
Victor Stinner533d7832011-03-14 13:22:54 -04003149 if (path_list == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 PyErr_Clear();
Martin v. Löwis796ea532011-10-30 09:07:07 +01003151 subname_start++;
3152 len = PyUnicode_GET_LENGTH(name) - (subname_start + 1);
3153 subname = PyUnicode_Substring(name, subname_start, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 }
Martin v. Löwis796ea532011-10-30 09:07:07 +01003155 if (subname == NULL)
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003156 goto error;
Martin v. Löwis796ea532011-10-30 09:07:07 +01003157 fdp = find_module(name, subname, path_list,
Victor Stinner2fd76e42011-03-14 15:19:39 -04003158 &bufobj, &fp, &loader);
Martin v. Löwis796ea532011-10-30 09:07:07 +01003159 Py_DECREF(subname);
Victor Stinner533d7832011-03-14 13:22:54 -04003160 Py_XDECREF(path_list);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00003161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 if (fdp == NULL) {
3163 Py_XDECREF(loader);
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003164 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 }
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00003166
Martin v. Löwis796ea532011-10-30 09:07:07 +01003167 newm = load_module(name, fp, bufobj, fdp->type, loader);
Victor Stinner2fd76e42011-03-14 15:19:39 -04003168 Py_XDECREF(bufobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 Py_XDECREF(loader);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00003170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 if (fp)
3172 fclose(fp);
3173 if (newm == NULL) {
3174 /* load_module probably removed name from modules because of
3175 * the error. Put back the original module object. We're
3176 * going to return NULL in this case regardless of whether
3177 * replacing name succeeds, so the return value is ignored.
3178 */
Martin v. Löwis796ea532011-10-30 09:07:07 +01003179 PyDict_SetItem(modules, name, m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 }
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003181
3182error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 imp_modules_reloading_clear();
Martin v. Löwis796ea532011-10-30 09:07:07 +01003184 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003186}
3187
3188
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003189/* Higher-level import emulator which emulates the "import" statement
3190 more accurately -- it invokes the __import__() function from the
3191 builtins of the current globals. This means that the import is
3192 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00003193 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00003194 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00003195 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00003196 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003197
3198PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003199PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 static PyObject *silly_list = NULL;
3202 static PyObject *builtins_str = NULL;
3203 static PyObject *import_str = NULL;
3204 PyObject *globals = NULL;
3205 PyObject *import = NULL;
3206 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00003207 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 /* Initialize constant string objects */
3211 if (silly_list == NULL) {
3212 import_str = PyUnicode_InternFromString("__import__");
3213 if (import_str == NULL)
3214 return NULL;
3215 builtins_str = PyUnicode_InternFromString("__builtins__");
3216 if (builtins_str == NULL)
3217 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00003218 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 if (silly_list == NULL)
3220 return NULL;
3221 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 /* Get the builtins from current globals */
3224 globals = PyEval_GetGlobals();
3225 if (globals != NULL) {
3226 Py_INCREF(globals);
3227 builtins = PyObject_GetItem(globals, builtins_str);
3228 if (builtins == NULL)
3229 goto err;
3230 }
3231 else {
3232 /* No globals -- use standard builtins, and fake globals */
3233 builtins = PyImport_ImportModuleLevel("builtins",
3234 NULL, NULL, NULL, 0);
3235 if (builtins == NULL)
3236 return NULL;
3237 globals = Py_BuildValue("{OO}", builtins_str, builtins);
3238 if (globals == NULL)
3239 goto err;
3240 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 /* Get the __import__ function from the builtins */
3243 if (PyDict_Check(builtins)) {
3244 import = PyObject_GetItem(builtins, import_str);
3245 if (import == NULL)
3246 PyErr_SetObject(PyExc_KeyError, import_str);
3247 }
3248 else
3249 import = PyObject_GetAttr(builtins, import_str);
3250 if (import == NULL)
3251 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00003254 Always use absolute import here.
3255 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
3257 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00003258 if (r == NULL)
3259 goto err;
3260 Py_DECREF(r);
3261
3262 modules = PyImport_GetModuleDict();
3263 r = PyDict_GetItem(modules, module_name);
3264 if (r != NULL)
3265 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003266
3267 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 Py_XDECREF(globals);
3269 Py_XDECREF(builtins);
3270 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00003271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003273}
3274
3275
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003276/* Module 'imp' provides Python access to the primitives used for
3277 importing modules.
3278*/
3279
Guido van Rossum79f25d91997-04-29 20:08:16 +00003280static PyObject *
Barry Warsaw28a691b2010-04-17 00:19:56 +00003281imp_make_magic(long magic)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 char buf[4];
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 buf[0] = (char) ((magic >> 0) & 0xff);
3286 buf[1] = (char) ((magic >> 8) & 0xff);
3287 buf[2] = (char) ((magic >> 16) & 0xff);
3288 buf[3] = (char) ((magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 return PyBytes_FromStringAndSize(buf, 4);
Victor Stinner3e2b7172010-11-09 09:32:19 +00003291}
Barry Warsaw28a691b2010-04-17 00:19:56 +00003292
3293static PyObject *
3294imp_get_magic(PyObject *self, PyObject *noargs)
3295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 return imp_make_magic(pyc_magic);
Barry Warsaw28a691b2010-04-17 00:19:56 +00003297}
3298
3299static PyObject *
3300imp_get_tag(PyObject *self, PyObject *noargs)
3301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 return PyUnicode_FromString(pyc_tag);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003303}
3304
Guido van Rossum79f25d91997-04-29 20:08:16 +00003305static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003306imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 PyObject *list;
3309 struct filedescr *fdp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 list = PyList_New(0);
3312 if (list == NULL)
3313 return NULL;
3314 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
3315 PyObject *item = Py_BuildValue("ssi",
3316 fdp->suffix, fdp->mode, fdp->type);
3317 if (item == NULL) {
3318 Py_DECREF(list);
3319 return NULL;
3320 }
3321 if (PyList_Append(list, item) < 0) {
3322 Py_DECREF(list);
3323 Py_DECREF(item);
3324 return NULL;
3325 }
3326 Py_DECREF(item);
3327 }
3328 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003329}
3330
Guido van Rossum79f25d91997-04-29 20:08:16 +00003331static PyObject *
Victor Stinner533d7832011-03-14 13:22:54 -04003332call_find_module(PyObject *name, PyObject *path_list)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 extern int fclose(FILE *);
3335 PyObject *fob, *ret;
3336 PyObject *pathobj;
3337 struct filedescr *fdp;
Victor Stinner7d8b77c2011-03-12 08:45:02 -05003338 FILE *fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 int fd = -1;
3340 char *found_encoding = NULL;
3341 char *encoding = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003342
Victor Stinner533d7832011-03-14 13:22:54 -04003343 if (path_list == Py_None)
3344 path_list = NULL;
3345 fdp = find_module(NULL, name, path_list,
Victor Stinner2fd76e42011-03-14 15:19:39 -04003346 &pathobj, &fp, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 if (fdp == NULL)
3348 return NULL;
3349 if (fp != NULL) {
3350 fd = fileno(fp);
3351 if (fd != -1)
3352 fd = dup(fd);
3353 fclose(fp);
3354 fp = NULL;
Antoine Pitrou72146122012-02-22 18:03:04 +01003355 if (fd == -1)
3356 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 }
3358 if (fd != -1) {
3359 if (strchr(fdp->mode, 'b') == NULL) {
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02003360 /* PyTokenizer_FindEncodingFilename() returns PyMem_MALLOC'ed
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 memory. */
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02003362 found_encoding = PyTokenizer_FindEncodingFilename(fd, pathobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 lseek(fd, 0, 0); /* Reset position */
Victor Stinner2fd76e42011-03-14 15:19:39 -04003364 if (found_encoding == NULL && PyErr_Occurred()) {
3365 Py_XDECREF(pathobj);
Antoine Pitrou4f22a8d2012-02-22 18:05:43 +01003366 close(fd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 return NULL;
Victor Stinner2fd76e42011-03-14 15:19:39 -04003368 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 encoding = (found_encoding != NULL) ? found_encoding :
3370 (char*)PyUnicode_GetDefaultEncoding();
3371 }
Victor Stinner2fd76e42011-03-14 15:19:39 -04003372 fob = PyFile_FromFd(fd, NULL, fdp->mode, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 (char*)encoding, NULL, NULL, 1);
3374 if (fob == NULL) {
Victor Stinner2fd76e42011-03-14 15:19:39 -04003375 Py_XDECREF(pathobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 close(fd);
3377 PyMem_FREE(found_encoding);
3378 return NULL;
3379 }
3380 }
3381 else {
3382 fob = Py_None;
3383 Py_INCREF(fob);
3384 }
Victor Stinner2fd76e42011-03-14 15:19:39 -04003385 if (pathobj == NULL) {
3386 Py_INCREF(Py_None);
3387 pathobj = Py_None;
3388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 ret = Py_BuildValue("NN(ssi)",
3390 fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
3391 PyMem_FREE(found_encoding);
Brett Cannon3bb42d92007-10-20 03:43:15 +00003392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 return ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003394}
3395
Guido van Rossum79f25d91997-04-29 20:08:16 +00003396static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003397imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003398{
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003399 PyObject *name, *path_list = NULL;
3400 if (!PyArg_ParseTuple(args, "U|O:find_module",
3401 &name, &path_list))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 return NULL;
Victor Stinnerad3c03b2011-03-14 09:21:33 -04003403 return call_find_module(name, path_list);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003404}
3405
3406static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003407imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003408{
Victor Stinner95872862011-03-07 18:20:56 +01003409 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 int ret;
3411 PyObject *m;
Victor Stinner95872862011-03-07 18:20:56 +01003412 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 return NULL;
3414 ret = init_builtin(name);
3415 if (ret < 0)
3416 return NULL;
3417 if (ret == 0) {
3418 Py_INCREF(Py_None);
3419 return Py_None;
3420 }
Victor Stinner95872862011-03-07 18:20:56 +01003421 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 Py_XINCREF(m);
3423 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003424}
3425
Guido van Rossum79f25d91997-04-29 20:08:16 +00003426static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003427imp_init_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003428{
Victor Stinner53dc7352011-03-20 01:50:21 +01003429 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 int ret;
3431 PyObject *m;
Victor Stinner53dc7352011-03-20 01:50:21 +01003432 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01003434 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 if (ret < 0)
3436 return NULL;
3437 if (ret == 0) {
3438 Py_INCREF(Py_None);
3439 return Py_None;
3440 }
Victor Stinner53dc7352011-03-20 01:50:21 +01003441 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 Py_XINCREF(m);
3443 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003444}
3445
Guido van Rossum79f25d91997-04-29 20:08:16 +00003446static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003447imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00003448{
Victor Stinner53dc7352011-03-20 01:50:21 +01003449 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00003450
Victor Stinner53dc7352011-03-20 01:50:21 +01003451 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 return NULL;
3453 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00003454}
3455
Guido van Rossum79f25d91997-04-29 20:08:16 +00003456static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00003457imp_is_frozen_package(PyObject *self, PyObject *args)
3458{
Victor Stinner53dc7352011-03-20 01:50:21 +01003459 PyObject *name;
Brett Cannon8d110132009-03-15 02:20:16 +00003460
Victor Stinner53dc7352011-03-20 01:50:21 +01003461 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 return NULL;
3463 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00003464}
3465
3466static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003467imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003468{
Victor Stinner95872862011-03-07 18:20:56 +01003469 PyObject *name;
3470 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 return NULL;
3472 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003473}
3474
Guido van Rossum79f25d91997-04-29 20:08:16 +00003475static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003476imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003477{
Victor Stinner53dc7352011-03-20 01:50:21 +01003478 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01003480 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 return NULL;
3482 p = find_frozen(name);
3483 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003484}
3485
3486static FILE *
Victor Stinner2f42ae52011-03-20 00:41:24 +01003487get_file(PyObject *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 FILE *fp;
3490 if (mode[0] == 'U')
3491 mode = "r" PY_STDIOTEXTMODE;
3492 if (fob == NULL) {
Victor Stinner2f42ae52011-03-20 00:41:24 +01003493 fp = _Py_fopen(pathname, mode);
Victor Stinner35734762011-12-18 21:05:22 +01003494 if (!fp) {
3495 if (!PyErr_Occurred())
3496 PyErr_SetFromErrno(PyExc_IOError);
3497 return NULL;
3498 }
3499 return fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 }
3501 else {
3502 int fd = PyObject_AsFileDescriptor(fob);
3503 if (fd == -1)
3504 return NULL;
Victor Stinner35734762011-12-18 21:05:22 +01003505 if (!_PyVerify_fd(fd)) {
3506 PyErr_SetFromErrno(PyExc_IOError);
3507 return NULL;
3508 }
3509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 /* the FILE struct gets a new fd, so that it can be closed
3511 * independently of the file descriptor given
3512 */
3513 fd = dup(fd);
Victor Stinner35734762011-12-18 21:05:22 +01003514 if (fd == -1) {
3515 PyErr_SetFromErrno(PyExc_IOError);
3516 return NULL;
3517 }
3518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 fp = fdopen(fd, mode);
Victor Stinner35734762011-12-18 21:05:22 +01003520 if (!fp) {
3521 PyErr_SetFromErrno(PyExc_IOError);
3522 return NULL;
3523 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 return fp;
Victor Stinner35734762011-12-18 21:05:22 +01003525 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003526}
3527
Guido van Rossum79f25d91997-04-29 20:08:16 +00003528static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003529imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003530{
Victor Stinner2f42ae52011-03-20 00:41:24 +01003531 PyObject *name, *pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 PyObject *fob = NULL;
3533 PyObject *m;
3534 FILE *fp;
Victor Stinner2f42ae52011-03-20 00:41:24 +01003535 if (!PyArg_ParseTuple(args, "UO&|O:load_compiled",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 &name,
Victor Stinner2f42ae52011-03-20 00:41:24 +01003537 PyUnicode_FSDecoder, &pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 &fob))
3539 return NULL;
Victor Stinner2f42ae52011-03-20 00:41:24 +01003540 fp = get_file(pathname, fob, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 if (fp == NULL) {
Victor Stinnerebc00522010-12-03 17:06:43 +00003542 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 return NULL;
3544 }
Victor Stinner2f42ae52011-03-20 00:41:24 +01003545 m = load_compiled_module(name, pathname, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 fclose(fp);
Victor Stinnerebc00522010-12-03 17:06:43 +00003547 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003549}
3550
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003551#ifdef HAVE_DYNAMIC_LOADING
3552
Guido van Rossum79f25d91997-04-29 20:08:16 +00003553static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003554imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003555{
Victor Stinnerfefd70c2011-03-14 15:54:07 -04003556 PyObject *name, *pathname, *fob = NULL, *mod;
3557 FILE *fp;
3558
3559 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
3560 &name, PyUnicode_FSDecoder, &pathname, &fob))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 return NULL;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04003562 if (fob != NULL) {
3563 fp = get_file(NULL, fob, "r");
Victor Stinnere9ddbf62011-03-22 10:46:35 +01003564 if (fp == NULL) {
3565 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 return NULL;
Victor Stinnere9ddbf62011-03-22 10:46:35 +01003567 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04003569 else
3570 fp = NULL;
3571 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
Victor Stinnere9ddbf62011-03-22 10:46:35 +01003572 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 if (fp)
3574 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04003575 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003576}
3577
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003578#endif /* HAVE_DYNAMIC_LOADING */
3579
Guido van Rossum79f25d91997-04-29 20:08:16 +00003580static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003581imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003582{
Victor Stinner2f42ae52011-03-20 00:41:24 +01003583 PyObject *name, *pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 PyObject *fob = NULL;
3585 PyObject *m;
3586 FILE *fp;
Victor Stinner2f42ae52011-03-20 00:41:24 +01003587 if (!PyArg_ParseTuple(args, "UO&|O:load_source",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 &name,
Victor Stinner2f42ae52011-03-20 00:41:24 +01003589 PyUnicode_FSDecoder, &pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 &fob))
3591 return NULL;
Victor Stinner2f42ae52011-03-20 00:41:24 +01003592 fp = get_file(pathname, fob, "r");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 if (fp == NULL) {
Victor Stinnerebc00522010-12-03 17:06:43 +00003594 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 return NULL;
3596 }
Victor Stinner2f42ae52011-03-20 00:41:24 +01003597 m = load_source_module(name, pathname, fp);
Victor Stinnerebc00522010-12-03 17:06:43 +00003598 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 fclose(fp);
3600 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003601}
3602
Guido van Rossum79f25d91997-04-29 20:08:16 +00003603static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003604imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003605{
Victor Stinner6ae1e7f2011-03-20 22:37:17 +01003606 PyObject *name, *fob, *pathname, *pathname_obj, *ret;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 char *suffix; /* Unused */
3608 char *mode;
3609 int type;
3610 FILE *fp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003611
Victor Stinner6ae1e7f2011-03-20 22:37:17 +01003612 if (!PyArg_ParseTuple(args, "UOO(ssi):load_module",
3613 &name, &fob, &pathname_obj, &suffix, &mode, &type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 return NULL;
Victor Stinner6ae1e7f2011-03-20 22:37:17 +01003615 if (pathname_obj != Py_None) {
3616 if (!PyUnicode_FSDecoder(pathname_obj, &pathname))
3617 return NULL;
3618 }
3619 else
3620 pathname = NULL;
3621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 if (*mode) {
3623 /* Mode must start with 'r' or 'U' and must not contain '+'.
3624 Implicit in this test is the assumption that the mode
3625 may contain other modifiers like 'b' or 't'. */
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
3628 PyErr_Format(PyExc_ValueError,
3629 "invalid file open mode %.200s", mode);
Victor Stinner6ae1e7f2011-03-20 22:37:17 +01003630 Py_XDECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 return NULL;
3632 }
3633 }
3634 if (fob == Py_None)
3635 fp = NULL;
3636 else {
3637 fp = get_file(NULL, fob, mode);
3638 if (fp == NULL) {
Victor Stinner6ae1e7f2011-03-20 22:37:17 +01003639 Py_XDECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 return NULL;
3641 }
3642 }
Victor Stinner41c5fec2011-03-13 21:46:30 -04003643 ret = load_module(name, fp, pathname, type, NULL);
Victor Stinner6ae1e7f2011-03-20 22:37:17 +01003644 Py_XDECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 if (fp)
3646 fclose(fp);
3647 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003648}
3649
3650static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003651imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003652{
Victor Stinnerc9abda02011-03-14 13:33:46 -04003653 PyObject *name, *pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 PyObject * ret;
Victor Stinnerc9abda02011-03-14 13:33:46 -04003655 if (!PyArg_ParseTuple(args, "UO&:load_package",
3656 &name, PyUnicode_FSDecoder, &pathname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 return NULL;
Victor Stinnerc9abda02011-03-14 13:33:46 -04003658 ret = load_package(name, pathname);
Victor Stinnerebc00522010-12-03 17:06:43 +00003659 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003661}
3662
3663static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003664imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003665{
Victor Stinnerfe19d212011-03-14 14:53:28 -04003666 PyObject *name;
3667 if (!PyArg_ParseTuple(args, "U:new_module", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 return NULL;
Victor Stinnerfe19d212011-03-14 14:53:28 -04003669 return PyModule_NewObject(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003670}
3671
Christian Heimes13a7a212008-01-07 17:13:09 +00003672static PyObject *
3673imp_reload(PyObject *self, PyObject *v)
3674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 return PyImport_ReloadModule(v);
Christian Heimes13a7a212008-01-07 17:13:09 +00003676}
3677
3678PyDoc_STRVAR(doc_reload,
3679"reload(module) -> module\n\
3680\n\
3681Reload the module. The module must have been successfully imported before.");
3682
Barry Warsaw28a691b2010-04-17 00:19:56 +00003683static PyObject *
3684imp_cache_from_source(PyObject *self, PyObject *args, PyObject *kws)
3685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 static char *kwlist[] = {"path", "debug_override", NULL};
Barry Warsaw28a691b2010-04-17 00:19:56 +00003687
Victor Stinner2f42ae52011-03-20 00:41:24 +01003688 PyObject *pathname, *cpathname;
Benjamin Peterson294a9fc2010-10-16 03:12:39 +00003689 PyObject *debug_override = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 int debug = !Py_OptimizeFlag;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 if (!PyArg_ParseTupleAndKeywords(
Victor Stinner3ea23dd2010-10-15 20:34:32 +00003693 args, kws, "O&|O", kwlist,
Victor Stinner2f42ae52011-03-20 00:41:24 +01003694 PyUnicode_FSDecoder, &pathname, &debug_override))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003696
Benjamin Peterson294a9fc2010-10-16 03:12:39 +00003697 if (debug_override != NULL &&
3698 (debug = PyObject_IsTrue(debug_override)) < 0) {
Victor Stinner2f42ae52011-03-20 00:41:24 +01003699 Py_DECREF(pathname);
Benjamin Peterson294a9fc2010-10-16 03:12:39 +00003700 return NULL;
3701 }
Barry Warsaw28a691b2010-04-17 00:19:56 +00003702
Martin v. Löwis30260a72011-10-23 17:35:46 +02003703 if (PyUnicode_READY(pathname) < 0)
3704 return NULL;
3705
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003706 cpathname = make_compiled_pathname(pathname, debug);
Victor Stinner2f42ae52011-03-20 00:41:24 +01003707 Py_DECREF(pathname);
Barry Warsaw28a691b2010-04-17 00:19:56 +00003708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 if (cpathname == NULL) {
3710 PyErr_Format(PyExc_SystemError, "path buffer too short");
3711 return NULL;
3712 }
Victor Stinner2f42ae52011-03-20 00:41:24 +01003713 return cpathname;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003714}
3715
3716PyDoc_STRVAR(doc_cache_from_source,
Éric Araujo5df11082011-11-03 03:38:44 +01003717"cache_from_source(path, [debug_override]) -> path\n\
3718Given the path to a .py file, return the path to its .pyc/.pyo file.\n\
Barry Warsaw28a691b2010-04-17 00:19:56 +00003719\n\
3720The .py file does not need to exist; this simply returns the path to the\n\
3721.pyc/.pyo file calculated as if the .py file were imported. The extension\n\
3722will be .pyc unless __debug__ is not defined, then it will be .pyo.\n\
3723\n\
3724If debug_override is not None, then it must be a boolean and is taken as\n\
3725the value of __debug__ instead.");
3726
3727static PyObject *
3728imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws)
3729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 static char *kwlist[] = {"path", NULL};
Victor Stinnerc9abda02011-03-14 13:33:46 -04003731 PyObject *pathname, *source;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 if (!PyArg_ParseTupleAndKeywords(
Victor Stinnerebc00522010-12-03 17:06:43 +00003734 args, kws, "O&", kwlist,
Victor Stinnerc9abda02011-03-14 13:33:46 -04003735 PyUnicode_FSDecoder, &pathname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003737
Victor Stinnerc9abda02011-03-14 13:33:46 -04003738 source = make_source_pathname(pathname);
3739 if (source == NULL) {
3740 PyErr_Format(PyExc_ValueError, "Not a PEP 3147 pyc path: %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 pathname);
Victor Stinnerc9abda02011-03-14 13:33:46 -04003742 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 return NULL;
3744 }
Victor Stinnerc9abda02011-03-14 13:33:46 -04003745 Py_DECREF(pathname);
3746 return source;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003747}
3748
3749PyDoc_STRVAR(doc_source_from_cache,
Éric Araujo5df11082011-11-03 03:38:44 +01003750"source_from_cache(path) -> path\n\
3751Given the path to a .pyc./.pyo file, return the path to its .py file.\n\
Barry Warsaw28a691b2010-04-17 00:19:56 +00003752\n\
3753The .pyc/.pyo file does not need to exist; this simply returns the path to\n\
3754the .py file calculated to correspond to the .pyc/.pyo file. If path\n\
3755does not conform to PEP 3147 format, ValueError will be raised.");
3756
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003757/* Doc strings */
3758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003759PyDoc_STRVAR(doc_imp,
3760"This module provides the components needed to build your own\n\
3761__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003763PyDoc_STRVAR(doc_find_module,
3764"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003765Search for a module. If path is omitted or None, search for a\n\
3766built-in, frozen or special module and continue search in sys.path.\n\
3767The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003768package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003769
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003770PyDoc_STRVAR(doc_load_module,
3771"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003772Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003773The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003775PyDoc_STRVAR(doc_get_magic,
3776"get_magic() -> string\n\
3777Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003778
Barry Warsaw28a691b2010-04-17 00:19:56 +00003779PyDoc_STRVAR(doc_get_tag,
3780"get_tag() -> string\n\
3781Return the magic tag for .pyc or .pyo files.");
3782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003783PyDoc_STRVAR(doc_get_suffixes,
3784"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003785Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003786that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003788PyDoc_STRVAR(doc_new_module,
3789"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003790Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003791The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003792
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003793PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003794"lock_held() -> boolean\n\
3795Return True if the import lock is currently held, else False.\n\
3796On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003797
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003798PyDoc_STRVAR(doc_acquire_lock,
3799"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003800Acquires the interpreter's import lock for the current thread.\n\
3801This lock should be used by import hooks to ensure thread-safety\n\
3802when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003803On platforms without threads, this function does nothing.");
3804
3805PyDoc_STRVAR(doc_release_lock,
3806"release_lock() -> None\n\
3807Release the interpreter's import lock.\n\
3808On platforms without threads, this function does nothing.");
3809
Guido van Rossum79f25d91997-04-29 20:08:16 +00003810static PyMethodDef imp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3812 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3813 {"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag},
3814 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3815 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3816 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3817 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3818 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3819 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
3820 {"reload", imp_reload, METH_O, doc_reload},
3821 {"cache_from_source", (PyCFunction)imp_cache_from_source,
3822 METH_VARARGS | METH_KEYWORDS, doc_cache_from_source},
3823 {"source_from_cache", (PyCFunction)imp_source_from_cache,
3824 METH_VARARGS | METH_KEYWORDS, doc_source_from_cache},
3825 /* The rest are obsolete */
3826 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
3827 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
3828 {"init_builtin", imp_init_builtin, METH_VARARGS},
3829 {"init_frozen", imp_init_frozen, METH_VARARGS},
3830 {"is_builtin", imp_is_builtin, METH_VARARGS},
3831 {"is_frozen", imp_is_frozen, METH_VARARGS},
3832 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003833#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003835#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 {"load_package", imp_load_package, METH_VARARGS},
3837 {"load_source", imp_load_source, METH_VARARGS},
Brett Cannon442c9b92011-03-23 16:14:42 -07003838 {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003840};
3841
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003842static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003843setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 PyObject *v;
3846 int err;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 v = PyLong_FromLong((long)value);
3849 err = PyDict_SetItemString(d, name, v);
3850 Py_XDECREF(v);
3851 return err;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003852}
3853
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003854typedef struct {
3855 PyObject_HEAD
3856} NullImporter;
3857
3858static int
3859NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3860{
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003861#ifndef MS_WINDOWS
3862 PyObject *path;
3863 struct stat statbuf;
3864 int rv;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3867 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003868
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003869 if (!PyArg_ParseTuple(args, "O&:NullImporter",
3870 PyUnicode_FSConverter, &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003872
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003873 if (PyBytes_GET_SIZE(path) == 0) {
3874 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 PyErr_SetString(PyExc_ImportError, "empty pathname");
3876 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 }
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003878
3879 rv = stat(PyBytes_AS_STRING(path), &statbuf);
3880 Py_DECREF(path);
3881 if (rv == 0) {
3882 /* it exists */
3883 if (S_ISDIR(statbuf.st_mode)) {
3884 /* it's a directory */
3885 PyErr_SetString(PyExc_ImportError, "existing directory");
3886 return -1;
3887 }
3888 }
3889#else /* MS_WINDOWS */
3890 PyObject *pathobj;
3891 DWORD rv;
Victor Stinner255dfdb2010-09-29 10:28:51 +00003892 wchar_t *path;
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003893
3894 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3895 return -1;
3896
3897 if (!PyArg_ParseTuple(args, "U:NullImporter",
3898 &pathobj))
3899 return -1;
3900
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003901 if (PyUnicode_GET_LENGTH(pathobj) == 0) {
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003902 PyErr_SetString(PyExc_ImportError, "empty pathname");
3903 return -1;
3904 }
3905
Victor Stinnerbeb4135b2010-10-07 01:02:42 +00003906 path = PyUnicode_AsWideCharString(pathobj, NULL);
Victor Stinner255dfdb2010-09-29 10:28:51 +00003907 if (path == NULL)
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003908 return -1;
3909 /* see issue1293 and issue3677:
3910 * stat() on Windows doesn't recognise paths like
3911 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
3912 */
3913 rv = GetFileAttributesW(path);
Victor Stinner255dfdb2010-09-29 10:28:51 +00003914 PyMem_Free(path);
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003915 if (rv != INVALID_FILE_ATTRIBUTES) {
3916 /* it exists */
3917 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
3918 /* it's a directory */
3919 PyErr_SetString(PyExc_ImportError, "existing directory");
3920 return -1;
3921 }
3922 }
3923#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 return 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003925}
3926
3927static PyObject *
3928NullImporter_find_module(NullImporter *self, PyObject *args)
3929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003931}
3932
3933static PyMethodDef NullImporter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3935 "Always return None"
3936 },
3937 {NULL} /* Sentinel */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003938};
3939
3940
Christian Heimes9cd17752007-11-18 19:35:23 +00003941PyTypeObject PyNullImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 PyVarObject_HEAD_INIT(NULL, 0)
3943 "imp.NullImporter", /*tp_name*/
3944 sizeof(NullImporter), /*tp_basicsize*/
3945 0, /*tp_itemsize*/
3946 0, /*tp_dealloc*/
3947 0, /*tp_print*/
3948 0, /*tp_getattr*/
3949 0, /*tp_setattr*/
3950 0, /*tp_reserved*/
3951 0, /*tp_repr*/
3952 0, /*tp_as_number*/
3953 0, /*tp_as_sequence*/
3954 0, /*tp_as_mapping*/
3955 0, /*tp_hash */
3956 0, /*tp_call*/
3957 0, /*tp_str*/
3958 0, /*tp_getattro*/
3959 0, /*tp_setattro*/
3960 0, /*tp_as_buffer*/
3961 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3962 "Null importer object", /* tp_doc */
3963 0, /* tp_traverse */
3964 0, /* tp_clear */
3965 0, /* tp_richcompare */
3966 0, /* tp_weaklistoffset */
3967 0, /* tp_iter */
3968 0, /* tp_iternext */
3969 NullImporter_methods, /* tp_methods */
3970 0, /* tp_members */
3971 0, /* tp_getset */
3972 0, /* tp_base */
3973 0, /* tp_dict */
3974 0, /* tp_descr_get */
3975 0, /* tp_descr_set */
3976 0, /* tp_dictoffset */
3977 (initproc)NullImporter_init, /* tp_init */
3978 0, /* tp_alloc */
3979 PyType_GenericNew /* tp_new */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003980};
3981
Martin v. Löwis1a214512008-06-11 05:26:20 +00003982static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003983 PyModuleDef_HEAD_INIT,
3984 "imp",
3985 doc_imp,
3986 0,
3987 imp_methods,
3988 NULL,
3989 NULL,
3990 NULL,
3991 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00003992};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003993
Jason Tishler6bc06ec2003-09-04 11:59:50 +00003994PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00003995PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 if (PyType_Ready(&PyNullImporter_Type) < 0)
4000 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 m = PyModule_Create(&impmodule);
4003 if (m == NULL)
4004 goto failure;
4005 d = PyModule_GetDict(m);
4006 if (d == NULL)
4007 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
4010 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
4011 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
4012 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
4013 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
4014 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
4015 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
4016 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
4017 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
4018 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 Py_INCREF(&PyNullImporter_Type);
4021 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
4022 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00004023 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 Py_XDECREF(m);
4025 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004026}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004027
4028
Guido van Rossumb18618d2000-05-03 23:44:39 +00004029/* API for embedding applications that want to add their own entries
4030 to the table of built-in modules. This should normally be called
4031 *before* Py_Initialize(). When the table resize fails, -1 is
4032 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004033
4034 After a similar function by Just van Rossum. */
4035
4036int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004037PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 static struct _inittab *our_copy = NULL;
4040 struct _inittab *p;
4041 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 /* Count the number of entries in both tables */
4044 for (n = 0; newtab[n].name != NULL; n++)
4045 ;
4046 if (n == 0)
4047 return 0; /* Nothing to do */
4048 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
4049 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 /* Allocate new memory for the combined table */
4052 p = our_copy;
4053 PyMem_RESIZE(p, struct _inittab, i+n+1);
4054 if (p == NULL)
4055 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 /* Copy the tables into the new memory */
4058 if (our_copy != PyImport_Inittab)
4059 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
4060 PyImport_Inittab = our_copy = p;
4061 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004064}
4065
4066/* Shorthand to add a single entry given a name and a function */
4067
4068int
Brett Cannona826f322009-04-02 03:41:46 +00004069PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 newtab[0].name = (char *)name;
4076 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004079}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004080
4081#ifdef __cplusplus
4082}
4083#endif