[pypy-svn] r44920 - in pypy/dist/pypy/lang/scheme: . test

fijal at codespeak.net fijal at codespeak.net
Wed Jul 11 10:20:44 CEST 2007


Author: fijal
Date: Wed Jul 11 10:20:44 2007
New Revision: 44920

Modified:
   pypy/dist/pypy/lang/scheme/interactive.py
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
   pypy/dist/pypy/lang/scheme/test/test_interactive.py
Log:
(jlg, fijal)
* Improve interactive interpreter to show exceptions
* Move UnboundVariable to new exception class


Modified: pypy/dist/pypy/lang/scheme/interactive.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/interactive.py	(original)
+++ pypy/dist/pypy/lang/scheme/interactive.py	Wed Jul 11 10:20:44 2007
@@ -3,7 +3,7 @@
 scheme interpreter
 """
 
-from pypy.lang.scheme.object import ExecutionContext
+from pypy.lang.scheme.object import ExecutionContext, SchemeException
 from pypy.lang.scheme.ssparser import parse
 import os, sys
 
@@ -23,7 +23,10 @@
         sys.stdout.write(ps)
         to_exec += sys.stdin.readline()
         if check_parens(to_exec):
-            print parse(to_exec).eval(ctx)
+            try:
+                print parse(to_exec).eval(ctx)
+            except SchemeException, e:
+                print "error: %s" % e
             to_exec = ""
             cont = False
         else:

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Wed Jul 11 10:20:44 2007
@@ -1,5 +1,12 @@
 import autopath
 
+class SchemeException(Exception):
+    pass
+
+class UnboundVariable(SchemeException):
+    def __str__(self):
+        return "Unbound variable %s" % self.args[0]
+        
 class W_Root(object):
     def to_string(self):
         return ''
@@ -47,7 +54,7 @@
         else:
             #reference to undefined identifier
             #unbound
-            raise Exception("Unbound variable: %s" % (self.name, ))
+            raise UnboundVariable(self.name)
 
 class W_Boolean(W_Root):
     def __init__(self, val):
@@ -423,7 +430,7 @@
             loc.obj = obj
             return obj
 
-        raise Exception("Unbound variable: %s" % (name, ))
+        raise UnboundVariable(name)
 
     def set(self, name, obj):
         """update existing location or create new location"""

Modified: pypy/dist/pypy/lang/scheme/test/test_eval.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_eval.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_eval.py	Wed Jul 11 10:20:44 2007
@@ -1,9 +1,6 @@
 import py
 from pypy.lang.scheme.ssparser import parse
-from pypy.lang.scheme.object import W_Boolean, W_Fixnum, W_Float, W_String
-from pypy.lang.scheme.object import W_Nil, W_Pair, W_Symbol, W_Identifier
-from pypy.lang.scheme.object import W_Procedure, W_Lambda
-from pypy.lang.scheme.object import ExecutionContext
+from pypy.lang.scheme.object import *
 
 def test_eval_obj():
     w_num = W_Pair(W_Identifier("+"),
@@ -79,7 +76,7 @@
     loc2 = ctx.get_location("x")
     assert ctx.get("x").to_number() == 43
     assert loc1 is loc2
-    py.test.raises(Exception, eval_expr, ctx, "(set! y 42)")
+    py.test.raises(UnboundVariable, eval_expr, ctx, "(set! y 42)")
 
 def test_func():
     ctx = ExecutionContext()

Modified: pypy/dist/pypy/lang/scheme/test/test_interactive.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/test/test_interactive.py	(original)
+++ pypy/dist/pypy/lang/scheme/test/test_interactive.py	Wed Jul 11 10:20:44 2007
@@ -37,3 +37,10 @@
         child.expect(".. ")
         child.sendline(" 2)")
         child.expect("3W")
+
+    def test_unbound_variable(self):
+        child = self.spawn()
+        child.expect("-> ")
+        child.sendline("x")
+        child.expect("Unbound variable x")
+        child.expect("-> ")



More information about the Pypy-commit mailing list