10 from test.test_support import (unlink, TESTFN, unload, run_unittest,
11 check_warnings, TestFailed)
14 def remove_files(name):
15 for f in (name + os.extsep + "py",
16 name + os.extsep + "pyc",
17 name + os.extsep + "pyo",
18 name + os.extsep + "pyw",
24 class ImportTest(unittest.TestCase):
26 def testCaseSensitivity(self):
27 # Brief digression to test that import is case-sensitive: if we got this
28 # far, we know for sure that "random" exists.
34 self.fail("import of RAnDoM should have failed (case mismatch)")
36 def testDoubleConst(self):
37 # Another brief digression to test the accuracy of manifest float constants.
38 from test import double_const # don't blink -- that *was* the test
41 def test_with_extension(ext):
42 # ext normally ".py"; perhaps ".pyw"
44 pyo = TESTFN + os.extsep + "pyo"
45 if sys.platform.startswith('java'):
46 pyc = TESTFN + "$py.class"
48 pyc = TESTFN + os.extsep + "pyc"
51 print >> f, "# This tests Python's ability to import a", ext, "file."
52 a = random.randrange(1000)
53 b = random.randrange(1000)
60 mod = __import__(TESTFN)
61 except ImportError, 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)
74 except ImportError, err:
75 self.fail("import from .pyc/.pyo failed: %s" % err)
85 del sys.modules[TESTFN]
87 sys.path.insert(0, os.curdir)
89 test_with_extension(os.extsep + "py")
90 if sys.platform.startswith("win"):
91 for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
92 test_with_extension(ext)
96 if os.name == 'posix':
97 def test_execute_bit_not_copied(self):
98 # Issue 6070: under posix .pyc files got their execute bit set if
99 # the .py file had the execute bit set, but they aren't executable.
100 oldmask = os.umask(022)
101 sys.path.insert(0, os.curdir)
103 fname = TESTFN + os.extsep + "py"
104 f = open(fname, 'w').close()
105 os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH |
106 stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
109 if not os.path.exists(fn):
111 if not os.path.exists(fn): raise TestFailed("__import__ did "
112 "not result in creation of either a .pyc or .pyo file")
114 self.assertEquals(stat.S_IMODE(s.st_mode),
115 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
119 if TESTFN in sys.modules: del sys.modules[TESTFN]
122 def testImpModule(self):
123 # Verify that the imp module can correctly load and find .py files
125 x = imp.find_module("os")
126 os = imp.load_module("os", *x)
128 def test_module_with_large_stack(self, module='longlist'):
129 # create module w/list of 65000 elements to test bug #561858
130 filename = module + os.extsep + 'py'
132 # create a file with a list of 65000 elements
133 f = open(filename, 'w+')
135 for i in range(65000):
140 # compile & remove .py file, we only need .pyc (or .pyo)
141 f = open(filename, 'r')
142 py_compile.compile(filename)
146 # need to be able to load from current dir
150 exec 'import ' + module
154 for ext in 'pyc', 'pyo':
155 fname = module + os.extsep + ext
156 if os.path.exists(fname):
159 def test_failing_import_sticks(self):
160 source = TESTFN + os.extsep + "py"
161 f = open(source, "w")
162 print >> f, "a = 1/0"
165 # New in 2.4, we shouldn't be able to import that no matter how often
167 sys.path.insert(0, os.curdir)
171 mod = __import__(TESTFN)
172 except ZeroDivisionError:
173 if TESTFN in sys.modules:
174 self.fail("damaged module in sys.modules on %i. try" % i)
176 self.fail("was able to import a damaged module on %i. try" % i)
181 def test_failing_reload(self):
182 # A failing reload should leave the module object in sys.modules.
183 source = TESTFN + os.extsep + "py"
184 f = open(source, "w")
189 sys.path.insert(0, os.curdir)
191 mod = __import__(TESTFN)
192 self.assert_(TESTFN in sys.modules, "expected module in sys.modules")
193 self.assertEquals(mod.a, 1, "module has wrong attribute values")
194 self.assertEquals(mod.b, 2, "module has wrong attribute values")
196 # On WinXP, just replacing the .py file wasn't enough to
197 # convince reload() to reparse it. Maybe the timestamp didn't
198 # move enough. We force it to get reparsed by removing the
202 # Now damage the module.
203 f = open(source, "w")
205 print >> f, "b = 20//0"
208 self.assertRaises(ZeroDivisionError, reload, mod)
210 # But we still expect the module to be in sys.modules.
211 mod = sys.modules.get(TESTFN)
212 self.failIf(mod is None, "expected module to still be in sys.modules")
214 # We should have replaced a w/ 10, but the old b value should
216 self.assertEquals(mod.a, 10, "module has wrong attribute values")
217 self.assertEquals(mod.b, 2, "module has wrong attribute values")
222 if TESTFN in sys.modules:
223 del sys.modules[TESTFN]
225 def test_infinite_reload(self):
226 # Bug #742342 reports that Python segfaults (infinite recursion in C)
227 # when faced with self-recursive reload()ing.
229 sys.path.insert(0, os.path.dirname(__file__))
231 import infinite_reload
235 def test_import_name_binding(self):
236 # import x.y.z binds x in the current namespace
238 import test.test_support
239 self.assert_(x is test, x.__name__)
240 self.assert_(hasattr(test.test_support, "__file__"))
242 # import x.y.z as w binds z as w
243 import test.test_support as y
244 self.assert_(y is test.test_support, y.__name__)
246 def test_import_initless_directory_warning(self):
247 with warnings.catch_warnings():
248 # Just a random non-package directory we always expect to be
249 # somewhere in sys.path...
250 warnings.simplefilter('error', ImportWarning)
251 self.assertRaises(ImportWarning, __import__, "site-packages")
253 def test_importbyfilename(self):
254 path = os.path.abspath(TESTFN)
257 except ImportError, err:
258 self.assertEqual("Import by filename is not supported.",
261 self.fail("import by path didn't raise an exception")
264 class TestPycRewriting(unittest.TestCase):
265 # Test that the `co_filename` attribute on code objects always points
266 # to the right file, even when various things happen (e.g. both the .py
267 # and the .pyc file are renamed).
269 module_name = "unlikely_module_name"
272 code_filename = sys._getframe().f_code.co_filename
273 module_filename = __file__
277 func_filename = func.func_code.co_filename
279 dir_name = os.path.abspath(TESTFN)
280 file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
281 compiled_name = file_name + ("c" if __debug__ else "o")
284 self.sys_path = sys.path[:]
285 self.orig_module = sys.modules.pop(self.module_name, None)
286 os.mkdir(self.dir_name)
287 with open(self.file_name, "w") as f:
288 f.write(self.module_source)
289 sys.path.insert(0, self.dir_name)
292 sys.path[:] = self.sys_path
293 if self.orig_module is not None:
294 sys.modules[self.module_name] = self.orig_module
296 del sys.modules[self.module_name]
297 for file_name in self.file_name, self.compiled_name:
298 if os.path.exists(file_name):
300 if os.path.exists(self.dir_name):
301 shutil.rmtree(self.dir_name)
303 def import_module(self):
305 __import__(self.module_name, ns, ns)
306 return sys.modules[self.module_name]
308 def test_basics(self):
309 mod = self.import_module()
310 self.assertEqual(mod.module_filename, self.file_name)
311 self.assertEqual(mod.code_filename, self.file_name)
312 self.assertEqual(mod.func_filename, self.file_name)
313 del sys.modules[self.module_name]
314 mod = self.import_module()
315 self.assertEqual(mod.module_filename, self.compiled_name)
316 self.assertEqual(mod.code_filename, self.file_name)
317 self.assertEqual(mod.func_filename, self.file_name)
319 def test_incorrect_code_name(self):
320 py_compile.compile(self.file_name, dfile="another_module.py")
321 mod = self.import_module()
322 self.assertEqual(mod.module_filename, self.compiled_name)
323 self.assertEqual(mod.code_filename, self.file_name)
324 self.assertEqual(mod.func_filename, self.file_name)
326 def test_module_without_source(self):
327 target = "another_module.py"
328 py_compile.compile(self.file_name, dfile=target)
329 os.remove(self.file_name)
330 mod = self.import_module()
331 self.assertEqual(mod.module_filename, self.compiled_name)
332 self.assertEqual(mod.code_filename, target)
333 self.assertEqual(mod.func_filename, target)
335 def test_foreign_code(self):
336 py_compile.compile(self.file_name)
337 with open(self.compiled_name, "rb") as f:
339 code = marshal.load(f)
340 constants = list(code.co_consts)
341 foreign_code = test_main.func_code
342 pos = constants.index(1)
343 constants[pos] = foreign_code
344 code = type(code)(code.co_argcount, code.co_nlocals, code.co_stacksize,
345 code.co_flags, code.co_code, tuple(constants),
346 code.co_names, code.co_varnames, code.co_filename,
347 code.co_name, code.co_firstlineno, code.co_lnotab,
348 code.co_freevars, code.co_cellvars)
349 with open(self.compiled_name, "wb") as f:
351 marshal.dump(code, f)
352 mod = self.import_module()
353 self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
355 class PathsTests(unittest.TestCase):
360 self.syspath = sys.path[:]
363 shutil.rmtree(self.path)
364 sys.path = self.syspath
366 # http://bugs.python.org/issue1293
367 def test_trailing_slash(self):
368 f = open(os.path.join(self.path, 'test_trailing_slash.py'), 'w')
369 f.write("testdata = 'test_trailing_slash'")
371 sys.path.append(self.path+'/')
372 mod = __import__("test_trailing_slash")
373 self.assertEqual(mod.testdata, 'test_trailing_slash')
374 unload("test_trailing_slash")
376 class RelativeImport(unittest.TestCase):
379 del sys.modules["test.relimport"]
383 def test_relimport_star(self):
384 # This will import * from .test_import.
385 from . import relimport
386 self.assertTrue(hasattr(relimport, "RelativeImport"))
388 def test_issue3221(self):
389 def check_absolute():
390 exec "from os import path" in ns
391 def check_relative():
392 exec "from . import relimport" in ns
393 # Check both OK with __package__ and __name__ correct
394 ns = dict(__package__='test', __name__='test.notarealmodule')
397 # Check both OK with only __name__ wrong
398 ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
401 # Check relative fails with only __package__ wrong
402 ns = dict(__package__='foo', __name__='test.notarealmodule')
403 with check_warnings() as w:
405 self.assert_('foo' in str(w.message))
406 self.assertEqual(w.category, RuntimeWarning)
407 self.assertRaises(SystemError, check_relative)
408 # Check relative fails with __package__ and __name__ wrong
409 ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
410 with check_warnings() as w:
412 self.assert_('foo' in str(w.message))
413 self.assertEqual(w.category, RuntimeWarning)
414 self.assertRaises(SystemError, check_relative)
415 # Check both fail with package set to a non-string
416 ns = dict(__package__=object())
417 self.assertRaises(ValueError, check_absolute)
418 self.assertRaises(ValueError, check_relative)
420 def test_main(verbose=None):
421 run_unittest(ImportTest, TestPycRewriting, PathsTests, RelativeImport)
423 if __name__ == '__main__':
424 # test needs to be a package, so we can do relative import
425 from test.test_import import test_main