make file closing more robust
[python/dscho.git] / Lib / test / test_import.py
blobdbfadfb708992084a4f5361553e046cfc87e2585
1 import unittest
2 import os
3 import random
4 import shutil
5 import sys
6 import py_compile
7 import warnings
8 import imp
9 import marshal
10 from test.support import unlink, TESTFN, unload, run_unittest
13 def remove_files(name):
14 for f in (name + ".py",
15 name + ".pyc",
16 name + ".pyo",
17 name + ".pyw",
18 name + "$py.class"):
19 if os.path.exists(f):
20 os.remove(f)
23 class ImportTest(unittest.TestCase):
25 def testCaseSensitivity(self):
26 # Brief digression to test that import is case-sensitive: if we got this
27 # far, we know for sure that "random" exists.
28 try:
29 import RAnDoM
30 except ImportError:
31 pass
32 else:
33 self.fail("import of RAnDoM should have failed (case mismatch)")
35 def testDoubleConst(self):
36 # Another brief digression to test the accuracy of manifest float constants.
37 from test import double_const # don't blink -- that *was* the test
39 def testImport(self):
40 def test_with_extension(ext):
41 # ext normally ".py"; perhaps ".pyw"
42 source = TESTFN + ext
43 pyo = TESTFN + ".pyo"
44 if sys.platform.startswith('java'):
45 pyc = TESTFN + "$py.class"
46 else:
47 pyc = TESTFN + ".pyc"
49 with open(source, "w") as f:
50 print("# This tests Python's ability to import a", ext, "file.", file=f)
51 a = random.randrange(1000)
52 b = random.randrange(1000)
53 print("a =", a, file=f)
54 print("b =", b, file=f)
56 if TESTFN in sys.modules:
57 del sys.modules[TESTFN]
58 try:
59 try:
60 mod = __import__(TESTFN)
61 except ImportError as err:
62 self.fail("import from %s failed: %s" % (ext, err))
64 self.assertEquals(mod.a, a,
65 "module loaded (%s) but contents invalid" % mod)
66 self.assertEquals(mod.b, b,
67 "module loaded (%s) but contents invalid" % mod)
68 finally:
69 unlink(source)
70 unlink(pyc)
71 unlink(pyo)
72 del sys.modules[TESTFN]
74 sys.path.insert(0, os.curdir)
75 try:
76 test_with_extension(".py")
77 if sys.platform.startswith("win"):
78 for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
79 test_with_extension(ext)
80 finally:
81 del sys.path[0]
83 def testImpModule(self):
84 # Verify that the imp module can correctly load and find .py files
85 import imp
86 x = imp.find_module("os")
87 os = imp.load_module("os", *x)
89 def test_module_with_large_stack(self, module='longlist'):
90 # create module w/list of 65000 elements to test bug #561858
91 filename = module + '.py'
93 # create a file with a list of 65000 elements
94 f = open(filename, 'w+')
95 f.write('d = [\n')
96 for i in range(65000):
97 f.write('"",\n')
98 f.write(']')
99 f.close()
101 # compile & remove .py file, we only need .pyc (or .pyo)
102 f = open(filename, 'r')
103 py_compile.compile(filename)
104 f.close()
105 os.unlink(filename)
107 # need to be able to load from current dir
108 sys.path.append('')
110 # this used to crash
111 exec('import ' + module)
113 # cleanup
114 del sys.path[-1]
115 for ext in '.pyc', '.pyo':
116 fname = module + ext
117 if os.path.exists(fname):
118 os.unlink(fname)
120 def test_failing_import_sticks(self):
121 source = TESTFN + ".py"
122 f = open(source, "w")
123 print("a = 1/0", file=f)
124 f.close()
126 # New in 2.4, we shouldn't be able to import that no matter how often
127 # we try.
128 sys.path.insert(0, os.curdir)
129 if TESTFN in sys.modules:
130 del sys.modules[TESTFN]
131 try:
132 for i in 1, 2, 3:
133 try:
134 mod = __import__(TESTFN)
135 except ZeroDivisionError:
136 if TESTFN in sys.modules:
137 self.fail("damaged module in sys.modules on %i. try" % i)
138 else:
139 self.fail("was able to import a damaged module on %i. try" % i)
140 finally:
141 sys.path.pop(0)
142 remove_files(TESTFN)
144 def test_import_name_binding(self):
145 # import x.y.z binds x in the current namespace
146 import test as x
147 import test.support
148 self.assert_(x is test, x.__name__)
149 self.assert_(hasattr(test.support, "__file__"))
151 # import x.y.z as w binds z as w
152 import test.support as y
153 self.assert_(y is test.support, y.__name__)
155 def test_import_initless_directory_warning(self):
156 with warnings.catch_warnings():
157 # Just a random non-package directory we always expect to be
158 # somewhere in sys.path...
159 warnings.simplefilter('error', ImportWarning)
160 self.assertRaises(ImportWarning, __import__, "site-packages")
162 def test_failing_reload(self):
163 # A failing reload should leave the module object in sys.modules.
164 source = TESTFN + ".py"
165 with open(source, "w") as f:
166 f.write("a = 1\nb=2\n")
168 sys.path.insert(0, os.curdir)
169 try:
170 mod = __import__(TESTFN)
171 self.assert_(TESTFN in sys.modules, "expected module in sys.modules")
172 self.assertEquals(mod.a, 1, "module has wrong attribute values")
173 self.assertEquals(mod.b, 2, "module has wrong attribute values")
175 # On WinXP, just replacing the .py file wasn't enough to
176 # convince reload() to reparse it. Maybe the timestamp didn't
177 # move enough. We force it to get reparsed by removing the
178 # compiled file too.
179 remove_files(TESTFN)
181 # Now damage the module.
182 with open(source, "w") as f:
183 f.write("a = 10\nb=20//0\n")
185 self.assertRaises(ZeroDivisionError, imp.reload, mod)
186 # But we still expect the module to be in sys.modules.
187 mod = sys.modules.get(TESTFN)
188 self.failIf(mod is None, "expected module to still be in sys.modules")
190 # We should have replaced a w/ 10, but the old b value should
191 # stick.
192 self.assertEquals(mod.a, 10, "module has wrong attribute values")
193 self.assertEquals(mod.b, 2, "module has wrong attribute values")
195 finally:
196 sys.path.pop(0)
197 remove_files(TESTFN)
198 if TESTFN in sys.modules:
199 del sys.modules[TESTFN]
201 def test_file_to_source(self):
202 # check if __file__ points to the source file where available
203 source = TESTFN + ".py"
204 with open(source, "w") as f:
205 f.write("test = None\n")
207 sys.path.insert(0, os.curdir)
208 try:
209 mod = __import__(TESTFN)
210 self.failUnless(mod.__file__.endswith('.py'))
211 os.remove(source)
212 del sys.modules[TESTFN]
213 mod = __import__(TESTFN)
214 ext = mod.__file__[-4:]
215 self.failUnless(ext in ('.pyc', '.pyo'), ext)
216 finally:
217 sys.path.pop(0)
218 remove_files(TESTFN)
219 if TESTFN in sys.modules:
220 del sys.modules[TESTFN]
223 def test_importbyfilename(self):
224 path = os.path.abspath(TESTFN)
225 try:
226 __import__(path)
227 except ImportError as err:
228 self.assertEqual("Import by filename is not supported.",
229 err.args[0])
230 else:
231 self.fail("import by path didn't raise an exception")
233 class TestPycRewriting(unittest.TestCase):
234 # Test that the `co_filename` attribute on code objects always points
235 # to the right file, even when various things happen (e.g. both the .py
236 # and the .pyc file are renamed).
238 module_name = "unlikely_module_name"
239 module_source = """
240 import sys
241 code_filename = sys._getframe().f_code.co_filename
242 module_filename = __file__
243 constant = 1
244 def func():
245 pass
246 func_filename = func.__code__.co_filename
248 dir_name = os.path.abspath(TESTFN)
249 file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
250 compiled_name = file_name + ("c" if __debug__ else "o")
252 def setUp(self):
253 self.sys_path = sys.path[:]
254 self.orig_module = sys.modules.pop(self.module_name, None)
255 os.mkdir(self.dir_name)
256 with open(self.file_name, "w") as f:
257 f.write(self.module_source)
258 sys.path.insert(0, self.dir_name)
260 def tearDown(self):
261 sys.path[:] = self.sys_path
262 if self.orig_module is not None:
263 sys.modules[self.module_name] = self.orig_module
264 else:
265 del sys.modules[self.module_name]
266 for file_name in self.file_name, self.compiled_name:
267 if os.path.exists(file_name):
268 os.remove(file_name)
269 if os.path.exists(self.dir_name):
270 shutil.rmtree(self.dir_name)
272 def import_module(self):
273 ns = globals()
274 __import__(self.module_name, ns, ns)
275 return sys.modules[self.module_name]
277 def test_basics(self):
278 mod = self.import_module()
279 self.assertEqual(mod.module_filename, self.file_name)
280 self.assertEqual(mod.code_filename, self.file_name)
281 self.assertEqual(mod.func_filename, self.file_name)
282 del sys.modules[self.module_name]
283 mod = self.import_module()
284 self.assertEqual(mod.module_filename, self.file_name)
285 self.assertEqual(mod.code_filename, self.file_name)
286 self.assertEqual(mod.func_filename, self.file_name)
288 def test_incorrect_code_name(self):
289 py_compile.compile(self.file_name, dfile="another_module.py")
290 mod = self.import_module()
291 self.assertEqual(mod.module_filename, self.file_name)
292 self.assertEqual(mod.code_filename, self.file_name)
293 self.assertEqual(mod.func_filename, self.file_name)
295 def test_module_without_source(self):
296 target = "another_module.py"
297 py_compile.compile(self.file_name, dfile=target)
298 os.remove(self.file_name)
299 mod = self.import_module()
300 self.assertEqual(mod.module_filename, self.compiled_name)
301 self.assertEqual(mod.code_filename, target)
302 self.assertEqual(mod.func_filename, target)
304 def test_foreign_code(self):
305 py_compile.compile(self.file_name)
306 with open(self.compiled_name, "rb") as f:
307 header = f.read(8)
308 code = marshal.load(f)
309 constants = list(code.co_consts)
310 foreign_code = test_main.__code__
311 pos = constants.index(1)
312 constants[pos] = foreign_code
313 code = type(code)(code.co_argcount, code.co_kwonlyargcount,
314 code.co_nlocals, code.co_stacksize,
315 code.co_flags, code.co_code, tuple(constants),
316 code.co_names, code.co_varnames, code.co_filename,
317 code.co_name, code.co_firstlineno, code.co_lnotab,
318 code.co_freevars, code.co_cellvars)
319 with open(self.compiled_name, "wb") as f:
320 f.write(header)
321 marshal.dump(code, f)
322 mod = self.import_module()
323 self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
325 class PathsTests(unittest.TestCase):
326 SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',
327 'test\u00b0\u00b3\u00b2')
328 path = TESTFN
330 def setUp(self):
331 os.mkdir(self.path)
332 self.syspath = sys.path[:]
334 def tearDown(self):
335 shutil.rmtree(self.path)
336 sys.path = self.syspath
338 # http://bugs.python.org/issue1293
339 def test_trailing_slash(self):
340 f = open(os.path.join(self.path, 'test_trailing_slash.py'), 'w')
341 f.write("testdata = 'test_trailing_slash'")
342 f.close()
343 sys.path.append(self.path+'/')
344 mod = __import__("test_trailing_slash")
345 self.assertEqual(mod.testdata, 'test_trailing_slash')
346 unload("test_trailing_slash")
348 # http://bugs.python.org/issue3677
349 def _test_UNC_path(self):
350 f = open(os.path.join(self.path, 'test_trailing_slash.py'), 'w')
351 f.write("testdata = 'test_trailing_slash'")
352 f.close()
353 #create the UNC path, like \\myhost\c$\foo\bar
354 path = os.path.abspath(self.path)
355 import socket
356 hn = socket.gethostname()
357 drive = path[0]
358 unc = "\\\\%s\\%s$"%(hn, drive)
359 unc += path[2:]
360 sys.path.append(path)
361 mod = __import__("test_trailing_slash")
362 self.assertEqual(mod.testdata, 'test_trailing_slash')
363 unload("test_trailing_slash")
365 if sys.platform == "win32":
366 test_UNC_path = _test_UNC_path
369 class RelativeImport(unittest.TestCase):
370 def tearDown(self):
371 try:
372 del sys.modules["test.relimport"]
373 except:
374 pass
376 def test_relimport_star(self):
377 # This will import * from .test_import.
378 from . import relimport
379 self.assertTrue(hasattr(relimport, "RelativeImport"))
381 def test_issue3221(self):
382 # Note for mergers: the 'absolute' tests from the 2.x branch
383 # are missing in Py3k because implicit relative imports are
384 # a thing of the past
385 def check_relative():
386 exec("from . import relimport", ns)
387 # Check relative import OK with __package__ and __name__ correct
388 ns = dict(__package__='test', __name__='test.notarealmodule')
389 check_relative()
390 # Check relative import OK with only __name__ wrong
391 ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
392 check_relative()
393 # Check relative import fails with only __package__ wrong
394 ns = dict(__package__='foo', __name__='test.notarealmodule')
395 self.assertRaises(SystemError, check_relative)
396 # Check relative import fails with __package__ and __name__ wrong
397 ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
398 self.assertRaises(SystemError, check_relative)
399 # Check relative import fails with package set to a non-string
400 ns = dict(__package__=object())
401 self.assertRaises(ValueError, check_relative)
403 def test_main(verbose=None):
404 run_unittest(ImportTest, TestPycRewriting, PathsTests, RelativeImport)
406 if __name__ == '__main__':
407 # test needs to be a package, so we can do relative import
408 from test.test_import import test_main
409 test_main()