[pypy-svn] r22497 - pypy/dist/pypy/lib/logic

auc at codespeak.net auc at codespeak.net
Mon Jan 23 10:12:35 CET 2006


Author: auc
Date: Mon Jan 23 10:12:33 2006
New Revision: 22497

Modified:
   pypy/dist/pypy/lib/logic/test_unification.py
   pypy/dist/pypy/lib/logic/test_variable.py
   pypy/dist/pypy/lib/logic/unification.py
   pypy/dist/pypy/lib/logic/variable.py
Log:
checks against same variable creation


Modified: pypy/dist/pypy/lib/logic/test_unification.py
==============================================================================
--- pypy/dist/pypy/lib/logic/test_unification.py	(original)
+++ pypy/dist/pypy/lib/logic/test_unification.py	Mon Jan 23 10:12:33 2006
@@ -1,14 +1,16 @@
 import unification as u
+import variable as v
 from py.test import raises, skip
+from threading import Thread
 
 class TestUnification:
     
     def setup_method(self, meth):
         u._store = u.Store()
 
-    def test_already_in_store(self):
-        x = u.var('x')
-        raises(u.AlreadyInStore, u.var, 'x')
+##     def test_already_in_store(self):
+##         x = u.var('x')
+##         raises(u.AlreadyInStore, u.var, 'x')
 
     def test_already_bound(self):
         x = u.var('x')
@@ -102,3 +104,26 @@
         assert b.val == {1:a,  2:42}
         
         
+    def test_threads_creating_vars(self):
+        def create_var(*args):
+            x = u.var('x')
+
+        def create_var2(*args):
+            raises(v.AlreadyExists, u.var, 'x')
+
+        t1, t2 = (FunThread(create_var),
+                  FunThread(create_var2))
+        t1.start()
+        t2.start()
+        
+
+class FunThread(Thread):
+
+    def __init__(self, fun, *args):
+        Thread.__init__(self)
+        self.fun = fun
+        self.args = args
+
+    def run(self):
+        self.fun(self.args)
+            

Modified: pypy/dist/pypy/lib/logic/test_variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/test_variable.py	(original)
+++ pypy/dist/pypy/lib/logic/test_variable.py	Mon Jan 23 10:12:33 2006
@@ -1,4 +1,6 @@
+from py.test import raises
 import unification as u
+import variable as v
 from threading import Thread
 
 class Consumer(Thread):
@@ -22,6 +24,11 @@
     def setup_method(self, meth):
         u._store = u.Store()
 
+
+##     def test_no_same_name(self):
+##         x = u.var('x')
+##         raises(v.AlreadyExists, u.var, 'x')
+
     def test_one_thread_reading_one_var(self):
         cons = Consumer()
         x = u.var('x')

Modified: pypy/dist/pypy/lib/logic/unification.py
==============================================================================
--- pypy/dist/pypy/lib/logic/unification.py	(original)
+++ pypy/dist/pypy/lib/logic/unification.py	Mon Jan 23 10:12:33 2006
@@ -10,7 +10,7 @@
 ## Unification in Oz (almost verbatim from CTM*)
 ## =============================================
 ##
-## Unification as put in Oz is a very powerful operation. It is
+## Unification as put in Oz is "a very powerful operation". It is
 ## provided through the '=' operator.
 ##
 ## Binding a variable to a value is a special case of an operation
@@ -152,8 +152,9 @@
          (also called determined variables)."""
     
     def __init__(self):
-        # set of all known vars
+        # mapping of names to vars (all of them)
         self.vars = set()
+        self.names = set()
         self.in_transaction = False
 
     def add_unbound(self, var):
@@ -162,6 +163,7 @@
             raise AlreadyInStore(var.name)
         print "adding %s to the store" % var
         self.vars.add(var)
+        self.names.add(var.name)
         # put into new singleton equiv. set
         var.val = EqSet([var])
 

Modified: pypy/dist/pypy/lib/logic/variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/variable.py	(original)
+++ pypy/dist/pypy/lib/logic/variable.py	Mon Jan 23 10:12:33 2006
@@ -11,6 +11,10 @@
     def __str__(self):
         return "%s is not a variable" % self.name
 
+class AlreadyExists(VariableException):
+    def __str__(self):
+        return "%s already exists" % self.name
+
 #----------- Variables ----------------------------------
 class EqSet(set):
     """An equivalence set for variables"""
@@ -27,6 +31,8 @@
 class Var(object):
 
     def __init__(self, name, store):
+        if name in store.names:
+            raise AlreadyExists(name)
         self.name = name
         self.store = store
         # top-level 'commited' binding



More information about the Pypy-commit mailing list