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

jlg at codespeak.net jlg at codespeak.net
Thu Jul 5 14:14:58 CEST 2007


Author: jlg
Date: Thu Jul  5 14:14:56 2007
New Revision: 44739

Modified:
   pypy/dist/pypy/lang/scheme/object.py
   pypy/dist/pypy/lang/scheme/test/test_eval.py
Log:
after copy contex cannot create new locations in globalscope

Modified: pypy/dist/pypy/lang/scheme/object.py
==============================================================================
--- pypy/dist/pypy/lang/scheme/object.py	(original)
+++ pypy/dist/pypy/lang/scheme/object.py	Thu Jul  5 14:14:56 2007
@@ -237,7 +237,7 @@
         assert isinstance(w_identifier, W_Identifier)
 
         w_val = lst.cdr.car.eval(ctx)
-        ctx.gset(w_identifier.name, w_val)
+        ctx.set(w_identifier.name, w_val)
         return w_val
 
 class Sete(W_Macro):
@@ -334,7 +334,7 @@
 
     { "IDENTIFIER": Location(W_Root()) }
     """
-    def __init__(self, globalscope=None, scope=None):
+    def __init__(self, globalscope=None, scope=None, closure=False):
         if globalscope is None:
             self.globalscope = dict(OPERATION_MAP)
         else:
@@ -345,12 +345,13 @@
         else:
             self.scope = scope
 
+        self.closure = closure
+
     def copy(self):
-        return ExecutionContext(self.globalscope, dict(self.scope))
+        return ExecutionContext(self.globalscope, dict(self.scope), True)
 
     def get(self, name):
         loc = self.scope.get(name, None)
-
         if loc is not None:
             return loc.obj
 
@@ -360,17 +361,10 @@
 
         return None
 
-    def set(self, name, obj):
-        """update existing location or create new location new"""
-        loc = self.scope.get(name, None)
-
-        if loc is not None:
-            loc.obj = obj
-        else:
-            self.put(name, obj)
-
     def sete(self, name, obj):
-        """update existing location or raise"""
+        """update existing location or raise
+        directly used by (set! <var> <expr>) macro
+        """
         loc = self.scope.get(name, None)
         if loc is not None:
             loc.obj = obj
@@ -383,21 +377,36 @@
 
         raise "Unbound"
 
-
-    def gset(self, name, obj):
+    def set(self, name, obj):
         """update existing location or create new location new"""
-        loc = self.globalscope.get(name, None)
+        if self.closure:
+            loc = self.scope.get(name, None)
+        else:
+            loc = self.globalscope.get(name, None)
 
         if loc is not None:
             loc.obj = obj
         else:
-            self.gput(name, obj)
-
-    def gput(self, name, obj):
-        """create new location"""
-        self.globalscope[name] = Location(obj)
+            self.put(name, obj)
 
     def put(self, name, obj):
         """create new location"""
-        self.scope[name] = Location(obj)
+        if self.closure:
+            self.scope[name] = Location(obj)
+        else:
+            self.globalscope[name] = Location(obj)
+
+    def get_location(self, name):
+        """internal/test use only
+        returns location bound to variable
+        """
+        loc = self.scope.get(name, None)
+        if loc is not None:
+            return loc
+
+        loc = self.globalscope.get(name, None)
+        if loc is not None:
+            return loc
+
+        return None
 

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	Thu Jul  5 14:14:56 2007
@@ -74,8 +74,11 @@
 def test_sete():
     ctx = ExecutionContext()
     eval_expr(ctx, "(define x 42)")
+    loc1 = ctx.get_location("x")
     eval_expr(ctx, "(set! x 43)")
+    loc2 = ctx.get_location("x")
     assert ctx.get("x").to_number() == 43
+    assert loc1 is loc2
     py.test.raises("Unbound", eval_expr, ctx, "(set! y 42)")
 
 def test_func():
@@ -227,4 +230,5 @@
     eval_expr(ctx, """(define long_body (lambda () (define x 42) (+ x 1)))""")
     w_result = eval_expr(ctx, "(long_body)")
     assert w_result.to_number() == 43
-    #assert ctx.get("x") is None
+    assert ctx.get("x") is None
+



More information about the Pypy-commit mailing list