summaryrefslogtreecommitdiff
path: root/Cheetah/ImportManager.py
diff options
context:
space:
mode:
Diffstat (limited to 'Cheetah/ImportManager.py')
-rw-r--r--Cheetah/ImportManager.py54
1 files changed, 32 insertions, 22 deletions
diff --git a/Cheetah/ImportManager.py b/Cheetah/ImportManager.py
index dbb2c96..8ea7419 100644
--- a/Cheetah/ImportManager.py
+++ b/Cheetah/ImportManager.py
@@ -17,9 +17,10 @@ This is a hacked/documented version of Gordon McMillan's iu.py. I have:
- reorganized the code layout to enhance readability
"""
-import sys
import imp
import marshal
+import py_compile
+import sys
from Cheetah.compat import string_type
_installed = False
@@ -77,7 +78,7 @@ def _os_bootstrap():
if dirname is None:
def dirname(a, sep=sep):
- for i in range(len(a)-1, -1, -1):
+ for i in range(len(a) - 1, -1, -1):
c = a[i]
if c == '/' or c == sep:
return a[:i]
@@ -100,7 +101,7 @@ _os_bootstrap()
def packageName(s):
- for i in range(len(s)-1, -1, -1):
+ for i in range(len(s) - 1, -1, -1):
if s[i] == '.':
break
else:
@@ -114,14 +115,14 @@ def nameSplit(s):
for j in range(len(s)):
if s[j] == '.':
rslt.append(s[i:j])
- i = j+1
+ i = j + 1
if i < len(s):
rslt.append(s[i:])
return rslt
def getPathExt(fnm):
- for i in range(len(fnm)-1, -1, -1):
+ for i in range(len(fnm) - 1, -1, -1):
if fnm[i] == '.':
return fnm[i:]
return ''
@@ -186,18 +187,18 @@ class DirOwner(Owner):
py = pyc = None
for pth, ispkg, pkgpth in possibles:
for ext, mode, typ in getsuffixes():
- attempt = pth+ext
+ attempt = pth + ext
try:
st = _os_stat(attempt)
except Exception:
pass
else:
if typ == imp.C_EXTENSION:
- fp = open(attempt, 'rb')
- mod = imp.load_module(
- nm, fp, attempt, (ext, mode, typ))
- mod.__file__ = attempt
- return mod
+ with open(attempt, 'rb') as fp:
+ mod = imp.load_module(
+ nm, fp, attempt, (ext, mode, typ))
+ mod.__file__ = attempt
+ return mod
elif typ == imp.PY_SOURCE:
py = (attempt, st)
else:
@@ -209,23 +210,32 @@ class DirOwner(Owner):
while True:
if pyc is None or py and pyc[1][8] < py[1][8]:
try:
- co = compile(open(py[0], 'r').read()+'\n', py[0], 'exec')
+ with open(py[0], 'r') as py_code_file:
+ py_code = py_code_file.read()
+ co = compile(py_code + '\n', py[0], 'exec')
+ try:
+ py_compile.compile(py[0])
+ except IOError:
+ pass
+ __file__ = py[0]
break
except SyntaxError as e:
print("Invalid syntax in %s" % py[0])
print(e.args)
raise
elif pyc:
- stuff = open(pyc[0], 'rb').read()
+ with open(pyc[0], 'rb') as pyc_file:
+ stuff = pyc_file.read()
try:
co = loadco(stuff[8:])
+ __file__ = pyc[0]
break
except (ValueError, EOFError):
pyc = None
else:
return None
mod = newmod(nm)
- mod.__file__ = co.co_filename
+ mod.__file__ = __file__
if ispkg:
mod.__path__ = [pkgpth]
subimporter = PathImportDirector(mod.__path__)
@@ -268,7 +278,7 @@ class FrozenImportDirector(ImportDirector):
if hasattr(mod, '__path__'):
mod.__importsub__ = \
lambda name, pname=nm, owner=self: \
- owner.getmod(pname+'.'+name)
+ owner.getmod(pname + '.' + name)
return mod
return None
@@ -313,10 +323,10 @@ class RegistryImportDirector(ImportDirector):
stuff = self.map.get(nm)
if stuff:
fnm, desc = stuff
- fp = open(fnm, 'rb')
- mod = imp.load_module(nm, fp, fnm, desc)
- mod.__file__ = fnm
- return mod
+ with open(fnm, 'rb') as fp:
+ mod = imp.load_module(nm, fp, fnm, desc)
+ mod.__file__ = fnm
+ return mod
return None
@@ -466,7 +476,7 @@ class ImportManager:
raise ImportError("No module named %s" % fqname)
if fromlist is None:
if context:
- return sys.modules[context+'.'+nmparts[0]]
+ return sys.modules[context + '.' + nmparts[0]]
return sys.modules[nmparts[0]]
bottommod = sys.modules[ctx]
if hasattr(bottommod, '__path__'):
@@ -475,7 +485,7 @@ class ImportManager:
while i < len(fromlist):
nm = fromlist[i]
if nm == '*':
- fromlist[i:i+1] = list(getattr(bottommod, '__all__', []))
+ fromlist[i:i+1] = list(getattr(bottommod, '__all__', [])) # noqa: E226,E501 missing whitespace around operator
if i >= len(fromlist):
break
nm = fromlist[i]
@@ -483,7 +493,7 @@ class ImportManager:
if not hasattr(bottommod, nm):
if self.threaded:
self._acquire()
- mod = self.doimport(nm, ctx, ctx+'.'+nm)
+ mod = self.doimport(nm, ctx, ctx + '.' + nm)
if self.threaded:
self._release()
if not mod: