id	summary	reporter	owner	description	type	status	priority	component	severity	resolution	keywords	cc
159	Yaml failed to restore loops in objects when __setstate__ is defined	viktor.x.voroshylo@…	xi	"Below code works with cPickle but failed with yaml:

{{{
import yaml, cPickle

class A(yaml.YAMLObject) :
    def __setstate__(self, state) :
        print ""A""
        self.__dict__.update(state)

class B(yaml.YAMLObject) :
    def __setstate__(self, state) :
        print ""B""
        self.__dict__.update(state)

class C(yaml.YAMLObject) :
    def __setstate__(self, state) :
        print ""C""
        self.__dict__.update(state)

import cPickle
        
a = A()
a.b = B()
a.b.c = C()
a.b.c.b = a.b
cPickle.loads(cPickle.dumps(a))
yaml.load(yaml.dump(a))
}}}

Here is a patch with possible solution for this problem:

{{{
Index: constructor.py
===================================================================
--- constructor.py	(revision 1736)
+++ constructor.py	(working copy)
@@ -64,8 +64,11 @@
         if node in self.constructed_objects:
             return self.constructed_objects[node]
         if node in self.recursive_objects:
-            raise ConstructorError(None, None,
+            obj = self.recursive_objects[node]
+            if obj is None :
+                raise ConstructorError(None, None,
                     ""found unconstructable recursive node"", node.start_mark)
+            return obj
         self.recursive_objects[node] = None
         constructor = None
         tag_suffix = None
@@ -97,6 +100,7 @@
             generator = data
             data = generator.next()
             if self.deep_construct:
+                self.recursive_objects[node] = data
                 for dummy in generator:
                     pass
             else:
}}}"	defect	new	normal	pyyaml	normal			
