resolves issue #59

resolves issue #48

Have the path.pyimport() helper raise an EnvironmentError if an
import of a given file returns a module that does not appear to
be coming from the actual path.  E.g. for a directory layout like this:

    a / test_whatever.py
    b / test_whatever.py

calling py.path.local("b/test_whatever.py").pyimport() will
fail if the other globally scoped test_whatever module was
loaded already.

--HG--
branch : trunk
This commit is contained in:
holger krekel
2009-10-28 19:51:20 +01:00
parent 1f01fafec7
commit 86fc12dd15
5 changed files with 54 additions and 1 deletions

View File

@@ -516,7 +516,16 @@ class LocalPath(FSBase):
self._prependsyspath(self.dirpath())
modname = self.purebasename
mod = __import__(modname, None, None, ['__doc__'])
#self._module = mod
modfile = mod.__file__
if modfile[-4:] in ('.pyc', '.pyo'):
modfile = modfile[:-1]
elif modfile.endswith('$py.class'):
modfile = modfile[:-9] + '.py'
if not self.samefile(modfile):
raise EnvironmentError("mismatch:\n"
"imported module %r\n"
"does not stem from %r\n"
"maybe __init__.py files are missing?" % (mod, str(self)))
return mod
else:
try:

View File

@@ -70,6 +70,12 @@ class TmpTestdir:
py.std.sys.path.remove(p)
if hasattr(self, '_olddir'):
self._olddir.chdir()
# delete modules that have been loaded from tmpdir
for name, mod in list(sys.modules.items()):
if mod:
fn = getattr(mod, '__file__', None)
if fn and fn.startswith(str(self.tmpdir)):
del sys.modules[name]
def getreportrecorder(self, obj):
if isinstance(obj, py._com.Registry):