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