Commits

Ralf Schmitt  committed 7d1548e

add an 'attr' parameter to initpkg, which allows to set additional module attributes.

  • Participants
  • Parent commits 0ce3d92

Comments (0)

Files changed (2)

 
 __version__ = "1.0"
 
-def initpkg(pkgname, exportdefs):
+def initpkg(pkgname, exportdefs, attr=dict()):
     """ initialize given package from the export definitions. """
     oldmod = sys.modules[pkgname]
     d = {}
         d['__loader__'] = oldmod.__loader__
     if hasattr(oldmod, '__path__'):
         d['__path__'] = [os.path.abspath(p) for p in oldmod.__path__]
+    d.update(attr)
     oldmod.__dict__.update(d)
     mod = ApiModule(pkgname, exportdefs, implprefix=pkgname, attr=d)
     sys.modules[pkgname]  = mod

File test_apipkg.py

     monkeypatch.syspath_prepend(tmpdir)
     import dotted_name_lookup
     assert dotted_name_lookup.abs == py.std.os.path.abspath
+
+def test_extra_attributes(tmpdir, monkeypatch):
+    pkgdir = tmpdir.mkdir("extra_attributes")
+    pkgdir.join('__init__.py').write(py.code.Source("""
+        import apipkg
+        apipkg.initpkg(__name__, dict(abs='os:path.abspath'), dict(foo='bar'))
+    """))
+    monkeypatch.syspath_prepend(tmpdir)
+    import extra_attributes
+    assert extra_attributes.foo == 'bar'