annotate Lib/test/test_pkgimport.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 2aa7aef4e403
children dba99828d91c 6768b2350c57 63237fcefd99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18937
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 import os, sys, string, random, tempfile, unittest
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 20654
diff changeset
3 from test.test_support import run_unittest
18937
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 class TestImport(unittest.TestCase):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 def __init__(self, *args, **kw):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 self.package_name = 'PACKAGE_'
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 while sys.modules.has_key(self.package_name):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 self.package_name += random.choose(string.letters)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 self.module_name = self.package_name + '.foo'
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 unittest.TestCase.__init__(self, *args, **kw)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 def remove_modules(self):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 for module_name in (self.package_name, self.module_name):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 if sys.modules.has_key(module_name):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 del sys.modules[module_name]
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 def setUp(self):
24710
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
20 self.test_dir = tempfile.mkdtemp()
18937
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 sys.path.append(self.test_dir)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 self.package_dir = os.path.join(self.test_dir,
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 self.package_name)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 os.mkdir(self.package_dir)
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
25 open(os.path.join(self.package_dir, '__init__'+os.extsep+'py'), 'w')
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20013
diff changeset
26 self.module_path = os.path.join(self.package_dir, 'foo'+os.extsep+'py')
18937
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 def tearDown(self):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 for file in os.listdir(self.package_dir):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 os.remove(os.path.join(self.package_dir, file))
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 os.rmdir(self.package_dir)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 os.rmdir(self.test_dir)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 self.assertNotEqual(sys.path.count(self.test_dir), 0)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 sys.path.remove(self.test_dir)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 self.remove_modules()
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 def rewrite_file(self, contents):
18943
e01d29639d8e This test failed under python -O.
Tim Peters <tim.peters@gmail.com>
parents: 18937
diff changeset
38 for extension in "co":
e01d29639d8e This test failed under python -O.
Tim Peters <tim.peters@gmail.com>
parents: 18937
diff changeset
39 compiled_path = self.module_path + extension
e01d29639d8e This test failed under python -O.
Tim Peters <tim.peters@gmail.com>
parents: 18937
diff changeset
40 if os.path.exists(compiled_path):
e01d29639d8e This test failed under python -O.
Tim Peters <tim.peters@gmail.com>
parents: 18937
diff changeset
41 os.remove(compiled_path)
e01d29639d8e This test failed under python -O.
Tim Peters <tim.peters@gmail.com>
parents: 18937
diff changeset
42 f = open(self.module_path, 'w')
e01d29639d8e This test failed under python -O.
Tim Peters <tim.peters@gmail.com>
parents: 18937
diff changeset
43 f.write(contents)
e01d29639d8e This test failed under python -O.
Tim Peters <tim.peters@gmail.com>
parents: 18937
diff changeset
44 f.close()
19119
600a9a2b7a18 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18943
diff changeset
45
18937
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 def test_package_import__semantics(self):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 # Generate a couple of broken modules to try importing.
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # ...try loading the module when there's a SyntaxError
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 self.rewrite_file('for')
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 try: __import__(self.module_name)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 except SyntaxError: pass
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 else: raise RuntimeError, 'Failed to induce SyntaxError'
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 self.assert_(not sys.modules.has_key(self.module_name) and
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 not hasattr(sys.modules[self.package_name], 'foo'))
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # ...make up a variable name that isn't bound in __builtins__
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 var = 'a'
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 while var in dir(__builtins__):
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 var += random.choose(string.letters)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # ...make a module that just contains that
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 self.rewrite_file(var)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 try: __import__(self.module_name)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 except NameError: pass
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 else: raise RuntimeError, 'Failed to induce NameError.'
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 # ...now change the module so that the NameError doesn't
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 # happen
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 self.rewrite_file('%s = 1' % var)
32885
2aa7aef4e403 Removed reliance on damaged module object appearing in sys.modules
Tim Peters <tim.peters@gmail.com>
parents: 24710
diff changeset
73 module = __import__(self.module_name).foo
18937
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 self.assertEqual(getattr(module, var), 1)
f858f987e772 Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
76
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
77 def test_main():
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
78 run_unittest(TestImport)
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
79
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
80
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
81 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
82 test_main()