[py-svn] apipkg commit 56f2070fcd8e: allow aliasing of whole modules

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Oct 13 23:52:23 CEST 2010


# HG changeset patch -- Bitbucket.org
# Project apipkg
# URL http://bitbucket.org/hpk42/apipkg/overview
# User holger krekel <holger at merlinux.eu>
# Date 1287006719 -7200
# Node ID 56f2070fcd8e0192bad2eb35c66e6cd0ad858209
# Parent  b3d10fd5ae0b3669eed0f7eb0701dc19ac12858c
allow aliasing of whole modules

--- a/test_apipkg.py
+++ b/test_apipkg.py
@@ -124,6 +124,18 @@ class TestScenarios:
         assert isinstance(recmodule, apipkg.ApiModule)
         assert recmodule.some.__name__ == "someclass"
 
+    def test_module_alias_import(self, monkeypatch, tmpdir):
+        pkgdir = tmpdir.mkdir("aliasimport")
+        pkgdir.join('__init__.py').write(py.code.Source("""
+            import apipkg
+            apipkg.initpkg(__name__, exportdefs={
+                'some': 'os.path',
+            })
+        """))
+        monkeypatch.syspath_prepend(tmpdir)
+        import aliasimport
+        assert aliasimport.some is py.std.os.path
+
 def xtest_nested_absolute_imports():
     import email
     api_email = apipkg.ApiModule('email',{

--- a/apipkg.py
+++ b/apipkg.py
@@ -30,7 +30,9 @@ def initpkg(pkgname, exportdefs):
 
 def importobj(modpath, attrname):
     module = __import__(modpath, None, None, ['__doc__'])
-    return getattr(module, attrname)
+    if attrname:
+        return getattr(module, attrname)
+    return module
 
 class ApiModule(ModuleType):
     def __init__(self, name, importspec, implprefix=None, attr=None):
@@ -49,7 +51,9 @@ class ApiModule(ModuleType):
                 sys.modules[subname] = apimod
                 setattr(self, name, apimod)
             else:
-                modpath, attrname = importspec.split(':')
+                parts = importspec.split(':')
+                modpath = parts.pop(0)
+                attrname = parts and parts[0] or ""
                 if modpath[0] == '.':
                     modpath = implprefix + modpath
                 if name == '__doc__':

--- a/CHANGELOG
+++ b/CHANGELOG
@@ -4,6 +4,8 @@ 1.0.0b7
 - make apipkg memorize the absolute path where a package starts
   importing so that subsequent chdir + imports won't break. 
 
+- allow to alias modules
+
 1.0.0b6
 ----------------------------------------



More information about the pytest-commit mailing list