source: pyyaml/trunk/tests/test_recursive.py @ 222

Revision 222, 1.3 KB checked in by xi, 8 years ago (diff)

Subclass all base classes from object.

Hold references to the objects being represented (should fix #22).

The value of a mapping node is represented as a list of pairs (key, value)
now.

Sort dictionary items (fix #23).

Recursive structures are now loaded and dumped correctly, including complex
structures like recursive tuples (fix #5). Thanks Peter Murphy for the patches.
To make it possible, representer functions are allowed to be generators.
In this case, the first generated value is an object. Other values produced
by the representer are ignored.

Make Representer not try to guess !!pairs when a list is represented.
You need to construct a !!pairs node explicitly now.

Do not check for duplicate mapping keys as it didn't work correctly anyway.

Line 
1
2import test_appliance
3
4from yaml import *
5
6class AnInstance:
7
8    def __init__(self, foo, bar):
9        self.foo = foo
10        self.bar = bar
11
12    def __repr__(self):
13        try:
14            return "%s(foo=%r, bar=%r)" % (self.__class__.__name__,
15                    self.foo, self.bar)
16        except RuntimeError:
17            return "%s(foo=..., bar=...)" % self.__class__.__name__
18
19class AnInstanceWithState(AnInstance):
20
21    def __getstate__(self):
22        return {'attributes': [self.foo, self.bar]}
23
24    def __setstate__(self, state):
25        self.foo, self.bar = state['attributes']
26
27class TestRecursive(test_appliance.TestAppliance):
28
29    def _testRecursive(self, test_name, recursive_filename):
30        exec file(recursive_filename, 'r').read()
31        value1 = value
32        output1 = None
33        value2 = None
34        output2 = None
35        try:
36            output1 = dump(value1)
37            #print "OUTPUT %s:" % test_name
38            #print output1
39            value2 = load(output1)
40            output2 = dump(value2)
41            self.failUnlessEqual(output1, output2)
42        except:
43            print "VALUE1:", value1
44            print "VALUE2:", value2
45            print "OUTPUT1:"
46            print output1
47            print "OUTPUT2:"
48            print output2
49            raise
50
51TestRecursive.add_tests('testRecursive', '.recursive')
52
Note: See TracBrowser for help on using the repository browser.