Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_pkgimport.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 3c9232a684ae |
| children | 0c49260e85a0 |
| rev | line source |
|---|---|
| 60648 | 1 import os |
| 2 import sys | |
| 3 import shutil | |
| 4 import string | |
| 5 import random | |
| 6 import tempfile | |
| 7 import unittest | |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
| 60648 | 9 from imp import cache_from_source |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43014
diff
changeset
|
10 from test.support import run_unittest |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 class TestImport(unittest.TestCase): |
|
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 __init__(self, *args, **kw): |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 self.package_name = 'PACKAGE_' |
|
39575
63237fcefd99
Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents:
32885
diff
changeset
|
16 while self.package_name in sys.modules: |
|
42585
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
39575
diff
changeset
|
17 self.package_name += random.choose(string.ascii_letters) |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 self.module_name = self.package_name + '.foo' |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 unittest.TestCase.__init__(self, *args, **kw) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 def remove_modules(self): |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 for module_name in (self.package_name, self.module_name): |
|
39575
63237fcefd99
Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents:
32885
diff
changeset
|
23 if module_name in sys.modules: |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 del sys.modules[module_name] |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 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
|
27 self.test_dir = tempfile.mkdtemp() |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 sys.path.append(self.test_dir) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 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
|
30 self.package_name) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 os.mkdir(self.package_dir) |
|
65768
b01fc3a42d35
Merged revisions 85984 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents:
55593
diff
changeset
|
32 open(os.path.join(self.package_dir, '__init__.py'), 'w').close() |
| 42622 | 33 self.module_path = os.path.join(self.package_dir, 'foo.py') |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 def tearDown(self): |
| 60648 | 36 shutil.rmtree(self.test_dir) |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 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
|
38 sys.path.remove(self.test_dir) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 self.remove_modules() |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 def rewrite_file(self, contents): |
| 60648 | 42 compiled_path = cache_from_source(self.module_path) |
| 43 if os.path.exists(compiled_path): | |
| 44 os.remove(compiled_path) | |
| 45 with open(self.module_path, 'w') as f: | |
| 46 f.write(contents) | |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18943
diff
changeset
|
47 |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 def test_package_import__semantics(self): |
|
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 # 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
|
51 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 # ...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
|
53 self.rewrite_file('for') |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 try: __import__(self.module_name) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 except SyntaxError: pass |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
56 else: raise RuntimeError('Failed to induce SyntaxError') # self.fail()? |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
57 self.assertNotIn(self.module_name, sys.modules) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
58 self.assertFalse(hasattr(sys.modules[self.package_name], 'foo')) |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 # ...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
|
61 var = 'a' |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 while var in dir(__builtins__): |
|
42585
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
39575
diff
changeset
|
63 var += random.choose(string.ascii_letters) |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 # ...make a module that just contains that |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 self.rewrite_file(var) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 try: __import__(self.module_name) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 except NameError: pass |
|
43014
9834301a2fd3
Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents:
42622
diff
changeset
|
70 else: raise RuntimeError('Failed to induce NameError.') |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 # ...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
|
73 # happen |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 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
|
75 module = __import__(self.module_name).foo |
|
18937
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 self.assertEqual(getattr(module, var), 1) |
|
f858f987e772
Unit test for improved package import semantics.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
78 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
79 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
|
80 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
|
81 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
82 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
83 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
|
84 test_main() |
