Ralf Schmitt avatar Ralf Schmitt committed 144664a

allow dotted name lookups, e.g. "fromkeys = 'UserDict:UserDict.fromkeys'.

Comments (0)

Files changed (2)

 def importobj(modpath, attrname):
     module = __import__(modpath, None, None, ['__doc__'])
-    if attrname:
-        return getattr(module, attrname)
-    return module
+    if not attrname:
+        return module
+    retval = module
+    names = attrname.split(".")
+    for x in names:
+        retval = getattr(retval, x)
+    return retval
 class ApiModule(ModuleType):
     def __init__(self, name, importspec, implprefix=None, attr=None):
     assert res == 0
+def test_dotted_name_lookup(tmpdir, monkeypatch):
+    pkgdir = tmpdir.mkdir("dotted_name_lookup")
+    pkgdir.join('__init__.py').write(py.code.Source("""
+        import apipkg
+        apipkg.initpkg(__name__, dict(fromkeys='UserDict:UserDict.fromkeys'))
+    """))
+    monkeypatch.syspath_prepend(tmpdir)
+    import dotted_name_lookup
+    d = dotted_name_lookup.fromkeys(["a", "b"])
+    print d
+    assert d.keys() == ["a", "b"]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.