annotate Lib/test/test_pkgutil.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents 6323bdc2d671
children 096b010ae90b
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()
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
18 sys.path.insert(0, self.dirname)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
19
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
20 def tearDown(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
21 del sys.path[0]
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
22 shutil.rmtree(self.dirname)
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
46455
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
81 class PkgutilPEP302Tests(unittest.TestCase):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
82
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
83 class MyTestLoader(object):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
84 def load_module(self, fullname):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
85 # Create an empty module
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
86 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
87 mod.__file__ = "<%s>" % self.__class__.__name__
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
88 mod.__loader__ = self
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
89 # Make it a package
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
90 mod.__path__ = []
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
91 # Count how many times the module is reloaded
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
92 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
93 return mod
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
94
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
95 def get_data(self, path):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
96 return "Hello, world!"
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
97
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
98 class MyTestImporter(object):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
99 def find_module(self, fullname, path=None):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
100 return PkgutilPEP302Tests.MyTestLoader()
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
101
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
102 def setUp(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
103 sys.meta_path.insert(0, self.MyTestImporter())
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
104
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
105 def tearDown(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
106 del sys.meta_path[0]
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
107
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
108 def test_getdata_pep302(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
109 # Use a dummy importer/loader
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
110 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
111 del sys.modules['foo']
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 test_alreadyloaded(self):
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
114 # 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
115 # 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
116 # occurs.
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
117 import foo
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
118 self.assertEqual(foo.loads, 1)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
119 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
120 self.assertEqual(foo.loads, 1)
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
121 del sys.modules['foo']
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
122
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
123 def test_main():
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
124 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
125 # 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
126 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
127 zipimport._zip_directory_cache.clear()
46455
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
128
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
129 if __name__ == '__main__':
ef06ed804918 Add test file missing from rev 62350
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
130 test_main()