Use a LazyName for module attributes, they should only be generated if needed.

This commit is contained in:
Dave Halter
2014-08-15 15:20:40 +02:00
parent 868dab4f51
commit fd90dfc4f5
2 changed files with 18 additions and 2 deletions

View File

@@ -220,6 +220,20 @@ class FakeName(pr.Name):
super(FakeName, self).__init__(FakeSubModule, names, p, p, parent)
class LazyName(FakeName):
def __init__(self, name, parent_callback):
super(LazyName, self).__init__(name)
self._parent_callback = parent_callback
@property
def parent(self):
return self._parent_callback()
@parent.setter
def parent(self, value):
pass # Do nothing, lower level can try to set the parent.
def stmts_to_stmt(statements):
"""
Sometimes we want to have something like a result_set and unite some

View File

@@ -617,10 +617,12 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, pr.Module)):
@memoize_default()
def _module_attributes(self):
def parent_callback():
return Instance(self._evaluator, compiled.create(self._evaluator, str))
names = ['__file__', '__package__', '__doc__', '__name__', '__version__']
# All the additional module attributes are strings.
parent = Instance(self._evaluator, compiled.create(self._evaluator, str))
return [helpers.FakeName(n, parent) for n in names]
return [helpers.LazyName(n, parent_callback) for n in names]
@memoize_default()
def _sub_modules(self):