|
|
@@ -25,12 +25,6 @@ sharedPackages = {}
|
|
|
|
|
|
vfs = VirtualFileSystem.getGlobalPtr()
|
|
|
|
|
|
-# Possible file types.
|
|
|
-FTPythonSource = 0
|
|
|
-FTPythonCompiled = 1
|
|
|
-FTExtensionModule = 2
|
|
|
-FTFrozenModule = 3
|
|
|
-
|
|
|
compiledExtensions = [ 'pyc', 'pyo' ]
|
|
|
if not __debug__:
|
|
|
# In optimized mode, we prefer loading .pyo files over .pyc files.
|
|
|
@@ -44,7 +38,10 @@ class VFSImporter:
|
|
|
(among other places). """
|
|
|
|
|
|
def __init__(self, path):
|
|
|
- self.dir_path = Filename.fromOsSpecific(path)
|
|
|
+ if isinstance(path, Filename):
|
|
|
+ self.dir_path = Filename(path)
|
|
|
+ else:
|
|
|
+ self.dir_path = Filename.fromOsSpecific(path)
|
|
|
|
|
|
def find_module(self, fullname, path = None):
|
|
|
if path is None:
|
|
|
@@ -60,7 +57,8 @@ class VFSImporter:
|
|
|
filename.setExtension('py')
|
|
|
vfile = vfs.getFile(filename, True)
|
|
|
if vfile:
|
|
|
- return VFSLoader(dir_path, vfile, filename, FTPythonSource)
|
|
|
+ return VFSLoader(dir_path, vfile, filename,
|
|
|
+ desc=('.py', 'U', imp.PY_SOURCE))
|
|
|
|
|
|
# If there's no .py file, but there's a .pyc file, load that
|
|
|
# anyway.
|
|
|
@@ -69,7 +67,8 @@ class VFSImporter:
|
|
|
filename.setExtension(ext)
|
|
|
vfile = vfs.getFile(filename, True)
|
|
|
if vfile:
|
|
|
- return VFSLoader(dir_path, vfile, filename, FTPythonCompiled)
|
|
|
+ return VFSLoader(dir_path, vfile, filename,
|
|
|
+ desc=('.'+ext, 'rb', imp.PY_COMPILED))
|
|
|
|
|
|
# Look for a C/C++ extension module.
|
|
|
for desc in imp.get_suffixes():
|
|
|
@@ -79,22 +78,21 @@ class VFSImporter:
|
|
|
filename = Filename(path + desc[0])
|
|
|
vfile = vfs.getFile(filename, True)
|
|
|
if vfile:
|
|
|
- return VFSLoader(dir_path, vfile, filename, FTExtensionModule,
|
|
|
- desc = desc)
|
|
|
+ return VFSLoader(dir_path, vfile, filename, desc=desc)
|
|
|
|
|
|
# Finally, consider a package, i.e. a directory containing
|
|
|
# __init__.py.
|
|
|
filename = Filename(path, '__init__.py')
|
|
|
vfile = vfs.getFile(filename, True)
|
|
|
if vfile:
|
|
|
- return VFSLoader(dir_path, vfile, filename, FTPythonSource,
|
|
|
- packagePath = path)
|
|
|
+ return VFSLoader(dir_path, vfile, filename, packagePath=path,
|
|
|
+ desc=('.py', 'U', imp.PY_SOURCE))
|
|
|
for ext in compiledExtensions:
|
|
|
filename = Filename(path, '__init__.' + ext)
|
|
|
vfile = vfs.getFile(filename, True)
|
|
|
if vfile:
|
|
|
- return VFSLoader(dir_path, vfile, filename, FTPythonCompiled,
|
|
|
- packagePath = path)
|
|
|
+ return VFSLoader(dir_path, vfile, filename, packagePath=path,
|
|
|
+ desc=('.'+ext, 'rb', imp.PY_COMPILED))
|
|
|
|
|
|
#print >>sys.stderr, "not found."
|
|
|
return None
|
|
|
@@ -103,22 +101,20 @@ class VFSLoader:
|
|
|
""" The second part of VFSImporter, this is created for a
|
|
|
particular .py file or directory. """
|
|
|
|
|
|
- def __init__(self, dir_path, vfile, filename, fileType,
|
|
|
- desc = None, packagePath = None):
|
|
|
+ def __init__(self, dir_path, vfile, filename, desc, packagePath=None):
|
|
|
self.dir_path = dir_path
|
|
|
self.timestamp = None
|
|
|
if vfile:
|
|
|
self.timestamp = vfile.getTimestamp()
|
|
|
self.filename = filename
|
|
|
- self.fileType = fileType
|
|
|
self.desc = desc
|
|
|
self.packagePath = packagePath
|
|
|
|
|
|
def load_module(self, fullname, loadingShared = False):
|
|
|
#print >>sys.stderr, "load_module(%s), dir_path = %s, filename = %s" % (fullname, self.dir_path, self.filename)
|
|
|
- if self.fileType == FTFrozenModule:
|
|
|
+ if self.desc[2] == imp.PY_FROZEN:
|
|
|
return self._import_frozen_module(fullname)
|
|
|
- if self.fileType == FTExtensionModule:
|
|
|
+ if self.desc[2] == imp.C_EXTENSION:
|
|
|
return self._import_extension_module(fullname)
|
|
|
|
|
|
# Check if this is a child of a shared package.
|
|
|
@@ -152,7 +148,7 @@ class VFSLoader:
|
|
|
path = Filename(self.dir_path, Filename.fromOsSpecific(path))
|
|
|
vfile = vfs.getFile(path)
|
|
|
if not vfile:
|
|
|
- raise IOError
|
|
|
+ raise IOError("Could not find '%s'" % (path))
|
|
|
return vfile.readFile(True)
|
|
|
|
|
|
def is_package(self, fullname):
|
|
|
@@ -171,8 +167,8 @@ class VFSLoader:
|
|
|
""" Returns the Python source for this file, if it is
|
|
|
available, or None if it is not. May raise IOError. """
|
|
|
|
|
|
- if self.fileType == FTPythonCompiled or \
|
|
|
- self.fileType == FTExtensionModule:
|
|
|
+ if self.desc[2] == imp.PY_COMPILED or \
|
|
|
+ self.desc[2] == imp.C_EXTENSION:
|
|
|
return None
|
|
|
|
|
|
filename = Filename(self.filename)
|
|
|
@@ -180,7 +176,7 @@ class VFSLoader:
|
|
|
filename.setText()
|
|
|
vfile = vfs.getFile(filename)
|
|
|
if not vfile:
|
|
|
- raise IOError
|
|
|
+ raise IOError("Could not find '%s'" % (filename))
|
|
|
return vfile.readFile(True)
|
|
|
|
|
|
def _import_extension_module(self, fullname):
|
|
|
@@ -242,14 +238,14 @@ class VFSLoader:
|
|
|
ValueError, SyntaxError, or a number of other errors generated
|
|
|
by the low-level system. """
|
|
|
|
|
|
- if self.fileType == FTPythonCompiled:
|
|
|
+ if self.desc[2] == imp.PY_COMPILED:
|
|
|
# It's a pyc file; just read it directly.
|
|
|
pycVfile = vfs.getFile(self.filename, False)
|
|
|
if pycVfile:
|
|
|
return self._loadPyc(pycVfile, None)
|
|
|
raise IOError('Could not read %s' % (self.filename))
|
|
|
|
|
|
- elif self.fileType == FTExtensionModule:
|
|
|
+ elif self.desc[2] == imp.C_EXTENSION:
|
|
|
return None
|
|
|
|
|
|
# It's a .py file (or an __init__.py file; same thing). Read
|