apply underscore_memoization to builtin

This commit is contained in:
Dave Halter
2014-01-05 10:41:41 +01:00
parent 471cf742dc
commit d0a1f66777

View File

@@ -79,16 +79,12 @@ class BuiltinModule(object):
self.name = name self.name = name
self.path = path and os.path.abspath(path) self.path = path and os.path.abspath(path)
self._parser = None
self._module = None
@property @property
@cache.underscore_memoization
def parser(self): def parser(self):
""" get the parser lazy """ """ get the parser lazy """
if self._parser is None: return cache.load_parser(self.path, self.name) or self._load_module()
self._parser = cache.load_parser(self.path, self.name) \
or self._load_module()
return self._parser
def _load_module(self): def _load_module(self):
source = _generate_code(self.module, self._load_mixins()) source = _generate_code(self.module, self._load_mixins())
@@ -98,7 +94,9 @@ class BuiltinModule(object):
return p return p
@property @property
@cache.underscore_memoization
def module(self): def module(self):
"""get module also lazy"""
def load_module(name, path): def load_module(name, path):
if path: if path:
self.sys_path.insert(0, path) self.sys_path.insert(0, path)
@@ -107,36 +105,33 @@ class BuiltinModule(object):
content = {} content = {}
try: try:
exec_function('import %s as module' % name, content) exec_function('import %s as module' % name, content)
self._module = content['module'] module = content['module']
except AttributeError: except AttributeError:
# use sys.modules, because you cannot access some modules # use sys.modules, because you cannot access some modules
# directly. -> #59 # directly. -> #59
self._module = sys.modules[name] module = sys.modules[name]
sys.path = temp sys.path = temp
if path: if path:
self.sys_path.pop(0) self.sys_path.pop(0)
return module
# module might already be defined # module might already be defined
if not self._module: path = self.path
path = self.path name = self.name
name = self.name if self.path:
if self.path: dot_path = []
p = self.path
dot_path = [] # search for the builtin with the correct path
p = self.path while p and p not in sys.path:
# search for the builtin with the correct path p, sep, mod = p.rpartition(os.path.sep)
while p and p not in sys.path: dot_path.append(mod.partition('.')[0])
p, sep, mod = p.rpartition(os.path.sep) if p:
dot_path.append(mod.partition('.')[0]) name = ".".join(reversed(dot_path))
if p: path = p
name = ".".join(reversed(dot_path)) else:
path = p path = os.path.dirname(self.path)
else: return load_module(name, path)
path = os.path.dirname(self.path)
load_module(name, path)
return self._module
def _load_mixins(self): def _load_mixins(self):
""" """
@@ -432,13 +427,10 @@ class Builtin(object):
else: else:
name = '__builtin__' name = '__builtin__'
_builtin = None
@property @property
@cache.underscore_memoization
def builtin(self): def builtin(self):
if self._builtin is None: return BuiltinModule(name=self.name)
self._builtin = BuiltinModule(name=self.name)
return self._builtin
@property @property
def scope(self): def scope(self):