| Revision 329,
1.3 KB
checked in by xi, 4 years ago
(diff) |
|
Fixed the remaining Python 3 compatibility issues.
|
| Rev | Line | |
|---|
| [142] | 1 | |
|---|
| [322] | 2 | import yaml |
|---|
| [222] | 3 | |
|---|
| 4 | class AnInstance: |
|---|
| [142] | 5 | |
|---|
| [222] | 6 | def __init__(self, foo, bar): |
|---|
| 7 | self.foo = foo |
|---|
| 8 | self.bar = bar |
|---|
| [142] | 9 | |
|---|
| [222] | 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__ |
|---|
| [142] | 16 | |
|---|
| [222] | 17 | class AnInstanceWithState(AnInstance): |
|---|
| [142] | 18 | |
|---|
| [222] | 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 | |
|---|
| [322] | 25 | def test_recursive(recursive_filename, verbose=False): |
|---|
| [329] | 26 | context = globals().copy() |
|---|
| 27 | exec(open(recursive_filename, 'rb').read(), context) |
|---|
| 28 | value1 = context['value'] |
|---|
| [322] | 29 | output1 = None |
|---|
| 30 | value2 = None |
|---|
| 31 | output2 = None |
|---|
| 32 | try: |
|---|
| 33 | output1 = yaml.dump(value1) |
|---|
| 34 | value2 = yaml.load(output1) |
|---|
| 35 | output2 = yaml.dump(value2) |
|---|
| 36 | assert output1 == output2, (output1, output2) |
|---|
| 37 | finally: |
|---|
| 38 | if verbose: |
|---|
| [328] | 39 | print("VALUE1:", value1) |
|---|
| 40 | print("VALUE2:", value2) |
|---|
| 41 | print("OUTPUT1:") |
|---|
| 42 | print(output1) |
|---|
| 43 | print("OUTPUT2:") |
|---|
| 44 | print(output2) |
|---|
| [222] | 45 | |
|---|
| [322] | 46 | test_recursive.unittest = ['.recursive'] |
|---|
| [222] | 47 | |
|---|
| [322] | 48 | if __name__ == '__main__': |
|---|
| 49 | import test_appliance |
|---|
| 50 | test_appliance.run(globals()) |
|---|
| 51 | |
|---|
Note: See
TracBrowser
for help on using the repository browser.