[Python-checkins] distutils2: better resolve_dotted_name
tarek.ziade
python-checkins at python.org
Thu Aug 19 08:34:13 CEST 2010
tarek.ziade pushed 8604326d8247 to distutils2:
http://hg.python.org/distutils2/rev/8604326d8247
changeset: 550:8604326d8247
parent: 521:e51ba85f26cd
user: Konrad Delong <konryd at gmail.com>
date: Mon Aug 09 17:58:39 2010 +0200
summary: better resolve_dotted_name
files: src/distutils2/tests/test_util.py, src/distutils2/util.py
diff --git a/src/distutils2/tests/test_util.py b/src/distutils2/tests/test_util.py
--- a/src/distutils2/tests/test_util.py
+++ b/src/distutils2/tests/test_util.py
@@ -352,6 +352,21 @@
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
--- a/src/distutils2/util.py
+++ b/src/distutils2/util.py
@@ -636,25 +636,29 @@
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,
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list