summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonrad Delong <konryd@gmail.com>2010-08-09 17:58:39 +0200
committerKonrad Delong <konryd@gmail.com>2010-08-09 17:58:39 +0200
commita7fc8b08965b3f56045b0df1887f2c8a4aaf3d1d (patch)
tree1a1889771cf46e17eb3c361212333bcaef13a9d8 /src
parent760df097438dff3b81fc0d12a4cddcf723dbf6c4 (diff)
downloaddisutils2-a7fc8b08965b3f56045b0df1887f2c8a4aaf3d1d.tar.gz
better resolve_dotted_name
Diffstat (limited to 'src')
-rw-r--r--src/distutils2/tests/test_util.py15
-rw-r--r--src/distutils2/util.py24
2 files changed, 29 insertions, 10 deletions
diff --git a/src/distutils2/tests/test_util.py b/src/distutils2/tests/test_util.py
index 26f4ee1..71011c2 100644
--- a/src/distutils2/tests/test_util.py
+++ b/src/distutils2/tests/test_util.py
@@ -352,6 +352,21 @@ class UtilTestCase(support.EnvironGuard,
self.assertRaises(ImportError, resolve_dotted_name,
"distutils2.tests.test_util.UtilTestCase.nonexistent_attribute")
+ def test_import_nested_first_time(self):
+ tmp_dir = self.mkdtemp()
+ os.makedirs(os.path.join(tmp_dir, 'a', 'b'))
+ self.write_file(os.path.join(tmp_dir, 'a', '__init__.py'), '')
+ self.write_file(os.path.join(tmp_dir, 'a', 'b', '__init__.py'), '')
+ self.write_file(os.path.join(tmp_dir, 'a', 'b', 'c.py'), 'class Foo: pass')
+
+ try:
+ sys.path.append(tmp_dir)
+ resolve_dotted_name("a.b.c.Foo")
+ # assert nothing raised
+ finally:
+ sys.path.remove(tmp_dir)
+
+
@unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher')
def test_run_2to3_on_code(self):
content = "print 'test'"
diff --git a/src/distutils2/util.py b/src/distutils2/util.py
index 178cb95..2a97df6 100644
--- a/src/distutils2/util.py
+++ b/src/distutils2/util.py
@@ -636,25 +636,29 @@ def find_packages(paths=(os.curdir,), exclude=()):
packages.append(package_name)
return packages
+def resolve_dotted_name(name):
+ """Resolves the name and returns the corresponding object."""
+ parts = name.split('.')
+ cursor = len(parts)
+ module_name, rest = parts[:cursor], parts[cursor:]
-def resolve_dotted_name(dotted_name):
- module_name, rest = dotted_name.split('.')[0], dotted_name.split('.')[1:]
- while len(rest) > 0:
+ while cursor > 0:
try:
- ret = __import__(module_name)
+ ret = __import__('.'.join(module_name))
break
except ImportError:
- if rest == []:
+ if cursor == 0:
raise
- module_name += ('.' + rest[0])
- rest = rest[1:]
- while len(rest) > 0:
+ cursor -= 1
+ module_name = parts[:cursor]
+ rest = parts[cursor:]
+
+ for part in parts[1:]:
try:
- ret = getattr(ret, rest.pop(0))
+ ret = getattr(ret, part)
except AttributeError:
raise ImportError
return ret
-
# utility functions for 2to3 support
def run_2to3(files, doctests_only=False, fixer_names=None, options=None,