1 2import yaml 3 4class AnInstance: 5 6 def __init__(self, foo, bar): 7 self.foo = foo 8 self.bar = bar 9 10 def __repr__(self): 11 try: 12 return "%s(foo=%r, bar=%r)" % (self.__class__.__name__, 13 self.foo, self.bar) 14 except RuntimeError: 15 return "%s(foo=..., bar=...)" % self.__class__.__name__ 16 17class AnInstanceWithState(AnInstance): 18 19 def __getstate__(self): 20 return {'attributes': [self.foo, self.bar]} 21 22 def __setstate__(self, state): 23 self.foo, self.bar = state['attributes'] 24 25def test_recursive(recursive_filename, verbose=False): 26 context = globals().copy() 27 with open(recursive_filename, 'rb') as file: 28 exec(file.read(), context) 29 value1 = context['value'] 30 output1 = None 31 value2 = None 32 output2 = None 33 try: 34 output1 = yaml.dump(value1) 35 value2 = yaml.unsafe_load(output1) 36 output2 = yaml.dump(value2) 37 assert output1 == output2, (output1, output2) 38 finally: 39 if verbose: 40 print("VALUE1:", value1) 41 print("VALUE2:", value2) 42 print("OUTPUT1:") 43 print(output1) 44 print("OUTPUT2:") 45 print(output2) 46 47test_recursive.unittest = ['.recursive'] 48 49if __name__ == '__main__': 50 import test_appliance 51 test_appliance.run(globals()) 52 53