Ticket #74: python3000.patch

File python3000.patch, 175.1 kB (added by idadesub@users.sourceforge.net, 3 months ago)

update the original patch to python 3.0b3.

  • lib/yaml/__init__.py

    old new  
     1import io 
    12 
    2 from error import * 
     3from yaml.error import * 
    34 
    4 from tokens import * 
    5 from events import * 
    6 from nodes import * 
     5from yaml.tokens import * 
     6from yaml.events import * 
     7from yaml.nodes import * 
    78 
    8 from loader import * 
    9 from dumper import * 
     9from yaml.loader import * 
     10from yaml.dumper import * 
    1011 
    1112try: 
    12     from cyaml import * 
     13    from yaml.cyaml import * 
    1314except ImportError: 
    1415    pass 
    1516 
     
    8889    """ 
    8990    getvalue = None 
    9091    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() 
    9693        getvalue = stream.getvalue 
    9794    dumper = Dumper(stream, canonical=canonical, indent=indent, width=width, 
    9895            allow_unicode=allow_unicode, line_break=line_break) 
     
    112109    """ 
    113110    getvalue = None 
    114111    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() 
    120113        getvalue = stream.getvalue 
    121114    dumper = Dumper(stream, canonical=canonical, indent=indent, width=width, 
    122115            allow_unicode=allow_unicode, line_break=line_break, 
     
    148141    """ 
    149142    getvalue = None 
    150143    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() 
    156145        getvalue = stream.getvalue 
    157146    dumper = Dumper(stream, default_style=default_style, 
    158147            default_flow_style=default_flow_style, 
     
    256245            cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml) 
    257246            cls.yaml_dumper.add_representer(cls, cls.to_yaml) 
    258247 
    259 class YAMLObject(object): 
     248class YAMLObject(metaclass=YAMLObjectMetaclass): 
    260249    """ 
    261250    An object that can dump itself to a YAML stream 
    262251    and load itself from a YAML stream. 
    263252    """ 
    264253 
    265     __metaclass__ = YAMLObjectMetaclass 
    266254    __slots__ = ()  # no direct instantiation, so allow immutable subclasses 
    267255 
    268256    yaml_loader = Loader 
  • lib/yaml/composer.py

    old new  
    11 
    22__all__ = ['Composer', 'ComposerError'] 
    33 
    4 from error import MarkedYAMLError 
    5 from events import * 
    6 from nodes import * 
     4from yaml.error import MarkedYAMLError 
     5from yaml.events import * 
     6from yaml.nodes import * 
    77 
    88class ComposerError(MarkedYAMLError): 
    99    pass 
    1010 
    11 class Composer(object)
     11class Composer
    1212 
    1313    def __init__(self): 
    1414        self.anchors = {} 
     
    8888    def compose_scalar_node(self, anchor): 
    8989        event = self.get_event() 
    9090        tag = event.tag 
    91         if tag is None or tag == u'!': 
     91        if tag is None or tag == '!': 
    9292            tag = self.resolve(ScalarNode, event.value, event.implicit) 
    9393        node = ScalarNode(tag, event.value, 
    9494                event.start_mark, event.end_mark, style=event.style) 
     
    9999    def compose_sequence_node(self, anchor): 
    100100        start_event = self.get_event() 
    101101        tag = start_event.tag 
    102         if tag is None or tag == u'!': 
     102        if tag is None or tag == '!': 
    103103            tag = self.resolve(SequenceNode, None, start_event.implicit) 
    104104        node = SequenceNode(tag, [], 
    105105                start_event.start_mark, None, 
     
    117117    def compose_mapping_node(self, anchor): 
    118118        start_event = self.get_event() 
    119119        tag = start_event.tag 
    120         if tag is None or tag == u'!': 
     120        if tag is None or tag == '!': 
    121121            tag = self.resolve(MappingNode, None, start_event.implicit) 
    122122        node = MappingNode(tag, [], 
    123123                start_event.start_mark, None, 
  • lib/yaml/constructor.py

    old new  
    22__all__ = ['BaseConstructor', 'SafeConstructor', 'Constructor', 
    33    'ConstructorError'] 
    44 
    5 from error import * 
    6 from nodes import * 
     5from yaml.error import * 
     6from yaml.nodes import * 
    77 
    88import datetime 
    99 
    10 try: 
    11     set 
    12 except NameError: 
    13     from sets import Set as set 
    14  
    15 import binascii, re, sys, types 
     10import binascii, re, sys, types, base64 
    1611 
    1712class ConstructorError(MarkedYAMLError): 
    1813    pass 
    1914 
    20 class BaseConstructor(object)
     15class BaseConstructor
    2116 
    2217    yaml_constructors = {} 
    2318    yaml_multi_constructors = {} 
     
    9691            data = constructor(self, tag_suffix, node) 
    9792        if isinstance(data, types.GeneratorType): 
    9893            generator = data 
    99             data = generator.next(
     94            data = next(generator
    10095            if self.deep_construct: 
    10196                for dummy in generator: 
    10297                    pass 
     
    133128            key = self.construct_object(key_node, deep=deep) 
    134129            try: 
    135130                hash(key) 
    136             except TypeError, exc: 
     131            except TypeError as exc: 
    137132                raise ConstructorError("while constructing a mapping", node.start_mark, 
    138133                        "found unacceptable key (%s)" % exc, key_node.start_mark) 
    139134            value = self.construct_object(value_node, deep=deep) 
     
    169164    def construct_scalar(self, node): 
    170165        if isinstance(node, MappingNode): 
    171166            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': 
    173168                    return self.construct_scalar(value_node) 
    174169        return BaseConstructor.construct_scalar(self, node) 
    175170 
     
    178173        index = 0 
    179174        while index < len(node.value): 
    180175            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': 
    182177                del node.value[index] 
    183178                if isinstance(value_node, MappingNode): 
    184179                    self.flatten_mapping(value_node) 
     
    200195                    raise ConstructorError("while constructing a mapping", node.start_mark, 
    201196                            "expected a mapping or list of mappings for merging, but found %s" 
    202197                            % 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' 
    205200                index += 1 
    206201            else: 
    207202                index += 1 
     
    218213        return None 
    219214 
    220215    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, 
    227222    } 
    228223 
    229224    def construct_yaml_bool(self, node): 
     
    290285    def construct_yaml_binary(self, node): 
    291286        value = self.construct_scalar(node) 
    292287        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: 
    295290            raise ConstructorError(None, None, 
    296291                    "failed to decode base64 data: %s" % exc, node.start_mark)  
    297292 
    298293    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]) 
    300295                -(?P<month>[0-9][0-9]?) 
    301296                -(?P<day>[0-9][0-9]?) 
    302297                (?:(?:[Tt]|[ \t]+) 
     
    387382        data.update(value) 
    388383 
    389384    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) 
    395386 
    396387    def construct_yaml_seq(self, node): 
    397388        data = [] 
     
    416407 
    417408    def construct_undefined(self, node): 
    418409        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
    420411                node.start_mark) 
    421412 
    422413SafeConstructor.add_constructor( 
    423         u'tag:yaml.org,2002:null', 
     414        'tag:yaml.org,2002:null', 
    424415        SafeConstructor.construct_yaml_null) 
    425416 
    426417SafeConstructor.add_constructor( 
    427         u'tag:yaml.org,2002:bool', 
     418        'tag:yaml.org,2002:bool', 
    428419        SafeConstructor.construct_yaml_bool) 
    429420 
    430421SafeConstructor.add_constructor( 
    431         u'tag:yaml.org,2002:int', 
     422        'tag:yaml.org,2002:int', 
    432423        SafeConstructor.construct_yaml_int) 
    433424 
    434425SafeConstructor.add_constructor( 
    435         u'tag:yaml.org,2002:float', 
     426        'tag:yaml.org,2002:float', 
    436427        SafeConstructor.construct_yaml_float) 
    437428 
    438429SafeConstructor.add_constructor( 
    439         u'tag:yaml.org,2002:binary', 
     430        'tag:yaml.org,2002:binary', 
    440431        SafeConstructor.construct_yaml_binary) 
    441432 
    442433SafeConstructor.add_constructor( 
    443         u'tag:yaml.org,2002:timestamp', 
     434        'tag:yaml.org,2002:timestamp', 
    444435        SafeConstructor.construct_yaml_timestamp) 
    445436 
    446437SafeConstructor.add_constructor( 
    447         u'tag:yaml.org,2002:omap', 
     438        'tag:yaml.org,2002:omap', 
    448439        SafeConstructor.construct_yaml_omap) 
    449440 
    450441SafeConstructor.add_constructor( 
    451         u'tag:yaml.org,2002:pairs', 
     442        'tag:yaml.org,2002:pairs', 
    452443        SafeConstructor.construct_yaml_pairs) 
    453444 
    454445SafeConstructor.add_constructor( 
    455         u'tag:yaml.org,2002:set', 
     446        'tag:yaml.org,2002:set', 
    456447        SafeConstructor.construct_yaml_set) 
    457448 
    458449SafeConstructor.add_constructor( 
    459         u'tag:yaml.org,2002:str', 
     450        'tag:yaml.org,2002:str', 
    460451        SafeConstructor.construct_yaml_str) 
    461452 
    462453SafeConstructor.add_constructor( 
    463         u'tag:yaml.org,2002:seq', 
     454        'tag:yaml.org,2002:seq', 
    464455        SafeConstructor.construct_yaml_seq) 
    465456 
    466457SafeConstructor.add_constructor( 
    467         u'tag:yaml.org,2002:map', 
     458        'tag:yaml.org,2002:map', 
    468459        SafeConstructor.construct_yaml_map) 
    469460 
    470461SafeConstructor.add_constructor(None, 
     
    473464class Constructor(SafeConstructor): 
    474465 
    475466    def construct_python_str(self, node): 
    476         return self.construct_scalar(node).encode('utf-8') 
     467        return self.construct_scalar(node) 
    477468 
    478469    def construct_python_unicode(self, node): 
    479470        return self.construct_scalar(node) 
    480471 
     472    def construct_python_bytes(self, node): 
     473        return self.construct_scalar(node).encode('utf-8') 
     474 
    481475    def construct_python_long(self, node): 
    482         return long(self.construct_yaml_int(node)
     476        return self.construct_yaml_int(node
    483477 
    484478    def construct_python_complex(self, node): 
    485479       return complex(self.construct_scalar(node)) 
     
    493487                    "expected non-empty name appended to the tag", mark) 
    494488        try: 
    495489            __import__(name) 
    496         except ImportError, exc: 
     490        except ImportError as exc: 
    497491            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) 
    499493        return sys.modules[name] 
    500494 
    501495    def find_python_name(self, name, mark): 
    502496        if not name: 
    503497            raise ConstructorError("while constructing a Python object", mark, 
    504498                    "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) 
    511501        else: 
    512             module_name = '__builtin__
     502            module_name = 'builtins
    513503            object_name = name 
    514504        try: 
    515505            __import__(module_name) 
    516         except ImportError, exc: 
     506        except ImportError as exc: 
    517507            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) 
    519509        module = sys.modules[module_name] 
    520510        if not hasattr(module, object_name): 
    521511            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
    523513                        module.__name__), mark) 
    524514        return getattr(module, object_name) 
    525515 
     
    527517        value = self.construct_scalar(node) 
    528518        if value: 
    529519            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
    531521                    node.start_mark) 
    532522        return self.find_python_name(suffix, node.start_mark) 
    533523 
     
    535525        value = self.construct_scalar(node) 
    536526        if value: 
    537527            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
    539529                    node.start_mark) 
    540530        return self.find_python_module(suffix, node.start_mark) 
    541531 
    542     class classobj: pass 
    543  
    544532    def make_python_instance(self, suffix, node, 
    545533            args=None, kwds=None, newobj=False): 
    546534        if not args: 
     
    548536        if not kwds: 
    549537            kwds = {} 
    550538        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): 
    557540            return cls.__new__(cls, *args, **kwds) 
    558541        else: 
    559542            return cls(*args, **kwds) 
     
    620603        return self.construct_python_object_apply(suffix, node, newobj=True) 
    621604 
    622605Constructor.add_constructor( 
    623     u'tag:yaml.org,2002:python/none', 
     606    'tag:yaml.org,2002:python/none', 
    624607    Constructor.construct_yaml_null) 
    625608 
    626609Constructor.add_constructor( 
    627     u'tag:yaml.org,2002:python/bool', 
     610    'tag:yaml.org,2002:python/bool', 
    628611    Constructor.construct_yaml_bool) 
    629612 
    630613Constructor.add_constructor( 
    631     u'tag:yaml.org,2002:python/str', 
     614    'tag:yaml.org,2002:python/str', 
    632615    Constructor.construct_python_str) 
    633616 
    634617Constructor.add_constructor( 
    635     u'tag:yaml.org,2002:python/unicode', 
     618    'tag:yaml.org,2002:python/unicode', 
    636619    Constructor.construct_python_unicode) 
    637620 
    638621Constructor.add_constructor( 
    639     u'tag:yaml.org,2002:python/int', 
     622    'tag:yaml.org,2002:python/bytes', 
     623    Constructor.construct_python_bytes) 
     624 
     625Constructor.add_constructor( 
     626    'tag:yaml.org,2002:python/int', 
    640627    Constructor.construct_yaml_int) 
    641628 
    642629Constructor.add_constructor( 
    643     u'tag:yaml.org,2002:python/long', 
     630    'tag:yaml.org,2002:python/long', 
    644631    Constructor.construct_python_long) 
    645632 
    646633Constructor.add_constructor( 
    647     u'tag:yaml.org,2002:python/float', 
     634    'tag:yaml.org,2002:python/float', 
    648635    Constructor.construct_yaml_float) 
    649636 
    650637Constructor.add_constructor( 
    651     u'tag:yaml.org,2002:python/complex', 
     638    'tag:yaml.org,2002:python/complex', 
    652639    Constructor.construct_python_complex) 
    653640 
    654641Constructor.add_constructor( 
    655     u'tag:yaml.org,2002:python/list', 
     642    'tag:yaml.org,2002:python/list', 
    656643    Constructor.construct_yaml_seq) 
    657644 
    658645Constructor.add_constructor( 
    659     u'tag:yaml.org,2002:python/tuple', 
     646    'tag:yaml.org,2002:python/tuple', 
    660647    Constructor.construct_python_tuple) 
    661648 
    662649Constructor.add_constructor( 
    663     u'tag:yaml.org,2002:python/dict', 
     650    'tag:yaml.org,2002:python/dict', 
    664651    Constructor.construct_yaml_map) 
    665652 
    666653Constructor.add_multi_constructor( 
    667     u'tag:yaml.org,2002:python/name:', 
     654    'tag:yaml.org,2002:python/name:', 
    668655    Constructor.construct_python_name) 
    669656 
    670657Constructor.add_multi_constructor( 
    671     u'tag:yaml.org,2002:python/module:', 
     658    'tag:yaml.org,2002:python/module:', 
    672659    Constructor.construct_python_module) 
    673660 
    674661Constructor.add_multi_constructor( 
    675     u'tag:yaml.org,2002:python/object:', 
     662    'tag:yaml.org,2002:python/object:', 
    676663    Constructor.construct_python_object) 
    677664 
    678665Constructor.add_multi_constructor( 
    679     u'tag:yaml.org,2002:python/object/apply:', 
     666    'tag:yaml.org,2002:python/object/apply:', 
    680667    Constructor.construct_python_object_apply) 
    681668 
    682669Constructor.add_multi_constructor( 
    683     u'tag:yaml.org,2002:python/object/new:', 
     670    'tag:yaml.org,2002:python/object/new:', 
    684671    Constructor.construct_python_object_new) 
    685672 
  • lib/yaml/dumper.py

    old new  
    11 
    22__all__ = ['BaseDumper', 'SafeDumper', 'Dumper'] 
    33 
    4 from emitter import * 
    5 from serializer import * 
    6 from representer import * 
    7 from resolver import * 
     4from yaml.emitter import * 
     5from yaml.serializer import * 
     6from yaml.representer import * 
     7from yaml.resolver import * 
    88 
    99class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver): 
    1010 
  • lib/yaml/emitter.py

    old new  
    88 
    99__all__ = ['Emitter', 'EmitterError'] 
    1010 
    11 from error import YAMLError 
    12 from events import * 
     11from yaml.error import YAMLError 
     12from yaml.events import * 
    1313 
    1414import re 
    1515 
    1616class EmitterError(YAMLError): 
    1717    pass 
    1818 
    19 class ScalarAnalysis(object)
     19class ScalarAnalysis
    2020    def __init__(self, scalar, empty, multiline, 
    2121            allow_flow_plain, allow_block_plain, 
    2222            allow_single_quoted, allow_double_quoted, 
     
    3030        self.allow_double_quoted = allow_double_quoted 
    3131        self.allow_block = allow_block 
    3232 
    33 class Emitter(object)
     33class Emitter
    3434 
    3535    DEFAULT_TAG_PREFIXES = { 
    36         u'!' : u'!', 
    37         u'tag:yaml.org,2002:' : u'!!', 
     36        '!' : '!', 
     37        'tag:yaml.org,2002:' : '!!', 
    3838    } 
    3939 
    4040    def __init__(self, stream, canonical=None, indent=None, width=None, 
     
    8787        self.best_width = 80 
    8888        if width and width > self.best_indent*2: 
    8989            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']: 
    9292            self.best_line_break = line_break 
    9393 
    9494        # Tag prefixes. 
     
    176176                self.write_version_directive(version_text) 
    177177            self.tag_prefixes = self.DEFAULT_TAG_PREFIXES.copy() 
    178178            if self.event.tags: 
    179                 handles = self.event.tags.keys() 
    180                 handles.sort() 
     179                handles = sorted(self.event.tags.keys()) 
    181180                for handle in handles: 
    182181                    prefix = self.event.tags[handle] 
    183182                    self.tag_prefixes[prefix] = handle 
     
    189188                    and not self.check_empty_document()) 
    190189            if not implicit: 
    191190                self.write_indent() 
    192                 self.write_indicator(u'---', True) 
     191                self.write_indicator('---', True) 
    193192                if self.canonical: 
    194193                    self.write_indent() 
    195194            self.state = self.expect_document_root 
     
    204203        if isinstance(self.event, DocumentEndEvent): 
    205204            self.write_indent() 
    206205            if self.event.explicit: 
    207                 self.write_indicator(u'...', True) 
     206                self.write_indicator('...', True) 
    208207                self.write_indent() 
    209208            self.flush_stream() 
    210209            self.state = self.expect_document_start 
     
    227226        if isinstance(self.event, AliasEvent): 
    228227            self.expect_alias() 
    229228        elif isinstance(self.event, (ScalarEvent, CollectionStartEvent)): 
    230             self.process_anchor(u'&') 
     229            self.process_anchor('&') 
    231230            self.process_tag() 
    232231            if isinstance(self.event, ScalarEvent): 
    233232                self.expect_scalar() 
     
    249248    def expect_alias(self): 
    250249        if self.event.anchor is None: 
    251250            raise EmitterError("anchor is not specified for alias") 
    252         self.process_anchor(u'*') 
     251        self.process_anchor('*') 
    253252        self.state = self.states.pop() 
    254253 
    255254    def expect_scalar(self): 
     
    261260    # Flow sequence handlers. 
    262261 
    263262    def expect_flow_sequence(self): 
    264         self.write_indicator(u'[', True, whitespace=True) 
     263        self.write_indicator('[', True, whitespace=True) 
    265264        self.flow_level += 1 
    266265        self.increase_indent(flow=True) 
    267266        self.state = self.expect_first_flow_sequence_item 
     
    270269        if isinstance(self.event, SequenceEndEvent): 
    271270            self.indent = self.indents.pop() 
    272271            self.flow_level -= 1 
    273             self.write_indicator(u']', False) 
     272            self.write_indicator(']', False) 
    274273            self.state = self.states.pop() 
    275274        else: 
    276275            if self.canonical or self.column > self.best_width: 
     
    283282            self.indent = self.indents.pop() 
    284283            self.flow_level -= 1 
    285284            if self.canonical: 
    286                 self.write_indicator(u',', False) 
     285                self.write_indicator(',', False) 
    287286                self.write_indent() 
    288             self.write_indicator(u']', False) 
     287            self.write_indicator(']', False) 
    289288            self.state = self.states.pop() 
    290289        else: 
    291             self.write_indicator(u',', False) 
     290            self.write_indicator(',', False) 
    292291            if self.canonical or self.column > self.best_width: 
    293292                self.write_indent() 
    294293            self.states.append(self.expect_flow_sequence_item) 
     
    297296    # Flow mapping handlers. 
    298297 
    299298    def expect_flow_mapping(self): 
    300         self.write_indicator(u'{', True, whitespace=True) 
     299        self.write_indicator('{', True, whitespace=True) 
    301300        self.flow_level += 1 
    302301        self.increase_indent(flow=True) 
    303302        self.state = self.expect_first_flow_mapping_key 
     
    306305        if isinstance(self.event, MappingEndEvent): 
    307306            self.indent = self.indents.pop() 
    308307            self.flow_level -= 1 
    309             self.write_indicator(u'}', False) 
     308            self.write_indicator('}', False) 
    310309            self.state = self.states.pop() 
    311310        else: 
    312311            if self.canonical or self.column > self.best_width: 
     
    315314                self.states.append(self.expect_flow_mapping_simple_value) 
    316315                self.expect_node(mapping=True, simple_key=True) 
    317316            else: 
    318                 self.write_indicator(u'?', True) 
     317                self.write_indicator('?', True) 
    319318                self.states.append(self.expect_flow_mapping_value) 
    320319                self.expect_node(mapping=True) 
    321320 
     
    324323            self.indent = self.indents.pop() 
    325324            self.flow_level -= 1 
    326325            if self.canonical: 
    327                 self.write_indicator(u',', False) 
     326                self.write_indicator(',', False) 
    328327                self.write_indent() 
    329             self.write_indicator(u'}', False) 
     328            self.write_indicator('}', False) 
    330329            self.state = self.states.pop() 
    331330        else: 
    332             self.write_indicator(u',', False) 
     331            self.write_indicator(',', False) 
    333332            if self.canonical or self.column > self.best_width: 
    334333                self.write_indent() 
    335334            if not self.canonical and self.check_simple_key(): 
    336335                self.states.append(self.expect_flow_mapping_simple_value) 
    337336                self.expect_node(mapping=True, simple_key=True) 
    338337            else: 
    339                 self.write_indicator(u'?', True) 
     338                self.write_indicator('?', True) 
    340339                self.states.append(self.expect_flow_mapping_value) 
    341340                self.expect_node(mapping=True) 
    342341 
    343342    def expect_flow_mapping_simple_value(self): 
    344         self.write_indicator(u':', False) 
     343        self.write_indicator(':', False) 
    345344        self.states.append(self.expect_flow_mapping_key) 
    346345        self.expect_node(mapping=True) 
    347346 
    348347    def expect_flow_mapping_value(self): 
    349348        if self.canonical or self.column > self.best_width: 
    350349            self.write_indent() 
    351         self.write_indicator(u':', True) 
     350        self.write_indicator(':', True) 
    352351        self.states.append(self.expect_flow_mapping_key) 
    353352        self.expect_node(mapping=True) 
    354353 
     
    368367            self.state = self.states.pop() 
    369368        else: 
    370369            self.write_indent() 
    371             self.write_indicator(u'-', True, indention=True) 
     370            self.write_indicator('-', True, indention=True) 
    372371            self.states.append(self.expect_block_sequence_item) 
    373372            self.expect_node(sequence=True) 
    374373 
     
    391390                self.states.append(self.expect_block_mapping_simple_value) 
    392391                self.expect_node(mapping=True, simple_key=True) 
    393392            else: 
    394                 self.write_indicator(u'?', True, indention=True) 
     393                self.write_indicator('?', True, indention=True) 
    395394                self.states.append(self.expect_block_mapping_value) 
    396395                self.expect_node(mapping=True) 
    397396 
    398397    def expect_block_mapping_simple_value(self): 
    399         self.write_indicator(u':', False) 
     398        self.write_indicator(':', False) 
    400399        self.states.append(self.expect_block_mapping_key) 
    401400        self.expect_node(mapping=True) 
    402401 
    403402    def expect_block_mapping_value(self): 
    404403        self.write_indent() 
    405         self.write_indicator(u':', True, indention=True) 
     404        self.write_indicator(':', True, indention=True) 
    406405        self.states.append(self.expect_block_mapping_key) 
    407406        self.expect_node(mapping=True) 
    408407 
     
    421420            return False 
    422421        event = self.events[0] 
    423422        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 == '') 
    425424 
    426425    def check_simple_key(self): 
    427426        length = 0 
     
    466465                self.prepared_tag = None 
    467466                return 
    468467            if self.event.implicit[0] and tag is None: 
    469                 tag = u'!' 
     468                tag = '!' 
    470469                self.prepared_tag = None 
    471470        else: 
    472471            if (not self.canonical or tag is None) and self.event.implicit: 
     
    529528        major, minor = version 
    530529        if major != 1: 
    531530            raise EmitterError("unsupported YAML version: %d.%d" % (major, minor)) 
    532         return u'%d.%d' % (major, minor) 
     531        return '%d.%d' % (major, minor) 
    533532 
    534533    def prepare_tag_handle(self, handle): 
    535534        if not handle: 
    536535            raise EmitterError("tag handle must not be empty") 
    537         if handle[0] != u'!' or handle[-1] != u'!': 
     536        if handle[0] != '!' or handle[-1] != '!': 
    538537            raise EmitterError("tag handle must start and end with '!': %r" 
    539538                    % (handle.encode('utf-8'))) 
    540539        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 '-_'): 
    543542                raise EmitterError("invalid character %r in the tag handle: %r" 
    544543                        % (ch.encode('utf-8'), handle.encode('utf-8'))) 
    545544        return handle 
     
    549548            raise EmitterError("tag prefix must not be empty") 
    550549        chunks = [] 
    551550        start = end = 0 
    552         if prefix[0] == u'!': 
     551        if prefix[0] == '!': 
    553552            end = 1 
    554553        while end < len(prefix): 
    555554            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 '-;/?!:@&=+$,_.~*\'()[]': 
    558557                end += 1 
    559558            else: 
    560559                if start < end: 
     
    562561                start = end = end+1 
    563562                data = ch.encode('utf-8') 
    564563                for ch in data: 
    565                     chunks.append(u'%%%02X' % ord(ch)) 
     564                    chunks.append('%%%02X' % ord(ch)) 
    566565        if start < end: 
    567566            chunks.append(prefix[start:end]) 
    568         return u''.join(chunks) 
     567        return ''.join(chunks) 
    569568 
    570569    def prepare_tag(self, tag): 
    571570        if not tag: 
    572571            raise EmitterError("tag must not be empty") 
    573         if tag == u'!': 
     572        if tag == '!': 
    574573            return tag 
    575574        handle = None 
    576575        suffix = tag 
    577576        for prefix in self.tag_prefixes: 
    578577            if tag.startswith(prefix)   \ 
    579                     and (prefix == u'!' or len(prefix) < len(tag)): 
     578                    and (prefix == '!' or len(prefix) < len(tag)): 
    580579                handle = self.tag_prefixes[prefix] 
    581580                suffix = tag[len(prefix):] 
    582581        chunks = [] 
    583582        start = end = 0 
    584583        while end < len(suffix): 
    585584            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 != '!'): 
    589588                end += 1 
    590589            else: 
    591590                if start < end: 
     
    593592                start = end = end+1 
    594593                data = ch.encode('utf-8') 
    595594                for ch in data: 
    596                     chunks.append(u'%%%02X' % ord(ch)) 
     595                    chunks.append('%%%02X' % ord(ch)) 
    597596        if start < end: 
    598597            chunks.append(suffix[start:end]) 
    599         suffix_text = u''.join(chunks) 
     598        suffix_text = ''.join(chunks) 
    600599        if handle: 
    601             return u'%s%s' % (handle, suffix_text) 
     600            return '%s%s' % (handle, suffix_text) 
    602601        else: 
    603             return u'!<%s>' % suffix_text 
     602            return '!<%s>' % suffix_text 
    604603 
    605604    def prepare_anchor(self, anchor): 
    606605        if not anchor: 
    607606            raise EmitterError("anchor must not be empty") 
    608607        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 '-_'): 
    611610                raise EmitterError("invalid character %r in the anchor: %r" 
    612611                        % (ch.encode('utf-8'), anchor.encode('utf-8'))) 
    613612        return anchor 
     
    638637        mixed_breaks_spaces = False    # anything else 
    639638 
    640639        # Check document indicators. 
    641         if scalar.startswith(u'---') or scalar.startswith(u'...'): 
     640        if scalar.startswith('---') or scalar.startswith('...'): 
    642641            block_indicators = True 
    643642            flow_indicators = True 
    644643 
     
    647646 
    648647        # Last character or followed by a whitespace. 
    649648        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') 
    651650 
    652651        # The current series of whitespaces contain plain spaces. 
    653652        spaces = False 
     
    671670 
    672671            if index == 0: 
    673672                # Leading indicators are special characters. 
    674                 if ch in u'#,[]{}&*!|>\'\"%@`':  
     673                if ch in '#,[]{}&*!|>\'\"%@`':  
    675674                    flow_indicators = True 
    676675                    block_indicators = True 
    677                 if ch in u'?:': 
     676                if ch in '?:': 
    678677                    flow_indicators = True 
    679678                    if followed_by_space: 
    680679                        block_indicators = True 
    681                 if ch == u'-' and followed_by_space: 
     680                if ch == '-' and followed_by_space: 
    682681                    flow_indicators = True 
    683682                    block_indicators = True 
    684683            else: 
    685684                # Some indicators cannot appear within a scalar as well. 
    686                 if ch in u',?[]{}': 
     685                if ch in ',?[]{}': 
    687686                    flow_indicators = True 
    688                 if ch == u':': 
     687                if ch == ':': 
    689688                    flow_indicators = True 
    690689                    if followed_by_space: 
    691690                        block_indicators = True 
    692                 if ch == u'#' and preceeded_by_space: 
     691                if ch == '#' and preceeded_by_space: 
    693692                    flow_indicators = True 
    694693                    block_indicators = True 
    695694 
    696695            # Check for line breaks, special, and unicode characters. 
    697696 
    698             if ch in u'\n\x85\u2028\u2029': 
     697            if ch in '\n\x85\u2028\u2029': 
    699698                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': 
    703702                    unicode_characters = True 
    704703                    if not self.allow_unicode: 
    705704                        special_characters = True 
     
    709708            # Spaces, line breaks, and how they are mixed. State machine. 
    710709 
    711710            # Start or continue series of whitespaces. 
    712