def test_disable_hooks(self): """ Tests the old (deprecated) names. These deprecated aliases should be removed by version 1.0 """ example_PY2_check = False standard_library.enable_hooks() # deprecated name old_meta_path = copy.copy(sys.meta_path) standard_library.disable_hooks() standard_library.scrub_future_sys_modules() if utils.PY2: self.assertTrue(len(old_meta_path) == len(sys.meta_path) + 1) else: self.assertTrue(len(old_meta_path) == len(sys.meta_path)) # An example of fragile import code that we don't want to break: try: import builtins except ImportError: example_PY2_check = True if utils.PY2: self.assertTrue(example_PY2_check) else: self.assertFalse(example_PY2_check) standard_library.install_hooks() # Imports should succeed again now: import builtins import html if utils.PY2: self.assertTrue(standard_library.detect_hooks()) self.assertTrue(len(old_meta_path) == len(sys.meta_path))
def run_pyflakes_for_package(package_name, extra_ignore=None): """ If pyflakes is installed, run it across the given package name returning any warnings found. """ # Pyflakes v0.6.1 incorrectly assumes we're running under Python 3 # if "import builtins" succeeds, which it does with "from future # import standard_library" in effect. standard_library.disable_hooks() from pyflakes.checker import Checker standard_library.enable_hooks() def pyflakes_checker(path): with open(path, "U") as source_file: source = source_file.read() try: tree = compile(source, path, "exec", PyCF_ONLY_AST) except (SyntaxError, IndentationError) as value: info = (path, value.lineno, value.args[0]) yield "Invalid syntax in %s:%d: %s" % info else: result = Checker(tree, path) for warning in result.messages: yield str(warning) args = (pyflakes_checker, package_name, extra_ignore) return _run_checker_for_package(*args)
def test_disable_hooks(self): example_PY2_check = False standard_library.disable_hooks() # An example of fragile import code that we don't want to break: try: import builtins except ImportError: example_PY2_check = True if utils.PY2: self.assertTrue(example_PY2_check) else: self.assertFalse(example_PY2_check) standard_library.enable_hooks() # The import should succeed again now: import builtins