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.

131 lines
3.8 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} as it will break _importlib".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):
  73. self.modules = {}
  74. for name in names:
  75. if not name.endswith('.__init__'):
  76. import_name = name
  77. else:
  78. import_name = name[:-len('.__init__')]
  79. if '.' not in name:
  80. package = None
  81. elif import_name == name:
  82. package = name.rsplit('.', 1)[0]
  83. else:
  84. package = import_name
  85. module = imp.new_module(import_name)
  86. module.__loader__ = self
  87. module.__file__ = '<mock __file__>'
  88. module.__package__ = package
  89. module.attr = name
  90. if import_name != name:
  91. module.__path__ = ['<mock __path__>']
  92. self.modules[import_name] = module
  93. def __getitem__(self, name):
  94. return self.modules[name]
  95. def find_module(self, fullname, path=None):
  96. if fullname not in self.modules:
  97. return None
  98. else:
  99. return self
  100. def load_module(self, fullname):
  101. if fullname not in self.modules:
  102. raise ImportError
  103. else:
  104. sys.modules[fullname] = self.modules[fullname]
  105. return self.modules[fullname]
  106. def __enter__(self):
  107. self._uncache = uncache(*self.modules.keys())
  108. self._uncache.__enter__()
  109. return self
  110. def __exit__(self, *exc_info):
  111. self._uncache.__exit__(None, None, None)