[pypy-svn] r20905 - pypy/dist/pypy/module/recparser/test

adim at codespeak.net adim at codespeak.net
Thu Dec 8 19:12:18 CET 2005


Author: adim
Date: Thu Dec  8 19:12:17 2005
New Revision: 20905

Added:
   pypy/dist/pypy/module/recparser/test/test_compilehooks.py
Log:
added a small high-level test for compile hooks


Added: pypy/dist/pypy/module/recparser/test/test_compilehooks.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/recparser/test/test_compilehooks.py	Thu Dec  8 19:12:17 2005
@@ -0,0 +1,28 @@
+class AppTest_CompilerHooks:
+
+    def test_basic_hook(self):
+        # define the hook
+        def threebecomestwo(ast, enc):
+            class ChangeConstVisitor:
+                def visitConst(self, node):
+                    if node.value == 3:
+                        node.value = 2
+
+                def defaultvisit(self, node):
+                    for child in node.getChildNodes():
+                        child.accept(self)
+
+                def __getattr__(self, attrname):
+                    if attrname.startswith('visit'):
+                        return self.defaultvisit
+                    raise AttributeError(attrname)
+                
+            ast.accept(ChangeConstVisitor())
+            return ast
+
+        # install the hook
+        import parser
+        parser.install_compiler_hook(threebecomestwo)
+        d = {}
+        exec "a = 3" in d
+        assert d['a'] == 2 # well, yes ...



More information about the Pypy-commit mailing list