[py-svn] py-trunk commit 59d0e0018fd7: test and fix for apipkg (also available in apipkg default branch)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Sun Aug 1 20:43:57 CEST 2010


# HG changeset patch -- Bitbucket.org
# Project py-trunk
# URL http://bitbucket.org/hpk42/py-trunk/overview
# User holger krekel <holger at merlinux.eu>
# Date 1280688182 -7200
# Node ID 59d0e0018fd75ad6a5af19cbac2ef56a9cae601a
# Parent  f37b61032c119cfa7b9c7ccd6bd875038d6d917d
test and fix for apipkg (also available in apipkg default branch)

--- a/testing/acceptance_test.py
+++ b/testing/acceptance_test.py
@@ -147,3 +147,19 @@ class TestGeneralUsage:
         result = testdir.runpytest()
         assert result.ret == 0
         assert "should not be seen" not in result.stdout.str()
+
+    @py.test.mark.skipif("not hasattr(os, 'symlink')")
+    def test_chdir(self, testdir):
+        testdir.tmpdir.join("py").mksymlinkto(py._pydir)
+        p = testdir.tmpdir.join("main.py")
+        p.write(py.code.Source("""
+            import sys, os
+            sys.path.insert(0, '')
+            import py
+            print (py.__file__)
+            print (py.__path__)
+            os.chdir(os.path.dirname(os.getcwd()))
+            print (py.log.Producer)
+        """))
+        result = testdir.runpython(p, prepend=False)
+        assert not result.ret

--- a/py/_plugin/pytest_pytester.py
+++ b/py/_plugin/pytest_pytester.py
@@ -326,10 +326,11 @@ class TmpTestdir:
                 (str(py._pydir.dirpath()), cmdlinename))
             return (sys.executable, "-c", source,)
 
-    def runpython(self, script):
-        s = self._getsysprepend()
-        if s:
-            script.write(s + "\n" + script.read())
+    def runpython(self, script, prepend=True):
+        if prepend:
+            s = self._getsysprepend()
+            if s:
+                script.write(s + "\n" + script.read())
         return self.run(sys.executable, script)
 
     def _getsysprepend(self):

--- a/py/apipkg.py
+++ b/py/apipkg.py
@@ -5,20 +5,27 @@ see http://pypi.python.org/pypi/apipkg
 
 (c) holger krekel, 2009 - MIT license
 """
+import os
 import sys
 from types import ModuleType
 
-__version__ = "1.0b6"
+__version__ = "1.0b7"
 
 def initpkg(pkgname, exportdefs):
     """ initialize given package from the export definitions. """
-    mod = ApiModule(pkgname, exportdefs, implprefix=pkgname)
     oldmod = sys.modules[pkgname]
-    mod.__file__ = getattr(oldmod, '__file__', None)
-    mod.__version__ = getattr(oldmod, '__version__', '0')
-    for name in ('__path__', '__loader__'):
-        if hasattr(oldmod, name):
-            setattr(mod, name, getattr(oldmod, name))
+    d = {}
+    f = getattr(oldmod, '__file__', None)
+    if f:
+        f = os.path.abspath(f)
+    d['__file__'] = f
+    d['__version__'] = getattr(oldmod, '__version__', '0')
+    if hasattr(oldmod, '__loader__'):
+        d['__loader__'] = oldmod.__loader__
+    if hasattr(oldmod, '__path__'):
+        d['__path__'] = [os.path.abspath(p) for p in oldmod.__path__]
+    oldmod.__dict__.update(d)
+    mod = ApiModule(pkgname, exportdefs, implprefix=pkgname, attr=d)
     sys.modules[pkgname]  = mod
 
 def importobj(modpath, attrname):
@@ -26,11 +33,15 @@ def importobj(modpath, attrname):
     return getattr(module, attrname)
 
 class ApiModule(ModuleType):
-    def __init__(self, name, importspec, implprefix=None):
+    def __init__(self, name, importspec, implprefix=None, attr=None):
         self.__name__ = name
         self.__all__ = [x for x in importspec if x != '__onfirstaccess__']
         self.__map__ = {}
         self.__implprefix__ = implprefix or name
+        if attr:
+            for name, val in attr.items():
+                #print "setting", self.__name__, name, val
+                setattr(self, name, val)
         for name, importspec in importspec.items():
             if isinstance(importspec, dict):
                 subname = '%s.%s'%(self.__name__, name)
@@ -58,6 +69,7 @@ class ApiModule(ModuleType):
 
     def __makeattr(self, name):
         """lazily compute value for name or raise AttributeError if unknown."""
+        #print "makeattr", self.__name__, name
         target = None
         if '__onfirstaccess__' in self.__map__:
             target = self.__map__.pop('__onfirstaccess__')



More information about the pytest-commit mailing list