r66707 - in sandbox/trunk/2to3/lib2to3: fixes/fix_import.py tests/test_fixers.py
Author: benjamin.peterson Date: Wed Oct 1 01:27:10 2008 New Revision: 66707 Log: fix #4001: fix_imports didn't check for __init__.py before converting to relative imports Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_import.py sandbox/trunk/2to3/lib2to3/tests/test_fixers.py Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_import.py ============================================================================== --- sandbox/trunk/2to3/lib2to3/fixes/fix_import.py (original) +++ sandbox/trunk/2to3/lib2to3/fixes/fix_import.py Wed Oct 1 01:27:10 2008 @@ -54,6 +54,10 @@ imp_name = imp_name.split('.', 1)[0].strip() base_path = dirname(file_path) base_path = join(base_path, imp_name) + # If there is no __init__.py next to the file its not in a package + # so can't be a relative import. + if not exists(join(dirname(base_path), '__init__.py')): + return False for ext in ['.py', pathsep, '.pyc', '.so', '.sl', '.pyd']: if exists(base_path + ext): return True Modified: sandbox/trunk/2to3/lib2to3/tests/test_fixers.py ============================================================================== --- sandbox/trunk/2to3/lib2to3/tests/test_fixers.py (original) +++ sandbox/trunk/2to3/lib2to3/tests/test_fixers.py Wed Oct 1 01:27:10 2008 @@ -9,10 +9,10 @@ import support # Python imports +import os import unittest from itertools import chain from operator import itemgetter -from os.path import dirname, pathsep # Local imports from .. import pygram @@ -3272,14 +3272,19 @@ # Need to replace fix_import's exists method # so we can check that it's doing the right thing self.files_checked = [] + self.present_files = set() self.always_exists = True def fake_exists(name): self.files_checked.append(name) - return self.always_exists + return self.always_exists or (name in self.present_files) from ..fixes import fix_import fix_import.exists = fake_exists + def tearDown(self): + from lib2to3.fixes import fix_import + fix_import.exists = os.path.exists + def check_both(self, b, a): self.always_exists = True FixerTestCase.check(self, b, a) @@ -3289,10 +3294,12 @@ def test_files_checked(self): def p(path): # Takes a unix path and returns a path with correct separators - return pathsep.join(path.split("/")) + return os.path.pathsep.join(path.split("/")) self.always_exists = False - expected_extensions = ('.py', pathsep, '.pyc', '.so', '.sl', '.pyd') + self.present_files = set(['__init__.py']) + expected_extensions = ('.py', os.path.pathsep, '.pyc', '.so', + '.sl', '.pyd') names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py")) for name in names_to_test: @@ -3300,11 +3307,32 @@ self.filename = name self.unchanged("import jam") - if dirname(name): name = dirname(name) + '/jam' - else: name = 'jam' + if os.path.dirname(name): + name = os.path.dirname(name) + '/jam' + else: + name = 'jam' expected_checks = set(name + ext for ext in expected_extensions) + expected_checks.add("__init__.py") + + self.assertEqual(set(self.files_checked), expected_checks) + + def test_not_in_package(self): + s = "import bar" + self.always_exists = False + self.present_files = set(["bar.py"]) + self.unchanged(s) - self.failUnlessEqual(set(self.files_checked), expected_checks) + def test_in_package(self): + b = "import bar" + a = "from . import bar" + self.always_exists = False + self.present_files = set(["__init__.py", "bar.py"]) + self.check(b, a) + + def test_comments_and_indent(self): + b = "import bar # Foo" + a = "from . import bar # Foo" + self.check(b, a) def test_from(self): b = "from foo import bar, baz"
participants (1)
-
benjamin.peterson