[pypy-svn] r25082 - in pypy/dist/pypy/objspace: . test

auc at codespeak.net auc at codespeak.net
Tue Mar 28 14:08:12 CEST 2006


Author: auc
Date: Tue Mar 28 14:08:11 2006
New Revision: 25082

Modified:
   pypy/dist/pypy/objspace/logic.py
   pypy/dist/pypy/objspace/test/test_logicobjspace.py
Log:
unification of instances


Modified: pypy/dist/pypy/objspace/logic.py
==============================================================================
--- pypy/dist/pypy/objspace/logic.py	(original)
+++ pypy/dist/pypy/objspace/logic.py	Tue Mar 28 14:08:11 2006
@@ -471,7 +471,7 @@
 
     
 def _unify_values(space, w_v1, w_v2):
-    print " :unify values", w_v1, w_v2
+    print "  :unify values", w_v1, w_v2
     # unify object of the same type ... FIXME
     if not space.is_w(space.type(w_v1),
                       space.type(w_v2)):
@@ -481,17 +481,24 @@
         isinstance(w_v2, W_ListObject)) or \
         (isinstance(w_v1, W_TupleObject) and \
          isinstance(w_v1, W_TupleObject)):
-        _unify_iterables(space, w_v1, w_v2)
+        return _unify_iterables(space, w_v1, w_v2)
     elif isinstance(w_v1, W_DictObject) and \
         isinstance(w_v1, W_DictObject):
-        _unify_mappings(space, w_v1, w_v2)
+        return _unify_mappings(space, w_v1, w_v2)
         # ... token equality
-        if not space.eq_w(w_v1, w_v2):
-            fail(space, w_v1, w_v2)
-        return space.w_None
+    if not space.eq_w(w_v1, w_v2):
+        return _unify_instances(space, w_v1, w_v2)
+        #fail(space, w_v1, w_v2)
+    return space.w_None
+
+def _unify_instances(space, w_i1, w_i2):
+    print "   :unify instances"
+    return _unify_mappings(space,
+                           w_i1.getdict(),
+                           w_i2.getdict())
 
 def _unify_iterables(space, w_i1, w_i2):
-    print " :unify iterables", w_i1, w_i2
+    print "   :unify iterables", w_i1, w_i2
     # assert lengths
     if len(w_i1.wrappeditems) != len(w_i2.wrappeditems):
         fail(space, w_i1, w_i2)
@@ -506,7 +513,7 @@
         unify(space, w_xi, w_yi)
 
 def _unify_mappings(space, w_m1, w_m2):
-    print "  :unify mappings", w_m1, w_m2
+    print "   :unify mappings", w_m1, w_m2
 ##     if len(w_m1.wrappeditems) != len(w_m2.wrappeditems):
 ##         fail(space, w_i1, w_i2)
     for w_xk in w_m1.content.keys():

Modified: pypy/dist/pypy/objspace/test/test_logicobjspace.py
==============================================================================
--- pypy/dist/pypy/objspace/test/test_logicobjspace.py	(original)
+++ pypy/dist/pypy/objspace/test/test_logicobjspace.py	Tue Mar 28 14:08:11 2006
@@ -181,6 +181,21 @@
         assert Z == W == 42
 
 
+    def test_unify_instances(self):
+        class Foo(object):
+            def __init__(self, a):
+                self.a = a
+                self.b = newvar()
+
+        f1 = Foo(newvar())
+        f2 = Foo(42)
+        unify(f1, f2)
+        assert f1.a == f2.a == 42
+        assert alias_of(f1.b, f2.b)
+        unify(f2.b, 'foo')
+        assert f1.b == f2.b == 'foo'
+                
+
 class AppTest_LogicThreads(object):
 
     def setup_class(cls):



More information about the Pypy-commit mailing list