[py-svn] r10442 - in py/branch/py-collect: . path/local
hpk at codespeak.net
hpk at codespeak.net
Fri Apr 8 18:37:44 CEST 2005
Author: hpk
Date: Fri Apr 8 18:37:44 2005
New Revision: 10442
Modified:
py/branch/py-collect/initpkg.py
py/branch/py-collect/path/local/local.py
Log:
refactor the package object to just provide
a getimportname(path) method, returning None
if the package doesn't know a canonical import name.
Modified: py/branch/py-collect/initpkg.py
==============================================================================
--- py/branch/py-collect/initpkg.py (original)
+++ py/branch/py-collect/initpkg.py Fri Apr 8 18:37:44 2005
@@ -70,20 +70,16 @@
current = getattr(current, x)
return current
- def importfrompath(self, path):
+ def getimportname(self, path):
+ if not path.ext.startswith('.py'):
+ return None
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__))
+ return None
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
+ return dottedname
def _loadimpl(self, relfile):
""" load implementation for the given relfile. """
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 18:37:44 2005
@@ -323,11 +323,9 @@
def pyimport(self, modname=None, ensuresyspath=True):
""" return the path as an imported python module.
- if name is None, look for the containing package
+ if modname 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)
@@ -345,10 +343,13 @@
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)
- modname = ".".join(names)
+ modname = pkg.__package__.getimportname(self)
+ assert modname is not None, "package %s doesn't know %s" % (
+ pkg.__name__, self)
+ else:
+ names = self.new(ext='').relto(pkgpath.dirpath())
+ names = names.split(self.sep)
+ modname = ".".join(names)
else:
#print "sys.path[0] is", sys.path[0]
modname = self.purebasename
@@ -357,7 +358,8 @@
except ImportError:
if modname in sys.modules:
del sys.modules[modname]
- raise ImportError("cannot import from path %s" %(self,))
+ raise
+ raise ImportError("cannot import fspath %s" %(self,))
else:
try:
return sys.modules[modname]
More information about the pytest-commit
mailing list