Ralf Schmitt avatar Ralf Schmitt committed 7c23fa6

make AliasModule proxy attribute access methods to original module.

Comments (0)

Files changed (2)

         return dict
     __dict__ = property(__dict__)
 
-class AliasModule(ModuleType):
-    def __init__(self, name, modpath):
-        self.__name = name
-        self.__modpath = modpath
 
-    def __repr__(self):
-        return '<AliasModule %r for %r>' % (self.__name, self.__modpath)
+def AliasModule(modname, modpath):
+    mod = []
 
-    def __getattr__(self, name):
-        mod = importobj(self.__modpath, None)
-        result = getattr(mod, name)
-        setattr(self, name, result)
-        for k, v in mod.__dict__.items():
-            setattr(self, k, v)
-        return result
+    def getmod():
+        if not mod:
+            mod.append(importobj(modpath, None))
+        return mod[0]
 
-    def __dict__(self):
-        # force all the content of the module to be loaded when __dict__ is read
-        dictdescr = ModuleType.__dict__['__dict__']
-        dict = dictdescr.__get__(self)
-        if dict is not None:
-            hasattr(self, 'some')
-        return dict
-    __dict__ = property(__dict__)
+    class AliasModule(ModuleType):
+
+        def __repr__(self):
+            return '<AliasModule %r for %r>' % (modname, modpath)
+
+        def __getattribute__(self, name):
+            return getattr(getmod(), name)
+
+        def __setattr__(self, name, value):
+            setattr(getmod(), name, value)
+
+        def __delattr__(self, name):
+            delattr(getmod(), name)
+
+    return AliasModule(modname)
     am.version
     assert repr(am) == r
 
+def test_aliasmodule_proxy_methods(tmpdir, monkeypatch):
+    pkgdir = tmpdir
+    pkgdir.join('aliasmodule_proxy.py').write(py.code.Source("""
+        def doit():
+            return 42
+    """))
+
+    pkgdir.join('my_aliasmodule_proxy.py').write(py.code.Source("""
+        import apipkg
+        apipkg.initpkg(__name__, dict(proxy='aliasmodule_proxy'))
+
+        def doit():
+            return 42
+    """))
+
+    monkeypatch.syspath_prepend(tmpdir)
+    import aliasmodule_proxy as orig
+    from my_aliasmodule_proxy import proxy
+
+    doit = proxy.doit
+    assert doit is orig.doit
+
+    del proxy.doit
+    py.test.raises(AttributeError, "orig.doit")
+
+    proxy.doit = doit
+    assert orig.doit is doit
+
+
 def test_initpkg_without_old_module():
     apipkg.initpkg("initpkg_without_old_module",
                    dict(modules="sys:modules"))
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.