test and fix for apipkg (also available in apipkg default branch)

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-08-01 20:43:02 +02:00
parent ba1f6336af
commit 8f2b0d0889
3 changed files with 41 additions and 12 deletions

View File

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

View File

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

View File

@ -147,3 +147,19 @@ class TestGeneralUsage:
result = testdir.runpytest() result = testdir.runpytest()
assert result.ret == 0 assert result.ret == 0
assert "should not be seen" not in result.stdout.str() 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