diff --git a/py/_plugin/pytest_pytester.py b/py/_plugin/pytest_pytester.py index b589e6994..d96c4c802 100644 --- 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): diff --git a/py/apipkg.py b/py/apipkg.py index 5571b04ad..3a2bdd134 100644 --- 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__') diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 428f2cba5..5b2940d65 100644 --- 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