annotate Lib/test/test_pkgutil.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 17b5afd321a8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46455
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
1 from test.test_support import run_unittest
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
2 import unittest
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
3 import sys
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
4 import imp
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
5 import pkgutil
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
6 import os
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
7 import os.path
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
8 import tempfile
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
9 import shutil
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
10 import zipfile
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
11
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
12
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
13
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
14 class PkgutilTests(unittest.TestCase):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
15
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
16 def setUp(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
17 self.dirname = tempfile.mkdtemp()
72798
17b5afd321a8 Issue #7367: Ensure test directory always gets removed.
Ned Deily <nad@acm.org>
parents: 72769
diff changeset
18 self.addCleanup(shutil.rmtree, self.dirname)
46455
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
19 sys.path.insert(0, self.dirname)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
20
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
21 def tearDown(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
22 del sys.path[0]
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
23
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
24 def test_getdata_filesys(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
25 pkg = 'test_getdata_filesys'
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
26
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
27 # Include a LF and a CRLF, to test that binary data is read back
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
28 RESOURCE_DATA = 'Hello, world!\nSecond line\r\nThird line'
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
29
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
30 # Make a package with some resources
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
31 package_dir = os.path.join(self.dirname, pkg)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
32 os.mkdir(package_dir)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
33 # Empty init.py
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
34 f = open(os.path.join(package_dir, '__init__.py'), "wb")
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
35 f.close()
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
36 # Resource files, res.txt, sub/res.txt
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
37 f = open(os.path.join(package_dir, 'res.txt'), "wb")
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
38 f.write(RESOURCE_DATA)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
39 f.close()
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
40 os.mkdir(os.path.join(package_dir, 'sub'))
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
41 f = open(os.path.join(package_dir, 'sub', 'res.txt'), "wb")
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
42 f.write(RESOURCE_DATA)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
43 f.close()
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
44
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
45 # Check we can read the resources
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
46 res1 = pkgutil.get_data(pkg, 'res.txt')
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
47 self.assertEqual(res1, RESOURCE_DATA)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
48 res2 = pkgutil.get_data(pkg, 'sub/res.txt')
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
49 self.assertEqual(res2, RESOURCE_DATA)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
50
46474
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46455
diff changeset
51 del sys.modules[pkg]
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46455
diff changeset
52
46455
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
53 def test_getdata_zipfile(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
54 zip = 'test_getdata_zipfile.zip'
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
55 pkg = 'test_getdata_zipfile'
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
56
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
57 # Include a LF and a CRLF, to test that binary data is read back
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
58 RESOURCE_DATA = 'Hello, world!\nSecond line\r\nThird line'
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
59
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
60 # Make a package with some resources
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
61 zip_file = os.path.join(self.dirname, zip)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
62 z = zipfile.ZipFile(zip_file, 'w')
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
63
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
64 # Empty init.py
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
65 z.writestr(pkg + '/__init__.py', "")
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
66 # Resource files, res.txt, sub/res.txt
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
67 z.writestr(pkg + '/res.txt', RESOURCE_DATA)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
68 z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
69 z.close()
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
70
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
71 # Check we can read the resources
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
72 sys.path.insert(0, zip_file)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
73 res1 = pkgutil.get_data(pkg, 'res.txt')
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
74 self.assertEqual(res1, RESOURCE_DATA)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
75 res2 = pkgutil.get_data(pkg, 'sub/res.txt')
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
76 self.assertEqual(res2, RESOURCE_DATA)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
77 del sys.path[0]
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
78
46474
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46455
diff changeset
79 del sys.modules[pkg]
5c43159f5ad0 Some tests did not pass on repeated calls (regrtest -R::)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46455
diff changeset
80
72769
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
81 def test_unreadable_dir_on_syspath(self):
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
82 # issue7367 - walk_packages failed if unreadable dir on sys.path
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
83 package_name = "unreadable_package"
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
84 d = os.path.join(self.dirname, package_name)
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
85 # this does not appear to create an unreadable dir on Windows
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
86 # but the test should not fail anyway
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
87 os.mkdir(d, 0)
72798
17b5afd321a8 Issue #7367: Ensure test directory always gets removed.
Ned Deily <nad@acm.org>
parents: 72769
diff changeset
88 self.addCleanup(os.rmdir, d)
72769
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
89 for t in pkgutil.walk_packages(path=[self.dirname]):
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
90 self.fail("unexpected package found")
096b010ae90b Issue #7367: Add test case to test_pkgutil for walking path with
Ned Deily <nad@acm.org>
parents: 46506
diff changeset
91
46455
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
92 class PkgutilPEP302Tests(unittest.TestCase):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
93
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
94 class MyTestLoader(object):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
95 def load_module(self, fullname):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
96 # Create an empty module
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
97 mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
98 mod.__file__ = "<%s>" % self.__class__.__name__
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
99 mod.__loader__ = self
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
100 # Make it a package
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
101 mod.__path__ = []
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
102 # Count how many times the module is reloaded
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
103 mod.__dict__['loads'] = mod.__dict__.get('loads',0) + 1
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
104 return mod
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
105
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
106 def get_data(self, path):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
107 return "Hello, world!"
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
108
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
109 class MyTestImporter(object):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
110 def find_module(self, fullname, path=None):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
111 return PkgutilPEP302Tests.MyTestLoader()
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
112
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
113 def setUp(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
114 sys.meta_path.insert(0, self.MyTestImporter())
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
115
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
116 def tearDown(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
117 del sys.meta_path[0]
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
118
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
119 def test_getdata_pep302(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
120 # Use a dummy importer/loader
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
121 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
122 del sys.modules['foo']
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
123
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
124 def test_alreadyloaded(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
125 # Ensure that get_data works without reloading - the "loads" module
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
126 # variable in the example loader should count how many times a reload
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
127 # occurs.
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
128 import foo
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
129 self.assertEqual(foo.loads, 1)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
130 self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
131 self.assertEqual(foo.loads, 1)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
132 del sys.modules['foo']
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
133
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
134 def test_main():
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
135 run_unittest(PkgutilTests, PkgutilPEP302Tests)
46506
6323bdc2d671 Correct an apparent refleak in test_pkgutil: zipimport._zip_directory_cache contains
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46474
diff changeset
136 # this is necessary if test is run repeated (like when finding leaks)
6323bdc2d671 Correct an apparent refleak in test_pkgutil: zipimport._zip_directory_cache contains
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46474
diff changeset
137 import zipimport
6323bdc2d671 Correct an apparent refleak in test_pkgutil: zipimport._zip_directory_cache contains
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46474
diff changeset
138 zipimport._zip_directory_cache.clear()
46455
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
139
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
140 if __name__ == '__main__':
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
141 test_main()