[py-svn] r10422 - in py/branch/py-collect: . path/local path/local/testing path/testing test test/testing
hpk at codespeak.net
hpk at codespeak.net
Fri Apr 8 03:35:28 CEST 2005
Author: hpk
Date: Fri Apr 8 03:35:28 2005
New Revision: 10422
Modified:
py/branch/py-collect/initpkg.py
py/branch/py-collect/path/local/local.py
py/branch/py-collect/path/local/testing/test_local.py
py/branch/py-collect/path/testing/common.py
py/branch/py-collect/path/testing/fscommon.py
py/branch/py-collect/test/collect.py
py/branch/py-collect/test/testing/test_collect.py
Log:
hopefully a serious blow to the import dragons ...
basically py.test now doesn't use or imply a custom import
hook anymore! I wonder if getpymodule() could
just call the new pyimport(), should be phased out
or be replaced by an even more powerful interfacing
mechanism. Currently, it allows to import from C-coded
extension modules on-the-fly (invoking distutils in
the background).
While this import-rework should make py.test interact more
nicely with import-magic-using applications, it also derives
us from a way to import things remotely, but that was limited
anyway.
Modified: py/branch/py-collect/initpkg.py
==============================================================================
--- py/branch/py-collect/initpkg.py (original)
+++ py/branch/py-collect/initpkg.py Fri Apr 8 03:35:28 2005
@@ -70,6 +70,21 @@
current = getattr(current, x)
return current
+ def importfrompath(self, path):
+ import py
+ base = py.path.local(self.implmodule.__file__).dirpath()
+ if not path.relto(base):
+ raise ImportError("cannot import '%s' from '%s'" %(
+ path, self.module.__name__))
+ names = path.new(ext='').relto(base).split(path.sep)
+ dottedname = ".".join([self.implmodule.__name__] + names)
+ try:
+ return __import__(dottedname, None, None, ['name'])
+ except ImportError:
+ if dottedname in sys.modules:
+ del sys.modules[dottedname]
+ raise
+
def _loadimpl(self, relfile):
""" load implementation for the given relfile. """
parts = [x.strip() for x in relfile.split('/') if x and x!= '.']
Modified: py/branch/py-collect/path/local/local.py
==============================================================================
--- py/branch/py-collect/path/local/local.py (original)
+++ py/branch/py-collect/path/local/local.py Fri Apr 8 03:35:28 2005
@@ -321,6 +321,40 @@
""" return string representation of the Path. """
return self.strpath
+ def pyimport(self, name=None, ensuresyspath=True):
+ """ return the path as an imported python module.
+ if name is None, look for the containing package
+ and construct an according module name.
+ The module will be put/looked up in sys.modules.
+ insertpkgroot determines if the package's root
+ will be put
+ """
+ if not self.check():
+ raise py.error.ENOENT(self)
+ #assert self.check()
+ pkgpath = None
+ #print "trying to import", self
+ for p in self.parts(reverse=True)[1:]:
+ if p.join('__init__.py').check():
+ pkgpath = p
+ continue
+ if ensuresyspath:
+ #print "inserting into sys.path", p
+ py.std.sys.path.insert(0, str(p))
+ if pkgpath is not None:
+ pkg = __import__(pkgpath.basename, None, None, [])
+ assert py.path.local(pkg.__file__).relto(pkgpath)
+ if hasattr(pkg, '__package__'):
+ return pkg.__package__.importfrompath(self)
+ names = self.new(ext='').relto(pkgpath.dirpath())
+ names = names.split(self.sep)
+ dottedname = ".".join(names)
+ return __import__(dottedname, None, None, ['something'])
+ else:
+ #print "sys.path[0] is", sys.path[0]
+ return __import__(self.purebasename)
+ raise ImportError("cannot import from path %s" %(self,))
+
def getpymodule(self):
if self.ext != '.c':
return super(LocalPath, self).getpymodule()
Modified: py/branch/py-collect/path/local/testing/test_local.py
==============================================================================
--- py/branch/py-collect/path/local/testing/test_local.py (original)
+++ py/branch/py-collect/path/local/testing/test_local.py Fri Apr 8 03:35:28 2005
@@ -201,6 +201,35 @@
#def test_parentdirmatch(self):
# local.parentdirmatch('std', startmodule=__name__)
+ #
+
+ # importing tests
+ def test_pyimport(self):
+ obj = self.root.join('execfile.py').pyimport()
+ assert obj.x == 42
+ assert obj.__name__ == 'execfile'
+
+ def test_pyimport_a(self):
+ otherdir = self.root.join('otherdir')
+ mod = otherdir.join('a.py').pyimport()
+ assert mod.result == "got it"
+ assert mod.__name__ == 'otherdir.a'
+
+ def test_pyimport_b(self):
+ otherdir = self.root.join('otherdir')
+ mod = otherdir.join('b.py').pyimport()
+ assert mod.stuff == "got it"
+ assert mod.__name__ == 'otherdir.b'
+
+ def test_pyimport_c(self):
+ otherdir = self.root.join('otherdir')
+ mod = otherdir.join('c.py').pyimport()
+ assert mod.value == "got it"
+
+ def test_pyimport_d(self):
+ otherdir = self.root.join('otherdir')
+ mod = otherdir.join('d.py').pyimport()
+ assert mod.value2 == "got it"
#class XTestLocalPath(TestLocalPath):
# def __init__(self):
Modified: py/branch/py-collect/path/testing/common.py
==============================================================================
--- py/branch/py-collect/path/testing/common.py (original)
+++ py/branch/py-collect/path/testing/common.py Fri Apr 8 03:35:28 2005
@@ -16,6 +16,9 @@
def test_new_identical(self):
assert self.root == self.root.new()
+ def test_type_check_for_path(self):
+ assert self.root.check(path=1)
+
def test_join(self):
p = self.root.join('sampledir')
strp = str(p)
Modified: py/branch/py-collect/path/testing/fscommon.py
==============================================================================
--- py/branch/py-collect/path/testing/fscommon.py (original)
+++ py/branch/py-collect/path/testing/fscommon.py Fri Apr 8 03:35:28 2005
@@ -13,6 +13,9 @@
execfile = path.ensure('execfile')
execfile.write('x=42')
+ execfilepy = path.ensure('execfile.py')
+ execfilepy.write('x=42')
+
d = {1:2, 'hello': 'world', 'answer': 42}
path.ensure('samplepickle').dumpobj(d)
Modified: py/branch/py-collect/test/collect.py
==============================================================================
--- py/branch/py-collect/test/collect.py (original)
+++ py/branch/py-collect/test/collect.py Fri Apr 8 03:35:28 2005
@@ -231,7 +231,7 @@
try:
return self._obj
except AttributeError:
- self._obj = obj = self.fspath.getpymodule()
+ self._obj = obj = self.fspath.pyimport()
return obj
obj = property(obj, None, None, "module object")
Modified: py/branch/py-collect/test/testing/test_collect.py
==============================================================================
--- py/branch/py-collect/test/testing/test_collect.py (original)
+++ py/branch/py-collect/test/testing/test_collect.py Fri Apr 8 03:35:28 2005
@@ -50,7 +50,7 @@
py.test.raises(py.error.ENOENT, col.run)
def test_syntax_error_in_module():
- modpath = py.path.extpy(datadir.join('syntax_error.py'))
+ modpath = datadir.join('syntax_error.py')
col = py.test.collect.Module(modpath)
py.test.raises(SyntaxError, col.run)
@@ -141,7 +141,7 @@
class Instance(myfuncmixin, py.test.collect.Instance):
pass
""")
- o.ensure('somedir', 'check_something').write("""if 1:
+ o.ensure('somedir', 'check_something.py').write("""if 1:
def check_func():
assert 42 == 42
class CustomTestClass:
More information about the pytest-commit
mailing list