Modify ↓
Ticket #159 (new defect)
Yaml failed to restore loops in objects when __setstate__ is defined
| Reported by: | viktor.x.voroshylo@… | Owned by: | xi |
|---|---|---|---|
| Priority: | normal | Component: | pyyaml |
| Severity: | normal | Keywords: | |
| Cc: |
Description
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:
Attachments
Note: See
TracTickets for help on using
tickets.

Just a ping on this issue as I've just run into it as well.
I'm currently monkey patching PyYAML to get around this issue, but would appreciate a proper fix.