Changeset 222
- Timestamp:
- 08/03/06 12:07:29 (2 years ago)
- Files:
-
- pyyaml/trunk/lib/yaml/composer.py (modified) (3 diffs)
- pyyaml/trunk/lib/yaml/constructor.py (modified) (16 diffs)
- pyyaml/trunk/lib/yaml/emitter.py (modified) (2 diffs)
- pyyaml/trunk/lib/yaml/error.py (modified) (1 diff)
- pyyaml/trunk/lib/yaml/events.py (modified) (1 diff)
- pyyaml/trunk/lib/yaml/nodes.py (modified) (1 diff)
- pyyaml/trunk/lib/yaml/parser.py (modified) (1 diff)
- pyyaml/trunk/lib/yaml/reader.py (modified) (1 diff)
- pyyaml/trunk/lib/yaml/representer.py (modified) (10 diffs)
- pyyaml/trunk/lib/yaml/resolver.py (modified) (1 diff)
- pyyaml/trunk/lib/yaml/scanner.py (modified) (3 diffs)
- pyyaml/trunk/lib/yaml/serializer.py (modified) (3 diffs)
- pyyaml/trunk/lib/yaml/tokens.py (modified) (1 diff)
- pyyaml/trunk/tests/data/duplicate-key.former-loader-error.code (added)
- pyyaml/trunk/tests/data/duplicate-key.former-loader-error.data (moved) (moved from pyyaml/trunk/tests/data/duplicate-key.loader-error)
- pyyaml/trunk/tests/data/duplicate-mapping-key.former-loader-error.code (added)
- pyyaml/trunk/tests/data/duplicate-mapping-key.former-loader-error.data (moved) (moved from pyyaml/trunk/tests/data/duplicate-mapping-key.loader-error)
- pyyaml/trunk/tests/data/duplicate-merge-key.former-loader-error.code (added)
- pyyaml/trunk/tests/data/duplicate-merge-key.former-loader-error.data (moved) (moved from pyyaml/trunk/tests/data/duplicate-merge-key.loader-error)
- pyyaml/trunk/tests/data/duplicate-value-key.former-loader-error.code (added)
- pyyaml/trunk/tests/data/duplicate-value-key.former-loader-error.data (moved) (moved from pyyaml/trunk/tests/data/duplicate-value-key.loader-error)
- pyyaml/trunk/tests/data/recurive-list.recursive (added)
- pyyaml/trunk/tests/data/recursive-anchor.former-loader-error (moved) (moved from pyyaml/trunk/tests/data/recursive-anchor.loader-error)
- pyyaml/trunk/tests/data/recursive-dict.recursive (added)
- pyyaml/trunk/tests/data/recursive-set.recursive (added)
- pyyaml/trunk/tests/data/recursive-state.recursive (added)
- pyyaml/trunk/tests/data/recursive-tuple.recursive (added)
- pyyaml/trunk/tests/data/recursive.former-dumper-error (moved) (moved from pyyaml/trunk/tests/data/recursive.dumper-error)
- pyyaml/trunk/tests/test_constructor.py (modified) (1 diff)
- pyyaml/trunk/tests/test_recursive.py (modified) (1 diff)
- pyyaml/trunk/tests/test_resolver.py (modified) (1 diff)
- pyyaml/trunk/tests/test_structure.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pyyaml/trunk/lib/yaml/composer.py
r198 r222 9 9 pass 10 10 11 class Composer :11 class Composer(object): 12 12 13 13 def __init__(self): … … 100 100 if tag is None or tag == u'!': 101 101 tag = self.resolve(MappingNode, None, start_event.implicit) 102 node = MappingNode(tag, {},102 node = MappingNode(tag, [], 103 103 start_event.start_mark, None, 104 104 flow_style=start_event.flow_style) … … 106 106 self.anchors[anchor] = node 107 107 while not self.check_event(MappingEndEvent): 108 key_event = self.peek_event()108 #key_event = self.peek_event() 109 109 item_key = self.compose_node(node, None) 110 if item_key in node.value:111 raise ComposerError("while composing a mapping", start_event.start_mark,112 "found duplicate key", key_event.start_mark)110 #if item_key in node.value: 111 # raise ComposerError("while composing a mapping", start_event.start_mark, 112 # "found duplicate key", key_event.start_mark) 113 113 item_value = self.compose_node(node, item_key) 114 node.value[item_key] = item_value 114 #node.value[item_key] = item_value 115 node.value.append((item_key, item_value)) 115 116 end_event = self.get_event() 116 117 node.end_mark = end_event.end_mark pyyaml/trunk/lib/yaml/constructor.py
r198 r222 22 22 pass 23 23 24 class BaseConstructor :24 class BaseConstructor(object): 25 25 26 26 yaml_constructors = {} … … 30 30 self.constructed_objects = {} 31 31 self.recursive_objects = {} 32 self.state_generators = [] 33 self.deep_construct = False 32 34 33 35 def check_data(self): … … 40 42 return self.construct_document(self.get_node()) 41 43 44 def g(): yield None 45 generator_type = type(g()) 46 del g 47 42 48 def construct_document(self, node): 43 49 data = self.construct_object(node) 50 while self.state_generators: 51 state_generators = self.state_generators 52 self.state_generators = [] 53 for generator in state_generators: 54 for dummy in generator: 55 pass 44 56 self.constructed_objects = {} 45 57 self.recursive_objects = {} 58 self.deep_construct = False 46 59 return data 47 60 48 def construct_object(self, node): 61 def construct_object(self, node, deep=False): 62 if deep: 63 old_deep = self.deep_construct 64 self.deep_construct = True 49 65 if node in self.constructed_objects: 50 66 return self.constructed_objects[node] 51 67 if node in self.recursive_objects: 52 68 raise ConstructorError(None, None, 53 "found recursive node", node.start_mark)69 "found unconstructable recursive node", node.start_mark) 54 70 self.recursive_objects[node] = None 55 71 constructor = None 72 state_constructor = None 73 tag_suffix = None 56 74 if node.tag in self.yaml_constructors: 57 constructor = lambda node: self.yaml_constructors[node.tag](self, node)75 constructor = self.yaml_constructors[node.tag] 58 76 else: 59 77 for tag_prefix in self.yaml_multi_constructors: 60 78 if node.tag.startswith(tag_prefix): 61 79 tag_suffix = node.tag[len(tag_prefix):] 62 constructor = lambda node: \ 63 self.yaml_multi_constructors[tag_prefix](self, tag_suffix, node) 80 constructor = self.yaml_multi_constructors[tag_prefix] 64 81 break 65 82 else: 66 83 if None in self.yaml_multi_constructors: 67 constructor = lambda node: \68 self.yaml_multi_constructors[None](self, node.tag, node)84 tag_suffix = node.tag 85 constructor = self.yaml_multi_constructors[None] 69 86 elif None in self.yaml_constructors: 70 constructor = lambda node: \ 71 self.yaml_constructors[None](self, node) 87 constructor = self.yaml_constructors[None] 72 88 elif isinstance(node, ScalarNode): 73 constructor = self. construct_scalar89 constructor = self.__class__.construct_scalar 74 90 elif isinstance(node, SequenceNode): 75 constructor = self. construct_sequence91 constructor = self.__class__.construct_sequence 76 92 elif isinstance(node, MappingNode): 77 constructor = self.construct_mapping 78 else: 79 print node.tag 80 data = constructor(node) 93 constructor = self.__class__.construct_mapping 94 if tag_suffix is None: 95 data = constructor(self, node) 96 else: 97 data = constructor(self, tag_suffix, node) 98 if isinstance(data, self.generator_type): 99 generator = data 100 data = generator.next() 101 if self.deep_construct: 102 for dummy in generator: 103 pass 104 else: 105 self.state_generators.append(generator) 81 106 self.constructed_objects[node] = data 82 107 del self.recursive_objects[node] 108 if deep: 109 self.deep_construct = old_deep 83 110 return data 84 111 85 112 def construct_scalar(self, node): 86 113 if not isinstance(node, ScalarNode): 87 if isinstance(node, MappingNode):88 for key_node in node.value:89 if key_node.tag == u'tag:yaml.org,2002:value':90 return self.construct_scalar(node.value[key_node])91 114 raise ConstructorError(None, None, 92 115 "expected a scalar node, but found %s" % node.id, … … 94 117 return node.value 95 118 96 def construct_sequence(self, node ):119 def construct_sequence(self, node, deep=False): 97 120 if not isinstance(node, SequenceNode): 98 121 raise ConstructorError(None, None, 99 122 "expected a sequence node, but found %s" % node.id, 100 123 node.start_mark) 101 return [self.construct_object(child) for child in node.value] 102 103 def construct_mapping(self, node): 124 return [self.construct_object(child, deep=deep) 125 for child in node.value] 126 127 def construct_mapping(self, node, deep=False): 104 128 if not isinstance(node, MappingNode): 105 129 raise ConstructorError(None, None, … … 107 131 node.start_mark) 108 132 mapping = {} 109 merge = None 110 for key_node in node.value: 133 for key_node, value_node in node.value: 134 key = self.construct_object(key_node, deep=deep) 135 try: 136 hash(key) 137 except TypeError, exc: 138 raise ConstructorError("while constructing a mapping", node.start_mark, 139 "found unacceptable key (%s)" % exc, key_node.start_mark) 140 value = self.construct_object(value_node, deep=deep) 141 mapping[key] = value 142 return mapping 143 144 def construct_pairs(self, node, deep=False): 145 if not isinstance(node, MappingNode): 146 raise ConstructorError(None, None, 147 "expected a mapping node, but found %s" % node.id, 148 node.start_mark) 149 pairs = [] 150 for key_node, value_node in node.value: 151 key = self.construct_object(key_node, deep=deep) 152 value = self.construct_object(value_node, deep=deep) 153 pairs.append((key, value)) 154 return pairs 155 156 def add_constructor(cls, tag, constructor): 157 if not 'yaml_constructors' in cls.__dict__: 158 cls.yaml_constructors = cls.yaml_constructors.copy() 159 cls.yaml_constructors[tag] = constructor 160 add_constructor = classmethod(add_constructor) 161 162 def add_multi_constructor(cls, tag_prefix, multi_constructor): 163 if not 'yaml_multi_constructors' in cls.__dict__: 164 cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy() 165 cls.yaml_multi_constructors[tag_prefix] = multi_constructor 166 add_multi_constructor = classmethod(add_multi_constructor) 167 168 class SafeConstructor(BaseConstructor): 169 170 def construct_scalar(self, node): 171 if isinstance(node, MappingNode): 172 for key_node, value_node in node.value: 173 if key_node.tag == u'tag:yaml.org,2002:value': 174 return self.construct_scalar(value_node) 175 return BaseConstructor.construct_scalar(self, node) 176 177 def flatten_mapping(self, node): 178 merge = [] 179 index = 0 180 while index < len(node.value): 181 key_node, value_node = node.value[index] 111 182 if key_node.tag == u'tag:yaml.org,2002:merge': 112 if merge is not None: 113 raise ConstructorError("while constructing a mapping", node.start_mark, 114 "found duplicate merge key", key_node.start_mark) 115 value_node = node.value[key_node] 183 del node.value[index] 116 184 if isinstance(value_node, MappingNode): 117 merge = [self.construct_mapping(value_node)] 185 self.flatten_mapping(value_node) 186 merge.extend(value_node.value) 118 187 elif isinstance(value_node, SequenceNode): 119 merge = []188 submerge = [] 120 189 for subnode in value_node.value: 121 190 if not isinstance(subnode, MappingNode): … … 124 193 "expected a mapping for merging, but found %s" 125 194 % subnode.id, subnode.start_mark) 126 merge.append(self.construct_mapping(subnode)) 127 merge.reverse() 195 self.flatten_mapping(subnode) 196 submerge.append(subnode.value) 197 submerge.reverse() 198 for value in submerge: 199 merge.extend(value) 128 200 else: 129 201 raise ConstructorError("while constructing a mapping", node.start_mark, … … 131 203 % value_node.id, value_node.start_mark) 132 204 elif key_node.tag == u'tag:yaml.org,2002:value': 133 if '=' in mapping: 134 raise ConstructorError("while construction a mapping", node.start_mark, 135 "found duplicate value key", key_node.start_mark) 136 value = self.construct_object(node.value[key_node]) 137 mapping['='] = value 205 key_node.tag = u'tag:yaml.org,2002:str' 206 index += 1 138 207 else: 139 key = self.construct_object(key_node) 140 try: 141 duplicate_key = key in mapping 142 except TypeError, exc: 143 raise ConstructorError("while constructing a mapping", node.start_mark, 144 "found unacceptable key (%s)" % exc, key_node.start_mark) 145 if duplicate_key: 146 raise ConstructorError("while constructing a mapping", node.start_mark, 147 "found duplicate key", key_node.start_mark) 148 value = self.construct_object(node.value[key_node]) 149 mapping[key] = value 150 if merge is not None: 151 merge.append(mapping) 152 mapping = {} 153 for submapping in merge: 154 mapping.update(submapping) 155 return mapping 156 157 def construct_pairs(self, node): 158 if not isinstance(node, MappingNode): 159 raise ConstructorError(None, None, 160 "expected a mapping node, but found %s" % node.id, 161 node.start_mark) 162 pairs = [] 163 for key_node in node.value: 164 key = self.construct_object(key_node) 165 value = self.construct_object(node.value[key_node]) 166 pairs.append((key, value)) 167 return pairs 168 169 def add_constructor(cls, tag, constructor): 170 if not 'yaml_constructors' in cls.__dict__: 171 cls.yaml_constructors = cls.yaml_constructors.copy() 172 cls.yaml_constructors[tag] = constructor 173 add_constructor = classmethod(add_constructor) 174 175 def add_multi_constructor(cls, tag_prefix, multi_constructor): 176 if not 'yaml_multi_constructors' in cls.__dict__: 177 cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy() 178 cls.yaml_multi_constructors[tag_prefix] = multi_constructor 179 add_multi_constructor = classmethod(add_multi_constructor) 180 181 class SafeConstructor(BaseConstructor): 208 index += 1 209 if merge: 210 node.value = merge + node.value 211 212 def construct_mapping(self, node, deep=False): 213 if isinstance(node, MappingNode): 214 self.flatten_mapping(node) 215 return BaseConstructor.construct_mapping(self, node, deep=deep) 182 216 183 217 def construct_yaml_null(self, node): … … 297 331 # Note: we do not check for duplicate keys, because it's too 298 332 # CPU-expensive. 333 omap = [] 334 yield omap 299 335 if not isinstance(node, SequenceNode): 300 336 raise ConstructorError("while constructing an ordered map", node.start_mark, 301 337 "expected a sequence, but found %s" % node.id, node.start_mark) 302 omap = []303 338 for subnode in node.value: 304 339 if not isinstance(subnode, MappingNode): … … 310 345 "expected a single mapping item, but found %d items" % len(subnode.value), 311 346 subnode.start_mark) 312 key_node = subnode.value.keys()[0]347 key_node, value_node = subnode.value[0] 313 348 key = self.construct_object(key_node) 314 value = self.construct_object( subnode.value[key_node])349 value = self.construct_object(value_node) 315 350 omap.append((key, value)) 316 return omap317 351 318 352 def construct_yaml_pairs(self, node): 319 353 # Note: the same code as `construct_yaml_omap`. 354 pairs = [] 355 yield pairs 320 356 if not isinstance(node, SequenceNode): 321 357 raise ConstructorError("while constructing pairs", node.start_mark, 322 358 "expected a sequence, but found %s" % node.id, node.start_mark) 323 pairs = []324 359 for subnode in node.value: 325 360 if not isinstance(subnode, MappingNode): … … 331 366 "expected a single mapping item, but found %d items" % len(subnode.value), 332 367 subnode.start_mark) 333 key_node = subnode.value.keys()[0]368 key_node, value_node = subnode.value[0] 334 369 key = self.construct_object(key_node) 335 value = self.construct_object( subnode.value[key_node])370 value = self.construct_object(value_node) 336 371 pairs.append((key, value)) 337 return pairs338 372 339 373 def construct_yaml_set(self, node): 374 data = set() 375 yield data 340 376 value = self.construct_mapping(node) 341 return set(value)377 data.update(value) 342 378 343 379 def construct_yaml_str(self, node): … … 349 385 350 386 def construct_yaml_seq(self, node): 351 return self.construct_sequence(node) 387 data = [] 388 yield data 389 data.extend(self.construct_sequence(node)) 352 390 353 391 def construct_yaml_map(self, node): 354 return self.construct_mapping(node) 392 data = {} 393 yield data 394 value = self.construct_mapping(node) 395 data.update(value) 355 396 356 397 def construct_yaml_object(self, node, cls): 357 state = self.construct_mapping(node)358 398 data = cls.__new__(cls) 399 yield data 359 400 if hasattr(data, '__setstate__'): 401 state = self.construct_mapping(node, deep=True) 360 402 data.__setstate__(state) 361 403 else: 404 state = self.construct_mapping(node) 362 405 data.__dict__.update(state) 363 return data364 406 365 407 def construct_undefined(self, node): … … 435 477 436 478 def construct_python_tuple(self, node): 437 return tuple(self.construct_ yaml_seq(node))479 return tuple(self.construct_sequence(node)) 438 480 439 481 def find_python_module(self, name, mark): … … 526 568 # !!python/object:module.name { ... state ... } 527 569 instance = self.make_python_instance(suffix, node, newobj=True) 528 state = self.construct_mapping(node) 570 yield instance 571 deep = hasattr(instance, '__setstate__') 572 state = self.construct_mapping(node, deep=deep) 529 573 self.set_python_instance_state(instance, state) 530 return instance531 574 532 575 def construct_python_object_apply(self, suffix, node, newobj=False): … … 543 586 # is how an object is created, check make_python_instance for details. 544 587 if isinstance(node, SequenceNode): 545 args = self.construct_sequence(node )588 args = self.construct_sequence(node, deep=True) 546 589 kwds = {} 547 590 state = {} … … 549 592 dictitems = {} 550 593 else: 551 value = self.construct_mapping(node )594 value = self.construct_mapping(node, deep=True) 552 595 args = value.get('args', []) 553 596 kwds = value.get('kwds', {}) … … 568 611 return self.construct_python_object_apply(suffix, node, newobj=True) 569 612 570 571 613 Constructor.add_constructor( 572 614 u'tag:yaml.org,2002:python/none', pyyaml/trunk/lib/yaml/emitter.py
r218 r222 17 17 pass 18 18 19 class ScalarAnalysis :19 class ScalarAnalysis(object): 20 20 def __init__(self, scalar, empty, multiline, 21 21 allow_flow_plain, allow_block_plain, … … 31 31 self.allow_block = allow_block 32 32 33 class Emitter :33 class Emitter(object): 34 34 35 35 DEFAULT_TAG_PREFIXES = { pyyaml/trunk/lib/yaml/error.py
r136 r222 2 2 __all__ = ['Mark', 'YAMLError', 'MarkedYAMLError'] 3 3 4 class Mark :4 class Mark(object): 5 5 6 6 def __init__(self, name, index, line, column, buffer, pointer): pyyaml/trunk/lib/yaml/events.py
r137 r222 2 2 # Abstract classes. 3 3 4 class Event :4 class Event(object): 5 5 def __init__(self, start_mark=None, end_mark=None): 6 6 self.start_mark = start_mark pyyaml/trunk/lib/yaml/nodes.py
r153 r222 1 1 2 class Node :2 class Node(object): 3 3 def __init__(self, tag, value, start_mark, end_mark): 4 4 self.tag = tag pyyaml/trunk/lib/yaml/parser.py
r204 r222 70 70 pass 71 71 72 class Parser :72 class Parser(object): 73 73 # Since writing a recursive-descendant parser is a straightforward task, we 74 74 # do not give many comments here. pyyaml/trunk/lib/yaml/reader.py
r188 r222 78 78 self.name, self.position) 79 79 80 class Reader :80 class Reader(object): 81 81 # Reader: 82 82 # - determines the data encoding and converts it to unicode, pyyaml/trunk/lib/yaml/representer.py
r206 r222 22 22 pass 23 23 24 class BaseRepresenter :24 class BaseRepresenter(object): 25 25 26 26 yaml_representers = {} … … 31 31 self.default_flow_style = default_flow_style 32 32 self.represented_objects = {} 33 self.object_keeper = [] 34 self.alias_key = None 33 35 34 36 def represent(self, data): … … 36 38 self.serialize(node) 37 39 self.represented_objects = {} 40 self.object_keeper = [] 41 self.alias_key = None 38 42 39 43 class C: pass 40 44 c = C() 41 45 def f(): pass 46 def g(): yield None 42 47 classobj_type = type(C) 43 48 instance_type = type(c) 44 49 function_type = type(f) 50 generator_type = type(g()) 45 51 builtin_function_type = type(abs) 46 52 module_type = type(sys) 47 del C, c, f 53 del C, c, f, g 48 54 49 55 def get_classobj_bases(self, cls): … … 55 61 def represent_data(self, data): 56 62 if self.ignore_aliases(data): 57 alias_key = None58 else: 59 alias_key = id(data)60 if alias_key is not None:61 if alias_key in self.represented_objects:62 node = self.represented_objects[ alias_key]63 if node is None:64 raise RepresenterError("recursive objects are not allowed: %r" % data)63 self.alias_key = None 64 else: 65 self.alias_key = id(data) 66 if self.alias_key is not None: 67 if self.alias_key in self.represented_objects: 68 node = self.represented_objects[self.alias_key] 69 #if node is None: 70 # raise RepresenterError("recursive objects are not allowed: %r" % data) 65 71 return node 66 self.represented_objects[alias_key] = None 72 #self.represented_objects[alias_key] = None 73 self.object_keeper.append(data) 67 74 data_types = type(data).__mro__ 68 75 if type(data) is self.instance_type: … … 82 89 else: 83 90 node = ScalarNode(None, unicode(data)) 84 if alias_key is not None:85 self.represented_objects[alias_key] = node91 #if alias_key is not None: 92 # self.represented_objects[alias_key] = node 86 93 return node 87 94 … … 101 108 if style is None: 102 109 style = self.default_style 103 return ScalarNode(tag, value, style=style) 110 node = ScalarNode(tag, value, style=style) 111 if self.alias_key is not None: 112 self.represented_objects[self.alias_key] = node 113 return node 104 114 105 115 def represent_sequence(self, tag, sequence, flow_style=None): 116 value = [] 117 node = SequenceNode(tag, value, flow_style=flow_style) 118 if self.alias_key is not None: 119 self.represented_objects[self.alias_key] = node 106 120 best_style = True 107 value = []108 121 for item in sequence: 109 122 node_item = self.represent_data(item) 110 123 if not (isinstance(node_item, ScalarNode) and not node_item.style): 111 124 best_style = False 112 value.append( self.represent_data(item))125 value.append(node_item) 113 126 if flow_style is None: 114 flow_style = self.default_flow_style 127 if self.default_flow_style is not None: 128 node.flow_style = self.default_flow_style 129 else: 130 node.flow_style = best_style 131 return node 132 133 def represent_mapping(self, tag, mapping, flow_style=None): 134 value = [] 135 node = MappingNode(tag, value, flow_style=flow_style) 136 if self.alias_key is not None: 137 self.represented_objects[self.alias_key] = node 138 best_style = True 139 if hasattr(mapping, 'items'): 140 mapping = mapping.items() 141 mapping.sort() 142 for item_key, item_value in mapping: 143 node_key = self.represent_data(item_key) 144 node_value = self.represent_data(item_value) 145 if not (isinstance(node_key, ScalarNode) and not node_key.style): 146 best_style = False 147 if not (isinstance(node_value, ScalarNode) and not node_value.style): 148 best_style = False 149 value.append((node_key, node_value)) 115 150 if flow_style is None: 116 flow_style = best_style 117 return SequenceNode(tag, value, flow_style=flow_style) 118 119 def represent_mapping(self, tag, mapping, flow_style=None): 120 best_style = True 121 if hasattr(mapping, 'keys'): 122 value = {} 123 for item_key in mapping.keys(): 124 item_value = mapping[item_key] 125 node_key = self.represent_data(item_key) 126 node_value = self.represent_data(item_value) 127 if not (isinstance(node_key, ScalarNode) and not node_key.style): 128 best_style = False 129 if not (isinstance(node_value, ScalarNode) and not node_value.style): 130 best_style = False 131 value[node_key] = node_value 132 else: 133 value = [] 134 for item_key, item_value in mapping: 135 node_key = self.represent_data(item_key) 136 node_value = self.represent_data(item_value) 137 if not (isinstance(node_key, ScalarNode) and not node_key.style): 138 best_style = False 139 if not (isinstance(node_value, ScalarNode) and not node_value.style): 140 best_style = False 141 value.append((node_key, node_value)) 142 if flow_style is None: 143 flow_style = self.default_flow_style 144 if flow_style is None: 145 flow_style = best_style 146 return MappingNode(tag, value, flow_style=flow_style) 151 if self.default_flow_style is not None: 152 node.flow_style = self.default_flow_style 153 else: 154 node.flow_style = best_style 155 return node 147 156 148 157 def ignore_aliases(self, data): … … 209 218 210 219 def represent_list(self, data): 211 pairs = (len(data) > 0 and isinstance(data, list))212 if pairs:213 for item in data:214 if not isinstance(item, tuple) or len(item) != 2:215 pairs = False216 break217 if not pairs:220 #pairs = (len(data) > 0 and isinstance(data, list)) 221 #if pairs: 222 # for item in data: 223 # if not isinstance(item, tuple) or len(item) != 2: 224 # pairs = False 225 # break 226 #if not pairs: 218 227 return self.represent_sequence(u'tag:yaml.org,2002:seq', data) 219 value = []220 for item_key, item_value in data:221 value.append(self.represent_mapping(u'tag:yaml.org,2002:map',222 [(item_key, item_value)]))223 return SequenceNode(u'tag:yaml.org,2002:pairs', value)228 #value = [] 229 #for item_key, item_value in data: 230 # value.append(self.represent_mapping(u'tag:yaml.org,2002:map', 231 # [(item_key, item_value)])) 232 #return SequenceNode(u'tag:yaml.org,2002:pairs', value) 224 233 225 234 def represent_dict(self, data): … … 251 260 else: 252 261 state = data.__dict__.copy() 253 if isinstance(state, dict):254 state = state.items()255 state.sort()256 262 return self.represent_mapping(tag, state, flow_style=flow_style) 257 263 … … 385 391 state = data.__dict__ 386 392 if args is None and isinstance(state, dict): 387 state = state.items()388 state.sort()389 393 return self.represent_mapping( 390 394 u'tag:yaml.org,2002:python/object:'+class_name, state) … … 445 449 if not args and not listitems and not dictitems \ 446 450 and isinstance(state, dict) and newobj: 447 state = state.items()448 state.sort()449 451 return self.represent_mapping( 450 452 u'tag:yaml.org,2002:python/object:'+function_name, state) pyyaml/trunk/lib/yaml/resolver.py
r158 r222 10 10 pass 11 11 12 class BaseResolver :12 class BaseResolver(object): 13 13 14 14 DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str' pyyaml/trunk/lib/yaml/scanner.py
r198 r222 20 20 # ANCHOR(value) 21 21 # TAG(value) 22 # SCALAR(value, plain )22 # SCALAR(value, plain, style) 23 23 # 24 24 # Read comments in the Scanner code for more details. … … 33 33 pass 34 34 35 class SimpleKey :35 class SimpleKey(object): 36 36 # See below simple keys treatment. 37 37 … … 44 44 self.mark = mark 45 45 46 class Scanner :46 class Scanner(object): 47 47 48 48 def __init__(self): pyyaml/trunk/lib/yaml/serializer.py
r139 r222 9 9 pass 10 10 11 class Serializer :11 class Serializer(object): 12 12 13 13 ANCHOR_TEMPLATE = u'id%03d' … … 68 68 self.anchor_node(item) 69 69 elif isinstance(node, MappingNode): 70 if hasattr(node.value, 'keys'): 71 for key in node.value.keys(): 72 self.anchor_node(key) 73 self.anchor_node(node.value[key]) 74 else: 75 for key, value in node.value: 76 self.anchor_node(key) 77 self.anchor_node(value) 70 for key, value in node.value: 71 self.anchor_node(key) 72 self.anchor_node(value) 78 73 79 74 def generate_anchor(self, node): … … 109 104 self.emit(MappingStartEvent(alias, node.tag, implicit, 110 105 flow_style=node.flow_style)) 111 if hasattr(node.value, 'keys'): 112 for key in node.value.keys(): 113 self.serialize_node(key, node, None) 114 self.serialize_node(node.value[key], node, key) 115 else: 116 for key, value in node.value: 117 self.serialize_node(key, node, None) 118 self.serialize_node(value, node, key) 106 for key, value in node.value: 107 self.serialize_node(key, node, None) 108 self.serialize_node(value, node, key) 119 109 self.emit(MappingEndEvent()) 120 110 self.ascend_resolver() pyyaml/trunk/lib/yaml/tokens.py
r137 r222 1 1 2 class Token :2 class Token(object): 3 3 def __init__(self, start_mark, end_mark): 4 4 self.start_mark = start_mark pyyaml/trunk/tests/test_constructor.py
r173 r222 240 240 def __eq__(self, other): 241 241 return type(self) is type(other) and dict(self) == dict(other) 242 243 def execute(code): 244 exec code 245 return value 242 246 243 247 class TestConstructorTypes(test_appliance.TestAppliance): pyyaml/trunk/tests/test_recursive.py
r142 r222 1 1 2 import unittest 2 import test_appliance 3 3 4 f
