Ignore:
Timestamp:
08/03/06 12:07:29 (8 years ago)
Author:
xi
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pyyaml/trunk/tests/test_recursive.py

    r142 r222  
    11 
    2 import unittest 
     2import test_appliance 
     3 
    34from yaml import * 
    45 
    5 RECURSIVE = """ 
    6 --- &A 
    7 - *A: *A 
    8 """ 
     6class AnInstance: 
    97 
    10 class TestRecursive(unittest.TestCase): 
     8    def __init__(self, foo, bar): 
     9        self.foo = foo 
     10        self.bar = bar 
    1111 
    12     def testRecursive(self): 
    13         node = compose(RECURSIVE) 
    14         self._check(node) 
    15         document = serialize(node) 
    16         node = compose(document) 
    17         self._check(node) 
     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__ 
    1818 
    19     def _check(self, node): 
    20         self.failUnless(node in node.value[0].value) 
    21         self.failUnless(node.value[0].value[node] is node) 
     19class AnInstanceWithState(AnInstance): 
    2220 
     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 TracChangeset for help on using the changeset viewer.