Modify

Ticket #159 (new defect)

Opened 4 years ago

Last modified 2 days ago

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

Change History

comment:1 Changed 3 years ago by glimberg@…

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.

comment:2 Changed 2 days ago by maskodok <galihadiputro87@…>

The only thing more I could hope for is documentation of all these features (other than reading through the code).  Cipto Junaedy Is this in process? Can I help? About  Unit Link Terbaik di Indonesia Commonwealth Life Investra Link

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as new
as The resolution will be set. Next status will be 'closed'
to The owner will be changed from xi. Next status will be 'new'
The owner will be changed from xi to anonymous. Next status will be 'assigned'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.