Ticket #74: python3000.patch
| File python3000.patch, 175.1 kB (added by idadesub@users.sourceforge.net, 3 months ago) |
|---|
-
lib/yaml/__init__.py
old new 1 import io 1 2 2 from error import *3 from yaml.error import * 3 4 4 from tokens import *5 from events import *6 from nodes import *5 from yaml.tokens import * 6 from yaml.events import * 7 from yaml.nodes import * 7 8 8 from loader import *9 from dumper import *9 from yaml.loader import * 10 from yaml.dumper import * 10 11 11 12 try: 12 from cyaml import *13 from yaml.cyaml import * 13 14 except ImportError: 14 15 pass 15 16 … … 88 89 """ 89 90 getvalue = None 90 91 if stream is None: 91 try: 92 from cStringIO import StringIO 93 except ImportError: 94 from StringIO import StringIO 95 stream = StringIO() 92 stream = io.StringIO() 96 93 getvalue = stream.getvalue 97 94 dumper = Dumper(stream, canonical=canonical, indent=indent, width=width, 98 95 allow_unicode=allow_unicode, line_break=line_break) … … 112 109 """ 113 110 getvalue = None 114 111 if stream is None: 115 try: 116 from cStringIO import StringIO 117 except ImportError: 118 from StringIO import StringIO 119 stream = StringIO() 112 stream = io.StringIO() 120 113 getvalue = stream.getvalue 121 114 dumper = Dumper(stream, canonical=canonical, indent=indent, width=width, 122 115 allow_unicode=allow_unicode, line_break=line_break, … … 148 141 """ 149 142 getvalue = None 150 143 if stream is None: 151 try: 152 from cStringIO import StringIO 153 except ImportError: 154 from StringIO import StringIO 155 stream = StringIO() 144 stream = io.StringIO() 156 145 getvalue = stream.getvalue 157 146 dumper = Dumper(stream, default_style=default_style, 158 147 default_flow_style=default_flow_style, … … 256 245 cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml) 257 246 cls.yaml_dumper.add_representer(cls, cls.to_yaml) 258 247 259 class YAMLObject( object):248 class YAMLObject(metaclass=YAMLObjectMetaclass): 260 249 """ 261 250 An object that can dump itself to a YAML stream 262 251 and load itself from a YAML stream. 263 252 """ 264 253 265 __metaclass__ = YAMLObjectMetaclass266 254 __slots__ = () # no direct instantiation, so allow immutable subclasses 267 255 268 256 yaml_loader = Loader -
lib/yaml/composer.py
old new 1 1 2 2 __all__ = ['Composer', 'ComposerError'] 3 3 4 from error import MarkedYAMLError5 from events import *6 from nodes import *4 from yaml.error import MarkedYAMLError 5 from yaml.events import * 6 from yaml.nodes import * 7 7 8 8 class ComposerError(MarkedYAMLError): 9 9 pass 10 10 11 class Composer (object):11 class Composer: 12 12 13 13 def __init__(self): 14 14 self.anchors = {} … … 88 88 def compose_scalar_node(self, anchor): 89 89 event = self.get_event() 90 90 tag = event.tag 91 if tag is None or tag == u'!':91 if tag is None or tag == '!': 92 92 tag = self.resolve(ScalarNode, event.value, event.implicit) 93 93 node = ScalarNode(tag, event.value, 94 94 event.start_mark, event.end_mark, style=event.style) … … 99 99 def compose_sequence_node(self, anchor): 100 100 start_event = self.get_event() 101 101 tag = start_event.tag 102 if tag is None or tag == u'!':102 if tag is None or tag == '!': 103 103 tag = self.resolve(SequenceNode, None, start_event.implicit) 104 104 node = SequenceNode(tag, [], 105 105 start_event.start_mark, None, … … 117 117 def compose_mapping_node(self, anchor): 118 118 start_event = self.get_event() 119 119 tag = start_event.tag 120 if tag is None or tag == u'!':120 if tag is None or tag == '!': 121 121 tag = self.resolve(MappingNode, None, start_event.implicit) 122 122 node = MappingNode(tag, [], 123 123 start_event.start_mark, None, -
lib/yaml/constructor.py
old new 2 2 __all__ = ['BaseConstructor', 'SafeConstructor', 'Constructor', 3 3 'ConstructorError'] 4 4 5 from error import *6 from nodes import *5 from yaml.error import * 6 from yaml.nodes import * 7 7 8 8 import datetime 9 9 10 try: 11 set 12 except NameError: 13 from sets import Set as set 14 15 import binascii, re, sys, types 10 import binascii, re, sys, types, base64 16 11 17 12 class ConstructorError(MarkedYAMLError): 18 13 pass 19 14 20 class BaseConstructor (object):15 class BaseConstructor: 21 16 22 17 yaml_constructors = {} 23 18 yaml_multi_constructors = {} … … 96 91 data = constructor(self, tag_suffix, node) 97 92 if isinstance(data, types.GeneratorType): 98 93 generator = data 99 data = generator.next()94 data = next(generator) 100 95 if self.deep_construct: 101 96 for dummy in generator: 102 97 pass … … 133 128 key = self.construct_object(key_node, deep=deep) 134 129 try: 135 130 hash(key) 136 except TypeError ,exc:131 except TypeError as exc: 137 132 raise ConstructorError("while constructing a mapping", node.start_mark, 138 133 "found unacceptable key (%s)" % exc, key_node.start_mark) 139 134 value = self.construct_object(value_node, deep=deep) … … 169 164 def construct_scalar(self, node): 170 165 if isinstance(node, MappingNode): 171 166 for key_node, value_node in node.value: 172 if key_node.tag == u'tag:yaml.org,2002:value':167 if key_node.tag == 'tag:yaml.org,2002:value': 173 168 return self.construct_scalar(value_node) 174 169 return BaseConstructor.construct_scalar(self, node) 175 170 … … 178 173 index = 0 179 174 while index < len(node.value): 180 175 key_node, value_node = node.value[index] 181 if key_node.tag == u'tag:yaml.org,2002:merge':176 if key_node.tag == 'tag:yaml.org,2002:merge': 182 177 del node.value[index] 183 178 if isinstance(value_node, MappingNode): 184 179 self.flatten_mapping(value_node) … … 200 195 raise ConstructorError("while constructing a mapping", node.start_mark, 201 196 "expected a mapping or list of mappings for merging, but found %s" 202 197 % value_node.id, value_node.start_mark) 203 elif key_node.tag == u'tag:yaml.org,2002:value':204 key_node.tag = u'tag:yaml.org,2002:str'198 elif key_node.tag == 'tag:yaml.org,2002:value': 199 key_node.tag = 'tag:yaml.org,2002:str' 205 200 index += 1 206 201 else: 207 202 index += 1 … … 218 213 return None 219 214 220 215 bool_values = { 221 u'yes': True,222 u'no': False,223 u'true': True,224 u'false': False,225 u'on': True,226 u'off': False,216 'yes': True, 217 'no': False, 218 'true': True, 219 'false': False, 220 'on': True, 221 'off': False, 227 222 } 228 223 229 224 def construct_yaml_bool(self, node): … … 290 285 def construct_yaml_binary(self, node): 291 286 value = self.construct_scalar(node) 292 287 try: 293 return str(value).decode('base64')294 except (binascii.Error, UnicodeEncodeError) ,exc:288 return base64.decodestring(value.encode('utf-8')) 289 except (binascii.Error, UnicodeEncodeError) as exc: 295 290 raise ConstructorError(None, None, 296 291 "failed to decode base64 data: %s" % exc, node.start_mark) 297 292 298 293 timestamp_regexp = re.compile( 299 ur'''^(?P<year>[0-9][0-9][0-9][0-9])294 r'''^(?P<year>[0-9][0-9][0-9][0-9]) 300 295 -(?P<month>[0-9][0-9]?) 301 296 -(?P<day>[0-9][0-9]?) 302 297 (?:(?:[Tt]|[ \t]+) … … 387 382 data.update(value) 388 383 389 384 def construct_yaml_str(self, node): 390 value = self.construct_scalar(node) 391 try: 392 return value.encode('ascii') 393 except UnicodeEncodeError: 394 return value 385 return self.construct_scalar(node) 395 386 396 387 def construct_yaml_seq(self, node): 397 388 data = [] … … 416 407 417 408 def construct_undefined(self, node): 418 409 raise ConstructorError(None, None, 419 "could not determine a constructor for the tag %r" % node.tag .encode('utf-8'),410 "could not determine a constructor for the tag %r" % node.tag, 420 411 node.start_mark) 421 412 422 413 SafeConstructor.add_constructor( 423 u'tag:yaml.org,2002:null',414 'tag:yaml.org,2002:null', 424 415 SafeConstructor.construct_yaml_null) 425 416 426 417 SafeConstructor.add_constructor( 427 u'tag:yaml.org,2002:bool',418 'tag:yaml.org,2002:bool', 428 419 SafeConstructor.construct_yaml_bool) 429 420 430 421 SafeConstructor.add_constructor( 431 u'tag:yaml.org,2002:int',422 'tag:yaml.org,2002:int', 432 423 SafeConstructor.construct_yaml_int) 433 424 434 425 SafeConstructor.add_constructor( 435 u'tag:yaml.org,2002:float',426 'tag:yaml.org,2002:float', 436 427 SafeConstructor.construct_yaml_float) 437 428 438 429 SafeConstructor.add_constructor( 439 u'tag:yaml.org,2002:binary',430 'tag:yaml.org,2002:binary', 440 431 SafeConstructor.construct_yaml_binary) 441 432 442 433 SafeConstructor.add_constructor( 443 u'tag:yaml.org,2002:timestamp',434 'tag:yaml.org,2002:timestamp', 444 435 SafeConstructor.construct_yaml_timestamp) 445 436 446 437 SafeConstructor.add_constructor( 447 u'tag:yaml.org,2002:omap',438 'tag:yaml.org,2002:omap', 448 439 SafeConstructor.construct_yaml_omap) 449 440 450 441 SafeConstructor.add_constructor( 451 u'tag:yaml.org,2002:pairs',442 'tag:yaml.org,2002:pairs', 452 443 SafeConstructor.construct_yaml_pairs) 453 444 454 445 SafeConstructor.add_constructor( 455 u'tag:yaml.org,2002:set',446 'tag:yaml.org,2002:set', 456 447 SafeConstructor.construct_yaml_set) 457 448 458 449 SafeConstructor.add_constructor( 459 u'tag:yaml.org,2002:str',450 'tag:yaml.org,2002:str', 460 451 SafeConstructor.construct_yaml_str) 461 452 462 453 SafeConstructor.add_constructor( 463 u'tag:yaml.org,2002:seq',454 'tag:yaml.org,2002:seq', 464 455 SafeConstructor.construct_yaml_seq) 465 456 466 457 SafeConstructor.add_constructor( 467 u'tag:yaml.org,2002:map',458 'tag:yaml.org,2002:map', 468 459 SafeConstructor.construct_yaml_map) 469 460 470 461 SafeConstructor.add_constructor(None, … … 473 464 class Constructor(SafeConstructor): 474 465 475 466 def construct_python_str(self, node): 476 return self.construct_scalar(node) .encode('utf-8')467 return self.construct_scalar(node) 477 468 478 469 def construct_python_unicode(self, node): 479 470 return self.construct_scalar(node) 480 471 472 def construct_python_bytes(self, node): 473 return self.construct_scalar(node).encode('utf-8') 474 481 475 def construct_python_long(self, node): 482 return long(self.construct_yaml_int(node))476 return self.construct_yaml_int(node) 483 477 484 478 def construct_python_complex(self, node): 485 479 return complex(self.construct_scalar(node)) … … 493 487 "expected non-empty name appended to the tag", mark) 494 488 try: 495 489 __import__(name) 496 except ImportError ,exc:490 except ImportError as exc: 497 491 raise ConstructorError("while constructing a Python module", mark, 498 "cannot find module %r (%s)" % (name .encode('utf-8'), exc), mark)492 "cannot find module %r (%s)" % (name, exc), mark) 499 493 return sys.modules[name] 500 494 501 495 def find_python_name(self, name, mark): 502 496 if not name: 503 497 raise ConstructorError("while constructing a Python object", mark, 504 498 "expected non-empty name appended to the tag", mark) 505 if u'.' in name: 506 # Python 2.4 only 507 #module_name, object_name = name.rsplit('.', 1) 508 items = name.split('.') 509 object_name = items.pop() 510 module_name = '.'.join(items) 499 if '.' in name: 500 module_name, object_name = name.rsplit('.', 1) 511 501 else: 512 module_name = ' __builtin__'502 module_name = 'builtins' 513 503 object_name = name 514 504 try: 515 505 __import__(module_name) 516 except ImportError ,exc:506 except ImportError as exc: 517 507 raise ConstructorError("while constructing a Python object", mark, 518 "cannot find module %r (%s)" % (module_name .encode('utf-8'), exc), mark)508 "cannot find module %r (%s)" % (module_name, exc), mark) 519 509 module = sys.modules[module_name] 520 510 if not hasattr(module, object_name): 521 511 raise ConstructorError("while constructing a Python object", mark, 522 "cannot find %r in the module %r" % (object_name .encode('utf-8'),512 "cannot find %r in the module %r" % (object_name, 523 513 module.__name__), mark) 524 514 return getattr(module, object_name) 525 515 … … 527 517 value = self.construct_scalar(node) 528 518 if value: 529 519 raise ConstructorError("while constructing a Python name", node.start_mark, 530 "expected the empty value, but found %r" % value .encode('utf-8'),520 "expected the empty value, but found %r" % value, 531 521 node.start_mark) 532 522 return self.find_python_name(suffix, node.start_mark) 533 523 … … 535 525 value = self.construct_scalar(node) 536 526 if value: 537 527 raise ConstructorError("while constructing a Python module", node.start_mark, 538 "expected the empty value, but found %r" % value .encode('utf-8'),528 "expected the empty value, but found %r" % value, 539 529 node.start_mark) 540 530 return self.find_python_module(suffix, node.start_mark) 541 531 542 class classobj: pass543 544 532 def make_python_instance(self, suffix, node, 545 533 args=None, kwds=None, newobj=False): 546 534 if not args: … … 548 536 if not kwds: 549 537 kwds = {} 550 538 cls = self.find_python_name(suffix, node.start_mark) 551 if newobj and isinstance(cls, type(self.classobj)) \ 552 and not args and not kwds: 553 instance = self.classobj() 554 instance.__class__ = cls 555 return instance 556 elif newobj and isinstance(cls, type): 539 if newobj and isinstance(cls, type): 557 540 return cls.__new__(cls, *args, **kwds) 558 541 else: 559 542 return cls(*args, **kwds) … … 620 603 return self.construct_python_object_apply(suffix, node, newobj=True) 621 604 622 605 Constructor.add_constructor( 623 u'tag:yaml.org,2002:python/none',606 'tag:yaml.org,2002:python/none', 624 607 Constructor.construct_yaml_null) 625 608 626 609 Constructor.add_constructor( 627 u'tag:yaml.org,2002:python/bool',610 'tag:yaml.org,2002:python/bool', 628 611 Constructor.construct_yaml_bool) 629 612 630 613 Constructor.add_constructor( 631 u'tag:yaml.org,2002:python/str',614 'tag:yaml.org,2002:python/str', 632 615 Constructor.construct_python_str) 633 616 634 617 Constructor.add_constructor( 635 u'tag:yaml.org,2002:python/unicode',618 'tag:yaml.org,2002:python/unicode', 636 619 Constructor.construct_python_unicode) 637 620 638 621 Constructor.add_constructor( 639 u'tag:yaml.org,2002:python/int', 622 'tag:yaml.org,2002:python/bytes', 623 Constructor.construct_python_bytes) 624 625 Constructor.add_constructor( 626 'tag:yaml.org,2002:python/int', 640 627 Constructor.construct_yaml_int) 641 628 642 629 Constructor.add_constructor( 643 u'tag:yaml.org,2002:python/long',630 'tag:yaml.org,2002:python/long', 644 631 Constructor.construct_python_long) 645 632 646 633 Constructor.add_constructor( 647 u'tag:yaml.org,2002:python/float',634 'tag:yaml.org,2002:python/float', 648 635 Constructor.construct_yaml_float) 649 636 650 637 Constructor.add_constructor( 651 u'tag:yaml.org,2002:python/complex',638 'tag:yaml.org,2002:python/complex', 652 639 Constructor.construct_python_complex) 653 640 654 641 Constructor.add_constructor( 655 u'tag:yaml.org,2002:python/list',642 'tag:yaml.org,2002:python/list', 656 643 Constructor.construct_yaml_seq) 657 644 658 645 Constructor.add_constructor( 659 u'tag:yaml.org,2002:python/tuple',646 'tag:yaml.org,2002:python/tuple', 660 647 Constructor.construct_python_tuple) 661 648 662 649 Constructor.add_constructor( 663 u'tag:yaml.org,2002:python/dict',650 'tag:yaml.org,2002:python/dict', 664 651 Constructor.construct_yaml_map) 665 652 666 653 Constructor.add_multi_constructor( 667 u'tag:yaml.org,2002:python/name:',654 'tag:yaml.org,2002:python/name:', 668 655 Constructor.construct_python_name) 669 656 670 657 Constructor.add_multi_constructor( 671 u'tag:yaml.org,2002:python/module:',658 'tag:yaml.org,2002:python/module:', 672 659 Constructor.construct_python_module) 673 660 674 661 Constructor.add_multi_constructor( 675 u'tag:yaml.org,2002:python/object:',662 'tag:yaml.org,2002:python/object:', 676 663 Constructor.construct_python_object) 677 664 678 665 Constructor.add_multi_constructor( 679 u'tag:yaml.org,2002:python/object/apply:',666 'tag:yaml.org,2002:python/object/apply:', 680 667 Constructor.construct_python_object_apply) 681 668 682 669 Constructor.add_multi_constructor( 683 u'tag:yaml.org,2002:python/object/new:',670 'tag:yaml.org,2002:python/object/new:', 684 671 Constructor.construct_python_object_new) 685 672 -
lib/yaml/dumper.py
old new 1 1 2 2 __all__ = ['BaseDumper', 'SafeDumper', 'Dumper'] 3 3 4 from emitter import *5 from serializer import *6 from representer import *7 from resolver import *4 from yaml.emitter import * 5 from yaml.serializer import * 6 from yaml.representer import * 7 from yaml.resolver import * 8 8 9 9 class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver): 10 10 -
lib/yaml/emitter.py
old new 8 8 9 9 __all__ = ['Emitter', 'EmitterError'] 10 10 11 from error import YAMLError12 from events import *11 from yaml.error import YAMLError 12 from yaml.events import * 13 13 14 14 import re 15 15 16 16 class EmitterError(YAMLError): 17 17 pass 18 18 19 class ScalarAnalysis (object):19 class ScalarAnalysis: 20 20 def __init__(self, scalar, empty, multiline, 21 21 allow_flow_plain, allow_block_plain, 22 22 allow_single_quoted, allow_double_quoted, … … 30 30 self.allow_double_quoted = allow_double_quoted 31 31 self.allow_block = allow_block 32 32 33 class Emitter (object):33 class Emitter: 34 34 35 35 DEFAULT_TAG_PREFIXES = { 36 u'!' : u'!',37 u'tag:yaml.org,2002:' : u'!!',36 '!' : '!', 37 'tag:yaml.org,2002:' : '!!', 38 38 } 39 39 40 40 def __init__(self, stream, canonical=None, indent=None, width=None, … … 87 87 self.best_width = 80 88 88 if width and width > self.best_indent*2: 89 89 self.best_width = width 90 self.best_line_break = u'\n'91 if line_break in [ u'\r', u'\n', u'\r\n']:90 self.best_line_break = '\n' 91 if line_break in ['\r', '\n', '\r\n']: 92 92 self.best_line_break = line_break 93 93 94 94 # Tag prefixes. … … 176 176 self.write_version_directive(version_text) 177 177 self.tag_prefixes = self.DEFAULT_TAG_PREFIXES.copy() 178 178 if self.event.tags: 179 handles = self.event.tags.keys() 180 handles.sort() 179 handles = sorted(self.event.tags.keys()) 181 180 for handle in handles: 182 181 prefix = self.event.tags[handle] 183 182 self.tag_prefixes[prefix] = handle … … 189 188 and not self.check_empty_document()) 190 189 if not implicit: 191 190 self.write_indent() 192 self.write_indicator( u'---', True)191 self.write_indicator('---', True) 193 192 if self.canonical: 194 193 self.write_indent() 195 194 self.state = self.expect_document_root … … 204 203 if isinstance(self.event, DocumentEndEvent): 205 204 self.write_indent() 206 205 if self.event.explicit: 207 self.write_indicator( u'...', True)206 self.write_indicator('...', True) 208 207 self.write_indent() 209 208 self.flush_stream() 210 209 self.state = self.expect_document_start … … 227 226 if isinstance(self.event, AliasEvent): 228 227 self.expect_alias() 229 228 elif isinstance(self.event, (ScalarEvent, CollectionStartEvent)): 230 self.process_anchor( u'&')229 self.process_anchor('&') 231 230 self.process_tag() 232 231 if isinstance(self.event, ScalarEvent): 233 232 self.expect_scalar() … … 249 248 def expect_alias(self): 250 249 if self.event.anchor is None: 251 250 raise EmitterError("anchor is not specified for alias") 252 self.process_anchor( u'*')251 self.process_anchor('*') 253 252 self.state = self.states.pop() 254 253 255 254 def expect_scalar(self): … … 261 260 # Flow sequence handlers. 262 261 263 262 def expect_flow_sequence(self): 264 self.write_indicator( u'[', True, whitespace=True)263 self.write_indicator('[', True, whitespace=True) 265 264 self.flow_level += 1 266 265 self.increase_indent(flow=True) 267 266 self.state = self.expect_first_flow_sequence_item … … 270 269 if isinstance(self.event, SequenceEndEvent): 271 270 self.indent = self.indents.pop() 272 271 self.flow_level -= 1 273 self.write_indicator( u']', False)272 self.write_indicator(']', False) 274 273 self.state = self.states.pop() 275 274 else: 276 275 if self.canonical or self.column > self.best_width: … … 283 282 self.indent = self.indents.pop() 284 283 self.flow_level -= 1 285 284 if self.canonical: 286 self.write_indicator( u',', False)285 self.write_indicator(',', False) 287 286 self.write_indent() 288 self.write_indicator( u']', False)287 self.write_indicator(']', False) 289 288 self.state = self.states.pop() 290 289 else: 291 self.write_indicator( u',', False)290 self.write_indicator(',', False) 292 291 if self.canonical or self.column > self.best_width: 293 292 self.write_indent() 294 293 self.states.append(self.expect_flow_sequence_item) … … 297 296 # Flow mapping handlers. 298 297 299 298 def expect_flow_mapping(self): 300 self.write_indicator( u'{', True, whitespace=True)299 self.write_indicator('{', True, whitespace=True) 301 300 self.flow_level += 1 302 301 self.increase_indent(flow=True) 303 302 self.state = self.expect_first_flow_mapping_key … … 306 305 if isinstance(self.event, MappingEndEvent): 307 306 self.indent = self.indents.pop() 308 307 self.flow_level -= 1 309 self.write_indicator( u'}', False)308 self.write_indicator('}', False) 310 309 self.state = self.states.pop() 311 310 else: 312 311 if self.canonical or self.column > self.best_width: … … 315 314 self.states.append(self.expect_flow_mapping_simple_value) 316 315 self.expect_node(mapping=True, simple_key=True) 317 316 else: 318 self.write_indicator( u'?', True)317 self.write_indicator('?', True) 319 318 self.states.append(self.expect_flow_mapping_value) 320 319 self.expect_node(mapping=True) 321 320 … … 324 323 self.indent = self.indents.pop() 325 324 self.flow_level -= 1 326 325 if self.canonical: 327 self.write_indicator( u',', False)326 self.write_indicator(',', False) 328 327 self.write_indent() 329 self.write_indicator( u'}', False)328 self.write_indicator('}', False) 330 329 self.state = self.states.pop() 331 330 else: 332 self.write_indicator( u',', False)331 self.write_indicator(',', False) 333 332 if self.canonical or self.column > self.best_width: 334 333 self.write_indent() 335 334 if not self.canonical and self.check_simple_key(): 336 335 self.states.append(self.expect_flow_mapping_simple_value) 337 336 self.expect_node(mapping=True, simple_key=True) 338 337 else: 339 self.write_indicator( u'?', True)338 self.write_indicator('?', True) 340 339 self.states.append(self.expect_flow_mapping_value) 341 340 self.expect_node(mapping=True) 342 341 343 342 def expect_flow_mapping_simple_value(self): 344 self.write_indicator( u':', False)343 self.write_indicator(':', False) 345 344 self.states.append(self.expect_flow_mapping_key) 346 345 self.expect_node(mapping=True) 347 346 348 347 def expect_flow_mapping_value(self): 349 348 if self.canonical or self.column > self.best_width: 350 349 self.write_indent() 351 self.write_indicator( u':', True)350 self.write_indicator(':', True) 352 351 self.states.append(self.expect_flow_mapping_key) 353 352 self.expect_node(mapping=True) 354 353 … … 368 367 self.state = self.states.pop() 369 368 else: 370 369 self.write_indent() 371 self.write_indicator( u'-', True, indention=True)370 self.write_indicator('-', True, indention=True) 372 371 self.states.append(self.expect_block_sequence_item) 373 372 self.expect_node(sequence=True) 374 373 … … 391 390 self.states.append(self.expect_block_mapping_simple_value) 392 391 self.expect_node(mapping=True, simple_key=True) 393 392 else: 394 self.write_indicator( u'?', True, indention=True)393 self.write_indicator('?', True, indention=True) 395 394 self.states.append(self.expect_block_mapping_value) 396 395 self.expect_node(mapping=True) 397 396 398 397 def expect_block_mapping_simple_value(self): 399 self.write_indicator( u':', False)398 self.write_indicator(':', False) 400 399 self.states.append(self.expect_block_mapping_key) 401 400 self.expect_node(mapping=True) 402 401 403 402 def expect_block_mapping_value(self): 404 403 self.write_indent() 405 self.write_indicator( u':', True, indention=True)404 self.write_indicator(':', True, indention=True) 406 405 self.states.append(self.expect_block_mapping_key) 407 406 self.expect_node(mapping=True) 408 407 … … 421 420 return False 422 421 event = self.events[0] 423 422 return (isinstance(event, ScalarEvent) and event.anchor is None 424 and event.tag is None and event.implicit and event.value == u'')423 and event.tag is None and event.implicit and event.value == '') 425 424 426 425 def check_simple_key(self): 427 426 length = 0 … … 466 465 self.prepared_tag = None 467 466 return 468 467 if self.event.implicit[0] and tag is None: 469 tag = u'!'468 tag = '!' 470 469 self.prepared_tag = None 471 470 else: 472 471 if (not self.canonical or tag is None) and self.event.implicit: … … 529 528 major, minor = version 530 529 if major != 1: 531 530 raise EmitterError("unsupported YAML version: %d.%d" % (major, minor)) 532 return u'%d.%d' % (major, minor)531 return '%d.%d' % (major, minor) 533 532 534 533 def prepare_tag_handle(self, handle): 535 534 if not handle: 536 535 raise EmitterError("tag handle must not be empty") 537 if handle[0] != u'!' or handle[-1] != u'!':536 if handle[0] != '!' or handle[-1] != '!': 538 537 raise EmitterError("tag handle must start and end with '!': %r" 539 538 % (handle.encode('utf-8'))) 540 539 for ch in handle[1:-1]: 541 if not ( u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \542 or ch in u'-_'):540 if not ('0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 541 or ch in '-_'): 543 542 raise EmitterError("invalid character %r in the tag handle: %r" 544 543 % (ch.encode('utf-8'), handle.encode('utf-8'))) 545 544 return handle … … 549 548 raise EmitterError("tag prefix must not be empty") 550 549 chunks = [] 551 550 start = end = 0 552 if prefix[0] == u'!':551 if prefix[0] == '!': 553 552 end = 1 554 553 while end < len(prefix): 555 554 ch = prefix[end] 556 if u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \557 or ch in u'-;/?!:@&=+$,_.~*\'()[]':555 if '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 556 or ch in '-;/?!:@&=+$,_.~*\'()[]': 558 557 end += 1 559 558 else: 560 559 if start < end: … … 562 561 start = end = end+1 563 562 data = ch.encode('utf-8') 564 563 for ch in data: 565 chunks.append( u'%%%02X' % ord(ch))564 chunks.append('%%%02X' % ord(ch)) 566 565 if start < end: 567 566 chunks.append(prefix[start:end]) 568 return u''.join(chunks)567 return ''.join(chunks) 569 568 570 569 def prepare_tag(self, tag): 571 570 if not tag: 572 571 raise EmitterError("tag must not be empty") 573 if tag == u'!':572 if tag == '!': 574 573 return tag 575 574 handle = None 576 575 suffix = tag 577 576 for prefix in self.tag_prefixes: 578 577 if tag.startswith(prefix) \ 579 and (prefix == u'!' or len(prefix) < len(tag)):578 and (prefix == '!' or len(prefix) < len(tag)): 580 579 handle = self.tag_prefixes[prefix] 581 580 suffix = tag[len(prefix):] 582 581 chunks = [] 583 582 start = end = 0 584 583 while end < len(suffix): 585 584 ch = suffix[end] 586 if u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \587 or ch in u'-;/?:@&=+$,_.~*\'()[]' \588 or (ch == u'!' and handle != u'!'):585 if '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 586 or ch in '-;/?:@&=+$,_.~*\'()[]' \ 587 or (ch == '!' and handle != '!'): 589 588 end += 1 590 589 else: 591 590 if start < end: … … 593 592 start = end = end+1 594 593 data = ch.encode('utf-8') 595 594 for ch in data: 596 chunks.append( u'%%%02X' % ord(ch))595 chunks.append('%%%02X' % ord(ch)) 597 596 if start < end: 598 597 chunks.append(suffix[start:end]) 599 suffix_text = u''.join(chunks)598 suffix_text = ''.join(chunks) 600 599 if handle: 601 return u'%s%s' % (handle, suffix_text)600 return '%s%s' % (handle, suffix_text) 602 601 else: 603 return u'!<%s>' % suffix_text602 return '!<%s>' % suffix_text 604 603 605 604 def prepare_anchor(self, anchor): 606 605 if not anchor: 607 606 raise EmitterError("anchor must not be empty") 608 607 for ch in anchor: 609 if not ( u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \610 or ch in u'-_'):608 if not ('0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 609 or ch in '-_'): 611 610 raise EmitterError("invalid character %r in the anchor: %r" 612 611 % (ch.encode('utf-8'), anchor.encode('utf-8'))) 613 612 return anchor … … 638 637 mixed_breaks_spaces = False # anything else 639 638 640 639 # Check document indicators. 641 if scalar.startswith( u'---') or scalar.startswith(u'...'):640 if scalar.startswith('---') or scalar.startswith('...'): 642 641 block_indicators = True 643 642 flow_indicators = True 644 643 … … 647 646 648 647 # Last character or followed by a whitespace. 649 648 followed_by_space = (len(scalar) == 1 or 650 scalar[1] in u'\0 \t\r\n\x85\u2028\u2029')649 scalar[1] in '\0 \t\r\n\x85\u2028\u2029') 651 650 652 651 # The current series of whitespaces contain plain spaces. 653 652 spaces = False … … 671 670 672 671 if index == 0: 673 672 # Leading indicators are special characters. 674 if ch in u'#,[]{}&*!|>\'\"%@`':673 if ch in '#,[]{}&*!|>\'\"%@`': 675 674 flow_indicators = True 676 675 block_indicators = True 677 if ch in u'?:':676 if ch in '?:': 678 677 flow_indicators = True 679 678 if followed_by_space: 680 679 block_indicators = True 681 if ch == u'-' and followed_by_space:680 if ch == '-' and followed_by_space: 682 681 flow_indicators = True 683 682 block_indicators = True 684 683 else: 685 684 # Some indicators cannot appear within a scalar as well. 686 if ch in u',?[]{}':685 if ch in ',?[]{}': 687 686 flow_indicators = True 688 if ch == u':':687 if ch == ':': 689 688 flow_indicators = True 690 689 if followed_by_space: 691 690 block_indicators = True 692 if ch == u'#' and preceeded_by_space:691 if ch == '#' and preceeded_by_space: 693 692 flow_indicators = True 694 693 block_indicators = True 695 694 696 695 # Check for line breaks, special, and unicode characters. 697 696 698 if ch in u'\n\x85\u2028\u2029':697 if ch in '\n\x85\u2028\u2029': 699 698 line_breaks = True 700 if not (ch == u'\n' or u'\x20' <= ch <= u'\x7E'):701 if (ch == u'\x85' or u'\xA0' <= ch <= u'\uD7FF'702 or u'\uE000' <= ch <= u'\uFFFD') and ch != u'\uFEFF':699 if not (ch == '\n' or '\x20' <= ch <= '\x7E'): 700 if (ch == '\x85' or '\xA0' <= ch <= '\uD7FF' 701 or '\uE000' <= ch <= '\uFFFD') and ch != '\uFEFF': 703 702 unicode_characters = True 704 703 if not self.allow_unicode: 705 704 special_characters = True … … 709 708 # Spaces, line breaks, and how they are mixed. State machine. 710 709 711 710 # Start or continue series of whitespaces. 712
