handle nested packages properly

This commit is contained in:
David Rose 2009-04-29 19:37:14 +00:00
parent 30b37547b8
commit a8c6dfa020

View File

@ -37,7 +37,7 @@ class VFSImporter:
path = Filename(self.dir_path, basename) path = Filename(self.dir_path, basename)
# First, look for Python files. # First, look for Python files.
filename = path filename = Filename(path)
filename.setExtension('py') filename.setExtension('py')
vfile = vfs.getFile(filename, True) vfile = vfs.getFile(filename, True)
if vfile: if vfile:
@ -45,7 +45,7 @@ class VFSImporter:
# If there's no .py file, but there's a .pyc file, load that # If there's no .py file, but there's a .pyc file, load that
# anyway. # anyway.
filename = path filename = Filename(path)
filename.setExtension(pycExtension) filename.setExtension(pycExtension)
vfile = vfs.getFile(filename, True) vfile = vfs.getFile(filename, True)
if vfile: if vfile:
@ -56,7 +56,7 @@ class VFSImporter:
if desc[2] != imp.C_EXTENSION: if desc[2] != imp.C_EXTENSION:
continue continue
filename = path filename = Filename(path)
filename.setExtension(desc[0][1:]) filename.setExtension(desc[0][1:])
vfile = vfs.getFile(filename, True) vfile = vfs.getFile(filename, True)
if vfile: if vfile:
@ -70,12 +70,12 @@ class VFSImporter:
vfile = vfs.getFile(filename, True) vfile = vfs.getFile(filename, True)
if vfile: if vfile:
return VFSLoader(self, vfile, filename, FTPythonSource, return VFSLoader(self, vfile, filename, FTPythonSource,
package = True) packagePath = path)
filename = Filename(path, '__init__.' + pycExtension) filename = Filename(path, '__init__.' + pycExtension)
vfile = vfs.getFile(filename, True) vfile = vfs.getFile(filename, True)
if vfile: if vfile:
return VFSLoader(self, vfile, filename, FTPythonCompiled, return VFSLoader(self, vfile, filename, FTPythonCompiled,
package = True) packagePath = path)
return None return None
@ -84,14 +84,14 @@ class VFSLoader:
particular .py file or directory. """ particular .py file or directory. """
def __init__(self, importer, vfile, filename, fileType, def __init__(self, importer, vfile, filename, fileType,
desc = None, package = False): desc = None, packagePath = None):
self.importer = importer self.importer = importer
self.dir_path = importer.dir_path self.dir_path = importer.dir_path
self.timestamp = vfile.getTimestamp() self.timestamp = vfile.getTimestamp()
self.filename = filename self.filename = filename
self.fileType = fileType self.fileType = fileType
self.desc = desc self.desc = desc
self.package = package self.packagePath = packagePath
def load_module(self, fullname): def load_module(self, fullname):
if self.fileType == FTCompiledModule: if self.fileType == FTCompiledModule:
@ -104,8 +104,9 @@ class VFSLoader:
mod = sys.modules.setdefault(fullname, new.module(fullname)) mod = sys.modules.setdefault(fullname, new.module(fullname))
mod.__file__ = self.filename.cStr() mod.__file__ = self.filename.cStr()
mod.__loader__ = self mod.__loader__ = self
if self.package: if self.packagePath:
mod.__path__ = [] mod.__path__ = [self.packagePath.cStr()]
exec code in mod.__dict__ exec code in mod.__dict__
return mod return mod
@ -115,7 +116,7 @@ class VFSLoader:
return f.read() return f.read()
def is_package(self, fullname): def is_package(self, fullname):
return self.package return bool(self.packagePath)
def get_code(self, fullname): def get_code(self, fullname):
return self._read_code() return self._read_code()
@ -143,8 +144,6 @@ class VFSLoader:
""" Loads the compiled C/C++ shared object as a Python module, """ Loads the compiled C/C++ shared object as a Python module,
and returns it. """ and returns it. """
print "importing %s" % (fullname)
vfile = vfs.getFile(self.filename, False) vfile = vfs.getFile(self.filename, False)
# We can only import a compiled module if it already exists on # We can only import a compiled module if it already exists on
@ -184,7 +183,7 @@ class VFSLoader:
# It's a pyc file; just read it directly. # It's a pyc file; just read it directly.
pycVfile = vfs.getFile(self.filename, False) pycVfile = vfs.getFile(self.filename, False)
if pycVfile: if pycVfile:
return self._loadPyc(pycVfile) return self._loadPyc(pycVfile, None)
return None return None
elif self.fileType == FTCompiledModule: elif self.fileType == FTCompiledModule:
@ -202,7 +201,7 @@ class VFSLoader:
code = None code = None
if t_pyc and t_pyc >= self.timestamp: if t_pyc and t_pyc >= self.timestamp:
code = self._loadPyc(pycVfile) code = self._loadPyc(pycVfile, self.timestamp)
if not code: if not code:
source = self._read_source() source = self._read_source()
@ -212,13 +211,13 @@ class VFSLoader:
return code return code
def _loadPyc(self, vfile): def _loadPyc(self, vfile, timestamp):
""" Reads and returns the marshal data from a .pyc file. """ """ Reads and returns the marshal data from a .pyc file. """
code = None code = None
f = open(vfile, 'rb') f = open(vfile, 'rb')
if f.read(4) == imp.get_magic(): if f.read(4) == imp.get_magic():
t = struct.unpack('<I', f.read(4))[0] t = struct.unpack('<I', f.read(4))[0]
if t == self.timestamp: if not timestamp or t == timestamp:
code = marshal.loads(f.read()) code = marshal.loads(f.read())
f.close() f.close()
return code return code