You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.0 KiB

  1. from contextlib import contextmanager
  2. import imp
  3. import os.path
  4. from test import support
  5. import unittest
  6. import sys
  7. CASE_INSENSITIVE_FS = True
  8. # Windows is the only OS that is *always* case-insensitive
  9. # (OS X *can* be case-sensitive).
  10. if sys.platform not in ('win32', 'cygwin'):
  11. changed_name = __file__.upper()
  12. if changed_name == __file__:
  13. changed_name = __file__.lower()
  14. if not os.path.exists(changed_name):
  15. CASE_INSENSITIVE_FS = False
  16. def case_insensitive_tests(test):
  17. """Class decorator that nullifies tests requiring a case-insensitive
  18. file system."""
  19. return unittest.skipIf(not CASE_INSENSITIVE_FS,
  20. "requires a case-insensitive filesystem")(test)
  21. @contextmanager
  22. def uncache(*names):
  23. """Uncache a module from sys.modules.
  24. A basic sanity check is performed to prevent uncaching modules that either
  25. cannot/shouldn't be uncached.
  26. """
  27. for name in names:
  28. if name in ('sys', 'marshal', 'imp'):
  29. raise ValueError(
  30. "cannot uncache {0}".format(name))
  31. try:
  32. del sys.modules[name]
  33. except KeyError:
  34. pass
  35. try:
  36. yield
  37. finally:
  38. for name in names:
  39. try:
  40. del sys.modules[name]
  41. except KeyError:
  42. pass
  43. @contextmanager
  44. def import_state(**kwargs):
  45. """Context manager to manage the various importers and stored state in the
  46. sys module.
  47. The 'modules' attribute is not supported as the interpreter state stores a
  48. pointer to the dict that the interpreter uses internally;
  49. reassigning to sys.modules does not have the desired effect.
  50. """
  51. originals = {}
  52. try:
  53. for attr, default in (('meta_path', []), ('path', []),
  54. ('path_hooks', []),
  55. ('path_importer_cache', {})):
  56. originals[attr] = getattr(sys, attr)
  57. if attr in kwargs:
  58. new_value = kwargs[attr]
  59. del kwargs[attr]
  60. else:
  61. new_value = default
  62. setattr(sys, attr, new_value)
  63. if len(kwargs):
  64. raise ValueError(
  65. 'unrecognized arguments: {0}'.format(kwargs.keys()))
  66. yield
  67. finally:
  68. for attr, value in originals.items():
  69. setattr(sys, attr, value)
  70. class mock_modules:
  71. """A mock importer/loader."""
  72. def __init__(self, *names, module_code={}):
  73. self.modules = {}
  74. self.module_code = {}
  75. for name in names:
  76. if not name.endswith('.__init__'):
  77. import_name = name
  78. else:
  79. import_name = name[:-len('.__init__')]
  80. if '.' not in name:
  81. package = None
  82. elif import_name == name:
  83. package = name.rsplit('.', 1)[0]
  84. else:
  85. package = import_name
  86. module = imp.new_module(import_name)
  87. module.__loader__ = self
  88. module.__file__ = '<mock __file__>'
  89. module.__package__ = package
  90. module.attr = name
  91. if import_name != name:
  92. module.__path__ = ['<mock __path__>']
  93. self.modules[import_name] = module
  94. if import_name in module_code:
  95. self.module_code[import_name] = module_code[import_name]
  96. def __getitem__(self, name):
  97. return self.modules[name]
  98. def find_module(self, fullname, path=None):
  99. if fullname not in self.modules:
  100. return None
  101. else:
  102. return self
  103. def load_module(self, fullname):
  104. if fullname not in self.modules:
  105. raise ImportError
  106. else:
  107. sys.modules[fullname] = self.modules[fullname]
  108. if fullname in self.module_code:
  109. self.module_code[fullname]()
  110. return self.modules[fullname]
  111. def __enter__(self):
  112. self._uncache = uncache(*self.modules.keys())
  113. self._uncache.__enter__()
  114. return self
  115. def __exit__(self, *exc_info):
  116. self._uncache.__exit__(None, None, None)