[py-svn] apipkg commit 9fee4ebc2b03: initpkg now replaces the package in sys.modules

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Tue Oct 27 21:04:47 CET 2009


# HG changeset patch -- Bitbucket.org
# Project apipkg
# URL http://bitbucket.org/hpk42/apipkg/overview/
# User Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de>
# Date 1256298349 -7200
# Node ID 9fee4ebc2b037921b0bff990052ddae0d7265060
# Parent 526d7d388cd8f09d42067412594923983e4f3e0b
initpkg now replaces the package in sys.modules

importing a package which initializes itself with apipkg
will result in a ApiModule instead of a module

--- a/test_apipkg.py
+++ b/test_apipkg.py
@@ -144,20 +144,13 @@ def test_nested_absolute_imports():
             'Message': 'email.message:Message',
             },
         })
-    # nesting is supposed to replace things in sys.modules
+    # nesting is supposed to put nested items into sys.modules
     assert 'email.message2' in sys.modules
 
-
-def test_initpkg_no_replace(monkeypatch):
-    api = apipkg.ApiModule('email_no_replace', {})
-    monkeypatch.setitem(sys.modules, 'email_no_replace', api)
-    apipkg.initpkg('email_no_replace', {})
-    assert sys.modules['email_no_replace'] is api
-
- at py.test.mark.xfail
 def test_initpkg_do_replace(monkeypatch):
     api = apipkg.ApiModule('email_replace', {})
     monkeypatch.setitem(sys.modules, 'email_replace', api)
+    # initpkg will also replace in sys.modules
     apipkg.initpkg('email_replace', {}, replace=True)
     assert sys.modules['email_replace'] is not api
 

--- a/apipkg.py
+++ b/apipkg.py
@@ -10,13 +10,12 @@ from types import ModuleType
 
 __version__ = "1.0b1"
 
-def initpkg(pkgname, exportdefs):
-    """ initialize given package from the export definitions. """
-    pkgmodule = sys.modules[pkgname]
+def initpkg(pkgname, exportdefs, replace=False):
+    """ initialize given package from the export definitions.
+        replace it in sys.modules
+    """
     mod = ApiModule(pkgname, exportdefs)
-    for name, value in mod.__dict__.items():
-         if name[:2] != "__" or name == "__all__":
-            setattr(pkgmodule, name, value)
+    sys.modules[pkgname]  = mod
 
 def importobj(importspec):
     """ return object specified by importspec."""



More information about the pytest-commit mailing list