test_pkgutil.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from test.test_support import run_unittest
  2. import unittest
  3. import sys
  4. import imp
  5. import pkgutil
  6. import os
  7. import os.path
  8. import tempfile
  9. import shutil
  10. import zipfile
  11. class PkgutilTests(unittest.TestCase):
  12. def setUp(self):
  13. self.dirname = tempfile.mkdtemp()
  14. self.addCleanup(shutil.rmtree, self.dirname)
  15. sys.path.insert(0, self.dirname)
  16. def tearDown(self):
  17. del sys.path[0]
  18. def test_getdata_filesys(self):
  19. pkg = 'test_getdata_filesys'
  20. # Include a LF and a CRLF, to test that binary data is read back
  21. RESOURCE_DATA = 'Hello, world!\nSecond line\r\nThird line'
  22. # Make a package with some resources
  23. package_dir = os.path.join(self.dirname, pkg)
  24. os.mkdir(package_dir)
  25. # Empty init.py
  26. f = open(os.path.join(package_dir, '__init__.py'), "wb")
  27. f.close()
  28. # Resource files, res.txt, sub/res.txt
  29. f = open(os.path.join(package_dir, 'res.txt'), "wb")
  30. f.write(RESOURCE_DATA)
  31. f.close()
  32. os.mkdir(os.path.join(package_dir, 'sub'))
  33. f = open(os.path.join(package_dir, 'sub', 'res.txt'), "wb")
  34. f.write(RESOURCE_DATA)
  35. f.close()
  36. # Check we can read the resources
  37. res1 = pkgutil.get_data(pkg, 'res.txt')
  38. self.assertEqual(res1, RESOURCE_DATA)
  39. res2 = pkgutil.get_data(pkg, 'sub/res.txt')
  40. self.assertEqual(res2, RESOURCE_DATA)
  41. del sys.modules[pkg]
  42. def test_getdata_zipfile(self):
  43. zip = 'test_getdata_zipfile.zip'
  44. pkg = 'test_getdata_zipfile'
  45. # Include a LF and a CRLF, to test that binary data is read back
  46. RESOURCE_DATA = 'Hello, world!\nSecond line\r\nThird line'
  47. # Make a package with some resources
  48. zip_file = os.path.join(self.dirname, zip)
  49. z = zipfile.ZipFile(zip_file, 'w')
  50. # Empty init.py
  51. z.writestr(pkg + '/__init__.py', "")
  52. # Resource files, res.txt, sub/res.txt
  53. z.writestr(pkg + '/res.txt', RESOURCE_DATA)
  54. z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA)
  55. z.close()
  56. # Check we can read the resources
  57. sys.path.insert(0, zip_file)
  58. res1 = pkgutil.get_data(pkg, 'res.txt')
  59. self.assertEqual(res1, RESOURCE_DATA)
  60. res2 = pkgutil.get_data(pkg, 'sub/res.txt')
  61. self.assertEqual(res2, RESOURCE_DATA)
  62. del sys.path[0]
  63. del sys.modules[pkg]
  64. def test_unreadable_dir_on_syspath(self):
  65. # issue7367 - walk_packages failed if unreadable dir on sys.path
  66. package_name = "unreadable_package"
  67. d = os.path.join(self.dirname, package_name)
  68. # this does not appear to create an unreadable dir on Windows
  69. # but the test should not fail anyway
  70. os.mkdir(d, 0)
  71. self.addCleanup(os.rmdir, d)
  72. for t in pkgutil.walk_packages(path=[self.dirname]):
  73. self.fail("unexpected package found")
  74. class PkgutilPEP302Tests(unittest.TestCase):
  75. class MyTestLoader(object):
  76. def load_module(self, fullname):
  77. # Create an empty module
  78. mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
  79. mod.__file__ = "<%s>" % self.__class__.__name__
  80. mod.__loader__ = self
  81. # Make it a package
  82. mod.__path__ = []
  83. # Count how many times the module is reloaded
  84. mod.__dict__['loads'] = mod.__dict__.get('loads',0) + 1
  85. return mod
  86. def get_data(self, path):
  87. return "Hello, world!"
  88. class MyTestImporter(object):
  89. def find_module(self, fullname, path=None):
  90. return PkgutilPEP302Tests.MyTestLoader()
  91. def setUp(self):
  92. sys.meta_path.insert(0, self.MyTestImporter())
  93. def tearDown(self):
  94. del sys.meta_path[0]
  95. def test_getdata_pep302(self):
  96. # Use a dummy importer/loader
  97. self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
  98. del sys.modules['foo']
  99. def test_alreadyloaded(self):
  100. # Ensure that get_data works without reloading - the "loads" module
  101. # variable in the example loader should count how many times a reload
  102. # occurs.
  103. import foo
  104. self.assertEqual(foo.loads, 1)
  105. self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
  106. self.assertEqual(foo.loads, 1)
  107. del sys.modules['foo']
  108. def test_main():
  109. run_unittest(PkgutilTests, PkgutilPEP302Tests)
  110. # this is necessary if test is run repeated (like when finding leaks)
  111. import zipimport
  112. zipimport._zip_directory_cache.clear()
  113. if __name__ == '__main__':
  114. test_main()