[py-svn] apipkg commit ef6f2e4e8c1b: turn package paths absolute, so we dont get surprised via chdir
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Sun Aug 1 15:19:51 CEST 2010
# HG changeset patch -- Bitbucket.org
# Project apipkg
# URL http://bitbucket.org/hpk42/apipkg/overview
# User Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de>
# Date 1280667205 -7200
# Node ID ef6f2e4e8c1b97edfd8a946ee1fe84d719b99482
# Parent 6cb3976c8d8aac3d332ed8f507cfdad34b4279a1
turn package paths absolute, so we dont get surprised via chdir
--- a/test_apipkg.py
+++ b/test_apipkg.py
@@ -192,7 +192,7 @@ def test_initpkg_transfers_attrs(monkeyp
apipkg.initpkg('hello', {})
newmod = sys.modules['hello']
assert newmod != mod
- assert newmod.__file__ == mod.__file__
+ assert newmod.__file__ == py.path.local(mod.__file__)
assert newmod.__version__ == mod.__version__
assert newmod.__loader__ == mod.__loader__
@@ -203,7 +203,7 @@ def test_initpkg_not_transfers_not_exist
apipkg.initpkg('hello', {})
newmod = sys.modules['hello']
assert newmod != mod
- assert newmod.__file__ == mod.__file__
+ assert newmod.__file__ == py.path.local(mod.__file__)
assert not hasattr(newmod, '__loader__')
assert not hasattr(newmod, '__path__')
@@ -300,4 +300,36 @@ def test_bpython_getattr_override(tmpdir
})
d = api.__dict__
assert 'abspath' in d
-
+
+
+
+
+def test_chdir_with_relative_imports_shouldnt_break_lazy_loading(tmpdir):
+ execnet = py.test.importorskip('execnet')
+ pkg = tmpdir.mkdir('pkg')
+ messy = tmpdir.mkdir('messy')
+ pkg.join('__init__.py').write(py.code.Source("""
+ import apipkg
+ apipkg.initpkg(__name__, {
+ 'test': '.sub:test',
+ })
+ """))
+ pkg.join('sub.py').write('def test(): pass')
+ gw = execnet.makegateway()
+
+ def remote(channel, pkg, mess):
+ import os
+ import sys
+ sys.path.insert(0, '')
+ os.chdir(pkg)
+ import pkg
+ import py
+ py.builtin.print_(pkg.__path__, file=sys.stderr)
+ py.builtin.print_(pkg.__file__, file=sys.stderr)
+ py.builtin.print_(pkg, file=sys.stderr)
+ os.chdir(mess)
+ pkg.test()
+ ch = gw.remote_exec(remote, pkg=str(tmpdir), mess=str(messy))
+ ch.waitclose()
+
+
--- a/apipkg.py
+++ b/apipkg.py
@@ -5,6 +5,7 @@ see http://pypi.python.org/pypi/apipkg
(c) holger krekel, 2009 - MIT license
"""
+import os
import sys
from types import ModuleType
@@ -14,11 +15,16 @@ 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)
+ file = getattr(oldmod, '__file__', None)
+ if file:
+ file = os.path.abspath(file)
+ mod.__file__ = file
mod.__version__ = getattr(oldmod, '__version__', '0')
- for name in ('__path__', '__loader__'):
- if hasattr(oldmod, name):
- setattr(mod, name, getattr(oldmod, name))
+ if hasattr(oldmod, '__loader__'):
+ mod.__loader__ = oldmod.__loader__
+ if hasattr(oldmod, '__path__'):
+ mod.__path__ = [os.path.abspath(p) for p in oldmod.__path__]
+
sys.modules[pkgname] = mod
def importobj(modpath, attrname):
More information about the pytest-commit
mailing list