blob: f6cde957a8f2ca68e04cc3795a688fd2e40873cc [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)
Tim Peters36515e22001-11-18 04:06:29 +0000105*/
Guido van Rossum3ddee711991-12-16 13:06:34 +0000106
Nick Coghlancd419ab2010-09-11 00:39:25 +0000107/* MAGIC must change whenever the bytecode emitted by the compiler may no
108 longer be understood by older implementations of the eval loop (usually
109 due to the addition of new opcodes)
110 TAG must change for each major Python release. The magic number will take
111 care of any bytecode changes that occur during development.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000112*/
Benjamin Peterson6246d6d2010-09-10 21:51:44 +0000113#define MAGIC (3180 | ((long)'\r'<<16) | ((long)'\n'<<24))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000114#define TAG "cpython-32"
115#define CACHEDIR "__pycache__"
Victor Stinnerc9abda02011-03-14 13:33:46 -0400116static const Py_UNICODE CACHEDIR_UNICODE[] = {
117 '_', '_', 'p', 'y', 'c', 'a', 'c', 'h', 'e', '_', '_', '\0'};
Barry Warsaw28a691b2010-04-17 00:19:56 +0000118/* Current magic word and string tag as globals. */
Guido van Rossum96774c12000-05-01 20:19:08 +0000119static long pyc_magic = MAGIC;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000120static const char *pyc_tag = TAG;
Guido van Rossum96774c12000-05-01 20:19:08 +0000121
Victor Stinner95872862011-03-07 18:20:56 +0100122/* See _PyImport_FixupExtensionObject() below */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000123static PyObject *extensions = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124
Guido van Rossum771c6c81997-10-31 18:37:24 +0000125/* This table is defined in config.c: */
126extern struct _inittab _PyImport_Inittab[];
127
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000128/* Method from Parser/tokenizer.c */
Guido van Rossum40d20bc2007-10-22 00:09:51 +0000129extern char * PyTokenizer_FindEncoding(int);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000130
Guido van Rossum771c6c81997-10-31 18:37:24 +0000131struct _inittab *PyImport_Inittab = _PyImport_Inittab;
Guido van Rossum66f1fa81991-04-03 19:03:52 +0000132
Guido van Rossumed1170e1999-12-20 21:23:41 +0000133/* these tables define the module suffixes that Python recognizes */
134struct filedescr * _PyImport_Filetab = NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000135
Guido van Rossumed1170e1999-12-20 21:23:41 +0000136static const struct filedescr _PyImport_StandardFiletab[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 {".py", "U", PY_SOURCE},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000138#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 {".pyw", "U", PY_SOURCE},
Tim Peters36515e22001-11-18 04:06:29 +0000140#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 {".pyc", "rb", PY_COMPILED},
142 {0, 0}
Guido van Rossumed1170e1999-12-20 21:23:41 +0000143};
144
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000146/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147
148void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000149_PyImport_Init(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 const struct filedescr *scan;
152 struct filedescr *filetab;
153 int countD = 0;
154 int countS = 0;
Guido van Rossumed1170e1999-12-20 21:23:41 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 /* prepare _PyImport_Filetab: copy entries from
157 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
158 */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000159#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
161 ++countD;
Guido van Rossum04110fb2007-08-24 16:32:05 +0000162#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
164 ++countS;
165 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
166 if (filetab == NULL)
167 Py_FatalError("Can't initialize import file table.");
Guido van Rossum04110fb2007-08-24 16:32:05 +0000168#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 memcpy(filetab, _PyImport_DynLoadFiletab,
170 countD * sizeof(struct filedescr));
Guido van Rossum04110fb2007-08-24 16:32:05 +0000171#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 memcpy(filetab + countD, _PyImport_StandardFiletab,
173 countS * sizeof(struct filedescr));
174 filetab[countD + countS].suffix = NULL;
Guido van Rossumed1170e1999-12-20 21:23:41 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 _PyImport_Filetab = filetab;
Guido van Rossumed1170e1999-12-20 21:23:41 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 if (Py_OptimizeFlag) {
179 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
180 for (; filetab->suffix != NULL; filetab++) {
181 if (strcmp(filetab->suffix, ".pyc") == 0)
182 filetab->suffix = ".pyo";
183 }
184 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185}
186
Guido van Rossum25ce5661997-08-02 03:10:38 +0000187void
Just van Rossum52e14d62002-12-30 22:08:05 +0000188_PyImportHooks_Init(void)
189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 PyObject *v, *path_hooks = NULL, *zimpimport;
191 int err = 0;
Just van Rossum52e14d62002-12-30 22:08:05 +0000192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 /* adding sys.path_hooks and sys.path_importer_cache, setting up
194 zipimport */
195 if (PyType_Ready(&PyNullImporter_Type) < 0)
196 goto error;
Just van Rossum52e14d62002-12-30 22:08:05 +0000197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 if (Py_VerboseFlag)
199 PySys_WriteStderr("# installing zipimport hook\n");
Just van Rossum52e14d62002-12-30 22:08:05 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 v = PyList_New(0);
202 if (v == NULL)
203 goto error;
204 err = PySys_SetObject("meta_path", v);
205 Py_DECREF(v);
206 if (err)
207 goto error;
208 v = PyDict_New();
209 if (v == NULL)
210 goto error;
211 err = PySys_SetObject("path_importer_cache", v);
212 Py_DECREF(v);
213 if (err)
214 goto error;
215 path_hooks = PyList_New(0);
216 if (path_hooks == NULL)
217 goto error;
218 err = PySys_SetObject("path_hooks", path_hooks);
219 if (err) {
Just van Rossum52e14d62002-12-30 22:08:05 +0000220 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 PyErr_Print();
222 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
223 "path_importer_cache, or NullImporter failed"
224 );
225 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 zimpimport = PyImport_ImportModule("zipimport");
228 if (zimpimport == NULL) {
229 PyErr_Clear(); /* No zip import module -- okay */
230 if (Py_VerboseFlag)
231 PySys_WriteStderr("# can't import zipimport\n");
232 }
233 else {
234 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
235 "zipimporter");
236 Py_DECREF(zimpimport);
237 if (zipimporter == NULL) {
238 PyErr_Clear(); /* No zipimporter object -- okay */
239 if (Py_VerboseFlag)
240 PySys_WriteStderr(
241 "# can't import zipimport.zipimporter\n");
242 }
243 else {
244 /* sys.path_hooks.append(zipimporter) */
245 err = PyList_Append(path_hooks, zipimporter);
246 Py_DECREF(zipimporter);
247 if (err)
248 goto error;
249 if (Py_VerboseFlag)
250 PySys_WriteStderr(
251 "# installed zipimport hook\n");
252 }
253 }
254 Py_DECREF(path_hooks);
Just van Rossum52e14d62002-12-30 22:08:05 +0000255}
256
257void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258_PyImport_Fini(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 Py_XDECREF(extensions);
261 extensions = NULL;
262 PyMem_DEL(_PyImport_Filetab);
263 _PyImport_Filetab = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000264}
265
266
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000267/* Locking primitives to prevent parallel imports of the same module
268 in different threads to return with a partially loaded module.
269 These calls are serialized by the global interpreter lock. */
270
271#ifdef WITH_THREAD
272
Guido van Rossum49b56061998-10-01 20:42:43 +0000273#include "pythread.h"
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000274
Guido van Rossum65d5b571998-12-21 19:32:43 +0000275static PyThread_type_lock import_lock = 0;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000276static long import_lock_thread = -1;
277static int import_lock_level = 0;
278
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000279void
280_PyImport_AcquireLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 long me = PyThread_get_thread_ident();
283 if (me == -1)
284 return; /* Too bad */
285 if (import_lock == NULL) {
286 import_lock = PyThread_allocate_lock();
287 if (import_lock == NULL)
288 return; /* Nothing much we can do. */
289 }
290 if (import_lock_thread == me) {
291 import_lock_level++;
292 return;
293 }
294 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
295 {
296 PyThreadState *tstate = PyEval_SaveThread();
297 PyThread_acquire_lock(import_lock, 1);
298 PyEval_RestoreThread(tstate);
299 }
300 import_lock_thread = me;
301 import_lock_level = 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000302}
303
Benjamin Peterson0df35a92009-10-04 20:32:25 +0000304int
305_PyImport_ReleaseLock(void)
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 long me = PyThread_get_thread_ident();
308 if (me == -1 || import_lock == NULL)
309 return 0; /* Too bad */
310 if (import_lock_thread != me)
311 return -1;
312 import_lock_level--;
313 if (import_lock_level == 0) {
314 import_lock_thread = -1;
315 PyThread_release_lock(import_lock);
316 }
317 return 1;
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000318}
319
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000320/* This function is called from PyOS_AfterFork to ensure that newly
321 created child processes do not share locks with the parent.
322 We now acquire the import lock around fork() calls but on some platforms
323 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000324
325void
326_PyImport_ReInitLock(void)
327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 if (import_lock != NULL)
329 import_lock = PyThread_allocate_lock();
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000330 if (import_lock_level > 1) {
331 /* Forked as a side effect of import */
332 long me = PyThread_get_thread_ident();
333 PyThread_acquire_lock(import_lock, 0);
Victor Stinner942003c2011-03-07 16:57:48 +0100334 /* XXX: can the previous line fail? */
Nick Coghlanb2ddf792010-12-02 04:11:46 +0000335 import_lock_thread = me;
336 import_lock_level--;
337 } else {
338 import_lock_thread = -1;
339 import_lock_level = 0;
340 }
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000341}
342
Guido van Rossum75acc9c1998-03-03 22:26:50 +0000343#endif
344
Tim Peters69232342001-08-30 05:16:13 +0000345static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000346imp_lock_held(PyObject *self, PyObject *noargs)
Tim Peters69232342001-08-30 05:16:13 +0000347{
Tim Peters69232342001-08-30 05:16:13 +0000348#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return PyBool_FromLong(import_lock_thread != -1);
Tim Peters69232342001-08-30 05:16:13 +0000350#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 return PyBool_FromLong(0);
Tim Peters69232342001-08-30 05:16:13 +0000352#endif
353}
354
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000355static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000356imp_acquire_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000357{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000358#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 _PyImport_AcquireLock();
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000360#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 Py_INCREF(Py_None);
362 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000363}
364
365static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +0000366imp_release_lock(PyObject *self, PyObject *noargs)
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000367{
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000368#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (_PyImport_ReleaseLock() < 0) {
370 PyErr_SetString(PyExc_RuntimeError,
371 "not holding the import lock");
372 return NULL;
373 }
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000374#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 Py_INCREF(Py_None);
376 return Py_None;
Guido van Rossumc4f4ca92003-02-12 21:46:11 +0000377}
378
Guido van Rossumd8faa362007-04-27 19:54:29 +0000379static void
380imp_modules_reloading_clear(void)
381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 PyInterpreterState *interp = PyThreadState_Get()->interp;
383 if (interp->modules_reloading != NULL)
384 PyDict_Clear(interp->modules_reloading);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000385}
386
Guido van Rossum25ce5661997-08-02 03:10:38 +0000387/* Helper for sys */
388
389PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000390PyImport_GetModuleDict(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 PyInterpreterState *interp = PyThreadState_GET()->interp;
393 if (interp->modules == NULL)
394 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
395 return interp->modules;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000396}
397
Guido van Rossum3f5da241990-12-20 15:06:42 +0000398
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000399/* List of names to clear in sys */
400static char* sys_deletes[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 "path", "argv", "ps1", "ps2",
402 "last_type", "last_value", "last_traceback",
403 "path_hooks", "path_importer_cache", "meta_path",
404 /* misc stuff */
405 "flags", "float_info",
406 NULL
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000407};
408
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000409static char* sys_files[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 "stdin", "__stdin__",
411 "stdout", "__stdout__",
412 "stderr", "__stderr__",
413 NULL
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000414};
415
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000416
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000417/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000418
Guido van Rossum3f5da241990-12-20 15:06:42 +0000419void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420PyImport_Cleanup(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 Py_ssize_t pos, ndone;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 PyObject *key, *value, *dict;
424 PyInterpreterState *interp = PyThreadState_GET()->interp;
425 PyObject *modules = interp->modules;
Guido van Rossum758eec01998-01-19 21:58:26 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 if (modules == NULL)
428 return; /* Already done */
Guido van Rossum758eec01998-01-19 21:58:26 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* Delete some special variables first. These are common
431 places where user values hide and people complain when their
432 destructors fail. Since the modules containing them are
433 deleted *last* of all, they would come too late in the normal
434 destruction order. Sigh. */
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 value = PyDict_GetItemString(modules, "builtins");
437 if (value != NULL && PyModule_Check(value)) {
438 dict = PyModule_GetDict(value);
439 if (Py_VerboseFlag)
440 PySys_WriteStderr("# clear builtins._\n");
441 PyDict_SetItemString(dict, "_", Py_None);
442 }
443 value = PyDict_GetItemString(modules, "sys");
444 if (value != NULL && PyModule_Check(value)) {
445 char **p;
446 PyObject *v;
447 dict = PyModule_GetDict(value);
448 for (p = sys_deletes; *p != NULL; p++) {
449 if (Py_VerboseFlag)
450 PySys_WriteStderr("# clear sys.%s\n", *p);
451 PyDict_SetItemString(dict, *p, Py_None);
452 }
453 for (p = sys_files; *p != NULL; p+=2) {
454 if (Py_VerboseFlag)
455 PySys_WriteStderr("# restore sys.%s\n", *p);
456 v = PyDict_GetItemString(dict, *(p+1));
457 if (v == NULL)
458 v = Py_None;
459 PyDict_SetItemString(dict, *p, v);
460 }
461 }
Guido van Rossum05f9dce1998-02-19 20:58:44 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 /* First, delete __main__ */
464 value = PyDict_GetItemString(modules, "__main__");
465 if (value != NULL && PyModule_Check(value)) {
466 if (Py_VerboseFlag)
467 PySys_WriteStderr("# cleanup __main__\n");
468 _PyModule_Clear(value);
469 PyDict_SetItemString(modules, "__main__", Py_None);
470 }
Guido van Rossuma0fec2b1998-02-06 17:16:02 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 /* The special treatment of "builtins" here is because even
473 when it's not referenced as a module, its dictionary is
474 referenced by almost every module's __builtins__. Since
475 deleting a module clears its dictionary (even if there are
476 references left to it), we need to delete the "builtins"
477 module last. Likewise, we don't delete sys until the very
478 end because it is implicitly referenced (e.g. by print).
Guido van Rossum758eec01998-01-19 21:58:26 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 Also note that we 'delete' modules by replacing their entry
481 in the modules dict with None, rather than really deleting
482 them; this avoids a rehash of the modules dictionary and
483 also marks them as "non existent" so they won't be
484 re-imported. */
Guido van Rossum758eec01998-01-19 21:58:26 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 /* Next, repeatedly delete modules with a reference count of
487 one (skipping builtins and sys) and delete them */
488 do {
489 ndone = 0;
490 pos = 0;
491 while (PyDict_Next(modules, &pos, &key, &value)) {
492 if (value->ob_refcnt != 1)
493 continue;
494 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Victor Stinner9464d612011-03-07 17:08:21 +0100495 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 continue;
Victor Stinner9464d612011-03-07 17:08:21 +0100497 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 continue;
499 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100500 PySys_FormatStderr(
501 "# cleanup[1] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 _PyModule_Clear(value);
503 PyDict_SetItem(modules, key, Py_None);
504 ndone++;
505 }
506 }
507 } while (ndone > 0);
Guido van Rossum758eec01998-01-19 21:58:26 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 /* Next, delete all modules (still skipping builtins and sys) */
510 pos = 0;
511 while (PyDict_Next(modules, &pos, &key, &value)) {
512 if (PyUnicode_Check(key) && PyModule_Check(value)) {
Victor Stinner9464d612011-03-07 17:08:21 +0100513 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 continue;
Victor Stinner9464d612011-03-07 17:08:21 +0100515 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 continue;
517 if (Py_VerboseFlag)
Victor Stinner9464d612011-03-07 17:08:21 +0100518 PySys_FormatStderr("# cleanup[2] %U\n", key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 _PyModule_Clear(value);
520 PyDict_SetItem(modules, key, Py_None);
521 }
522 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 /* Next, delete sys and builtins (in that order) */
525 value = PyDict_GetItemString(modules, "sys");
526 if (value != NULL && PyModule_Check(value)) {
527 if (Py_VerboseFlag)
528 PySys_WriteStderr("# cleanup sys\n");
529 _PyModule_Clear(value);
530 PyDict_SetItemString(modules, "sys", Py_None);
531 }
532 value = PyDict_GetItemString(modules, "builtins");
533 if (value != NULL && PyModule_Check(value)) {
534 if (Py_VerboseFlag)
535 PySys_WriteStderr("# cleanup builtins\n");
536 _PyModule_Clear(value);
537 PyDict_SetItemString(modules, "builtins", Py_None);
538 }
Guido van Rossum758eec01998-01-19 21:58:26 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 /* Finally, clear and delete the modules directory */
541 PyDict_Clear(modules);
542 interp->modules = NULL;
543 Py_DECREF(modules);
544 Py_CLEAR(interp->modules_reloading);
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000545}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000546
547
Barry Warsaw28a691b2010-04-17 00:19:56 +0000548/* Helper for pythonrun.c -- return magic number and tag. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549
550long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000551PyImport_GetMagicNumber(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 return pyc_magic;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554}
555
556
Barry Warsaw28a691b2010-04-17 00:19:56 +0000557const char *
558PyImport_GetMagicTag(void)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return pyc_tag;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000561}
562
Guido van Rossum25ce5661997-08-02 03:10:38 +0000563/* Magic for extension modules (built-in as well as dynamically
564 loaded). To prevent initializing an extension module more than
565 once, we keep a static dictionary 'extensions' keyed by module name
566 (for built-in modules) or by filename (for dynamically loaded
Barry Warsaw92883382001-08-13 23:05:44 +0000567 modules), containing these modules. A copy of the module's
Victor Stinner95872862011-03-07 18:20:56 +0100568 dictionary is stored by calling _PyImport_FixupExtensionObject()
Guido van Rossum25ce5661997-08-02 03:10:38 +0000569 immediately after the module initialization function succeeds. A
570 copy can be retrieved from there by calling
Victor Stinner95872862011-03-07 18:20:56 +0100571 _PyImport_FindExtensionObject().
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572
Barry Warsaw7c9627b2010-06-17 18:38:20 +0000573 Modules which do support multiple initialization set their m_size
574 field to a non-negative number (indicating the size of the
575 module-specific state). They are still recorded in the extensions
576 dictionary, to avoid loading shared libraries twice.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000577*/
578
579int
Victor Stinner95872862011-03-07 18:20:56 +0100580_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
581 PyObject *filename)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 PyObject *modules, *dict;
584 struct PyModuleDef *def;
585 if (extensions == NULL) {
586 extensions = PyDict_New();
587 if (extensions == NULL)
588 return -1;
589 }
590 if (mod == NULL || !PyModule_Check(mod)) {
591 PyErr_BadInternalCall();
592 return -1;
593 }
594 def = PyModule_GetDef(mod);
595 if (!def) {
596 PyErr_BadInternalCall();
597 return -1;
598 }
599 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +0100600 if (PyDict_SetItem(modules, name, mod) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return -1;
602 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100603 PyDict_DelItem(modules, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 return -1;
605 }
606 if (def->m_size == -1) {
607 if (def->m_base.m_copy) {
608 /* Somebody already imported the module,
609 likely under a different name.
610 XXX this should really not happen. */
611 Py_DECREF(def->m_base.m_copy);
612 def->m_base.m_copy = NULL;
613 }
614 dict = PyModule_GetDict(mod);
615 if (dict == NULL)
616 return -1;
617 def->m_base.m_copy = PyDict_Copy(dict);
618 if (def->m_base.m_copy == NULL)
619 return -1;
620 }
Victor Stinner49d3f252010-10-17 01:24:53 +0000621 PyDict_SetItem(extensions, filename, (PyObject*)def);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 return 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000623}
624
Victor Stinner49d3f252010-10-17 01:24:53 +0000625int
626_PyImport_FixupBuiltin(PyObject *mod, char *name)
627{
628 int res;
Victor Stinner95872862011-03-07 18:20:56 +0100629 PyObject *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100630 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100631 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000632 return -1;
Victor Stinner95872862011-03-07 18:20:56 +0100633 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
634 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000635 return res;
636}
637
Guido van Rossum25ce5661997-08-02 03:10:38 +0000638PyObject *
Victor Stinner95872862011-03-07 18:20:56 +0100639_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 PyObject *mod, *mdict;
642 PyModuleDef* def;
643 if (extensions == NULL)
644 return NULL;
Victor Stinner49d3f252010-10-17 01:24:53 +0000645 def = (PyModuleDef*)PyDict_GetItem(extensions, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 if (def == NULL)
647 return NULL;
648 if (def->m_size == -1) {
649 /* Module does not support repeated initialization */
650 if (def->m_base.m_copy == NULL)
651 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100652 mod = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (mod == NULL)
654 return NULL;
655 mdict = PyModule_GetDict(mod);
656 if (mdict == NULL)
657 return NULL;
658 if (PyDict_Update(mdict, def->m_base.m_copy))
659 return NULL;
660 }
661 else {
662 if (def->m_base.m_init == NULL)
663 return NULL;
664 mod = def->m_base.m_init();
665 if (mod == NULL)
666 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100667 PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 Py_DECREF(mod);
669 }
670 if (_PyState_AddModule(mod, def) < 0) {
Victor Stinner95872862011-03-07 18:20:56 +0100671 PyDict_DelItem(PyImport_GetModuleDict(), name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 Py_DECREF(mod);
673 return NULL;
674 }
675 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100676 PySys_FormatStderr("import %U # previously loaded (%R)\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 name, filename);
678 return mod;
Brett Cannon9a5b25a2010-03-01 02:09:17 +0000679
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680}
681
Victor Stinner49d3f252010-10-17 01:24:53 +0000682PyObject *
Victor Stinner501c09a2011-02-23 00:02:00 +0000683_PyImport_FindBuiltin(const char *name)
Victor Stinner49d3f252010-10-17 01:24:53 +0000684{
Victor Stinner95872862011-03-07 18:20:56 +0100685 PyObject *res, *nameobj;
Victor Stinner21fcd0c2011-03-07 18:28:15 +0100686 nameobj = PyUnicode_InternFromString(name);
Victor Stinner95872862011-03-07 18:20:56 +0100687 if (nameobj == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +0000688 return NULL;
Victor Stinner95872862011-03-07 18:20:56 +0100689 res = _PyImport_FindExtensionObject(nameobj, nameobj);
690 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000691 return res;
692}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693
694/* Get the module object corresponding to a module name.
695 First check the modules dictionary if there's one there,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000696 if not, create a new one and insert it in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000697 Because the former action is most common, THIS DOES NOT RETURN A
698 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700PyObject *
Victor Stinner27ee0892011-03-04 12:57:09 +0000701PyImport_AddModuleObject(PyObject *name)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject *modules = PyImport_GetModuleDict();
704 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705
Victor Stinner27ee0892011-03-04 12:57:09 +0000706 if ((m = PyDict_GetItem(modules, name)) != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyModule_Check(m))
708 return m;
Victor Stinner27ee0892011-03-04 12:57:09 +0000709 m = PyModule_NewObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 if (m == NULL)
711 return NULL;
Victor Stinner27ee0892011-03-04 12:57:09 +0000712 if (PyDict_SetItem(modules, name, m) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 Py_DECREF(m);
714 return NULL;
715 }
716 Py_DECREF(m); /* Yes, it still exists, in modules! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719}
720
Victor Stinner27ee0892011-03-04 12:57:09 +0000721PyObject *
722PyImport_AddModule(const char *name)
723{
724 PyObject *nameobj, *module;
725 nameobj = PyUnicode_FromString(name);
726 if (nameobj == NULL)
727 return NULL;
728 module = PyImport_AddModuleObject(nameobj);
729 Py_DECREF(nameobj);
730 return module;
731}
732
733
Tim Peters1cd70172004-08-02 03:52:12 +0000734/* Remove name from sys.modules, if it's there. */
735static void
Victor Stinner27ee0892011-03-04 12:57:09 +0000736remove_module(PyObject *name)
Tim Peters1cd70172004-08-02 03:52:12 +0000737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner27ee0892011-03-04 12:57:09 +0000739 if (PyDict_GetItem(modules, name) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 return;
Victor Stinner27ee0892011-03-04 12:57:09 +0000741 if (PyDict_DelItem(modules, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 Py_FatalError("import: deleting existing key in"
743 "sys.modules failed");
Tim Peters1cd70172004-08-02 03:52:12 +0000744}
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000745
Victor Stinnerc9abda02011-03-14 13:33:46 -0400746static PyObject * get_sourcefile(PyObject *filename);
747static PyObject *make_source_pathname(PyObject *pathname);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000748static char *make_compiled_pathname(char *pathname, char *buf, size_t buflen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 int debug);
Christian Heimes3b06e532008-01-07 20:12:44 +0000750
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000751/* Execute a code object in a module and return the module object
Tim Peters1cd70172004-08-02 03:52:12 +0000752 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
753 * removed from sys.modules, to avoid leaving damaged module objects
754 * in sys.modules. The caller may wish to restore the original
755 * module object (if any) in this case; PyImport_ReloadModule is an
756 * example.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000757 *
758 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
759 * interface. The other two exist primarily for backward compatibility.
Tim Peters1cd70172004-08-02 03:52:12 +0000760 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000762PyImport_ExecCodeModule(char *name, PyObject *co)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 return PyImport_ExecCodeModuleWithPathnames(
765 name, co, (char *)NULL, (char *)NULL);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000766}
767
768PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000769PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return PyImport_ExecCodeModuleWithPathnames(
772 name, co, pathname, (char *)NULL);
Barry Warsaw28a691b2010-04-17 00:19:56 +0000773}
774
775PyObject *
776PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 char *cpathname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000778{
Victor Stinner27ee0892011-03-04 12:57:09 +0000779 PyObject *m = NULL;
780 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
781
782 nameobj = PyUnicode_FromString(name);
783 if (nameobj == NULL)
784 return NULL;
785
786 if (pathname != NULL) {
787 pathobj = PyUnicode_DecodeFSDefault(pathname);
788 if (pathobj == NULL)
789 goto error;
790 } else
791 pathobj = NULL;
792 if (cpathname != NULL) {
793 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
794 if (cpathobj == NULL)
795 goto error;
796 } else
797 cpathobj = NULL;
798 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
799error:
800 Py_DECREF(nameobj);
801 Py_XDECREF(pathobj);
802 Py_XDECREF(cpathobj);
803 return m;
804}
805
806PyObject*
807PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
808 PyObject *cpathname)
809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyObject *modules = PyImport_GetModuleDict();
811 PyObject *m, *d, *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000812
Victor Stinner27ee0892011-03-04 12:57:09 +0000813 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 if (m == NULL)
815 return NULL;
816 /* If the module is being reloaded, we get the old module back
817 and re-use its dict to exec the new code. */
818 d = PyModule_GetDict(m);
819 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
820 if (PyDict_SetItemString(d, "__builtins__",
821 PyEval_GetBuiltins()) != 0)
822 goto error;
823 }
824 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 if (pathname != NULL) {
Victor Stinnerc9abda02011-03-14 13:33:46 -0400826 v = get_sourcefile(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 if (v == NULL)
828 PyErr_Clear();
829 }
Victor Stinner27ee0892011-03-04 12:57:09 +0000830 else
831 v = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 if (v == NULL) {
833 v = ((PyCodeObject *)co)->co_filename;
834 Py_INCREF(v);
835 }
836 if (PyDict_SetItemString(d, "__file__", v) != 0)
837 PyErr_Clear(); /* Not important enough to report */
838 Py_DECREF(v);
Guido van Rossume32bf6e1998-02-11 05:53:02 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 /* Remember the pyc path name as the __cached__ attribute. */
Victor Stinner27ee0892011-03-04 12:57:09 +0000841 if (cpathname != NULL)
842 v = cpathname;
843 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 v = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 if (PyDict_SetItemString(d, "__cached__", v) != 0)
846 PyErr_Clear(); /* Not important enough to report */
Barry Warsaw28a691b2010-04-17 00:19:56 +0000847
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000848 v = PyEval_EvalCode(co, d, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 if (v == NULL)
850 goto error;
851 Py_DECREF(v);
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000852
Victor Stinner27ee0892011-03-04 12:57:09 +0000853 if ((m = PyDict_GetItem(modules, name)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyErr_Format(PyExc_ImportError,
Victor Stinner27ee0892011-03-04 12:57:09 +0000855 "Loaded module %R not found in sys.modules",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 name);
857 return NULL;
858 }
Guido van Rossumb65e85c1997-07-10 18:00:45 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 return m;
Tim Peters1cd70172004-08-02 03:52:12 +0000863
864 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 remove_module(name);
866 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000867}
868
869
Barry Warsaw28a691b2010-04-17 00:19:56 +0000870/* Like strrchr(string, '/') but searches for the rightmost of either SEP
871 or ALTSEP, if the latter is defined.
872*/
873static char *
874rightmost_sep(char *s)
875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 char *found, c;
877 for (found = NULL; (c = *s); s++) {
878 if (c == SEP
Barry Warsaw28a691b2010-04-17 00:19:56 +0000879#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 || c == ALTSEP
Barry Warsaw28a691b2010-04-17 00:19:56 +0000881#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 )
883 {
884 found = s;
885 }
886 }
887 return found;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000888}
889
890
Victor Stinnerc9abda02011-03-14 13:33:46 -0400891/* Like strrchr(string, '/') but searches for the rightmost of either SEP
892 or ALTSEP, if the latter is defined.
893*/
894static Py_UNICODE*
895rightmost_sep_unicode(Py_UNICODE *s)
896{
897 Py_UNICODE *found, c;
898 for (found = NULL; (c = *s); s++) {
899 if (c == SEP
900#ifdef ALTSEP
901 || c == ALTSEP
902#endif
903 )
904 {
905 found = s;
906 }
907 }
908 return found;
909}
910
911
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000912/* Given a pathname for a Python source file, fill a buffer with the
913 pathname for the corresponding compiled file. Return the pathname
914 for the compiled file, or NULL if there's no space in the buffer.
915 Doesn't set an exception. */
916
917static char *
Barry Warsaw28a691b2010-04-17 00:19:56 +0000918make_compiled_pathname(char *pathname, char *buf, size_t buflen, int debug)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 /* foo.py -> __pycache__/foo.<tag>.pyc */
921 size_t len = strlen(pathname);
922 size_t i, save;
923 char *pos;
924 int sep = SEP;
Barry Warsaw28a691b2010-04-17 00:19:56 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 /* Sanity check that the buffer has roughly enough space to hold what
927 will eventually be the full path to the compiled file. The 5 extra
928 bytes include the slash afer __pycache__, the two extra dots, the
929 extra trailing character ('c' or 'o') and null. This isn't exact
930 because the contents of the buffer can affect how many actual
931 characters of the string get into the buffer. We'll do a final
932 sanity check before writing the extension to ensure we do not
933 overflow the buffer.
934 */
935 if (len + strlen(CACHEDIR) + strlen(pyc_tag) + 5 > buflen)
936 return NULL;
Tim Petersc1731372001-08-04 08:12:36 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 /* Find the last path separator and copy everything from the start of
939 the source string up to and including the separator.
940 */
941 if ((pos = rightmost_sep(pathname)) == NULL) {
942 i = 0;
943 }
944 else {
945 sep = *pos;
946 i = pos - pathname + 1;
947 strncpy(buf, pathname, i);
948 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 save = i;
951 buf[i++] = '\0';
952 /* Add __pycache__/ */
953 strcat(buf, CACHEDIR);
954 i += strlen(CACHEDIR) - 1;
955 buf[i++] = sep;
956 buf[i++] = '\0';
957 /* Add the base filename, but remove the .py or .pyw extension, since
958 the tag name must go before the extension.
959 */
960 strcat(buf, pathname + save);
961 if ((pos = strrchr(buf, '.')) != NULL)
962 *++pos = '\0';
963 strcat(buf, pyc_tag);
964 /* The length test above assumes that we're only adding one character
965 to the end of what would normally be the extension. What if there
966 is no extension, or the string ends in '.' or '.p', and otherwise
967 fills the buffer? By appending 4 more characters onto the string
968 here, we could overrun the buffer.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 As a simple example, let's say buflen=32 and the input string is
971 'xxx.py'. strlen() would be 6 and the test above would yield:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 (6 + 11 + 10 + 5 == 32) > 32
Barry Warsaw28a691b2010-04-17 00:19:56 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 which is false and so the name mangling would continue. This would
976 be fine because we'd end up with this string in buf:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 __pycache__/xxx.cpython-32.pyc\0
Barry Warsaw28a691b2010-04-17 00:19:56 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 strlen(of that) == 30 + the nul fits inside a 32 character buffer.
981 We can even handle an input string of say 'xxxxx' above because
982 that's (5 + 11 + 10 + 5 == 31) > 32 which is also false. Name
983 mangling that yields:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 __pycache__/xxxxxcpython-32.pyc\0
Barry Warsaw28a691b2010-04-17 00:19:56 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 which is 32 characters including the nul, and thus fits in the
988 buffer. However, an input string of 'xxxxxx' would yield a result
989 string of:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 __pycache__/xxxxxxcpython-32.pyc\0
Barry Warsaw28a691b2010-04-17 00:19:56 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 which is 33 characters long (including the nul), thus overflowing
994 the buffer, even though the first test would fail, i.e.: the input
995 string is also 6 characters long, so 32 > 32 is false.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 The reason the first test fails but we still overflow the buffer is
998 that the test above only expects to add one extra character to be
999 added to the extension, and here we're adding three (pyc). We
1000 don't add the first dot, so that reclaims one of expected
1001 positions, leaving us overflowing by 1 byte (3 extra - 1 reclaimed
1002 dot - 1 expected extra == 1 overflowed).
Barry Warsaw28a691b2010-04-17 00:19:56 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 The best we can do is ensure that we still have enough room in the
1005 target buffer before we write the extension. Because it's always
1006 only the extension that can cause the overflow, and never the other
1007 path bytes we've written, it's sufficient to just do one more test
1008 here. Still, the assertion that follows can't hurt.
1009 */
Barry Warsaw28a691b2010-04-17 00:19:56 +00001010#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 printf("strlen(buf): %d; buflen: %d\n", (int)strlen(buf), (int)buflen);
Barry Warsaw28a691b2010-04-17 00:19:56 +00001012#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 if (strlen(buf) + 5 > buflen)
1014 return NULL;
1015 strcat(buf, debug ? ".pyc" : ".pyo");
1016 assert(strlen(buf) < buflen);
1017 return buf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001018}
1019
1020
Barry Warsaw28a691b2010-04-17 00:19:56 +00001021/* Given a pathname to a Python byte compiled file, return the path to the
1022 source file, if the path matches the PEP 3147 format. This does not check
1023 for any file existence, however, if the pyc file name does not match PEP
1024 3147 style, NULL is returned. buf must be at least as big as pathname;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001025 the resulting path will always be shorter.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026
Victor Stinnerc9abda02011-03-14 13:33:46 -04001027 (...)/__pycache__/foo.<tag>.pyc -> (...)/foo.py */
1028
1029static PyObject*
1030make_source_pathname(PyObject *pathobj)
Barry Warsaw28a691b2010-04-17 00:19:56 +00001031{
Victor Stinnerc9abda02011-03-14 13:33:46 -04001032 Py_UNICODE buf[MAXPATHLEN];
1033 Py_UNICODE *pathname;
1034 Py_UNICODE *left, *right, *dot0, *dot1, sep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 size_t i, j;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001036
1037 if (PyUnicode_GET_SIZE(pathobj) > MAXPATHLEN)
1038 return NULL;
1039 pathname = PyUnicode_AS_UNICODE(pathobj);
Barry Warsaw28a691b2010-04-17 00:19:56 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 /* Look back two slashes from the end. In between these two slashes
1042 must be the string __pycache__ or this is not a PEP 3147 style
1043 path. It's possible for there to be only one slash.
1044 */
Victor Stinnerc9abda02011-03-14 13:33:46 -04001045 right = rightmost_sep_unicode(pathname);
1046 if (right == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 return NULL;
1048 sep = *right;
1049 *right = '\0';
Victor Stinnerc9abda02011-03-14 13:33:46 -04001050 left = rightmost_sep_unicode(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 *right = sep;
1052 if (left == NULL)
1053 left = pathname;
1054 else
1055 left++;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001056 if (right-left != Py_UNICODE_strlen(CACHEDIR_UNICODE) ||
1057 Py_UNICODE_strncmp(left, CACHEDIR_UNICODE, right-left) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 /* Now verify that the path component to the right of the last slash
1061 has two dots in it.
1062 */
Victor Stinnerc9abda02011-03-14 13:33:46 -04001063 if ((dot0 = Py_UNICODE_strchr(right + 1, '.')) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 return NULL;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001065 if ((dot1 = Py_UNICODE_strchr(dot0 + 1, '.')) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 return NULL;
1067 /* Too many dots? */
Victor Stinnerc9abda02011-03-14 13:33:46 -04001068 if (Py_UNICODE_strchr(dot1 + 1, '.') != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 /* This is a PEP 3147 path. Start by copying everything from the
1072 start of pathname up to and including the leftmost slash. Then
1073 copy the file's basename, removing the magic tag and adding a .py
1074 suffix.
1075 */
Victor Stinnerc9abda02011-03-14 13:33:46 -04001076 Py_UNICODE_strncpy(buf, pathname, (i=left-pathname));
1077 Py_UNICODE_strncpy(buf+i, right+1, (j=dot0-right));
1078 buf[i+j] = 'p';
1079 buf[i+j+1] = 'y';
1080 return PyUnicode_FromUnicode(buf, i+j+2);
Barry Warsaw28a691b2010-04-17 00:19:56 +00001081}
1082
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001083/* Given a pathname for a Python source file, its time of last
1084 modification, and a pathname for a compiled file, check whether the
1085 compiled file represents the same version of the source. If so,
1086 return a FILE pointer for the compiled file, positioned just after
1087 the header; if not, return NULL.
1088 Doesn't set an exception. */
1089
1090static FILE *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001091check_compiled_module(char *pathname, time_t mtime, char *cpathname)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 FILE *fp;
1094 long magic;
1095 long pyc_mtime;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 fp = fopen(cpathname, "rb");
1098 if (fp == NULL)
1099 return NULL;
1100 magic = PyMarshal_ReadLongFromFile(fp);
1101 if (magic != pyc_magic) {
1102 if (Py_VerboseFlag)
1103 PySys_WriteStderr("# %s has bad magic\n", cpathname);
1104 fclose(fp);
1105 return NULL;
1106 }
1107 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
1108 if (pyc_mtime != mtime) {
1109 if (Py_VerboseFlag)
1110 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
1111 fclose(fp);
1112 return NULL;
1113 }
1114 if (Py_VerboseFlag)
1115 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
1116 return fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001117}
1118
1119
1120/* Read a code object from a file and check it for validity */
1121
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001123read_compiled_module(char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 PyObject *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 co = PyMarshal_ReadLastObjectFromFile(fp);
1128 if (co == NULL)
1129 return NULL;
1130 if (!PyCode_Check(co)) {
1131 PyErr_Format(PyExc_ImportError,
1132 "Non-code object in %.200s", cpathname);
1133 Py_DECREF(co);
1134 return NULL;
1135 }
1136 return (PyCodeObject *)co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001137}
1138
1139
1140/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001141 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001142
Guido van Rossum79f25d91997-04-29 20:08:16 +00001143static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001144load_compiled_module(char *name, char *cpathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 long magic;
1147 PyCodeObject *co;
1148 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 magic = PyMarshal_ReadLongFromFile(fp);
1151 if (magic != pyc_magic) {
1152 PyErr_Format(PyExc_ImportError,
1153 "Bad magic number in %.200s", cpathname);
1154 return NULL;
1155 }
1156 (void) PyMarshal_ReadLongFromFile(fp);
1157 co = read_compiled_module(cpathname, fp);
1158 if (co == NULL)
1159 return NULL;
1160 if (Py_VerboseFlag)
1161 PySys_WriteStderr("import %s # precompiled from %s\n",
1162 name, cpathname);
1163 m = PyImport_ExecCodeModuleWithPathnames(
1164 name, (PyObject *)co, cpathname, cpathname);
1165 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001168}
1169
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001170/* Parse a source file and return the corresponding code object */
1171
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172static PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173parse_source_module(const char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 PyCodeObject *co = NULL;
1176 mod_ty mod;
1177 PyCompilerFlags flags;
1178 PyArena *arena = PyArena_New();
1179 if (arena == NULL)
1180 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 flags.cf_flags = 0;
1183 mod = PyParser_ASTFromFile(fp, pathname, NULL,
1184 Py_file_input, 0, 0, &flags,
1185 NULL, arena);
1186 if (mod) {
1187 co = PyAST_Compile(mod, pathname, NULL, arena);
1188 }
1189 PyArena_Free(arena);
1190 return co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001191}
1192
1193
Guido van Rossum55a83382000-09-20 20:31:38 +00001194/* Helper to open a bytecode file for writing in exclusive mode */
1195
1196static FILE *
Christian Heimes05e8be12008-02-23 18:30:17 +00001197open_exclusive(char *filename, mode_t mode)
Guido van Rossum55a83382000-09-20 20:31:38 +00001198{
1199#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 /* Use O_EXCL to avoid a race condition when another process tries to
1201 write the same file. When that happens, our open() call fails,
1202 which is just fine (since it's only a cache).
1203 XXX If the file exists and is writable but the directory is not
1204 writable, the file will never be written. Oh well.
1205 */
1206 int fd;
1207 (void) unlink(filename);
1208 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
Tim Peters42c83af2000-09-29 04:03:10 +00001209#ifdef O_BINARY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 |O_BINARY /* necessary for Windows */
Tim Peters42c83af2000-09-29 04:03:10 +00001211#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001212#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 , mode, "ctxt=bin", "shr=nil"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001214#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 , mode
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001216#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 );
1218 if (fd < 0)
1219 return NULL;
1220 return fdopen(fd, "wb");
Guido van Rossum55a83382000-09-20 20:31:38 +00001221#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 /* Best we can do -- on Windows this can't happen anyway */
1223 return fopen(filename, "wb");
Guido van Rossum55a83382000-09-20 20:31:38 +00001224#endif
1225}
1226
1227
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001228/* Write a compiled module to a file, placing the time of last
1229 modification of its source into the header.
1230 Errors are ignored, if a write error occurs an attempt is made to
1231 remove the file. */
1232
1233static void
Christian Heimes05e8be12008-02-23 18:30:17 +00001234write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 FILE *fp;
1237 char *dirpath;
1238 time_t mtime = srcstat->st_mtime;
Alexandre Vassalotti9d58e3e2009-07-17 10:55:50 +00001239#ifdef MS_WINDOWS /* since Windows uses different permissions */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 mode_t mode = srcstat->st_mode & ~S_IEXEC;
Alexandre Vassalotti9d58e3e2009-07-17 10:55:50 +00001241#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
1243 mode_t dirmode = (srcstat->st_mode |
1244 S_IXUSR | S_IXGRP | S_IXOTH |
1245 S_IWUSR | S_IWGRP | S_IWOTH);
Brett Cannon9a5b25a2010-03-01 02:09:17 +00001246#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 int saved;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 /* Ensure that the __pycache__ directory exists. */
1250 dirpath = rightmost_sep(cpathname);
1251 if (dirpath == NULL) {
1252 if (Py_VerboseFlag)
1253 PySys_WriteStderr(
1254 "# no %s path found %s\n",
1255 CACHEDIR, cpathname);
1256 return;
1257 }
1258 saved = *dirpath;
1259 *dirpath = '\0';
Daniel Stutzbachc7937792010-09-09 21:18:04 +00001260
1261#ifdef MS_WINDOWS
1262 if (_mkdir(cpathname) < 0 && errno != EEXIST) {
1263#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (mkdir(cpathname, dirmode) < 0 && errno != EEXIST) {
Daniel Stutzbachc7937792010-09-09 21:18:04 +00001265#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 *dirpath = saved;
1267 if (Py_VerboseFlag)
1268 PySys_WriteStderr(
1269 "# cannot create cache dir %s\n", cpathname);
1270 return;
1271 }
1272 *dirpath = saved;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 fp = open_exclusive(cpathname, mode);
1275 if (fp == NULL) {
1276 if (Py_VerboseFlag)
1277 PySys_WriteStderr(
1278 "# can't create %s\n", cpathname);
1279 return;
1280 }
1281 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
1282 /* First write a 0 for mtime */
1283 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
1284 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
1285 if (fflush(fp) != 0 || ferror(fp)) {
1286 if (Py_VerboseFlag)
1287 PySys_WriteStderr("# can't write %s\n", cpathname);
1288 /* Don't keep partial file */
1289 fclose(fp);
1290 (void) unlink(cpathname);
1291 return;
1292 }
1293 /* Now write the true mtime */
1294 fseek(fp, 4L, 0);
1295 assert(mtime < LONG_MAX);
1296 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
1297 fflush(fp);
1298 fclose(fp);
1299 if (Py_VerboseFlag)
1300 PySys_WriteStderr("# wrote %s\n", cpathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001301}
1302
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001303static void
1304update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
1305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 PyObject *constants, *tmp;
1307 Py_ssize_t i, n;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (PyUnicode_Compare(co->co_filename, oldname))
1310 return;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 tmp = co->co_filename;
1313 co->co_filename = newname;
1314 Py_INCREF(co->co_filename);
1315 Py_DECREF(tmp);
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 constants = co->co_consts;
1318 n = PyTuple_GET_SIZE(constants);
1319 for (i = 0; i < n; i++) {
1320 tmp = PyTuple_GET_ITEM(constants, i);
1321 if (PyCode_Check(tmp))
1322 update_code_filenames((PyCodeObject *)tmp,
1323 oldname, newname);
1324 }
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001325}
1326
1327static int
1328update_compiled_module(PyCodeObject *co, char *pathname)
1329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 PyObject *oldname, *newname;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 newname = PyUnicode_DecodeFSDefault(pathname);
1333 if (newname == NULL)
1334 return -1;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 if (!PyUnicode_Compare(co->co_filename, newname)) {
1337 Py_DECREF(newname);
1338 return 0;
1339 }
Hirokazu Yamamoto4f447fb2009-03-04 01:52:10 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 oldname = co->co_filename;
1342 Py_INCREF(oldname);
1343 update_code_filenames(co, oldname, newname);
1344 Py_DECREF(oldname);
1345 Py_DECREF(newname);
1346 return 1;
Antoine Pitroud35cbf62009-01-06 19:02:24 +00001347}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001348
1349/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +00001350 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
1351 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001352
Guido van Rossum79f25d91997-04-29 20:08:16 +00001353static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001354load_source_module(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 struct stat st;
1357 FILE *fpc;
1358 char buf[MAXPATHLEN+1];
1359 char *cpathname;
1360 PyCodeObject *co;
1361 PyObject *m;
Brett Cannon9a5b25a2010-03-01 02:09:17 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 if (fstat(fileno(fp), &st) != 0) {
1364 PyErr_Format(PyExc_RuntimeError,
1365 "unable to get file status from '%s'",
1366 pathname);
1367 return NULL;
1368 }
Fred Drake4c82b232000-06-30 16:18:57 +00001369#if SIZEOF_TIME_T > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 /* Python's .pyc timestamp handling presumes that the timestamp fits
1371 in 4 bytes. This will be fine until sometime in the year 2038,
1372 when a 4-byte signed time_t will overflow.
1373 */
1374 if (st.st_mtime >> 32) {
1375 PyErr_SetString(PyExc_OverflowError,
1376 "modification time overflows a 4 byte field");
1377 return NULL;
1378 }
Fred Drake4c82b232000-06-30 16:18:57 +00001379#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 cpathname = make_compiled_pathname(
1381 pathname, buf, (size_t)MAXPATHLEN + 1, !Py_OptimizeFlag);
1382 if (cpathname != NULL &&
1383 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
1384 co = read_compiled_module(cpathname, fpc);
1385 fclose(fpc);
1386 if (co == NULL)
1387 return NULL;
1388 if (update_compiled_module(co, pathname) < 0)
1389 return NULL;
1390 if (Py_VerboseFlag)
1391 PySys_WriteStderr("import %s # precompiled from %s\n",
1392 name, cpathname);
1393 pathname = cpathname;
1394 }
1395 else {
1396 co = parse_source_module(pathname, fp);
1397 if (co == NULL)
1398 return NULL;
1399 if (Py_VerboseFlag)
1400 PySys_WriteStderr("import %s # from %s\n",
1401 name, pathname);
1402 if (cpathname) {
1403 PyObject *ro = PySys_GetObject("dont_write_bytecode");
1404 if (ro == NULL || !PyObject_IsTrue(ro))
1405 write_compiled_module(co, cpathname, &st);
1406 }
1407 }
1408 m = PyImport_ExecCodeModuleWithPathnames(
1409 name, (PyObject *)co, pathname, cpathname);
1410 Py_DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001413}
1414
Christian Heimes3b06e532008-01-07 20:12:44 +00001415/* Get source file -> unicode or None
1416 * Returns the path to the py file if available, else the given path
1417 */
1418static PyObject *
Victor Stinnerc9abda02011-03-14 13:33:46 -04001419get_sourcefile(PyObject *filename)
Christian Heimes3b06e532008-01-07 20:12:44 +00001420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 Py_ssize_t len;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001422 Py_UNICODE *fileuni;
1423 PyObject *py;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 struct stat statbuf;
Christian Heimes3b06e532008-01-07 20:12:44 +00001425
Victor Stinnerc9abda02011-03-14 13:33:46 -04001426 len = PyUnicode_GET_SIZE(filename);
1427 if (len == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 Py_RETURN_NONE;
Christian Heimes3b06e532008-01-07 20:12:44 +00001429
Victor Stinnerc9abda02011-03-14 13:33:46 -04001430 /* don't match *.pyc or *.pyo? */
1431 fileuni = PyUnicode_AS_UNICODE(filename);
1432 if (len < 5
1433 || fileuni[len-4] != '.'
1434 || (fileuni[len-3] != 'p' && fileuni[len-3] != 'P')
1435 || (fileuni[len-2] != 'y' && fileuni[len-2] != 'Y'))
1436 goto unchanged;
Christian Heimes3b06e532008-01-07 20:12:44 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 /* Start by trying to turn PEP 3147 path into source path. If that
1439 * fails, just chop off the trailing character, i.e. legacy pyc path
1440 * to py.
1441 */
Victor Stinnerc9abda02011-03-14 13:33:46 -04001442 py = make_source_pathname(filename);
1443 if (py == NULL) {
1444 PyErr_Clear();
1445 py = PyUnicode_FromUnicode(fileuni, len - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 }
Victor Stinnerc9abda02011-03-14 13:33:46 -04001447 if (py == NULL)
1448 goto error;
Barry Warsaw28a691b2010-04-17 00:19:56 +00001449
Victor Stinnerc9abda02011-03-14 13:33:46 -04001450 if (_Py_stat(py, &statbuf) == 0 && S_ISREG(statbuf.st_mode))
1451 return py;
1452 Py_DECREF(py);
1453 goto unchanged;
1454
1455error:
1456 PyErr_Clear();
1457unchanged:
1458 Py_INCREF(filename);
1459 return filename;
Christian Heimes3b06e532008-01-07 20:12:44 +00001460}
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001461
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001462/* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001463static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
Victor Stinnerc6963162011-03-12 09:26:54 -05001464static struct filedescr *find_module(char *, const char *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 char *, size_t, FILE **, PyObject **);
Victor Stinner53dc7352011-03-20 01:50:21 +01001466static struct _frozen * find_frozen(PyObject *);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001467
1468/* Load a package and return its module object WITH INCREMENTED
1469 REFERENCE COUNT */
1470
1471static PyObject *
Victor Stinnerc9abda02011-03-14 13:33:46 -04001472load_package(PyObject *name, PyObject *pathname)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 PyObject *m, *d;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001475 PyObject *file = NULL, *path_list = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 int err;
1477 char buf[MAXPATHLEN+1];
Victor Stinnerc9abda02011-03-14 13:33:46 -04001478 FILE *fp = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 struct filedescr *fdp;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001480 char *namestr;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001481
Victor Stinnerc9abda02011-03-14 13:33:46 -04001482 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (m == NULL)
1484 return NULL;
1485 if (Py_VerboseFlag)
Victor Stinnerc9abda02011-03-14 13:33:46 -04001486 PySys_FormatStderr("import %U # directory %U\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 name, pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 file = get_sourcefile(pathname);
1489 if (file == NULL)
Victor Stinnerc9abda02011-03-14 13:33:46 -04001490 return NULL;
1491 path_list = Py_BuildValue("[O]", file);
1492 if (path_list == NULL) {
1493 Py_DECREF(file);
1494 return NULL;
1495 }
1496 d = PyModule_GetDict(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 err = PyDict_SetItemString(d, "__file__", file);
Victor Stinnerc9abda02011-03-14 13:33:46 -04001498 Py_DECREF(file);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if (err == 0)
Victor Stinnerc9abda02011-03-14 13:33:46 -04001500 err = PyDict_SetItemString(d, "__path__", path_list);
1501 if (err != 0) {
1502 Py_DECREF(path_list);
1503 return NULL;
1504 }
1505 namestr = _PyUnicode_AsString(name);
1506 if (namestr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 goto error;
Victor Stinnerc9abda02011-03-14 13:33:46 -04001508 fdp = find_module(namestr, "__init__", path_list, buf, sizeof(buf), &fp, NULL);
1509 Py_DECREF(path_list);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (fdp == NULL) {
1511 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1512 PyErr_Clear();
1513 Py_INCREF(m);
Victor Stinnerc9abda02011-03-14 13:33:46 -04001514 return m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 }
1516 else
Victor Stinnerc9abda02011-03-14 13:33:46 -04001517 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 }
Victor Stinnerc9abda02011-03-14 13:33:46 -04001519 m = load_module(namestr, fp, buf, fdp->type, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (fp != NULL)
1521 fclose(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001523}
1524
1525
1526/* Helper to test for built-in module */
1527
1528static int
Victor Stinner95872862011-03-07 18:20:56 +01001529is_builtin(PyObject *name)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001530{
Victor Stinner95872862011-03-07 18:20:56 +01001531 int i, cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
Victor Stinner95872862011-03-07 18:20:56 +01001533 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
1534 if (cmp == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 if (PyImport_Inittab[i].initfunc == NULL)
1536 return -1;
1537 else
1538 return 1;
1539 }
1540 }
1541 return 0;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001542}
1543
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001544
Just van Rossum52e14d62002-12-30 22:08:05 +00001545/* Return an importer object for a sys.path/pkg.__path__ item 'p',
1546 possibly by fetching it from the path_importer_cache dict. If it
Thomas Wouters89f507f2006-12-13 04:49:30 +00001547 wasn't yet cached, traverse path_hooks until a hook is found
Just van Rossum52e14d62002-12-30 22:08:05 +00001548 that can handle the path item. Return None if no hook could;
1549 this tells our caller it should fall back to the builtin
1550 import mechanism. Cache the result in path_importer_cache.
1551 Returns a borrowed reference. */
1552
1553static PyObject *
1554get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 PyObject *p)
Just van Rossum52e14d62002-12-30 22:08:05 +00001556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 PyObject *importer;
1558 Py_ssize_t j, nhooks;
Just van Rossum52e14d62002-12-30 22:08:05 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 /* These conditions are the caller's responsibility: */
1561 assert(PyList_Check(path_hooks));
1562 assert(PyDict_Check(path_importer_cache));
Just van Rossum52e14d62002-12-30 22:08:05 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 nhooks = PyList_Size(path_hooks);
1565 if (nhooks < 0)
1566 return NULL; /* Shouldn't happen */
Just van Rossum52e14d62002-12-30 22:08:05 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 importer = PyDict_GetItem(path_importer_cache, p);
1569 if (importer != NULL)
1570 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 /* set path_importer_cache[p] to None to avoid recursion */
1573 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
1574 return NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +00001575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 for (j = 0; j < nhooks; j++) {
1577 PyObject *hook = PyList_GetItem(path_hooks, j);
1578 if (hook == NULL)
1579 return NULL;
1580 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
1581 if (importer != NULL)
1582 break;
Just van Rossum52e14d62002-12-30 22:08:05 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
1585 return NULL;
1586 }
1587 PyErr_Clear();
1588 }
1589 if (importer == NULL) {
1590 importer = PyObject_CallFunctionObjArgs(
1591 (PyObject *)&PyNullImporter_Type, p, NULL
1592 );
1593 if (importer == NULL) {
1594 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1595 PyErr_Clear();
1596 return Py_None;
1597 }
1598 }
1599 }
1600 if (importer != NULL) {
1601 int err = PyDict_SetItem(path_importer_cache, p, importer);
1602 Py_DECREF(importer);
1603 if (err != 0)
1604 return NULL;
1605 }
1606 return importer;
Just van Rossum52e14d62002-12-30 22:08:05 +00001607}
1608
Christian Heimes9cd17752007-11-18 19:35:23 +00001609PyAPI_FUNC(PyObject *)
1610PyImport_GetImporter(PyObject *path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
1614 if ((path_hooks = PySys_GetObject("path_hooks"))) {
1615 importer = get_path_importer(path_importer_cache,
1616 path_hooks, path);
1617 }
1618 }
1619 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
1620 return importer;
Christian Heimes9cd17752007-11-18 19:35:23 +00001621}
1622
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001623/* Search the path (default sys.path) for a module. Return the
1624 corresponding filedescr struct, and (via return arguments) the
1625 pathname and an open file. Return NULL if the module is not found. */
1626
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001627#ifdef MS_COREDLL
Victor Stinner4d6c1c42011-03-08 23:49:04 +01001628extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **,
1629 PyObject **p_path);
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001630#endif
1631
Victor Stinnerc6963162011-03-12 09:26:54 -05001632static int case_ok(char *, Py_ssize_t, Py_ssize_t, const char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +00001633static int find_init_module(char *); /* Forward */
Just van Rossum52e14d62002-12-30 22:08:05 +00001634static struct filedescr importhookdescr = {"", "", IMP_HOOK};
Guido van Rossum197346f1997-10-31 18:38:52 +00001635
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001636/* Get the path of a module: get its importer and call importer.find_module()
1637 hook, or check if the module if a package (if path/__init__.py exists).
1638
1639 -1: error: a Python error occurred
1640 0: ignore: an error occurred because of invalid data, but the error is not
1641 important enough to be reported.
1642 1: get path: module not found, but *buf contains its path
1643 2: found: *p_fd is the file descriptor (IMP_HOOK or PKG_DIRECTORY)
1644 and *buf is the path */
1645
1646static int
1647find_module_path(char *fullname, const char *name, PyObject *path,
1648 PyObject *path_hooks, PyObject *path_importer_cache,
1649 char *buf, size_t buflen,
1650 PyObject **p_loader, struct filedescr **p_fd)
1651{
1652 PyObject *path_bytes;
1653 const char *base;
1654 Py_ssize_t len;
1655 size_t namelen;
1656 struct stat statbuf;
1657 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
1658
1659 if (PyUnicode_Check(path)) {
1660 path_bytes = PyUnicode_EncodeFSDefault(path);
1661 if (path_bytes == NULL)
1662 return -1;
1663 }
1664 else if (PyBytes_Check(path)) {
1665 Py_INCREF(path);
1666 path_bytes = path;
1667 }
1668 else
1669 return 0;
1670
1671 namelen = strlen(name);
1672 base = PyBytes_AS_STRING(path_bytes);
1673 len = PyBytes_GET_SIZE(path_bytes);
1674 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
1675 Py_DECREF(path_bytes);
1676 return 0; /* Too long */
1677 }
1678 strcpy(buf, base);
1679 Py_DECREF(path_bytes);
1680
1681 if (strlen(buf) != len) {
1682 return 0; /* path_bytes contains '\0' */
1683 }
1684
1685 /* sys.path_hooks import hook */
1686 if (p_loader != NULL) {
1687 PyObject *importer;
1688
1689 importer = get_path_importer(path_importer_cache,
1690 path_hooks, path);
1691 if (importer == NULL) {
1692 return -1;
1693 }
1694 /* Note: importer is a borrowed reference */
1695 if (importer != Py_None) {
1696 PyObject *loader;
1697 loader = PyObject_CallMethod(importer,
1698 "find_module",
1699 "s", fullname);
1700 if (loader == NULL)
1701 return -1; /* error */
1702 if (loader != Py_None) {
1703 /* a loader was found */
1704 *p_loader = loader;
1705 *p_fd = &importhookdescr;
1706 return 2;
1707 }
1708 Py_DECREF(loader);
1709 return 0;
1710 }
1711 }
1712 /* no hook was found, use builtin import */
1713
1714 if (len > 0 && buf[len-1] != SEP
1715#ifdef ALTSEP
1716 && buf[len-1] != ALTSEP
1717#endif
1718 )
1719 buf[len++] = SEP;
1720 strcpy(buf+len, name);
1721 len += namelen;
1722
1723 /* Check for package import (buf holds a directory name,
1724 and there's an __init__ module in that directory */
1725#ifdef HAVE_STAT
1726 if (stat(buf, &statbuf) == 0 && /* it exists */
1727 S_ISDIR(statbuf.st_mode) && /* it's a directory */
1728 case_ok(buf, len, namelen, name)) { /* case matches */
1729 if (find_init_module(buf)) { /* and has __init__.py */
1730 *p_fd = &fd_package;
1731 return 2;
1732 }
1733 else {
1734 int err;
1735 PyObject *unicode = PyUnicode_DecodeFSDefault(buf);
1736 if (unicode == NULL)
1737 return -1;
1738 err = PyErr_WarnFormat(PyExc_ImportWarning, 1,
1739 "Not importing directory '%U': missing __init__.py",
1740 unicode);
1741 Py_DECREF(unicode);
1742 if (err)
1743 return -1;
1744 }
1745 }
1746#endif
1747 return 1;
1748}
1749
1750/* Find a module in search_path_list. For each path, try
1751 find_module_filename() or try each _PyImport_Filetab suffix.
1752
1753 If the module is found, return a file descriptor, write the path in
1754 *p_filename, write the pointer to the file object into *p_fp, and (if
1755 p_loader is not NULL) the loader into *p_loader.
1756
1757 Otherwise, raise an exception and return NULL. */
1758
Victor Stinner37580282011-03-20 01:34:43 +01001759static struct filedescr*
1760find_module_path_list(char *fullname, const char *name,
1761 PyObject *search_path_list, PyObject *path_hooks,
1762 PyObject *path_importer_cache,
1763 char *buf, size_t buflen,
1764 FILE **p_fp, PyObject **p_loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 Py_ssize_t i, npath;
1767 size_t len, namelen;
1768 struct filedescr *fdp = NULL;
1769 char *filemode;
1770 FILE *fp = NULL;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001771#if defined(PYOS_OS2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 size_t saved_len;
1773 size_t saved_namelen;
1774 char *saved_buf = NULL;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001775#endif
Victor Stinner53dc7352011-03-20 01:50:21 +01001776
Victor Stinner37580282011-03-20 01:34:43 +01001777 npath = PyList_Size(search_path_list);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 namelen = strlen(name);
1779 for (i = 0; i < npath; i++) {
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001780 PyObject *path;
1781 int ok;
1782
1783 path = PyList_GetItem(search_path_list, i);
1784 if (path == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 return NULL;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001786
1787 ok = find_module_path(fullname, name, path,
1788 path_hooks, path_importer_cache,
1789 buf, buflen,
1790 p_loader, &fdp);
1791 if (ok < 0)
1792 return NULL;
1793 if (ok == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 continue;
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001795 if (ok == 2)
1796 return fdp;
Amaury Forgeot d'Arcf1ca0b12008-06-11 17:40:47 +00001797
Victor Stinnerd68c2cf2011-03-12 16:02:28 -05001798 len = strlen(buf);
Andrew MacIntyred9400542002-02-26 11:41:34 +00001799#if defined(PYOS_OS2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 /* take a snapshot of the module spec for restoration
1801 * after the 8 character DLL hackery
1802 */
1803 saved_buf = strdup(buf);
1804 saved_len = len;
1805 saved_namelen = namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001806#endif /* PYOS_OS2 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum04110fb2007-08-24 16:32:05 +00001808#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 /* OS/2 limits DLLs to 8 character names (w/o
1810 extension)
1811 * so if the name is longer than that and its a
1812 * dynamically loaded module we're going to try,
1813 * truncate the name before trying
1814 */
Victor Stinnerc6963162011-03-12 09:26:54 -05001815 if (strlen(name) > 8) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 /* is this an attempt to load a C extension? */
1817 const struct filedescr *scan;
1818 scan = _PyImport_DynLoadFiletab;
1819 while (scan->suffix != NULL) {
1820 if (!strcmp(scan->suffix, fdp->suffix))
1821 break;
1822 else
1823 scan++;
1824 }
1825 if (scan->suffix != NULL) {
1826 /* yes, so truncate the name */
1827 namelen = 8;
Victor Stinnerc6963162011-03-12 09:26:54 -05001828 len -= strlen(name) - namelen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 buf[len] = '\0';
1830 }
1831 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001832#endif /* PYOS_OS2 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 strcpy(buf+len, fdp->suffix);
1834 if (Py_VerboseFlag > 1)
1835 PySys_WriteStderr("# trying %s\n", buf);
1836 filemode = fdp->mode;
1837 if (filemode[0] == 'U')
1838 filemode = "r" PY_STDIOTEXTMODE;
1839 fp = fopen(buf, filemode);
1840 if (fp != NULL) {
1841 if (case_ok(buf, len, namelen, name))
1842 break;
1843 else { /* continue search */
1844 fclose(fp);
1845 fp = NULL;
1846 }
1847 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001848#if defined(PYOS_OS2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 /* restore the saved snapshot */
1850 strcpy(buf, saved_buf);
1851 len = saved_len;
1852 namelen = saved_namelen;
Andrew MacIntyred9400542002-02-26 11:41:34 +00001853#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001855#if defined(PYOS_OS2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 /* don't need/want the module name snapshot anymore */
1857 if (saved_buf)
1858 {
1859 free(saved_buf);
1860 saved_buf = NULL;
1861 }
Andrew MacIntyred9400542002-02-26 11:41:34 +00001862#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (fp != NULL)
1864 break;
1865 }
1866 if (fp == NULL) {
1867 PyErr_Format(PyExc_ImportError,
1868 "No module named %.200s", name);
1869 return NULL;
1870 }
1871 *p_fp = fp;
1872 return fdp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001873}
1874
Victor Stinner37580282011-03-20 01:34:43 +01001875/* Find a module:
1876
1877 - try find_module() of each sys.meta_path hook
1878 - try find_frozen()
1879 - try is_builtin()
1880 - try _PyWin_FindRegisteredModule() (Windows only)
1881 - otherwise, call find_module_path_list() with search_path_list (if not
1882 NULL) or sys.path
1883
1884 Return:
1885
1886 - &fd_builtin (C_BUILTIN) if it is a builtin
1887 - &fd_frozen (PY_FROZEN) if it is frozen
1888 - &fd_package (PKG_DIRECTORY) and write the filename into *buf
1889 if it is a package
1890 - &importhookdescr (IMP_HOOK) and write the loader into *p_loader if a
1891 importer loader was found
1892 - a file descriptor (PY_SOURCE, PY_COMPILED, C_EXTENSION, PY_RESOURCE or
1893 PY_CODERESOURCE: see _PyImport_Filetab), write the filename into
1894 *buf and the pointer to the open file into *p_fp
1895 - NULL on error
1896
1897 By default, write an empty string into *buf, and *p_fp and *p_loader (if
1898 set) are set to NULL. Eg. *buf is an empty string for a builtin package. */
1899
1900static struct filedescr *
1901find_module(char *fullname, const char *name, PyObject *search_path_list,
1902 char *buf, size_t buflen, FILE **p_fp, PyObject **p_loader)
1903{
1904 Py_ssize_t i, npath;
1905 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
1906 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
1907 PyObject *path_hooks, *path_importer_cache;
1908 PyObject *fullname_obj, *nameobj;
1909
1910 *buf = '\0';
1911 *p_fp = NULL;
1912 if (p_loader != NULL)
1913 *p_loader = NULL;
1914
1915 if (strlen(name) > MAXPATHLEN) {
1916 PyErr_SetString(PyExc_OverflowError,
1917 "module name is too long");
1918 return NULL;
1919 }
1920
1921 /* sys.meta_path import hook */
1922 if (p_loader != NULL) {
1923 PyObject *meta_path;
1924
1925 meta_path = PySys_GetObject("meta_path");
1926 if (meta_path == NULL || !PyList_Check(meta_path)) {
1927 PyErr_SetString(PyExc_ImportError,
1928 "sys.meta_path must be a list of "
1929 "import hooks");
1930 return NULL;
1931 }
1932 Py_INCREF(meta_path); /* zap guard */
1933 npath = PyList_Size(meta_path);
1934 for (i = 0; i < npath; i++) {
1935 PyObject *loader;
1936 PyObject *hook = PyList_GetItem(meta_path, i);
1937 loader = PyObject_CallMethod(hook, "find_module",
1938 "sO", fullname,
1939 search_path_list != NULL ?
1940 search_path_list : Py_None);
1941 if (loader == NULL) {
1942 Py_DECREF(meta_path);
1943 return NULL; /* true error */
1944 }
1945 if (loader != Py_None) {
1946 /* a loader was found */
1947 *p_loader = loader;
1948 Py_DECREF(meta_path);
1949 return &importhookdescr;
1950 }
1951 Py_DECREF(loader);
1952 }
1953 Py_DECREF(meta_path);
1954 }
1955
1956 if (fullname != NULL) {
1957 fullname_obj = PyUnicode_FromString(fullname);
1958 if (fullname == NULL)
1959 return NULL;
1960 if (find_frozen(fullname_obj) != NULL) {
1961 Py_DECREF(fullname_obj);
1962 strcpy(buf, fullname);
1963 return &fd_frozen;
1964 }
1965 Py_DECREF(fullname_obj);
1966 }
1967
1968 if (search_path_list == NULL) {
1969#ifdef MS_COREDLL
1970 FILE *fp;
1971 struct filedescr *fdp;
1972 PyObject *filename, *filename_bytes;
1973#endif
1974 nameobj = PyUnicode_FromString(name);
1975 if (nameobj == NULL)
1976 return NULL;
1977 if (is_builtin(nameobj)) {
1978 Py_DECREF(nameobj);
1979 strcpy(buf, name);
1980 return &fd_builtin;
1981 }
1982#ifdef MS_COREDLL
1983 fp = _PyWin_FindRegisteredModule(nameobj, &fdp, &filename);
1984 if (fp != NULL) {
1985 Py_DECREF(nameobj);
1986 filename_bytes = PyUnicode_EncodeFSDefault(filename);
1987 Py_DECREF(filename);
1988 if (filename_bytes == NULL)
1989 return NULL;
1990 strncpy(buf, PyBytes_AS_STRING(filename_bytes), buflen);
1991 buf[buflen-1] = '\0';
1992 Py_DECREF(filename_bytes);
1993 *p_fp = fp;
1994 return fdp;
1995 }
1996 else if (PyErr_Occurred())
1997 return NULL;
1998#endif
1999 Py_DECREF(nameobj);
2000 search_path_list = PySys_GetObject("path");
2001 }
2002
2003 if (search_path_list == NULL || !PyList_Check(search_path_list)) {
2004 PyErr_SetString(PyExc_ImportError,
2005 "sys.path must be a list of directory names");
2006 return NULL;
2007 }
2008
2009 path_hooks = PySys_GetObject("path_hooks");
2010 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
2011 PyErr_SetString(PyExc_ImportError,
2012 "sys.path_hooks must be a list of "
2013 "import hooks");
2014 return NULL;
2015 }
2016 path_importer_cache = PySys_GetObject("path_importer_cache");
2017 if (path_importer_cache == NULL ||
2018 !PyDict_Check(path_importer_cache)) {
2019 PyErr_SetString(PyExc_ImportError,
2020 "sys.path_importer_cache must be a dict");
2021 return NULL;
2022 }
2023
2024 return find_module_path_list(fullname, name,
2025 search_path_list, path_hooks,
2026 path_importer_cache,
2027 buf, buflen,
2028 p_fp, p_loader);
2029}
2030
Martin v. Löwis18e16552006-02-15 17:27:45 +00002031/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
Tim Petersd1e87a82001-03-01 18:12:00 +00002032 * The arguments here are tricky, best shown by example:
2033 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
2034 * ^ ^ ^ ^
2035 * |--------------------- buf ---------------------|
2036 * |------------------- len ------------------|
2037 * |------ name -------|
2038 * |----- namelen -----|
2039 * buf is the full path, but len only counts up to (& exclusive of) the
2040 * extension. name is the module name, also exclusive of extension.
2041 *
2042 * We've already done a successful stat() or fopen() on buf, so know that
2043 * there's some match, possibly case-insensitive.
2044 *
Tim Peters50d8d372001-02-28 05:34:27 +00002045 * case_ok() is to return 1 if there's a case-sensitive match for
2046 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
2047 * exists.
Tim Petersd1e87a82001-03-01 18:12:00 +00002048 *
Tim Peters50d8d372001-02-28 05:34:27 +00002049 * case_ok() is used to implement case-sensitive import semantics even
2050 * on platforms with case-insensitive filesystems. It's trivial to implement
2051 * for case-sensitive filesystems. It's pretty much a cross-platform
2052 * nightmare for systems with case-insensitive filesystems.
2053 */
Guido van Rossum0980bd91998-02-13 17:18:36 +00002054
Tim Peters50d8d372001-02-28 05:34:27 +00002055/* First we may need a pile of platform-specific header files; the sequence
2056 * of #if's here should match the sequence in the body of case_ok().
2057 */
Jason Tishler7961aa62005-05-20 00:56:54 +00002058#if defined(MS_WINDOWS)
Guido van Rossum0980bd91998-02-13 17:18:36 +00002059#include <windows.h>
Guido van Rossum4c3f57c2001-01-10 20:40:46 +00002060
Tim Peters50d8d372001-02-28 05:34:27 +00002061#elif defined(DJGPP)
2062#include <dir.h>
2063
Jason Tishler7961aa62005-05-20 00:56:54 +00002064#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Tim Peters430f5d42001-03-01 01:30:56 +00002065#include <sys/types.h>
2066#include <dirent.h>
2067
Andrew MacIntyred9400542002-02-26 11:41:34 +00002068#elif defined(PYOS_OS2)
2069#define INCL_DOS
2070#define INCL_DOSERRORS
2071#define INCL_NOPMAPI
2072#include <os2.h>
Tim Peters50d8d372001-02-28 05:34:27 +00002073#endif
2074
Guido van Rossum0980bd91998-02-13 17:18:36 +00002075static int
Victor Stinnerc6963162011-03-12 09:26:54 -05002076case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, const char *name)
Guido van Rossum0980bd91998-02-13 17:18:36 +00002077{
Tim Peters50d8d372001-02-28 05:34:27 +00002078/* Pick a platform-specific implementation; the sequence of #if's here should
2079 * match the sequence just above.
2080 */
2081
Jason Tishler7961aa62005-05-20 00:56:54 +00002082/* MS_WINDOWS */
2083#if defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 WIN32_FIND_DATA data;
2085 HANDLE h;
Tim Peters50d8d372001-02-28 05:34:27 +00002086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 if (Py_GETENV("PYTHONCASEOK") != NULL)
2088 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 h = FindFirstFile(buf, &data);
2091 if (h == INVALID_HANDLE_VALUE) {
2092 PyErr_Format(PyExc_NameError,
2093 "Can't find file for module %.100s\n(filename %.300s)",
2094 name, buf);
2095 return 0;
2096 }
2097 FindClose(h);
2098 return strncmp(data.cFileName, name, namelen) == 0;
Tim Peters50d8d372001-02-28 05:34:27 +00002099
2100/* DJGPP */
2101#elif defined(DJGPP)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 struct ffblk ffblk;
2103 int done;
Tim Peters50d8d372001-02-28 05:34:27 +00002104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 if (Py_GETENV("PYTHONCASEOK") != NULL)
2106 return 1;
Tim Peters50d8d372001-02-28 05:34:27 +00002107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
2109 if (done) {
2110 PyErr_Format(PyExc_NameError,
2111 "Can't find file for module %.100s\n(filename %.300s)",
2112 name, buf);
2113 return 0;
2114 }
2115 return strncmp(ffblk.ff_name, name, namelen) == 0;
Guido van Rossum0980bd91998-02-13 17:18:36 +00002116
Jason Tishler7961aa62005-05-20 00:56:54 +00002117/* new-fangled macintosh (macosx) or Cygwin */
2118#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 DIR *dirp;
2120 struct dirent *dp;
2121 char dirname[MAXPATHLEN + 1];
2122 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
Tim Peters430f5d42001-03-01 01:30:56 +00002123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 if (Py_GETENV("PYTHONCASEOK") != NULL)
2125 return 1;
Tim Petersdbe6ebb2001-03-01 08:47:29 +00002126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 /* Copy the dir component into dirname; substitute "." if empty */
2128 if (dirlen <= 0) {
2129 dirname[0] = '.';
2130 dirname[1] = '\0';
2131 }
2132 else {
2133 assert(dirlen <= MAXPATHLEN);
2134 memcpy(dirname, buf, dirlen);
2135 dirname[dirlen] = '\0';
2136 }
2137 /* Open the directory and search the entries for an exact match. */
2138 dirp = opendir(dirname);
2139 if (dirp) {
2140 char *nameWithExt = buf + len - namelen;
2141 while ((dp = readdir(dirp)) != NULL) {
2142 const int thislen =
Tim Peters430f5d42001-03-01 01:30:56 +00002143#ifdef _DIRENT_HAVE_D_NAMELEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 dp->d_namlen;
Tim Peters430f5d42001-03-01 01:30:56 +00002145#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 strlen(dp->d_name);
Tim Peters430f5d42001-03-01 01:30:56 +00002147#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 if (thislen >= namelen &&
2149 strcmp(dp->d_name, nameWithExt) == 0) {
2150 (void)closedir(dirp);
2151 return 1; /* Found */
2152 }
2153 }
2154 (void)closedir(dirp);
2155 }
2156 return 0 ; /* Not found */
Tim Peters430f5d42001-03-01 01:30:56 +00002157
Andrew MacIntyred9400542002-02-26 11:41:34 +00002158/* OS/2 */
2159#elif defined(PYOS_OS2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 HDIR hdir = 1;
2161 ULONG srchcnt = 1;
2162 FILEFINDBUF3 ffbuf;
2163 APIRET rc;
Andrew MacIntyred9400542002-02-26 11:41:34 +00002164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 if (Py_GETENV("PYTHONCASEOK") != NULL)
2166 return 1;
Andrew MacIntyred9400542002-02-26 11:41:34 +00002167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 rc = DosFindFirst(buf,
2169 &hdir,
2170 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
2171 &ffbuf, sizeof(ffbuf),
2172 &srchcnt,
2173 FIL_STANDARD);
2174 if (rc != NO_ERROR)
2175 return 0;
2176 return strncmp(ffbuf.achName, name, namelen) == 0;
Andrew MacIntyred9400542002-02-26 11:41:34 +00002177
Tim Peters50d8d372001-02-28 05:34:27 +00002178/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
2179#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 return 1;
Guido van Rossum0980bd91998-02-13 17:18:36 +00002181
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00002182#endif
Tim Peters50d8d372001-02-28 05:34:27 +00002183}
Guido van Rossum4d1b3b91998-02-13 23:27:59 +00002184
Guido van Rossum197346f1997-10-31 18:38:52 +00002185#ifdef HAVE_STAT
Victor Stinner4f4402c2010-08-14 14:50:26 +00002186
Guido van Rossum197346f1997-10-31 18:38:52 +00002187/* Helper to look for __init__.py or __init__.py[co] in potential package */
2188static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002189find_init_module(char *buf)
Guido van Rossum197346f1997-10-31 18:38:52 +00002190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 const size_t save_len = strlen(buf);
2192 size_t i = save_len;
2193 char *pname; /* pointer to start of __init__ */
2194 struct stat statbuf;
Guido van Rossum197346f1997-10-31 18:38:52 +00002195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196/* For calling case_ok(buf, len, namelen, name):
2197 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
2198 * ^ ^ ^ ^
2199 * |--------------------- buf ---------------------|
2200 * |------------------- len ------------------|
2201 * |------ name -------|
2202 * |----- namelen -----|
Tim Peters0f9431f2001-07-05 03:47:53 +00002203 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 if (save_len + 13 >= MAXPATHLEN)
2205 return 0;
2206 buf[i++] = SEP;
2207 pname = buf + i;
2208 strcpy(pname, "__init__.py");
2209 if (stat(buf, &statbuf) == 0) {
2210 if (case_ok(buf,
2211 save_len + 9, /* len("/__init__") */
2212 8, /* len("__init__") */
2213 pname)) {
2214 buf[save_len] = '\0';
2215 return 1;
2216 }
2217 }
2218 i += strlen(pname);
2219 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
2220 if (stat(buf, &statbuf) == 0) {
2221 if (case_ok(buf,
2222 save_len + 9, /* len("/__init__") */
2223 8, /* len("__init__") */
2224 pname)) {
2225 buf[save_len] = '\0';
2226 return 1;
2227 }
2228 }
2229 buf[save_len] = '\0';
2230 return 0;
Guido van Rossum197346f1997-10-31 18:38:52 +00002231}
Guido van Rossum48a680c2001-03-02 06:34:14 +00002232
Guido van Rossum197346f1997-10-31 18:38:52 +00002233#endif /* HAVE_STAT */
2234
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002235
Victor Stinner95872862011-03-07 18:20:56 +01002236static int init_builtin(PyObject *); /* Forward */
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002237
Victor Stinner44c6c152010-08-09 00:59:10 +00002238static PyObject*
Victor Stinner95872862011-03-07 18:20:56 +01002239load_builtin(PyObject *name, int type)
Victor Stinner44c6c152010-08-09 00:59:10 +00002240{
2241 PyObject *m, *modules;
2242 int err;
2243
Victor Stinner44c6c152010-08-09 00:59:10 +00002244 if (type == C_BUILTIN)
2245 err = init_builtin(name);
2246 else
Victor Stinner95872862011-03-07 18:20:56 +01002247 err = PyImport_ImportFrozenModuleObject(name);
Victor Stinner44c6c152010-08-09 00:59:10 +00002248 if (err < 0)
2249 return NULL;
2250 if (err == 0) {
2251 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +01002252 "Purported %s module %R not found",
2253 type == C_BUILTIN ? "builtin" : "frozen",
Victor Stinner44c6c152010-08-09 00:59:10 +00002254 name);
2255 return NULL;
2256 }
2257
2258 modules = PyImport_GetModuleDict();
Victor Stinner95872862011-03-07 18:20:56 +01002259 m = PyDict_GetItem(modules, name);
Victor Stinner44c6c152010-08-09 00:59:10 +00002260 if (m == NULL) {
2261 PyErr_Format(
2262 PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +01002263 "%s module %R not properly initialized",
2264 type == C_BUILTIN ? "builtin" : "frozen",
Victor Stinner44c6c152010-08-09 00:59:10 +00002265 name);
2266 return NULL;
2267 }
2268 Py_INCREF(m);
2269 return m;
2270}
2271
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002272/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002273 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002274
Guido van Rossum79f25d91997-04-29 20:08:16 +00002275static PyObject *
Alexandre Vassalottie223eb82009-07-29 20:12:15 +00002276load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 PyObject *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 /* First check that there's an open file (if we need one) */
2281 switch (type) {
2282 case PY_SOURCE:
2283 case PY_COMPILED:
2284 if (fp == NULL) {
2285 PyErr_Format(PyExc_ValueError,
2286 "file object required for import (type code %d)",
2287 type);
2288 return NULL;
2289 }
2290 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 switch (type) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 case PY_SOURCE:
2295 m = load_source_module(name, pathname, fp);
2296 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 case PY_COMPILED:
2299 m = load_compiled_module(name, pathname, fp);
2300 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002301
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002302#ifdef HAVE_DYNAMIC_LOADING
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002303 case C_EXTENSION: {
2304 PyObject *nameobj, *pathobj;
2305 nameobj = PyUnicode_FromString(name);
2306 if (nameobj == NULL)
2307 return NULL;
2308 pathobj = PyUnicode_DecodeFSDefault(pathname);
2309 if (pathobj == NULL) {
2310 Py_DECREF(nameobj);
2311 return NULL;
2312 }
2313 m = _PyImport_LoadDynamicModule(nameobj, pathobj, fp);
2314 Py_DECREF(nameobj);
2315 Py_DECREF(pathobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 break;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002317 }
Guido van Rossum96a8fb71999-12-22 14:09:35 +00002318#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002319
Victor Stinnerc9abda02011-03-14 13:33:46 -04002320 case PKG_DIRECTORY: {
2321 PyObject *nameobj, *pathobj;
2322 nameobj = PyUnicode_FromString(name);
2323 if (nameobj == NULL)
2324 return NULL;
2325 pathobj = PyUnicode_DecodeFSDefault(pathname);
2326 if (pathobj == NULL) {
2327 Py_DECREF(nameobj);
2328 return NULL;
2329 }
2330 m = load_package(nameobj, pathobj);
2331 Py_DECREF(nameobj);
2332 Py_DECREF(pathobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 break;
Victor Stinnerc9abda02011-03-14 13:33:46 -04002334 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 case C_BUILTIN:
Victor Stinner95872862011-03-07 18:20:56 +01002337 case PY_FROZEN: {
2338 PyObject *nameobj = PyUnicode_FromString(name);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04002339 if (nameobj == NULL)
2340 return NULL;
2341 m = load_builtin(nameobj, type);
2342 Py_DECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 break;
Victor Stinner95872862011-03-07 18:20:56 +01002344 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 case IMP_HOOK: {
2347 if (loader == NULL) {
2348 PyErr_SetString(PyExc_ImportError,
2349 "import hook without loader");
2350 return NULL;
2351 }
2352 m = PyObject_CallMethod(loader, "load_module", "s", name);
2353 break;
2354 }
Just van Rossum52e14d62002-12-30 22:08:05 +00002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 default:
2357 PyErr_Format(PyExc_ImportError,
2358 "Don't know how to import %.200s (type code %d)",
2359 name, type);
2360 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002365}
2366
2367
2368/* Initialize a built-in module.
Thomas Wouters89f507f2006-12-13 04:49:30 +00002369 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002370 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +00002371
Guido van Rossum7f133ed1991-02-19 12:23:57 +00002372static int
Victor Stinner95872862011-03-07 18:20:56 +01002373init_builtin(PyObject *name)
Guido van Rossum7f133ed1991-02-19 12:23:57 +00002374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 struct _inittab *p;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002376
Victor Stinner95872862011-03-07 18:20:56 +01002377 if (_PyImport_FindExtensionObject(name, name) != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 return 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 for (p = PyImport_Inittab; p->name != NULL; p++) {
2381 PyObject *mod;
Victor Stinner95872862011-03-07 18:20:56 +01002382 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 if (p->initfunc == NULL) {
2384 PyErr_Format(PyExc_ImportError,
Victor Stinner95872862011-03-07 18:20:56 +01002385 "Cannot re-init internal module %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 name);
2387 return -1;
2388 }
2389 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +01002390 PySys_FormatStderr("import %U # builtin\n", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 mod = (*p->initfunc)();
2392 if (mod == 0)
2393 return -1;
Victor Stinner95872862011-03-07 18:20:56 +01002394 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 return -1;
2396 /* FixupExtension has put the module into sys.modules,
2397 so we can release our own reference. */
2398 Py_DECREF(mod);
2399 return 1;
2400 }
2401 }
2402 return 0;
Guido van Rossum7f133ed1991-02-19 12:23:57 +00002403}
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002404
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002405
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002406/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002407
Guido van Rossumcfd0a221996-06-17 17:06:34 +00002408static struct _frozen *
Victor Stinner53dc7352011-03-20 01:50:21 +01002409find_frozen(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002412
Victor Stinner53dc7352011-03-20 01:50:21 +01002413 if (name == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 return NULL;
Benjamin Petersond968e272008-11-05 22:48:33 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 for (p = PyImport_FrozenModules; ; p++) {
2417 if (p->name == NULL)
2418 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01002419 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 break;
2421 }
2422 return p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002423}
2424
Guido van Rossum79f25d91997-04-29 20:08:16 +00002425static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01002426get_frozen_object(PyObject *name)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 struct _frozen *p = find_frozen(name);
2429 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 if (p == NULL) {
2432 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002433 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 name);
2435 return NULL;
2436 }
2437 if (p->code == NULL) {
2438 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002439 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 name);
2441 return NULL;
2442 }
2443 size = p->size;
2444 if (size < 0)
2445 size = -size;
2446 return PyMarshal_ReadObjectFromString((char *)p->code, size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002447}
2448
Brett Cannon8d110132009-03-15 02:20:16 +00002449static PyObject *
Victor Stinner53dc7352011-03-20 01:50:21 +01002450is_frozen_package(PyObject *name)
Brett Cannon8d110132009-03-15 02:20:16 +00002451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 struct _frozen *p = find_frozen(name);
2453 int size;
Brett Cannon8d110132009-03-15 02:20:16 +00002454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 if (p == NULL) {
2456 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002457 "No such frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 name);
2459 return NULL;
2460 }
Brett Cannon8d110132009-03-15 02:20:16 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 size = p->size;
Brett Cannon8d110132009-03-15 02:20:16 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 if (size < 0)
2465 Py_RETURN_TRUE;
2466 else
2467 Py_RETURN_FALSE;
Brett Cannon8d110132009-03-15 02:20:16 +00002468}
2469
2470
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002471/* Initialize a frozen module.
Brett Cannon3c2ac442009-03-08 20:49:47 +00002472 Return 1 for success, 0 if the module is not found, and -1 with
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002473 an exception set if the initialization failed.
2474 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +00002475
2476int
Victor Stinner53dc7352011-03-20 01:50:21 +01002477PyImport_ImportFrozenModuleObject(PyObject *name)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002478{
Victor Stinner53dc7352011-03-20 01:50:21 +01002479 struct _frozen *p;
2480 PyObject *co, *m, *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 int ispackage;
2482 int size;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00002483
Victor Stinner53dc7352011-03-20 01:50:21 +01002484 p = find_frozen(name);
2485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 if (p == NULL)
2487 return 0;
2488 if (p->code == NULL) {
2489 PyErr_Format(PyExc_ImportError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002490 "Excluded frozen object named %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 name);
2492 return -1;
2493 }
2494 size = p->size;
2495 ispackage = (size < 0);
2496 if (ispackage)
2497 size = -size;
2498 if (Py_VerboseFlag)
Victor Stinner53dc7352011-03-20 01:50:21 +01002499 PySys_FormatStderr("import %U # frozen%s\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 name, ispackage ? " package" : "");
2501 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
2502 if (co == NULL)
2503 return -1;
2504 if (!PyCode_Check(co)) {
2505 PyErr_Format(PyExc_TypeError,
Victor Stinner53dc7352011-03-20 01:50:21 +01002506 "frozen object %R is not a code object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 name);
2508 goto err_return;
2509 }
2510 if (ispackage) {
2511 /* Set __path__ to the package name */
Victor Stinner53dc7352011-03-20 01:50:21 +01002512 PyObject *d, *l;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 int err;
Victor Stinner53dc7352011-03-20 01:50:21 +01002514 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 if (m == NULL)
2516 goto err_return;
2517 d = PyModule_GetDict(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 l = PyList_New(1);
2519 if (l == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 goto err_return;
2521 }
Victor Stinner53dc7352011-03-20 01:50:21 +01002522 Py_INCREF(name);
2523 PyList_SET_ITEM(l, 0, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 err = PyDict_SetItemString(d, "__path__", l);
2525 Py_DECREF(l);
2526 if (err != 0)
2527 goto err_return;
2528 }
Victor Stinner53dc7352011-03-20 01:50:21 +01002529 path = PyUnicode_FromString("<frozen>");
2530 if (path == NULL)
2531 goto err_return;
2532 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
2533 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 if (m == NULL)
2535 goto err_return;
2536 Py_DECREF(co);
2537 Py_DECREF(m);
2538 return 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002539err_return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 Py_DECREF(co);
2541 return -1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00002542}
Guido van Rossum74e6a111994-08-29 12:54:38 +00002543
Victor Stinner53dc7352011-03-20 01:50:21 +01002544int
2545PyImport_ImportFrozenModule(char *name)
2546{
2547 PyObject *nameobj;
2548 int ret;
2549 nameobj = PyUnicode_InternFromString(name);
2550 if (nameobj == NULL)
2551 return -1;
2552 ret = PyImport_ImportFrozenModuleObject(nameobj);
2553 Py_DECREF(nameobj);
2554 return ret;
2555}
2556
Guido van Rossum74e6a111994-08-29 12:54:38 +00002557
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002558/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +00002559 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +00002560
Guido van Rossum79f25d91997-04-29 20:08:16 +00002561PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002562PyImport_ImportModule(const char *name)
Guido van Rossum74e6a111994-08-29 12:54:38 +00002563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 PyObject *pname;
2565 PyObject *result;
Marc-André Lemburg3c61c352001-02-09 19:40:15 +00002566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 pname = PyUnicode_FromString(name);
2568 if (pname == NULL)
2569 return NULL;
2570 result = PyImport_Import(pname);
2571 Py_DECREF(pname);
2572 return result;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002573}
2574
Christian Heimes072c0f12008-01-03 23:01:04 +00002575/* Import a module without blocking
2576 *
2577 * At first it tries to fetch the module from sys.modules. If the module was
2578 * never loaded before it loads it with PyImport_ImportModule() unless another
2579 * thread holds the import lock. In the latter case the function raises an
2580 * ImportError instead of blocking.
2581 *
2582 * Returns the module object with incremented ref count.
2583 */
2584PyObject *
2585PyImport_ImportModuleNoBlock(const char *name)
2586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 PyObject *result;
2588 PyObject *modules;
2589 long me;
Christian Heimes072c0f12008-01-03 23:01:04 +00002590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 /* Try to get the module from sys.modules[name] */
2592 modules = PyImport_GetModuleDict();
2593 if (modules == NULL)
2594 return NULL;
Christian Heimes072c0f12008-01-03 23:01:04 +00002595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 result = PyDict_GetItemString(modules, name);
2597 if (result != NULL) {
2598 Py_INCREF(result);
2599 return result;
2600 }
2601 else {
2602 PyErr_Clear();
2603 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002604#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 /* check the import lock
2606 * me might be -1 but I ignore the error here, the lock function
2607 * takes care of the problem */
2608 me = PyThread_get_thread_ident();
2609 if (import_lock_thread == -1 || import_lock_thread == me) {
2610 /* no thread or me is holding the lock */
2611 return PyImport_ImportModule(name);
2612 }
2613 else {
2614 PyErr_Format(PyExc_ImportError,
2615 "Failed to import %.200s because the import lock"
2616 "is held by another thread.",
2617 name);
2618 return NULL;
2619 }
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002620#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 return PyImport_ImportModule(name);
Benjamin Peterson3e4f0552008-09-02 00:31:15 +00002622#endif
Christian Heimes072c0f12008-01-03 23:01:04 +00002623}
2624
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002625/* Forward declarations for helper routines */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002626static PyObject *get_parent(PyObject *globals, char *buf,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 Py_ssize_t *p_buflen, int level);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002628static PyObject *load_next(PyObject *mod, PyObject *altmod,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 char **p_name, char *buf, Py_ssize_t *p_buflen);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002630static int mark_miss(char *name);
2631static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 char *buf, Py_ssize_t buflen, int recursive);
Tim Petersdbd9ba62000-07-09 03:09:57 +00002633static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002634
2635/* The Magnum Opus of dotted-name import :-) */
2636
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002637static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002638import_module_level(char *name, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 PyObject *fromlist, int level)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00002640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 char buf[MAXPATHLEN+1];
2642 Py_ssize_t buflen = 0;
2643 PyObject *parent, *head, *next, *tail;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 if (strchr(name, '/') != NULL
Christian Heimes454f37b2008-01-10 00:10:02 +00002646#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 || strchr(name, '\\') != NULL
Christian Heimes454f37b2008-01-10 00:10:02 +00002648#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 ) {
2650 PyErr_SetString(PyExc_ImportError,
2651 "Import by filename is not supported.");
2652 return NULL;
2653 }
Christian Heimes454f37b2008-01-10 00:10:02 +00002654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 parent = get_parent(globals, buf, &buflen, level);
2656 if (parent == NULL)
2657 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002658
Benjamin Peterson556d8002010-06-27 22:37:28 +00002659 head = load_next(parent, level < 0 ? Py_None : parent, &name, buf,
2660 &buflen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 if (head == NULL)
2662 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 tail = head;
2665 Py_INCREF(tail);
2666 while (name) {
2667 next = load_next(tail, tail, &name, buf, &buflen);
2668 Py_DECREF(tail);
2669 if (next == NULL) {
2670 Py_DECREF(head);
2671 return NULL;
2672 }
2673 tail = next;
2674 }
2675 if (tail == Py_None) {
2676 /* If tail is Py_None, both get_parent and load_next found
2677 an empty module name: someone called __import__("") or
2678 doctored faulty bytecode */
2679 Py_DECREF(tail);
2680 Py_DECREF(head);
2681 PyErr_SetString(PyExc_ValueError,
2682 "Empty module name");
2683 return NULL;
2684 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 if (fromlist != NULL) {
2687 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
2688 fromlist = NULL;
2689 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 if (fromlist == NULL) {
2692 Py_DECREF(tail);
2693 return head;
2694 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 Py_DECREF(head);
2697 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
2698 Py_DECREF(tail);
2699 return NULL;
2700 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 return tail;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002703}
2704
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002705PyObject *
2706PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 PyObject *fromlist, int level)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 PyObject *result;
2710 _PyImport_AcquireLock();
2711 result = import_module_level(name, globals, locals, fromlist, level);
2712 if (_PyImport_ReleaseLock() < 0) {
2713 Py_XDECREF(result);
2714 PyErr_SetString(PyExc_RuntimeError,
2715 "not holding the import lock");
2716 return NULL;
2717 }
2718 return result;
Guido van Rossum75acc9c1998-03-03 22:26:50 +00002719}
2720
Fred Drake87590902004-05-28 20:21:36 +00002721/* Return the package that an import is being performed in. If globals comes
2722 from the module foo.bar.bat (not itself a package), this returns the
2723 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002724 the package's entry in sys.modules is returned, as a borrowed reference.
Fred Drake87590902004-05-28 20:21:36 +00002725
2726 The *name* of the returned package is returned in buf, with the length of
2727 the name in *p_buflen.
2728
2729 If globals doesn't come from a package or a module in a package, or a
2730 corresponding entry is not found in sys.modules, Py_None is returned.
2731*/
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002732static PyObject *
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002733get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 static PyObject *namestr = NULL;
2736 static PyObject *pathstr = NULL;
2737 static PyObject *pkgstr = NULL;
2738 PyObject *pkgname, *modname, *modpath, *modules, *parent;
2739 int orig_level = level;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 if (globals == NULL || !PyDict_Check(globals) || !level)
2742 return Py_None;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 if (namestr == NULL) {
2745 namestr = PyUnicode_InternFromString("__name__");
2746 if (namestr == NULL)
2747 return NULL;
2748 }
2749 if (pathstr == NULL) {
2750 pathstr = PyUnicode_InternFromString("__path__");
2751 if (pathstr == NULL)
2752 return NULL;
2753 }
2754 if (pkgstr == NULL) {
2755 pkgstr = PyUnicode_InternFromString("__package__");
2756 if (pkgstr == NULL)
2757 return NULL;
2758 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 *buf = '\0';
2761 *p_buflen = 0;
2762 pkgname = PyDict_GetItem(globals, pkgstr);
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 if ((pkgname != NULL) && (pkgname != Py_None)) {
2765 /* __package__ is set, so use it */
2766 char *pkgname_str;
2767 Py_ssize_t len;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 if (!PyUnicode_Check(pkgname)) {
2770 PyErr_SetString(PyExc_ValueError,
2771 "__package__ set to non-string");
2772 return NULL;
2773 }
2774 pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len);
2775 if (len == 0) {
2776 if (level > 0) {
2777 PyErr_SetString(PyExc_ValueError,
2778 "Attempted relative import in non-package");
2779 return NULL;
2780 }
2781 return Py_None;
2782 }
2783 if (len > MAXPATHLEN) {
2784 PyErr_SetString(PyExc_ValueError,
2785 "Package name too long");
2786 return NULL;
2787 }
2788 strcpy(buf, pkgname_str);
2789 } else {
2790 /* __package__ not set, so figure it out and set it */
2791 modname = PyDict_GetItem(globals, namestr);
2792 if (modname == NULL || !PyUnicode_Check(modname))
2793 return Py_None;
Brett Cannon9a5b25a2010-03-01 02:09:17 +00002794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 modpath = PyDict_GetItem(globals, pathstr);
2796 if (modpath != NULL) {
2797 /* __path__ is set, so modname is already the package name */
2798 char *modname_str;
2799 Py_ssize_t len;
2800 int error;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 modname_str = _PyUnicode_AsStringAndSize(modname, &len);
2803 if (len > MAXPATHLEN) {
2804 PyErr_SetString(PyExc_ValueError,
2805 "Module name too long");
2806 return NULL;
2807 }
2808 strcpy(buf, modname_str);
2809 error = PyDict_SetItem(globals, pkgstr, modname);
2810 if (error) {
2811 PyErr_SetString(PyExc_ValueError,
2812 "Could not set __package__");
2813 return NULL;
2814 }
2815 } else {
2816 /* Normal module, so work out the package name if any */
2817 char *start = _PyUnicode_AsString(modname);
2818 char *lastdot = strrchr(start, '.');
2819 size_t len;
2820 int error;
2821 if (lastdot == NULL && level > 0) {
2822 PyErr_SetString(PyExc_ValueError,
2823 "Attempted relative import in non-package");
2824 return NULL;
2825 }
2826 if (lastdot == NULL) {
2827 error = PyDict_SetItem(globals, pkgstr, Py_None);
2828 if (error) {
2829 PyErr_SetString(PyExc_ValueError,
2830 "Could not set __package__");
2831 return NULL;
2832 }
2833 return Py_None;
2834 }
2835 len = lastdot - start;
2836 if (len >= MAXPATHLEN) {
2837 PyErr_SetString(PyExc_ValueError,
2838 "Module name too long");
2839 return NULL;
2840 }
2841 strncpy(buf, start, len);
2842 buf[len] = '\0';
2843 pkgname = PyUnicode_FromString(buf);
2844 if (pkgname == NULL) {
2845 return NULL;
2846 }
2847 error = PyDict_SetItem(globals, pkgstr, pkgname);
2848 Py_DECREF(pkgname);
2849 if (error) {
2850 PyErr_SetString(PyExc_ValueError,
2851 "Could not set __package__");
2852 return NULL;
2853 }
2854 }
2855 }
2856 while (--level > 0) {
2857 char *dot = strrchr(buf, '.');
2858 if (dot == NULL) {
2859 PyErr_SetString(PyExc_ValueError,
2860 "Attempted relative import beyond "
2861 "toplevel package");
2862 return NULL;
2863 }
2864 *dot = '\0';
2865 }
2866 *p_buflen = strlen(buf);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 modules = PyImport_GetModuleDict();
2869 parent = PyDict_GetItemString(modules, buf);
2870 if (parent == NULL) {
2871 if (orig_level < 1) {
2872 PyObject *err_msg = PyBytes_FromFormat(
2873 "Parent module '%.200s' not found "
2874 "while handling absolute import", buf);
2875 if (err_msg == NULL) {
2876 return NULL;
2877 }
2878 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
2879 PyBytes_AsString(err_msg), 1)) {
2880 *buf = '\0';
2881 *p_buflen = 0;
2882 parent = Py_None;
2883 }
2884 Py_DECREF(err_msg);
2885 } else {
2886 PyErr_Format(PyExc_SystemError,
2887 "Parent module '%.200s' not loaded, "
2888 "cannot perform relative import", buf);
2889 }
2890 }
2891 return parent;
2892 /* We expect, but can't guarantee, if parent != None, that:
2893 - parent.__name__ == buf
2894 - parent.__dict__ is globals
2895 If this is violated... Who cares? */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002896}
2897
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002898/* altmod is either None or same as mod */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002899static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002900load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 Py_ssize_t *p_buflen)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 char *name = *p_name;
2904 char *dot = strchr(name, '.');
2905 size_t len;
2906 char *p;
2907 PyObject *result;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 if (strlen(name) == 0) {
2910 /* completely empty module name should only happen in
2911 'from . import' (or '__import__("")')*/
2912 Py_INCREF(mod);
2913 *p_name = NULL;
2914 return mod;
2915 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 if (dot == NULL) {
2918 *p_name = NULL;
2919 len = strlen(name);
2920 }
2921 else {
2922 *p_name = dot+1;
2923 len = dot-name;
2924 }
2925 if (len == 0) {
2926 PyErr_SetString(PyExc_ValueError,
2927 "Empty module name");
2928 return NULL;
2929 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 p = buf + *p_buflen;
2932 if (p != buf)
2933 *p++ = '.';
2934 if (p+len-buf >= MAXPATHLEN) {
2935 PyErr_SetString(PyExc_ValueError,
2936 "Module name too long");
2937 return NULL;
2938 }
2939 strncpy(p, name, len);
2940 p[len] = '\0';
2941 *p_buflen = p+len-buf;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 result = import_submodule(mod, p, buf);
2944 if (result == Py_None && altmod != mod) {
2945 Py_DECREF(result);
2946 /* Here, altmod must be None and mod must not be None */
2947 result = import_submodule(altmod, p, p);
2948 if (result != NULL && result != Py_None) {
2949 if (mark_miss(buf) != 0) {
2950 Py_DECREF(result);
2951 return NULL;
2952 }
2953 strncpy(buf, name, len);
2954 buf[len] = '\0';
2955 *p_buflen = len;
2956 }
2957 }
2958 if (result == NULL)
2959 return NULL;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 if (result == Py_None) {
2962 Py_DECREF(result);
2963 PyErr_Format(PyExc_ImportError,
2964 "No module named %.200s", name);
2965 return NULL;
2966 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 return result;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002969}
2970
2971static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002972mark_miss(char *name)
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 PyObject *modules = PyImport_GetModuleDict();
2975 return PyDict_SetItemString(modules, name, Py_None);
Guido van Rossumf5f5fdb1997-09-06 20:29:52 +00002976}
2977
2978static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00002979ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 int recursive)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 int i;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 if (!PyObject_HasAttrString(mod, "__path__"))
2985 return 1;
Guido van Rossum17fc85f1997-09-06 18:52:03 +00002986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 for (i = 0; ; i++) {
2988 PyObject *item = PySequence_GetItem(fromlist, i);
2989 int hasit;
2990 if (item == NULL) {
2991 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2992 PyErr_Clear();
2993 return 1;
2994 }
2995 return 0;
2996 }
2997 if (!PyUnicode_Check(item)) {
2998 PyErr_SetString(PyExc_TypeError,
2999 "Item in ``from list'' not a string");
3000 Py_DECREF(item);
3001 return 0;
3002 }
3003 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
3004 PyObject *all;
3005 Py_DECREF(item);
3006 /* See if the package defines __all__ */
3007 if (recursive)
3008 continue; /* Avoid endless recursion */
3009 all = PyObject_GetAttrString(mod, "__all__");
3010 if (all == NULL)
3011 PyErr_Clear();
3012 else {
3013 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
3014 Py_DECREF(all);
3015 if (!ret)
3016 return 0;
3017 }
3018 continue;
3019 }
3020 hasit = PyObject_HasAttr(mod, item);
3021 if (!hasit) {
3022 PyObject *item8;
3023 char *subname;
3024 PyObject *submod;
3025 char *p;
Victor Stinnerae6265f2010-05-15 16:27:27 +00003026 item8 = PyUnicode_EncodeFSDefault(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 if (!item8) {
3028 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
3029 return 0;
3030 }
3031 subname = PyBytes_AS_STRING(item8);
3032 if (buflen + strlen(subname) >= MAXPATHLEN) {
3033 PyErr_SetString(PyExc_ValueError,
3034 "Module name too long");
3035 Py_DECREF(item);
3036 return 0;
3037 }
3038 p = buf + buflen;
3039 *p++ = '.';
3040 strcpy(p, subname);
3041 submod = import_submodule(mod, subname, buf);
3042 Py_DECREF(item8);
3043 Py_XDECREF(submod);
3044 if (submod == NULL) {
3045 Py_DECREF(item);
3046 return 0;
3047 }
3048 }
3049 Py_DECREF(item);
3050 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00003051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 /* NOTREACHED */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00003053}
3054
Neil Schemenauer00b09662003-06-16 21:03:07 +00003055static int
3056add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 PyObject *modules)
Neil Schemenauer00b09662003-06-16 21:03:07 +00003058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 if (mod == Py_None)
3060 return 1;
3061 /* Irrespective of the success of this load, make a
3062 reference to it in the parent package module. A copy gets
3063 saved in the modules dictionary under the full name, so get a
3064 reference from there, if need be. (The exception is when the
3065 load failed with a SyntaxError -- then there's no trace in
3066 sys.modules. In that case, of course, do nothing extra.) */
3067 if (submod == NULL) {
3068 submod = PyDict_GetItemString(modules, fullname);
3069 if (submod == NULL)
3070 return 1;
3071 }
3072 if (PyModule_Check(mod)) {
3073 /* We can't use setattr here since it can give a
3074 * spurious warning if the submodule name shadows a
3075 * builtin name */
3076 PyObject *dict = PyModule_GetDict(mod);
3077 if (!dict)
3078 return 0;
3079 if (PyDict_SetItemString(dict, subname, submod) < 0)
3080 return 0;
3081 }
3082 else {
3083 if (PyObject_SetAttrString(mod, subname, submod) < 0)
3084 return 0;
3085 }
3086 return 1;
Neil Schemenauer00b09662003-06-16 21:03:07 +00003087}
3088
Guido van Rossum17fc85f1997-09-06 18:52:03 +00003089static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003090import_submodule(PyObject *mod, char *subname, char *fullname)
Guido van Rossum17fc85f1997-09-06 18:52:03 +00003091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 PyObject *modules = PyImport_GetModuleDict();
3093 PyObject *m = NULL;
Guido van Rossum74e6a111994-08-29 12:54:38 +00003094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 /* Require:
3096 if mod == None: subname == fullname
3097 else: mod.__name__ + "." + subname == fullname
3098 */
Guido van Rossum17fc85f1997-09-06 18:52:03 +00003099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
3101 Py_INCREF(m);
3102 }
3103 else {
Victor Stinner7d8b77c2011-03-12 08:45:02 -05003104 PyObject *path, *loader;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 char buf[MAXPATHLEN+1];
3106 struct filedescr *fdp;
Victor Stinner7d8b77c2011-03-12 08:45:02 -05003107 FILE *fp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 if (mod == Py_None)
3110 path = NULL;
3111 else {
3112 path = PyObject_GetAttrString(mod, "__path__");
3113 if (path == NULL) {
3114 PyErr_Clear();
3115 Py_INCREF(Py_None);
3116 return Py_None;
3117 }
3118 }
Guido van Rossum17fc85f1997-09-06 18:52:03 +00003119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
3121 &fp, &loader);
3122 Py_XDECREF(path);
3123 if (fdp == NULL) {
3124 if (!PyErr_ExceptionMatches(PyExc_ImportError))
3125 return NULL;
3126 PyErr_Clear();
3127 Py_INCREF(Py_None);
3128 return Py_None;
3129 }
3130 m = load_module(fullname, fp, buf, fdp->type, loader);
3131 Py_XDECREF(loader);
3132 if (fp)
3133 fclose(fp);
3134 if (!add_submodule(mod, m, fullname, subname, modules)) {
3135 Py_XDECREF(m);
3136 m = NULL;
3137 }
3138 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +00003141}
3142
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003143
3144/* Re-import a module of any kind and return its module object, WITH
3145 INCREMENTED REFERENCE COUNT */
3146
Guido van Rossum79f25d91997-04-29 20:08:16 +00003147PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003148PyImport_ReloadModule(PyObject *m)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 PyInterpreterState *interp = PyThreadState_Get()->interp;
3151 PyObject *modules_reloading = interp->modules_reloading;
3152 PyObject *modules = PyImport_GetModuleDict();
Victor Stinner7d8b77c2011-03-12 08:45:02 -05003153 PyObject *path = NULL, *loader, *existing_m = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 char *name, *subname;
3155 char buf[MAXPATHLEN+1];
3156 struct filedescr *fdp;
Victor Stinner7d8b77c2011-03-12 08:45:02 -05003157 FILE *fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 PyObject *newm;
Brett Cannon9a5b25a2010-03-01 02:09:17 +00003159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 if (modules_reloading == NULL) {
3161 Py_FatalError("PyImport_ReloadModule: "
3162 "no modules_reloading dictionary!");
3163 return NULL;
3164 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 if (m == NULL || !PyModule_Check(m)) {
3167 PyErr_SetString(PyExc_TypeError,
3168 "reload() argument must be module");
3169 return NULL;
3170 }
3171 name = (char*)PyModule_GetName(m);
3172 if (name == NULL)
3173 return NULL;
3174 if (m != PyDict_GetItemString(modules, name)) {
3175 PyErr_Format(PyExc_ImportError,
3176 "reload(): module %.200s not in sys.modules",
3177 name);
3178 return NULL;
3179 }
3180 existing_m = PyDict_GetItemString(modules_reloading, name);
3181 if (existing_m != NULL) {
3182 /* Due to a recursive reload, this module is already
3183 being reloaded. */
3184 Py_INCREF(existing_m);
3185 return existing_m;
3186 }
3187 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
3188 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 subname = strrchr(name, '.');
3191 if (subname == NULL)
3192 subname = name;
3193 else {
3194 PyObject *parentname, *parent;
3195 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
3196 if (parentname == NULL) {
3197 imp_modules_reloading_clear();
3198 return NULL;
3199 }
3200 parent = PyDict_GetItem(modules, parentname);
3201 if (parent == NULL) {
3202 PyErr_Format(PyExc_ImportError,
3203 "reload(): parent %U not in sys.modules",
3204 parentname);
3205 Py_DECREF(parentname);
3206 imp_modules_reloading_clear();
3207 return NULL;
3208 }
3209 Py_DECREF(parentname);
3210 subname++;
3211 path = PyObject_GetAttrString(parent, "__path__");
3212 if (path == NULL)
3213 PyErr_Clear();
3214 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
3216 Py_XDECREF(path);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00003217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 if (fdp == NULL) {
3219 Py_XDECREF(loader);
3220 imp_modules_reloading_clear();
3221 return NULL;
3222 }
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00003223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 newm = load_module(name, fp, buf, fdp->type, loader);
3225 Py_XDECREF(loader);
Phillip J. Eby7ec642a2004-09-23 04:37:36 +00003226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 if (fp)
3228 fclose(fp);
3229 if (newm == NULL) {
3230 /* load_module probably removed name from modules because of
3231 * the error. Put back the original module object. We're
3232 * going to return NULL in this case regardless of whether
3233 * replacing name succeeds, so the return value is ignored.
3234 */
3235 PyDict_SetItemString(modules, name, m);
3236 }
3237 imp_modules_reloading_clear();
3238 return newm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003239}
3240
3241
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003242/* Higher-level import emulator which emulates the "import" statement
3243 more accurately -- it invokes the __import__() function from the
3244 builtins of the current globals. This means that the import is
3245 done using whatever import hooks are installed in the current
Brett Cannonbc2eff32010-09-19 21:39:02 +00003246 environment.
Guido van Rossum6058eb41998-12-21 19:51:00 +00003247 A dummy list ["__doc__"] is passed as the 4th argument so that
Neal Norwitz80e7f272007-08-26 06:45:23 +00003248 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
Guido van Rossum6058eb41998-12-21 19:51:00 +00003249 will return <module "gencache"> instead of <module "win32com">. */
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003250
3251PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003252PyImport_Import(PyObject *module_name)
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 static PyObject *silly_list = NULL;
3255 static PyObject *builtins_str = NULL;
3256 static PyObject *import_str = NULL;
3257 PyObject *globals = NULL;
3258 PyObject *import = NULL;
3259 PyObject *builtins = NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00003260 PyObject *modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 PyObject *r = NULL;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 /* Initialize constant string objects */
3264 if (silly_list == NULL) {
3265 import_str = PyUnicode_InternFromString("__import__");
3266 if (import_str == NULL)
3267 return NULL;
3268 builtins_str = PyUnicode_InternFromString("__builtins__");
3269 if (builtins_str == NULL)
3270 return NULL;
Brett Cannonbc2eff32010-09-19 21:39:02 +00003271 silly_list = PyList_New(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 if (silly_list == NULL)
3273 return NULL;
3274 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 /* Get the builtins from current globals */
3277 globals = PyEval_GetGlobals();
3278 if (globals != NULL) {
3279 Py_INCREF(globals);
3280 builtins = PyObject_GetItem(globals, builtins_str);
3281 if (builtins == NULL)
3282 goto err;
3283 }
3284 else {
3285 /* No globals -- use standard builtins, and fake globals */
3286 builtins = PyImport_ImportModuleLevel("builtins",
3287 NULL, NULL, NULL, 0);
3288 if (builtins == NULL)
3289 return NULL;
3290 globals = Py_BuildValue("{OO}", builtins_str, builtins);
3291 if (globals == NULL)
3292 goto err;
3293 }
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 /* Get the __import__ function from the builtins */
3296 if (PyDict_Check(builtins)) {
3297 import = PyObject_GetItem(builtins, import_str);
3298 if (import == NULL)
3299 PyErr_SetObject(PyExc_KeyError, import_str);
3300 }
3301 else
3302 import = PyObject_GetAttr(builtins, import_str);
3303 if (import == NULL)
3304 goto err;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 /* Call the __import__ function with the proper argument list
Brett Cannonbc2eff32010-09-19 21:39:02 +00003307 Always use absolute import here.
3308 Calling for side-effect of import. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
3310 globals, silly_list, 0, NULL);
Brett Cannonbc2eff32010-09-19 21:39:02 +00003311 if (r == NULL)
3312 goto err;
3313 Py_DECREF(r);
3314
3315 modules = PyImport_GetModuleDict();
3316 r = PyDict_GetItem(modules, module_name);
3317 if (r != NULL)
3318 Py_INCREF(r);
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003319
3320 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 Py_XDECREF(globals);
3322 Py_XDECREF(builtins);
3323 Py_XDECREF(import);
Tim Peters50d8d372001-02-28 05:34:27 +00003324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 return r;
Guido van Rossumd47a0a81997-08-14 20:11:26 +00003326}
3327
3328
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003329/* Module 'imp' provides Python access to the primitives used for
3330 importing modules.
3331*/
3332
Guido van Rossum79f25d91997-04-29 20:08:16 +00003333static PyObject *
Barry Warsaw28a691b2010-04-17 00:19:56 +00003334imp_make_magic(long magic)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 char buf[4];
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 buf[0] = (char) ((magic >> 0) & 0xff);
3339 buf[1] = (char) ((magic >> 8) & 0xff);
3340 buf[2] = (char) ((magic >> 16) & 0xff);
3341 buf[3] = (char) ((magic >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 return PyBytes_FromStringAndSize(buf, 4);
Victor Stinner3e2b7172010-11-09 09:32:19 +00003344}
Barry Warsaw28a691b2010-04-17 00:19:56 +00003345
3346static PyObject *
3347imp_get_magic(PyObject *self, PyObject *noargs)
3348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 return imp_make_magic(pyc_magic);
Barry Warsaw28a691b2010-04-17 00:19:56 +00003350}
3351
3352static PyObject *
3353imp_get_tag(PyObject *self, PyObject *noargs)
3354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 return PyUnicode_FromString(pyc_tag);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003356}
3357
Guido van Rossum79f25d91997-04-29 20:08:16 +00003358static PyObject *
Neal Norwitz08ea61a2003-02-17 18:18:00 +00003359imp_get_suffixes(PyObject *self, PyObject *noargs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 PyObject *list;
3362 struct filedescr *fdp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 list = PyList_New(0);
3365 if (list == NULL)
3366 return NULL;
3367 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
3368 PyObject *item = Py_BuildValue("ssi",
3369 fdp->suffix, fdp->mode, fdp->type);
3370 if (item == NULL) {
3371 Py_DECREF(list);
3372 return NULL;
3373 }
3374 if (PyList_Append(list, item) < 0) {
3375 Py_DECREF(list);
3376 Py_DECREF(item);
3377 return NULL;
3378 }
3379 Py_DECREF(item);
3380 }
3381 return list;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003382}
3383
Guido van Rossum79f25d91997-04-29 20:08:16 +00003384static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003385call_find_module(char *name, PyObject *path)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 extern int fclose(FILE *);
3388 PyObject *fob, *ret;
3389 PyObject *pathobj;
3390 struct filedescr *fdp;
3391 char pathname[MAXPATHLEN+1];
Victor Stinner7d8b77c2011-03-12 08:45:02 -05003392 FILE *fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 int fd = -1;
3394 char *found_encoding = NULL;
3395 char *encoding = NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 pathname[0] = '\0';
3398 if (path == Py_None)
3399 path = NULL;
3400 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
3401 if (fdp == NULL)
3402 return NULL;
3403 if (fp != NULL) {
3404 fd = fileno(fp);
3405 if (fd != -1)
3406 fd = dup(fd);
3407 fclose(fp);
3408 fp = NULL;
3409 }
3410 if (fd != -1) {
3411 if (strchr(fdp->mode, 'b') == NULL) {
3412 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
3413 memory. */
3414 found_encoding = PyTokenizer_FindEncoding(fd);
3415 lseek(fd, 0, 0); /* Reset position */
3416 if (found_encoding == NULL && PyErr_Occurred())
3417 return NULL;
3418 encoding = (found_encoding != NULL) ? found_encoding :
3419 (char*)PyUnicode_GetDefaultEncoding();
3420 }
3421 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
3422 (char*)encoding, NULL, NULL, 1);
3423 if (fob == NULL) {
3424 close(fd);
3425 PyMem_FREE(found_encoding);
3426 return NULL;
3427 }
3428 }
3429 else {
3430 fob = Py_None;
3431 Py_INCREF(fob);
3432 }
3433 pathobj = PyUnicode_DecodeFSDefault(pathname);
3434 ret = Py_BuildValue("NN(ssi)",
3435 fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
3436 PyMem_FREE(found_encoding);
Brett Cannon3bb42d92007-10-20 03:43:15 +00003437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 return ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003439}
3440
Guido van Rossum79f25d91997-04-29 20:08:16 +00003441static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003442imp_find_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003443{
Victor Stinnerebc00522010-12-03 17:06:43 +00003444 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 PyObject *ret, *path = NULL;
Victor Stinnerebc00522010-12-03 17:06:43 +00003446 if (!PyArg_ParseTuple(args, "O&|O:find_module",
3447 PyUnicode_FSConverter, &name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 &path))
3449 return NULL;
Victor Stinnerebc00522010-12-03 17:06:43 +00003450 ret = call_find_module(PyBytes_AS_STRING(name), path);
3451 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003453}
3454
3455static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003456imp_init_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003457{
Victor Stinner95872862011-03-07 18:20:56 +01003458 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 int ret;
3460 PyObject *m;
Victor Stinner95872862011-03-07 18:20:56 +01003461 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 return NULL;
3463 ret = init_builtin(name);
3464 if (ret < 0)
3465 return NULL;
3466 if (ret == 0) {
3467 Py_INCREF(Py_None);
3468 return Py_None;
3469 }
Victor Stinner95872862011-03-07 18:20:56 +01003470 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 Py_XINCREF(m);
3472 return m;
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_init_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 int ret;
3480 PyObject *m;
Victor Stinner53dc7352011-03-20 01:50:21 +01003481 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 return NULL;
Victor Stinner53dc7352011-03-20 01:50:21 +01003483 ret = PyImport_ImportFrozenModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 if (ret < 0)
3485 return NULL;
3486 if (ret == 0) {
3487 Py_INCREF(Py_None);
3488 return Py_None;
3489 }
Victor Stinner53dc7352011-03-20 01:50:21 +01003490 m = PyImport_AddModuleObject(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 Py_XINCREF(m);
3492 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003493}
3494
Guido van Rossum79f25d91997-04-29 20:08:16 +00003495static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003496imp_get_frozen_object(PyObject *self, PyObject *args)
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00003497{
Victor Stinner53dc7352011-03-20 01:50:21 +01003498 PyObject *name;
Jack Jansen95ffa231995-10-03 14:38:41 +00003499
Victor Stinner53dc7352011-03-20 01:50:21 +01003500 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 return NULL;
3502 return get_frozen_object(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00003503}
3504
Guido van Rossum79f25d91997-04-29 20:08:16 +00003505static PyObject *
Brett Cannon8d110132009-03-15 02:20:16 +00003506imp_is_frozen_package(PyObject *self, PyObject *args)
3507{
Victor Stinner53dc7352011-03-20 01:50:21 +01003508 PyObject *name;
Brett Cannon8d110132009-03-15 02:20:16 +00003509
Victor Stinner53dc7352011-03-20 01:50:21 +01003510 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 return NULL;
3512 return is_frozen_package(name);
Brett Cannon8d110132009-03-15 02:20:16 +00003513}
3514
3515static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003516imp_is_builtin(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003517{
Victor Stinner95872862011-03-07 18:20:56 +01003518 PyObject *name;
3519 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 return NULL;
3521 return PyLong_FromLong(is_builtin(name));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003522}
3523
Guido van Rossum79f25d91997-04-29 20:08:16 +00003524static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003525imp_is_frozen(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003526{
Victor Stinner53dc7352011-03-20 01:50:21 +01003527 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 struct _frozen *p;
Victor Stinner53dc7352011-03-20 01:50:21 +01003529 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 return NULL;
3531 p = find_frozen(name);
3532 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003533}
3534
3535static FILE *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003536get_file(char *pathname, PyObject *fob, char *mode)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 FILE *fp;
3539 if (mode[0] == 'U')
3540 mode = "r" PY_STDIOTEXTMODE;
3541 if (fob == NULL) {
3542 fp = fopen(pathname, mode);
3543 }
3544 else {
3545 int fd = PyObject_AsFileDescriptor(fob);
3546 if (fd == -1)
3547 return NULL;
3548 if (!_PyVerify_fd(fd))
3549 goto error;
3550 /* the FILE struct gets a new fd, so that it can be closed
3551 * independently of the file descriptor given
3552 */
3553 fd = dup(fd);
3554 if (fd == -1)
3555 goto error;
3556 fp = fdopen(fd, mode);
3557 }
3558 if (fp)
3559 return fp;
Kristján Valur Jónssone1b04452009-03-31 17:43:39 +00003560error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 PyErr_SetFromErrno(PyExc_IOError);
3562 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003563}
3564
Guido van Rossum79f25d91997-04-29 20:08:16 +00003565static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003566imp_load_compiled(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 char *name;
Victor Stinnerebc00522010-12-03 17:06:43 +00003569 PyObject *pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 PyObject *fob = NULL;
3571 PyObject *m;
3572 FILE *fp;
Victor Stinnerebc00522010-12-03 17:06:43 +00003573 if (!PyArg_ParseTuple(args, "sO&|O:load_compiled",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 &name,
Victor Stinnerebc00522010-12-03 17:06:43 +00003575 PyUnicode_FSConverter, &pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 &fob))
3577 return NULL;
Victor Stinnerebc00522010-12-03 17:06:43 +00003578 fp = get_file(PyBytes_AS_STRING(pathname), fob, "rb");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 if (fp == NULL) {
Victor Stinnerebc00522010-12-03 17:06:43 +00003580 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 return NULL;
3582 }
Victor Stinnerebc00522010-12-03 17:06:43 +00003583 m = load_compiled_module(name, PyBytes_AS_STRING(pathname), fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 fclose(fp);
Victor Stinnerebc00522010-12-03 17:06:43 +00003585 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003587}
3588
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003589#ifdef HAVE_DYNAMIC_LOADING
3590
Guido van Rossum79f25d91997-04-29 20:08:16 +00003591static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003592imp_load_dynamic(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003593{
Victor Stinnerfefd70c2011-03-14 15:54:07 -04003594 PyObject *name, *pathname, *fob = NULL, *mod;
3595 FILE *fp;
3596
3597 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
3598 &name, PyUnicode_FSDecoder, &pathname, &fob))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 return NULL;
Victor Stinnerfefd70c2011-03-14 15:54:07 -04003600 if (fob != NULL) {
3601 fp = get_file(NULL, fob, "r");
3602 if (fp == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -04003605 else
3606 fp = NULL;
3607 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 if (fp)
3609 fclose(fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -04003610 return mod;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003611}
3612
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003613#endif /* HAVE_DYNAMIC_LOADING */
3614
Guido van Rossum79f25d91997-04-29 20:08:16 +00003615static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003616imp_load_source(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 char *name;
Victor Stinnerebc00522010-12-03 17:06:43 +00003619 PyObject *pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 PyObject *fob = NULL;
3621 PyObject *m;
3622 FILE *fp;
Victor Stinnerebc00522010-12-03 17:06:43 +00003623 if (!PyArg_ParseTuple(args, "sO&|O:load_source",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 &name,
Victor Stinnerebc00522010-12-03 17:06:43 +00003625 PyUnicode_FSConverter, &pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 &fob))
3627 return NULL;
Victor Stinnerebc00522010-12-03 17:06:43 +00003628 fp = get_file(PyBytes_AS_STRING(pathname), fob, "r");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 if (fp == NULL) {
Victor Stinnerebc00522010-12-03 17:06:43 +00003630 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 return NULL;
3632 }
Victor Stinnerebc00522010-12-03 17:06:43 +00003633 m = load_source_module(name, PyBytes_AS_STRING(pathname), fp);
3634 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 fclose(fp);
3636 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003637}
3638
Guido van Rossum79f25d91997-04-29 20:08:16 +00003639static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003640imp_load_module(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 char *name;
3643 PyObject *fob;
Victor Stinner1a563032010-10-15 22:43:10 +00003644 PyObject *pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 PyObject * ret;
3646 char *suffix; /* Unused */
3647 char *mode;
3648 int type;
3649 FILE *fp;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003650
Victor Stinner1a563032010-10-15 22:43:10 +00003651 if (!PyArg_ParseTuple(args, "sOO&(ssi):load_module",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 &name, &fob,
Victor Stinner1a563032010-10-15 22:43:10 +00003653 PyUnicode_FSConverter, &pathname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 &suffix, &mode, &type))
3655 return NULL;
3656 if (*mode) {
3657 /* Mode must start with 'r' or 'U' and must not contain '+'.
3658 Implicit in this test is the assumption that the mode
3659 may contain other modifiers like 'b' or 't'. */
Guido van Rossum5e2c5fa2002-05-30 17:33:07 +00003660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
3662 PyErr_Format(PyExc_ValueError,
3663 "invalid file open mode %.200s", mode);
Victor Stinner1a563032010-10-15 22:43:10 +00003664 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 return NULL;
3666 }
3667 }
3668 if (fob == Py_None)
3669 fp = NULL;
3670 else {
3671 fp = get_file(NULL, fob, mode);
3672 if (fp == NULL) {
Victor Stinner1a563032010-10-15 22:43:10 +00003673 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 return NULL;
3675 }
3676 }
Victor Stinner1a563032010-10-15 22:43:10 +00003677 ret = load_module(name, fp, PyBytes_AS_STRING(pathname), type, NULL);
3678 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 if (fp)
3680 fclose(fp);
3681 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003682}
3683
3684static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003685imp_load_package(PyObject *self, PyObject *args)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003686{
Victor Stinnerc9abda02011-03-14 13:33:46 -04003687 PyObject *name, *pathname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 PyObject * ret;
Victor Stinnerc9abda02011-03-14 13:33:46 -04003689 if (!PyArg_ParseTuple(args, "UO&:load_package",
3690 &name, PyUnicode_FSDecoder, &pathname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 return NULL;
Victor Stinnerc9abda02011-03-14 13:33:46 -04003692 ret = load_package(name, pathname);
Victor Stinnerebc00522010-12-03 17:06:43 +00003693 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 return ret;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003695}
3696
3697static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003698imp_new_module(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 char *name;
3701 if (!PyArg_ParseTuple(args, "s:new_module", &name))
3702 return NULL;
3703 return PyModule_New(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003704}
3705
Christian Heimes13a7a212008-01-07 17:13:09 +00003706static PyObject *
3707imp_reload(PyObject *self, PyObject *v)
3708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 return PyImport_ReloadModule(v);
Christian Heimes13a7a212008-01-07 17:13:09 +00003710}
3711
3712PyDoc_STRVAR(doc_reload,
3713"reload(module) -> module\n\
3714\n\
3715Reload the module. The module must have been successfully imported before.");
3716
Barry Warsaw28a691b2010-04-17 00:19:56 +00003717static PyObject *
3718imp_cache_from_source(PyObject *self, PyObject *args, PyObject *kws)
3719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 static char *kwlist[] = {"path", "debug_override", NULL};
Barry Warsaw28a691b2010-04-17 00:19:56 +00003721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 char buf[MAXPATHLEN+1];
Victor Stinner3ea23dd2010-10-15 20:34:32 +00003723 PyObject *pathbytes;
3724 char *cpathname;
Benjamin Peterson294a9fc2010-10-16 03:12:39 +00003725 PyObject *debug_override = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 int debug = !Py_OptimizeFlag;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 if (!PyArg_ParseTupleAndKeywords(
Victor Stinner3ea23dd2010-10-15 20:34:32 +00003729 args, kws, "O&|O", kwlist,
3730 PyUnicode_FSConverter, &pathbytes, &debug_override))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003732
Benjamin Peterson294a9fc2010-10-16 03:12:39 +00003733 if (debug_override != NULL &&
3734 (debug = PyObject_IsTrue(debug_override)) < 0) {
3735 Py_DECREF(pathbytes);
3736 return NULL;
3737 }
Barry Warsaw28a691b2010-04-17 00:19:56 +00003738
Victor Stinner3ea23dd2010-10-15 20:34:32 +00003739 cpathname = make_compiled_pathname(
3740 PyBytes_AS_STRING(pathbytes),
3741 buf, MAXPATHLEN+1, debug);
3742 Py_DECREF(pathbytes);
Barry Warsaw28a691b2010-04-17 00:19:56 +00003743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 if (cpathname == NULL) {
3745 PyErr_Format(PyExc_SystemError, "path buffer too short");
3746 return NULL;
3747 }
Victor Stinner88bd8912010-10-15 22:46:07 +00003748 return PyUnicode_DecodeFSDefault(buf);
Barry Warsaw28a691b2010-04-17 00:19:56 +00003749}
3750
3751PyDoc_STRVAR(doc_cache_from_source,
3752"Given the path to a .py file, return the path to its .pyc/.pyo file.\n\
3753\n\
3754The .py file does not need to exist; this simply returns the path to the\n\
3755.pyc/.pyo file calculated as if the .py file were imported. The extension\n\
3756will be .pyc unless __debug__ is not defined, then it will be .pyo.\n\
3757\n\
3758If debug_override is not None, then it must be a boolean and is taken as\n\
3759the value of __debug__ instead.");
3760
3761static PyObject *
3762imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws)
3763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 static char *kwlist[] = {"path", NULL};
Victor Stinnerc9abda02011-03-14 13:33:46 -04003765 PyObject *pathname, *source;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 if (!PyArg_ParseTupleAndKeywords(
Victor Stinnerebc00522010-12-03 17:06:43 +00003768 args, kws, "O&", kwlist,
Victor Stinnerc9abda02011-03-14 13:33:46 -04003769 PyUnicode_FSDecoder, &pathname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 return NULL;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003771
Victor Stinnerc9abda02011-03-14 13:33:46 -04003772 source = make_source_pathname(pathname);
3773 if (source == NULL) {
3774 PyErr_Format(PyExc_ValueError, "Not a PEP 3147 pyc path: %R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 pathname);
Victor Stinnerc9abda02011-03-14 13:33:46 -04003776 Py_DECREF(pathname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 return NULL;
3778 }
Victor Stinnerc9abda02011-03-14 13:33:46 -04003779 Py_DECREF(pathname);
3780 return source;
Barry Warsaw28a691b2010-04-17 00:19:56 +00003781}
3782
3783PyDoc_STRVAR(doc_source_from_cache,
3784"Given the path to a .pyc./.pyo file, return the path to its .py file.\n\
3785\n\
3786The .pyc/.pyo file does not need to exist; this simply returns the path to\n\
3787the .py file calculated to correspond to the .pyc/.pyo file. If path\n\
3788does not conform to PEP 3147 format, ValueError will be raised.");
3789
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003790/* Doc strings */
3791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003792PyDoc_STRVAR(doc_imp,
3793"This module provides the components needed to build your own\n\
3794__import__ function. Undocumented functions are obsolete.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003795
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003796PyDoc_STRVAR(doc_find_module,
3797"find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003798Search for a module. If path is omitted or None, search for a\n\
3799built-in, frozen or special module and continue search in sys.path.\n\
3800The module name cannot contain '.'; to search for a submodule of a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003801package, pass the submodule name and the package's __path__.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003803PyDoc_STRVAR(doc_load_module,
3804"load_module(name, file, filename, (suffix, mode, type)) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003805Load a module, given information returned by find_module().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003806The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003807
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003808PyDoc_STRVAR(doc_get_magic,
3809"get_magic() -> string\n\
3810Return the magic number for .pyc or .pyo files.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003811
Barry Warsaw28a691b2010-04-17 00:19:56 +00003812PyDoc_STRVAR(doc_get_tag,
3813"get_tag() -> string\n\
3814Return the magic tag for .pyc or .pyo files.");
3815
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003816PyDoc_STRVAR(doc_get_suffixes,
3817"get_suffixes() -> [(suffix, mode, type), ...]\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003818Return a list of (suffix, mode, type) tuples describing the files\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003819that find_module() looks for.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003820
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003821PyDoc_STRVAR(doc_new_module,
3822"new_module(name) -> module\n\
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003823Create a new module. Do not enter it in sys.modules.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003824The module name must include the full package name, if any.");
Guido van Rossum0207e6d1997-09-09 22:04:42 +00003825
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003826PyDoc_STRVAR(doc_lock_held,
Tim Petersa7c65092004-08-01 23:26:05 +00003827"lock_held() -> boolean\n\
3828Return True if the import lock is currently held, else False.\n\
3829On platforms without threads, return False.");
Tim Peters69232342001-08-30 05:16:13 +00003830
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003831PyDoc_STRVAR(doc_acquire_lock,
3832"acquire_lock() -> None\n\
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003833Acquires the interpreter's import lock for the current thread.\n\
3834This lock should be used by import hooks to ensure thread-safety\n\
3835when importing modules.\n\
Guido van Rossumc4f4ca92003-02-12 21:46:11 +00003836On platforms without threads, this function does nothing.");
3837
3838PyDoc_STRVAR(doc_release_lock,
3839"release_lock() -> None\n\
3840Release the interpreter's import lock.\n\
3841On platforms without threads, this function does nothing.");
3842
Guido van Rossum79f25d91997-04-29 20:08:16 +00003843static PyMethodDef imp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
3845 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
3846 {"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag},
3847 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
3848 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
3849 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
3850 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
3851 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
3852 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
3853 {"reload", imp_reload, METH_O, doc_reload},
3854 {"cache_from_source", (PyCFunction)imp_cache_from_source,
3855 METH_VARARGS | METH_KEYWORDS, doc_cache_from_source},
3856 {"source_from_cache", (PyCFunction)imp_source_from_cache,
3857 METH_VARARGS | METH_KEYWORDS, doc_source_from_cache},
3858 /* The rest are obsolete */
3859 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
3860 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
3861 {"init_builtin", imp_init_builtin, METH_VARARGS},
3862 {"init_frozen", imp_init_frozen, METH_VARARGS},
3863 {"is_builtin", imp_is_builtin, METH_VARARGS},
3864 {"is_frozen", imp_is_frozen, METH_VARARGS},
3865 {"load_compiled", imp_load_compiled, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003866#ifdef HAVE_DYNAMIC_LOADING
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
Guido van Rossum96a8fb71999-12-22 14:09:35 +00003868#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 {"load_package", imp_load_package, METH_VARARGS},
3870 {"load_source", imp_load_source, METH_VARARGS},
3871 {NULL, NULL} /* sentinel */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003872};
3873
Guido van Rossum1a8791e1998-08-04 22:46:29 +00003874static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003875setint(PyObject *d, char *name, int value)
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 PyObject *v;
3878 int err;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 v = PyLong_FromLong((long)value);
3881 err = PyDict_SetItemString(d, name, v);
3882 Py_XDECREF(v);
3883 return err;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00003884}
3885
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003886typedef struct {
3887 PyObject_HEAD
3888} NullImporter;
3889
3890static int
3891NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3892{
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003893#ifndef MS_WINDOWS
3894 PyObject *path;
3895 struct stat statbuf;
3896 int rv;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3899 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003900
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003901 if (!PyArg_ParseTuple(args, "O&:NullImporter",
3902 PyUnicode_FSConverter, &path))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003904
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003905 if (PyBytes_GET_SIZE(path) == 0) {
3906 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003907 PyErr_SetString(PyExc_ImportError, "empty pathname");
3908 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 }
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003910
3911 rv = stat(PyBytes_AS_STRING(path), &statbuf);
3912 Py_DECREF(path);
3913 if (rv == 0) {
3914 /* it exists */
3915 if (S_ISDIR(statbuf.st_mode)) {
3916 /* it's a directory */
3917 PyErr_SetString(PyExc_ImportError, "existing directory");
3918 return -1;
3919 }
3920 }
3921#else /* MS_WINDOWS */
3922 PyObject *pathobj;
3923 DWORD rv;
Victor Stinner255dfdb2010-09-29 10:28:51 +00003924 wchar_t *path;
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003925
3926 if (!_PyArg_NoKeywords("NullImporter()", kwds))
3927 return -1;
3928
3929 if (!PyArg_ParseTuple(args, "U:NullImporter",
3930 &pathobj))
3931 return -1;
3932
3933 if (PyUnicode_GET_SIZE(pathobj) == 0) {
3934 PyErr_SetString(PyExc_ImportError, "empty pathname");
3935 return -1;
3936 }
3937
Victor Stinnerbeb4135b2010-10-07 01:02:42 +00003938 path = PyUnicode_AsWideCharString(pathobj, NULL);
Victor Stinner255dfdb2010-09-29 10:28:51 +00003939 if (path == NULL)
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003940 return -1;
3941 /* see issue1293 and issue3677:
3942 * stat() on Windows doesn't recognise paths like
3943 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
3944 */
3945 rv = GetFileAttributesW(path);
Victor Stinner255dfdb2010-09-29 10:28:51 +00003946 PyMem_Free(path);
Victor Stinner1a4d12d2010-08-13 13:07:29 +00003947 if (rv != INVALID_FILE_ATTRIBUTES) {
3948 /* it exists */
3949 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
3950 /* it's a directory */
3951 PyErr_SetString(PyExc_ImportError, "existing directory");
3952 return -1;
3953 }
3954 }
3955#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 return 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003957}
3958
3959static PyObject *
3960NullImporter_find_module(NullImporter *self, PyObject *args)
3961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003963}
3964
3965static PyMethodDef NullImporter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
3967 "Always return None"
3968 },
3969 {NULL} /* Sentinel */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003970};
3971
3972
Christian Heimes9cd17752007-11-18 19:35:23 +00003973PyTypeObject PyNullImporter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 PyVarObject_HEAD_INIT(NULL, 0)
3975 "imp.NullImporter", /*tp_name*/
3976 sizeof(NullImporter), /*tp_basicsize*/
3977 0, /*tp_itemsize*/
3978 0, /*tp_dealloc*/
3979 0, /*tp_print*/
3980 0, /*tp_getattr*/
3981 0, /*tp_setattr*/
3982 0, /*tp_reserved*/
3983 0, /*tp_repr*/
3984 0, /*tp_as_number*/
3985 0, /*tp_as_sequence*/
3986 0, /*tp_as_mapping*/
3987 0, /*tp_hash */
3988 0, /*tp_call*/
3989 0, /*tp_str*/
3990 0, /*tp_getattro*/
3991 0, /*tp_setattro*/
3992 0, /*tp_as_buffer*/
3993 Py_TPFLAGS_DEFAULT, /*tp_flags*/
3994 "Null importer object", /* tp_doc */
3995 0, /* tp_traverse */
3996 0, /* tp_clear */
3997 0, /* tp_richcompare */
3998 0, /* tp_weaklistoffset */
3999 0, /* tp_iter */
4000 0, /* tp_iternext */
4001 NullImporter_methods, /* tp_methods */
4002 0, /* tp_members */
4003 0, /* tp_getset */
4004 0, /* tp_base */
4005 0, /* tp_dict */
4006 0, /* tp_descr_get */
4007 0, /* tp_descr_set */
4008 0, /* tp_dictoffset */
4009 (initproc)NullImporter_init, /* tp_init */
4010 0, /* tp_alloc */
4011 PyType_GenericNew /* tp_new */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004012};
4013
Martin v. Löwis1a214512008-06-11 05:26:20 +00004014static struct PyModuleDef impmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 PyModuleDef_HEAD_INIT,
4016 "imp",
4017 doc_imp,
4018 0,
4019 imp_methods,
4020 NULL,
4021 NULL,
4022 NULL,
4023 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00004024};
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004025
Jason Tishler6bc06ec2003-09-04 11:59:50 +00004026PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00004027PyInit_imp(void)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 PyObject *m, *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 if (PyType_Ready(&PyNullImporter_Type) < 0)
4032 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 m = PyModule_Create(&impmodule);
4035 if (m == NULL)
4036 goto failure;
4037 d = PyModule_GetDict(m);
4038 if (d == NULL)
4039 goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
4042 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
4043 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
4044 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
4045 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
4046 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
4047 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
4048 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
4049 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
4050 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 Py_INCREF(&PyNullImporter_Type);
4053 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
4054 return m;
Guido van Rossumaee0bad1997-09-05 07:33:22 +00004055 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 Py_XDECREF(m);
4057 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004058}
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004059
4060
Guido van Rossumb18618d2000-05-03 23:44:39 +00004061/* API for embedding applications that want to add their own entries
4062 to the table of built-in modules. This should normally be called
4063 *before* Py_Initialize(). When the table resize fails, -1 is
4064 returned and the existing table is unchanged.
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004065
4066 After a similar function by Just van Rossum. */
4067
4068int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004069PyImport_ExtendInittab(struct _inittab *newtab)
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 static struct _inittab *our_copy = NULL;
4072 struct _inittab *p;
4073 int i, n;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 /* Count the number of entries in both tables */
4076 for (n = 0; newtab[n].name != NULL; n++)
4077 ;
4078 if (n == 0)
4079 return 0; /* Nothing to do */
4080 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
4081 ;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 /* Allocate new memory for the combined table */
4084 p = our_copy;
4085 PyMem_RESIZE(p, struct _inittab, i+n+1);
4086 if (p == NULL)
4087 return -1;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 /* Copy the tables into the new memory */
4090 if (our_copy != PyImport_Inittab)
4091 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
4092 PyImport_Inittab = our_copy = p;
4093 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 return 0;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004096}
4097
4098/* Shorthand to add a single entry given a name and a function */
4099
4100int
Brett Cannona826f322009-04-02 03:41:46 +00004101PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 struct _inittab newtab[2];
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 memset(newtab, '\0', sizeof newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 newtab[0].name = (char *)name;
4108 newtab[0].initfunc = initfunc;
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 return PyImport_ExtendInittab(newtab);
Guido van Rossum09cae1f1998-05-14 02:32:54 +00004111}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004112
4113#ifdef __cplusplus
4114}
4115#endif