[Checkins] SVN: Sandbox/malthe/chameleon.core/ Bind code-generation utilities in closure as well.
Malthe Borch
mborch at gmail.com
Wed Nov 26 18:34:31 EST 2008
Log message for revision 93375:
Bind code-generation utilities in closure as well.
Changed:
U Sandbox/malthe/chameleon.core/CHANGES.txt
U Sandbox/malthe/chameleon.core/src/chameleon/core/codegen.py
U Sandbox/malthe/chameleon.core/src/chameleon/core/codegen.txt
U Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py
-=-
Modified: Sandbox/malthe/chameleon.core/CHANGES.txt
===================================================================
--- Sandbox/malthe/chameleon.core/CHANGES.txt 2008-11-26 23:16:30 UTC (rev 93374)
+++ Sandbox/malthe/chameleon.core/CHANGES.txt 2008-11-26 23:34:31 UTC (rev 93375)
@@ -4,6 +4,8 @@
HEAD
~~~~
+- Bind code-generation utilities in closure as well. [malthe]
+
- Reworked global scope initialization; we now bind these using a
closure. [malthe]
Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/codegen.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/codegen.py 2008-11-26 23:16:30 UTC (rev 93374)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/codegen.py 2008-11-26 23:34:31 UTC (rev 93375)
@@ -21,35 +21,25 @@
l.append(elt)
return l
-class Lookup(object):
- """Abstract base class for variable lookup implementations."""
-
- @classmethod
- def globals(cls):
- """Construct the globals dictionary to use as the execution context for
- the expression or suite.
- """
- return {
- '_lookup_attr': cls.lookup_attr,
- '_lookup_name': cls.lookup_name,
- }
-
- @classmethod
- def lookup_attr(cls, obj, key):
+def lookup_attr(obj, key):
+ try:
+ return getattr(obj, key)
+ except AttributeError, e:
try:
- return getattr(obj, key)
- except AttributeError, e:
- try:
- return obj[key]
- except (KeyError, TypeError, AttributeError):
- raise e
+ return obj[key]
+ except (KeyError, TypeError, AttributeError):
+ raise e
- @classmethod
- def lookup_name(cls, data, name):
- try:
- return data[name]
- except KeyError:
- raise NameError(name)
+def lookup_name(data, name):
+ try:
+ return data[name]
+ except KeyError:
+ raise NameError(name)
+
+lookup_globals = {
+ '_lookup_attr': lookup_attr,
+ '_lookup_name': lookup_name,
+ }
class TemplateASTTransformer(ASTTransformer):
"""Concrete AST transformer that implements the AST transformations needed
@@ -181,7 +171,9 @@
gen = ModuleCodeGenerator(tree)
gen.optimized = True
- self._globals = Lookup.globals()
+ from sourcecodegen import ModuleSourceCodeGenerator
+ generated = ModuleSourceCodeGenerator(tree).getSourceCode()
+
self.code = gen.getCode()
def __hash__(self):
Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/codegen.txt
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/codegen.txt 2008-11-26 23:16:30 UTC (rev 93374)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/codegen.txt 2008-11-26 23:34:31 UTC (rev 93375)
@@ -10,11 +10,11 @@
The ``Suite`` class compiles a source code suite and makes a code
object available.
- >>> from chameleon.core.codegen import Suite
+ >>> from chameleon.core.codegen import lookup_globals, Suite
>>> suite = Suite("""\
... print 'Hello World!'
... """)
- >>> exec suite.code in suite._globals
+ >>> exec suite.code
Hello World!
Syntax extension: Dictionary lookup using dot operator
@@ -28,7 +28,7 @@
... a = {'b': 1}
... assert a['b'] == a.b
... """)
- >>> exec suite.code in suite._globals
+ >>> exec suite.code in lookup_globals.copy()
Syntax extension: Dynamic scoping
---------------------------------
@@ -37,4 +37,4 @@
... econtext = {'a': 1}
... assert a == 1
... """)
- >>> exec suite.code in suite._globals
+ >>> exec suite.code in lookup_globals.copy()
Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py 2008-11-26 23:16:30 UTC (rev 93374)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py 2008-11-26 23:34:31 UTC (rev 93375)
@@ -724,6 +724,9 @@
stream.symbol_mapping['_init_scope'] = generation.initialize_scope
stream.symbol_mapping['_init_tal'] = generation.initialize_tal
+ # add code-generation lookup globals
+ stream.symbol_mapping.update(codegen.lookup_globals)
+
if global_scope:
assignments = (
clauses.Assign(
@@ -822,7 +825,7 @@
suite = codegen.Suite(self.source)
_locals = {}
- exec suite.code in suite._globals, _locals
+ exec suite.code in _locals
self.bind = _locals['bind']
self.func = self.bind()
@@ -874,7 +877,7 @@
tree, doctype = parser.parse(xmldoc)
_locals = {}
- exec cls.suite.code in cls.suite._globals, _locals
+ exec cls.suite.code in _locals
bind = _locals['bind']
bind.func_defaults = ((None,)*state['defaults']) or None
More information about the Checkins
mailing list