| 13 | | |
| 14 | | class MyLoader(Loader): |
| 15 | | pass |
| 16 | | class MyDumper(Dumper): |
| 17 | | pass |
| 18 | | |
| 19 | | class MyTestClass1: |
| 20 | | |
| 21 | | def __init__(self, x, y=0, z=0): |
| 22 | | self.x = x |
| 23 | | self.y = y |
| 24 | | self.z = z |
| 25 | | |
| 26 | | def __eq__(self, other): |
| 27 | | if isinstance(other, MyTestClass1): |
| 28 | | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
| 29 | | else: |
| 30 | | return False |
| 31 | | |
| 32 | | def construct1(constructor, node): |
| 33 | | mapping = constructor.construct_mapping(node) |
| 34 | | return MyTestClass1(**mapping) |
| 35 | | def represent1(representer, native): |
| 36 | | return representer.represent_mapping("!tag1", native.__dict__) |
| 37 | | |
| 38 | | add_constructor("!tag1", construct1, Loader=MyLoader) |
| 39 | | add_representer(MyTestClass1, represent1, Dumper=MyDumper) |
| 40 | | |
| 41 | | class MyTestClass2(MyTestClass1, YAMLObject): |
| 42 | | |
| 43 | | yaml_loader = MyLoader |
| 44 | | yaml_dumper = MyDumper |
| 45 | | yaml_tag = "!tag2" |
| 46 | | |
| 47 | | def from_yaml(cls, constructor, node): |
| 48 | | x = constructor.construct_yaml_int(node) |
| 49 | | return cls(x=x) |
| 50 | | from_yaml = classmethod(from_yaml) |
| 51 | | |
| 52 | | def to_yaml(cls, representer, native): |
| 53 | | return representer.represent_scalar(cls.yaml_tag, str(native.x)) |
| 54 | | to_yaml = classmethod(to_yaml) |
| 55 | | |
| 56 | | class MyTestClass3(MyTestClass2): |
| 57 | | |
| 58 | | yaml_tag = "!tag3" |
| 59 | | |
| 60 | | def from_yaml(cls, constructor, node): |
| 61 | | mapping = constructor.construct_mapping(node) |
| 62 | | if '=' in mapping: |
| 63 | | x = mapping['='] |
| 64 | | del mapping['='] |
| 65 | | mapping['x'] = x |
| 66 | | return cls(**mapping) |
| 67 | | from_yaml = classmethod(from_yaml) |
| 68 | | |
| 69 | | def to_yaml(cls, representer, native): |
| 70 | | return representer.represent_mapping(cls.yaml_tag, native.__dict__) |
| 71 | | to_yaml = classmethod(to_yaml) |
| 72 | | |
| 73 | | class YAMLObject1(YAMLObject): |
| 74 | | |
| 75 | | yaml_loader = MyLoader |
| 76 | | yaml_dumper = MyDumper |
| 77 | | yaml_tag = '!foo' |
| 78 | | |
| 79 | | def __init__(self, my_parameter=None, my_another_parameter=None): |
| 80 | | self.my_parameter = my_parameter |
| 81 | | self.my_another_parameter = my_another_parameter |
| 82 | | |
| 83 | | def __eq__(self, other): |
| 84 | | if isinstance(other, YAMLObject1): |
| 85 | | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
| 86 | | else: |
| 87 | | return False |
| 88 | | |
| 89 | | class YAMLObject2(YAMLObject): |
| 90 | | |
| 91 | | yaml_loader = MyLoader |
| 92 | | yaml_dumper = MyDumper |
| 93 | | yaml_tag = '!bar' |
| 94 | | |
| 95 | | def __init__(self, foo=1, bar=2, baz=3): |
| 96 | | self.foo = foo |
| 97 | | self.bar = bar |
| 98 | | self.baz = baz |
| 99 | | |
| 100 | | def __getstate__(self): |
| 101 | | return {1: self.foo, 2: self.bar, 3: self.baz} |
| 102 | | |
| 103 | | def __setstate__(self, state): |
| 104 | | self.foo = state[1] |
| 105 | | self.bar = state[2] |
| 106 | | self.baz = state[3] |
| 107 | | |
| 108 | | def __eq__(self, other): |
| 109 | | if isinstance(other, YAMLObject2): |
| 110 | | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
| 111 | | else: |
| 112 | | return False |
| 113 | | |
| 114 | | class AnObject(object): |
| 115 | | |
| 116 | | def __new__(cls, foo=None, bar=None, baz=None): |
| 117 | | self = object.__new__(cls) |
| 118 | | self.foo = foo |
| 119 | | self.bar = bar |
| 120 | | self.baz = baz |
| 121 | | return self |
| 122 | | |
| 123 | | def __cmp__(self, other): |
| 124 | | return cmp((type(self), self.foo, self.bar, self.baz), |
| 125 | | (type(other), other.foo, other.bar, other.baz)) |
| 126 | | |
| 127 | | def __eq__(self, other): |
| 128 | | return type(self) is type(other) and \ |
| 129 | | (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) |
| 130 | | |
| 131 | | class AnInstance: |
| 132 | | |
| 133 | | def __init__(self, foo=None, bar=None, baz=None): |
| 134 | | self.foo = foo |
| 135 | | self.bar = bar |
| 136 | | self.baz = baz |
| 137 | | |
| 138 | | def __cmp__(self, other): |
| 139 | | return cmp((type(self), self.foo, self.bar, self.baz), |
| 140 | | (type(other), other.foo, other.bar, other.baz)) |
| 141 | | |
| 142 | | def __eq__(self, other): |
| 143 | | return type(self) is type(other) and \ |
| 144 | | (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) |
| 145 | | |
| 146 | | class AState(AnInstance): |
| 147 | | |
| 148 | | def __getstate__(self): |
| 149 | | return { |
| 150 | | '_foo': self.foo, |
| 151 | | '_bar': self.bar, |
| 152 | | '_baz': self.baz, |
| 153 | | } |
| 154 | | |
| 155 | | def __setstate__(self, state): |
| 156 | | self.foo = state['_foo'] |
| 157 | | self.bar = state['_bar'] |
| 158 | | self.baz = state['_baz'] |
| 159 | | |
| 160 | | class ACustomState(AnInstance): |
| 161 | | |
| 162 | | def __getstate__(self): |
| 163 | | return (self.foo, self.bar, self.baz) |
| 164 | | |
| 165 | | def __setstate__(self, state): |
| 166 | | self.foo, self.bar, self.baz = state |
| 167 | | |
| 168 | | class InitArgs(AnInstance): |
| 169 | | |
| 170 | | def __getinitargs__(self): |
| 171 | | return (self.foo, self.bar, self.baz) |
| 172 | | |
| 173 | | def __getstate__(self): |
| 174 | | return {} |
| 175 | | |
| 176 | | class InitArgsWithState(AnInstance): |
| 177 | | |
| 178 | | def __getinitargs__(self): |
| 179 | | return (self.foo, self.bar) |
| 180 | | |
| 181 | | def __getstate__(self): |
| 182 | | return self.baz |
| 183 | | |
| 184 | | def __setstate__(self, state): |
| 185 | | self.baz = state |
| 186 | | |
| 187 | | class NewArgs(AnObject): |
| 188 | | |
| 189 | | def __getnewargs__(self): |
| 190 | | return (self.foo, self.bar, self.baz) |
| 191 | | |
| 192 | | def __getstate__(self): |
| 193 | | return {} |
| 194 | | |
| 195 | | class NewArgsWithState(AnObject): |
| 196 | | |
| 197 | | def __getnewargs__(self): |
| 198 | | return (self.foo, self.bar) |
| 199 | | |
| 200 | | def __getstate__(self): |
| 201 | | return self.baz |
| 202 | | |
| 203 | | def __setstate__(self, state): |
| 204 | | self.baz = state |
| 205 | | |
| 206 | | class Reduce(AnObject): |
| 207 | | |
| 208 | | def __reduce__(self): |
| 209 | | return self.__class__, (self.foo, self.bar, self.baz) |
| 210 | | |
| 211 | | class ReduceWithState(AnObject): |
| 212 | | |
| 213 | | def __reduce__(self): |
| 214 | | return self.__class__, (self.foo, self.bar), self.baz |
| 215 | | |
| 216 | | def __setstate__(self, state): |
| 217 | | self.baz = state |
| 218 | | |
| 219 | | class MyInt(int): |
| 220 | | |
| 221 | | def __eq__(self, other): |
| 222 | | return type(self) is type(other) and int(self) == int(other) |
| 223 | | |
| 224 | | class MyList(list): |
| 225 | | |
| 226 | | def __init__(self, n=1): |
| 227 | | self.extend([None]*n) |
| 228 | | |
| 229 | | def __eq__(self, other): |
| 230 | | return type(self) is type(other) and list(self) == list(other) |
| 231 | | |
| 232 | | class MyDict(dict): |
| 233 | | |
| 234 | | def __init__(self, n=1): |
| 235 | | for k in range(n): |
| 236 | | self[k] = None |
| 237 | | |
| 238 | | def __eq__(self, other): |
| 239 | | return type(self) is type(other) and dict(self) == dict(other) |
| 240 | | |
| 241 | | class FixedOffset(datetime.tzinfo): |
| 242 | | |
| 243 | | def __init__(self, offset, name): |
| 244 | | self.__offset = datetime.timedelta(minutes=offset) |
| 245 | | self.__name = name |
| 246 | | |
| 247 | | def utcoffset(self, dt): |
| 248 | | return self.__offset |
| 249 | | |
| 250 | | def tzname(self, dt): |
| 251 | | return self.__name |
| 252 | | |
| 253 | | def dst(self, dt): |
| 254 | | return datetime.timedelta(0) |
| 255 | | |
| 261 | | class TestConstructorTypes(test_appliance.TestAppliance): |
| 262 | | |
| 263 | | def _testTypes(self, test_name, data_filename, code_filename): |
| 264 | | data1 = None |
| 265 | | data2 = None |
| | 16 | def _make_objects(): |
| | 17 | global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3, YAMLObject1, YAMLObject2, \ |
| | 18 | AnObject, AnInstance, AState, ACustomState, InitArgs, InitArgsWithState, \ |
| | 19 | NewArgs, NewArgsWithState, Reduce, ReduceWithState, MyInt, MyList, MyDict, \ |
| | 20 | FixedOffset, execute |
| | 21 | |
| | 22 | class MyLoader(yaml.Loader): |
| | 23 | pass |
| | 24 | class MyDumper(yaml.Dumper): |
| | 25 | pass |
| | 26 | |
| | 27 | class MyTestClass1: |
| | 28 | def __init__(self, x, y=0, z=0): |
| | 29 | self.x = x |
| | 30 | self.y = y |
| | 31 | self.z = z |
| | 32 | def __eq__(self, other): |
| | 33 | if isinstance(other, MyTestClass1): |
| | 34 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
| | 35 | else: |
| | 36 | return False |
| | 37 | |
| | 38 | def construct1(constructor, node): |
| | 39 | mapping = constructor.construct_mapping(node) |
| | 40 | return MyTestClass1(**mapping) |
| | 41 | def represent1(representer, native): |
| | 42 | return representer.represent_mapping("!tag1", native.__dict__) |
| | 43 | |
| | 44 | yaml.add_constructor("!tag1", construct1, Loader=MyLoader) |
| | 45 | yaml.add_representer(MyTestClass1, represent1, Dumper=MyDumper) |
| | 46 | |
| | 47 | class MyTestClass2(MyTestClass1, yaml.YAMLObject): |
| | 48 | yaml_loader = MyLoader |
| | 49 | yaml_dumper = MyDumper |
| | 50 | yaml_tag = "!tag2" |
| | 51 | def from_yaml(cls, constructor, node): |
| | 52 | x = constructor.construct_yaml_int(node) |
| | 53 | return cls(x=x) |
| | 54 | from_yaml = classmethod(from_yaml) |
| | 55 | def to_yaml(cls, representer, native): |
| | 56 | return representer.represent_scalar(cls.yaml_tag, str(native.x)) |
| | 57 | to_yaml = classmethod(to_yaml) |
| | 58 | |
| | 59 | class MyTestClass3(MyTestClass2): |
| | 60 | yaml_tag = "!tag3" |
| | 61 | def from_yaml(cls, constructor, node): |
| | 62 | mapping = constructor.construct_mapping(node) |
| | 63 | if '=' in mapping: |
| | 64 | x = mapping['='] |
| | 65 | del mapping['='] |
| | 66 | mapping['x'] = x |
| | 67 | return cls(**mapping) |
| | 68 | from_yaml = classmethod(from_yaml) |
| | 69 | def to_yaml(cls, representer, native): |
| | 70 | return representer.represent_mapping(cls.yaml_tag, native.__dict__) |
| | 71 | to_yaml = classmethod(to_yaml) |
| | 72 | |
| | 73 | class YAMLObject1(yaml.YAMLObject): |
| | 74 | yaml_loader = MyLoader |
| | 75 | yaml_dumper = MyDumper |
| | 76 | yaml_tag = '!foo' |
| | 77 | def __init__(self, my_parameter=None, my_another_parameter=None): |
| | 78 | self.my_parameter = my_parameter |
| | 79 | self.my_another_parameter = my_another_parameter |
| | 80 | def __eq__(self, other): |
| | 81 | if isinstance(other, YAMLObject1): |
| | 82 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
| | 83 | else: |
| | 84 | return False |
| | 85 | |
| | 86 | class YAMLObject2(yaml.YAMLObject): |
| | 87 | yaml_loader = MyLoader |
| | 88 | yaml_dumper = MyDumper |
| | 89 | yaml_tag = '!bar' |
| | 90 | def __init__(self, foo=1, bar=2, baz=3): |
| | 91 | self.foo = foo |
| | 92 | self.bar = bar |
| | 93 | self.baz = baz |
| | 94 | def __getstate__(self): |
| | 95 | return {1: self.foo, 2: self.bar, 3: self.baz} |
| | 96 | def __setstate__(self, state): |
| | 97 | self.foo = state[1] |
| | 98 | self.bar = state[2] |
| | 99 | self.baz = state[3] |
| | 100 | def __eq__(self, other): |
| | 101 | if isinstance(other, YAMLObject2): |
| | 102 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
| | 103 | else: |
| | 104 | return False |
| | 105 | |
| | 106 | class AnObject(object): |
| | 107 | def __new__(cls, foo=None, bar=None, baz=None): |
| | 108 | self = object.__new__(cls) |
| | 109 | self.foo = foo |
| | 110 | self.bar = bar |
| | 111 | self.baz = baz |
| | 112 | return self |
| | 113 | def __cmp__(self, other): |
| | 114 | return cmp((type(self), self.foo, self.bar, self.baz), |
| | 115 | (type(other), other.foo, other.bar, other.baz)) |
| | 116 | def __eq__(self, other): |
| | 117 | return type(self) is type(other) and \ |
| | 118 | (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) |
| | 119 | |
| | 120 | class AnInstance: |
| | 121 | def __init__(self, foo=None, bar=None, baz=None): |
| | 122 | self.foo = foo |
| | 123 | self.bar = bar |
| | 124 | self.baz = baz |
| | 125 | def __cmp__(self, other): |
| | 126 | return cmp((type(self), self.foo, self.bar, self.baz), |
| | 127 | (type(other), other.foo, other.bar, other.baz)) |
| | 128 | def __eq__(self, other): |
| | 129 | return type(self) is type(other) and \ |
| | 130 | (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) |
| | 131 | |
| | 132 | class AState(AnInstance): |
| | 133 | def __getstate__(self): |
| | 134 | return { |
| | 135 | '_foo': self.foo, |
| | 136 | '_bar': self.bar, |
| | 137 | '_baz': self.baz, |
| | 138 | } |
| | 139 | def __setstate__(self, state): |
| | 140 | self.foo = state['_foo'] |
| | 141 | self.bar = state['_bar'] |
| | 142 | self.baz = state['_baz'] |
| | 143 | |
| | 144 | class ACustomState(AnInstance): |
| | 145 | def __getstate__(self): |
| | 146 | return (self.foo, self.bar, self.baz) |
| | 147 | def __setstate__(self, state): |
| | 148 | self.foo, self.bar, self.baz = state |
| | 149 | |
| | 150 | class InitArgs(AnInstance): |
| | 151 | def __getinitargs__(self): |
| | 152 | return (self.foo, self.bar, self.baz) |
| | 153 | def __getstate__(self): |
| | 154 | return {} |
| | 155 | |
| | 156 | class InitArgsWithState(AnInstance): |
| | 157 | def __getinitargs__(self): |
| | 158 | return (self.foo, self.bar) |
| | 159 | def __getstate__(self): |
| | 160 | return self.baz |
| | 161 | def __setstate__(self, state): |
| | 162 | self.baz = state |
| | 163 | |
| | 164 | class NewArgs(AnObject): |
| | 165 | def __getnewargs__(self): |
| | 166 | return (self.foo, self.bar, self.baz) |
| | 167 | def __getstate__(self): |
| | 168 | return {} |
| | 169 | |
| | 170 | class NewArgsWithState(AnObject): |
| | 171 | def __getnewargs__(self): |
| | 172 | return (self.foo, self.bar) |
| | 173 | def __getstate__(self): |
| | 174 | return self.baz |
| | 175 | def __setstate__(self, state): |
| | 176 | self.baz = state |
| | 177 | |
| | 178 | class Reduce(AnObject): |
| | 179 | def __reduce__(self): |
| | 180 | return self.__class__, (self.foo, self.bar, self.baz) |
| | 181 | |
| | 182 | class ReduceWithState(AnObject): |
| | 183 | def __reduce__(self): |
| | 184 | return self.__class__, (self.foo, self.bar), self.baz |
| | 185 | def __setstate__(self, state): |
| | 186 | self.baz = state |
| | 187 | |
| | 188 | class MyInt(int): |
| | 189 | def __eq__(self, other): |
| | 190 | return type(self) is type(other) and int(self) == int(other) |
| | 191 | |
| | 192 | class MyList(list): |
| | 193 | def __init__(self, n=1): |
| | 194 | self.extend([None]*n) |
| | 195 | def __eq__(self, other): |
| | 196 | return type(self) is type(other) and list(self) == list(other) |
| | 197 | |
| | 198 | class MyDict(dict): |
| | 199 | def __init__(self, n=1): |
| | 200 | for k in range(n): |
| | 201 | self[k] = None |
| | 202 | def __eq__(self, other): |
| | 203 | return type(self) is type(other) and dict(self) == dict(other) |
| | 204 | |
| | 205 | class FixedOffset(datetime.tzinfo): |
| | 206 | def __init__(self, offset, name): |
| | 207 | self.__offset = datetime.timedelta(minutes=offset) |
| | 208 | self.__name = name |
| | 209 | def utcoffset(self, dt): |
| | 210 | return self.__offset |
| | 211 | def tzname(self, dt): |
| | 212 | return self.__name |
| | 213 | def dst(self, dt): |
| | 214 | return datetime.timedelta(0) |
| | 215 | |
| | 216 | def _load_code(expression): |
| | 217 | return eval(expression) |
| | 218 | |
| | 219 | def _serialize_value(data): |
| | 220 | if isinstance(data, list): |
| | 221 | return '[%s]' % ', '.join(map(_serialize_value, data)) |
| | 222 | elif isinstance(data, dict): |
| | 223 | items = [] |
| | 224 | for key, value in data.items(): |
| | 225 | key = _serialize_value(key) |
| | 226 | value = _serialize_value(value) |
| | 227 | items.append("%s: %s" % (key, value)) |
| | 228 | items.sort() |
| | 229 | return '{%s}' % ', '.join(items) |
| | 230 | elif isinstance(data, datetime.datetime): |
| | 231 | return repr(data.utctimetuple()) |
| | 232 | elif isinstance(data, unicode): |
| | 233 | return data.encode('utf-8') |
| | 234 | else: |
| | 235 | return str(data) |
| | 236 | |
| | 237 | def test_constructor_types(data_filename, code_filename, verbose=False): |
| | 238 | _make_objects() |
| | 239 | native1 = None |
| | 240 | native2 = None |
| | 241 | try: |
| | 242 | native1 = list(yaml.load_all(open(data_filename, 'rb'), Loader=MyLoader)) |
| | 243 | if len(native1) == 1: |
| | 244 | native1 = native1[0] |
| | 245 | native2 = _load_code(open(code_filename, 'rb').read()) |
| 267 | | data1 = list(load_all(file(data_filename, 'rb'), Loader=MyLoader)) |
| 268 | | if len(data1) == 1: |
| 269 | | data1 = data1[0] |
| 270 | | data2 = eval(file(code_filename, 'rb').read()) |
| 271 | | self.failUnlessEqual(type(data1), type(data2)) |
| 272 | | try: |
| 273 | | self.failUnlessEqual(data1, data2) |
| 274 | | except (AssertionError, TypeError): |
| 275 | | if isinstance(data1, dict): |
| 276 | | data1 = [(repr(key), value) for key, value in data1.items()] |
| 277 | | data1.sort() |
| 278 | | data1 = repr(data1) |
| 279 | | data2 = [(repr(key), value) for key, value in data2.items()] |
| 280 | | data2.sort() |
| 281 | | data2 = repr(data2) |
| 282 | | if data1 != data2: |
| 283 | | raise |
| 284 | | elif isinstance(data1, list): |
| 285 | | self.failUnlessEqual(type(data1), type(data2)) |
| 286 | | self.failUnlessEqual(len(data1), len(data2)) |
| 287 | | for item1, item2 in zip(data1, data2): |
| 288 | | if (item1 != item1 or (item1 == 0.0 and item1 == 1.0)) and \ |
| 289 | | (item2 != item2 or (item2 == 0.0 and item2 == 1.0)): |
| 290 | | continue |
| 291 | | if isinstance(item1, datetime.datetime) \ |
| 292 | | and isinstance(item2, datetime.datetime): |
| 293 | | self.failUnlessEqual(item1.microsecond, |
| 294 | | item2.microsecond) |
| 295 | | if isinstance(item1, datetime.datetime): |
| 296 | | item1 = item1.utctimetuple() |
| 297 | | if isinstance(item2, datetime.datetime): |
| 298 | | item2 = item2.utctimetuple() |
| 299 | | self.failUnlessEqual(item1, item2) |
| 300 | | else: |
| 301 | | raise |
| 302 | | except: |
| 303 | | print |
| 304 | | print "DATA:" |
| 305 | | print file(data_filename, 'rb').read() |
| 306 | | print "CODE:" |
| 307 | | print file(code_filename, 'rb').read() |
| 308 | | print "NATIVES1:", data1 |
| 309 | | print "NATIVES2:", data2 |
| 310 | | raise |
| 311 | | |
| 312 | | TestConstructorTypes.add_tests('testTypes', '.data', '.code') |
| 313 | | |
| | 247 | if native1 == native2: |
| | 248 | return |
| | 249 | except TypeError: |
| | 250 | pass |
| | 251 | if verbose: |
| | 252 | print "SERIALIZED NATIVE1:" |
| | 253 | print _serialize_value(native1) |
| | 254 | print "SERIALIZED NATIVE2:" |
| | 255 | print _serialize_value(native2) |
| | 256 | assert _serialize_value(native1) == _serialize_value(native2), (native1, native2) |
| | 257 | finally: |
| | 258 | if verbose: |
| | 259 | print "NATIVE1:" |
| | 260 | pprint.pprint(native1) |
| | 261 | print "NATIVE2:" |
| | 262 | pprint.pprint(native2) |
| | 263 | |
| | 264 | test_constructor_types.unittest = ['.data', '.code'] |
| | 265 | |
| | 266 | if __name__ == '__main__': |
| | 267 | import sys, test_constructor |
| | 268 | sys.modules['test_constructor'] = sys.modules['__main__'] |
| | 269 | import test_appliance |
| | 270 | test_appliance.run(globals()) |
| | 271 | |