Changeset 328
- Timestamp:
- 12/29/08 12:24:05 (4 years ago)
- Location:
- pyyaml/trunk
- Files:
-
- 1 deleted
- 39 edited
- 2 copied
-
lib/yaml/cyaml.py (deleted)
-
lib/yaml/emitter.py (modified) (4 diffs)
-
lib/yaml/representer.py (modified) (1 diff)
-
lib/yaml/resolver.py (modified) (2 diffs)
-
lib/yaml/scanner.py (modified) (6 diffs)
-
lib3 (copied) (copied from pyyaml/trunk/lib)
-
lib3/yaml/__init__.py (modified) (9 diffs)
-
lib3/yaml/composer.py (modified) (6 diffs)
-
lib3/yaml/constructor.py (modified) (22 diffs)
-
lib3/yaml/dumper.py (modified) (1 diff)
-
lib3/yaml/emitter.py (modified) (65 diffs)
-
lib3/yaml/error.py (modified) (4 diffs)
-
lib3/yaml/loader.py (modified) (1 diff)
-
lib3/yaml/parser.py (modified) (8 diffs)
-
lib3/yaml/reader.py (modified) (10 diffs)
-
lib3/yaml/representer.py (modified) (20 diffs)
-
lib3/yaml/resolver.py (modified) (11 diffs)
-
lib3/yaml/scanner.py (modified) (49 diffs)
-
lib3/yaml/serializer.py (modified) (1 diff)
-
setup.py (modified) (4 diffs)
-
tests3 (copied) (copied from pyyaml/trunk/tests)
-
tests3/canonical.py (modified) (11 diffs)
-
tests3/data/construct-binary.code (modified) (1 diff)
-
tests3/data/emitting-unacceptable-unicode-character-bug.code (modified) (1 diff)
-
tests3/data/serializer-is-already-opened.dumper-error (modified) (1 diff)
-
tests3/data/serializer-is-closed-1.dumper-error (modified) (1 diff)
-
tests3/data/serializer-is-closed-2.dumper-error (modified) (1 diff)
-
tests3/data/serializer-is-not-opened-1.dumper-error (modified) (1 diff)
-
tests3/data/serializer-is-not-opened-2.dumper-error (modified) (1 diff)
-
tests3/test_appliance.py (modified) (7 diffs)
-
tests3/test_canonical.py (modified) (3 diffs)
-
tests3/test_constructor.py (modified) (3 diffs)
-
tests3/test_emitter.py (modified) (4 diffs)
-
tests3/test_errors.py (modified) (5 diffs)
-
tests3/test_mark.py (modified) (2 diffs)
-
tests3/test_reader.py (modified) (3 diffs)
-
tests3/test_recursive.py (modified) (2 diffs)
-
tests3/test_representer.py (modified) (1 diff)
-
tests3/test_resolver.py (modified) (5 diffs)
-
tests3/test_structure.py (modified) (9 diffs)
-
tests3/test_tokens.py (modified) (2 diffs)
-
tests3/test_yaml_ext.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pyyaml/trunk/lib/yaml/emitter.py
r313 r328 546 546 % (handle.encode('utf-8'))) 547 547 for ch in handle[1:-1]: 548 if not (u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\548 if not (u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \ 549 549 or ch in u'-_'): 550 550 raise EmitterError("invalid character %r in the tag handle: %r" … … 561 561 while end < len(prefix): 562 562 ch = prefix[end] 563 if u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\563 if u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \ 564 564 or ch in u'-;/?!:@&=+$,_.~*\'()[]': 565 565 end += 1 … … 591 591 while end < len(suffix): 592 592 ch = suffix[end] 593 if u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\593 if u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \ 594 594 or ch in u'-;/?:@&=+$,_.~*\'()[]' \ 595 595 or (ch == u'!' and handle != u'!'): … … 614 614 raise EmitterError("anchor must not be empty") 615 615 for ch in anchor: 616 if not (u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\616 if not (u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \ 617 617 or ch in u'-_'): 618 618 raise EmitterError("invalid character %r in the anchor: %r" -
pyyaml/trunk/lib/yaml/representer.py
r248 r328 287 287 SafeRepresenter.add_representer(datetime.date, 288 288 SafeRepresenter.represent_date) 289 289 290 SafeRepresenter.add_representer(datetime.datetime, 290 291 SafeRepresenter.represent_datetime) -
pyyaml/trunk/lib/yaml/resolver.py
r260 r328 193 193 u'tag:yaml.org,2002:merge', 194 194 re.compile(ur'^(?:<<)$'), 195 [ '<'])195 [u'<']) 196 196 197 197 Resolver.add_implicit_resolver( … … 214 214 u'tag:yaml.org,2002:value', 215 215 re.compile(ur'^(?:=)$'), 216 [ '='])216 [u'=']) 217 217 218 218 # The following resolver is only for documentation purposes. It cannot work -
pyyaml/trunk/lib/yaml/scanner.py
r222 r328 808 808 length = 0 809 809 ch = self.peek(length) 810 while u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\810 while u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \ 811 811 or ch in u'-_': 812 812 length += 1 … … 847 847 # See the specification for details. 848 848 ch = self.peek() 849 if not (u'0' <= ch <= '9'):849 if not (u'0' <= ch <= u'9'): 850 850 raise ScannerError("while scanning a directive", start_mark, 851 851 "expected a digit, but found %r" % ch.encode('utf-8'), … … 913 913 start_mark = self.get_mark() 914 914 indicator = self.peek() 915 if indicator == '*':915 if indicator == u'*': 916 916 name = 'alias' 917 917 else: … … 920 920 length = 0 921 921 ch = self.peek(length) 922 while u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\922 while u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \ 923 923 or ch in u'-_': 924 924 length += 1 … … 1369 1369 ch = self.peek(length) 1370 1370 if ch != u' ': 1371 while u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\1371 while u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \ 1372 1372 or ch in u'-_': 1373 1373 length += 1 … … 1389 1389 length = 0 1390 1390 ch = self.peek(length) 1391 while u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\1391 while u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \ 1392 1392 or ch in u'-;/?:@&=+$,_.!~*\'()[]%': 1393 1393 if ch == u'%': -
pyyaml/trunk/lib3/yaml/__init__.py
r314 r328 1 1 2 from error import * 3 4 from tokens import * 5 from events import * 6 from nodes import * 7 8 from loader import * 9 from dumper import * 10 11 __version__ = '3.07' 12 13 try: 14 from cyaml import * 15 __with_libyaml__ = True 16 except ImportError: 17 __with_libyaml__ = False 2 __version__ = '3.08' 3 __with_libyaml__ = False 4 5 from .error import * 6 7 from .tokens import * 8 from .events import * 9 from .nodes import * 10 11 from .loader import * 12 from .dumper import * 13 14 import io 18 15 19 16 def scan(stream, Loader=Loader): … … 92 89 getvalue = None 93 90 if stream is None: 94 try: 95 from cStringIO import StringIO 96 except ImportError: 97 from StringIO import StringIO 98 stream = StringIO() 91 stream = io.StringIO() 99 92 getvalue = stream.getvalue 100 93 dumper = Dumper(stream, canonical=canonical, indent=indent, width=width, … … 116 109 getvalue = None 117 110 if stream is None: 118 try: 119 from cStringIO import StringIO 120 except ImportError: 121 from StringIO import StringIO 122 stream = StringIO() 111 stream = io.StringIO() 123 112 getvalue = stream.getvalue 124 113 dumper = Dumper(stream, canonical=canonical, indent=indent, width=width, … … 152 141 getvalue = None 153 142 if stream is None: 154 try: 155 from cStringIO import StringIO 156 except ImportError: 157 from StringIO import StringIO 158 stream = StringIO() 143 stream = io.StringIO() 159 144 getvalue = stream.getvalue 160 145 dumper = Dumper(stream, default_style=default_style, … … 260 245 cls.yaml_dumper.add_representer(cls, cls.to_yaml) 261 246 262 class YAMLObject( object):247 class YAMLObject(metaclass=YAMLObjectMetaclass): 263 248 """ 264 249 An object that can dump itself to a YAML stream … … 266 251 """ 267 252 268 __metaclass__ = YAMLObjectMetaclass269 253 __slots__ = () # no direct instantiation, so allow immutable subclasses 270 254 … … 275 259 yaml_flow_style = None 276 260 261 @classmethod 277 262 def from_yaml(cls, loader, node): 278 263 """ … … 280 265 """ 281 266 return loader.construct_yaml_object(node, cls) 282 from_yaml = classmethod(from_yaml) 283 267 268 @classmethod 284 269 def to_yaml(cls, dumper, data): 285 270 """ … … 288 273 return dumper.represent_yaml_object(cls.yaml_tag, data, cls, 289 274 flow_style=cls.yaml_flow_style) 290 to_yaml = classmethod(to_yaml) 291 275 -
pyyaml/trunk/lib3/yaml/composer.py
r258 r328 2 2 __all__ = ['Composer', 'ComposerError'] 3 3 4 from error import MarkedYAMLError5 from events import *6 from nodes import *4 from .error import MarkedYAMLError 5 from .events import * 6 from .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): … … 67 67 if anchor not in self.anchors: 68 68 raise ComposerError(None, None, "found undefined alias %r" 69 % anchor .encode('utf-8'), event.start_mark)69 % anchor, event.start_mark) 70 70 return self.anchors[anchor] 71 71 event = self.peek_event() … … 74 74 if anchor in self.anchors: 75 75 raise ComposerError("found duplicate anchor %r; first occurence" 76 % anchor .encode('utf-8'), self.anchors[anchor].start_mark,76 % anchor, self.anchors[anchor].start_mark, 77 77 "second occurence", event.start_mark) 78 78 self.descend_resolver(parent, index) … … 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, … … 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, [], … … 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, [], -
pyyaml/trunk/lib3/yaml/constructor.py
r303 r328 3 3 'ConstructorError'] 4 4 5 from error import * 6 from nodes import * 7 8 import datetime 9 10 try: 11 set 12 except NameError: 13 from sets import Set as set 14 15 import binascii, re, sys, types 5 from .error import * 6 from .nodes import * 7 8 import collections, datetime, base64, binascii, re, sys, types 16 9 17 10 class ConstructorError(MarkedYAMLError): 18 11 pass 19 12 20 class BaseConstructor (object):13 class BaseConstructor: 21 14 22 15 yaml_constructors = {} … … 96 89 if isinstance(data, types.GeneratorType): 97 90 generator = data 98 data = generator.next()91 data = next(generator) 99 92 if self.deep_construct: 100 93 for dummy in generator: … … 131 124 for key_node, value_node in node.value: 132 125 key = self.construct_object(key_node, deep=deep) 133 try: 134 hash(key) 135 except TypeError, exc: 126 if not isinstance(key, collections.Hashable): 136 127 raise ConstructorError("while constructing a mapping", node.start_mark, 137 "found un acceptable key (%s)" % exc, key_node.start_mark)128 "found unhashable key", key_node.start_mark) 138 129 value = self.construct_object(value_node, deep=deep) 139 130 mapping[key] = value … … 152 143 return pairs 153 144 145 @classmethod 154 146 def add_constructor(cls, tag, constructor): 155 147 if not 'yaml_constructors' in cls.__dict__: 156 148 cls.yaml_constructors = cls.yaml_constructors.copy() 157 149 cls.yaml_constructors[tag] = constructor 158 add_constructor = classmethod(add_constructor) 159 150 151 @classmethod 160 152 def add_multi_constructor(cls, tag_prefix, multi_constructor): 161 153 if not 'yaml_multi_constructors' in cls.__dict__: 162 154 cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy() 163 155 cls.yaml_multi_constructors[tag_prefix] = multi_constructor 164 add_multi_constructor = classmethod(add_multi_constructor)165 156 166 157 class SafeConstructor(BaseConstructor): … … 169 160 if isinstance(node, MappingNode): 170 161 for key_node, value_node in node.value: 171 if key_node.tag == u'tag:yaml.org,2002:value':162 if key_node.tag == 'tag:yaml.org,2002:value': 172 163 return self.construct_scalar(value_node) 173 return BaseConstructor.construct_scalar(self,node)164 return super().construct_scalar(node) 174 165 175 166 def flatten_mapping(self, node): … … 178 169 while index < len(node.value): 179 170 key_node, value_node = node.value[index] 180 if key_node.tag == u'tag:yaml.org,2002:merge':171 if key_node.tag == 'tag:yaml.org,2002:merge': 181 172 del node.value[index] 182 173 if isinstance(value_node, MappingNode): … … 200 191 "expected a mapping or list of mappings for merging, but found %s" 201 192 % value_node.id, value_node.start_mark) 202 elif key_node.tag == u'tag:yaml.org,2002:value':203 key_node.tag = u'tag:yaml.org,2002:str'193 elif key_node.tag == 'tag:yaml.org,2002:value': 194 key_node.tag = 'tag:yaml.org,2002:str' 204 195 index += 1 205 196 else: … … 211 202 if isinstance(node, MappingNode): 212 203 self.flatten_mapping(node) 213 return BaseConstructor.construct_mapping(self,node, deep=deep)204 return super().construct_mapping(node, deep=deep) 214 205 215 206 def construct_yaml_null(self, node): … … 218 209 219 210 bool_values = { 220 u'yes':True,221 u'no':False,222 u'true':True,223 u'false':False,224 u'on':True,225 u'off':False,211 'yes': True, 212 'no': False, 213 'true': True, 214 'false': False, 215 'on': True, 216 'off': False, 226 217 } 227 218 … … 231 222 232 223 def construct_yaml_int(self, node): 233 value = s tr(self.construct_scalar(node))224 value = self.construct_scalar(node) 234 225 value = value.replace('_', '') 235 226 sign = +1 … … 264 255 265 256 def construct_yaml_float(self, node): 266 value = s tr(self.construct_scalar(node))257 value = self.construct_scalar(node) 267 258 value = value.replace('_', '').lower() 268 259 sign = +1 … … 288 279 289 280 def construct_yaml_binary(self, node): 290 value = self.construct_scalar(node)291 281 try: 292 return str(value).decode('base64') 293 except (binascii.Error, UnicodeEncodeError), exc: 294 raise ConstructorError(None, None, 295 "failed to decode base64 data: %s" % exc, node.start_mark) 282 value = self.construct_scalar(node).encode('ascii') 283 except UnicodeEncodeError as exc: 284 raise ConstructorError(None, None, 285 "failed to convert base64 data into ascii: %s" % exc, 286 node.start_mark) 287 try: 288 return base64.decodestring(value) 289 except binascii.Error as exc: 290 raise ConstructorError(None, None, 291 "failed to decode base64 data: %s" % exc, node.start_mark) 296 292 297 293 timestamp_regexp = re.compile( 298 ur'''^(?P<year>[0-9][0-9][0-9][0-9])294 r'''^(?P<year>[0-9][0-9][0-9][0-9]) 299 295 -(?P<month>[0-9][0-9]?) 300 296 -(?P<day>[0-9][0-9]?) … … 387 383 388 384 def construct_yaml_str(self, node): 389 value = self.construct_scalar(node) 390 try: 391 return value.encode('ascii') 392 except UnicodeEncodeError: 393 return value 385 return self.construct_scalar(node) 394 386 395 387 def construct_yaml_seq(self, node): … … 416 408 def construct_undefined(self, node): 417 409 raise ConstructorError(None, None, 418 "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, 419 411 node.start_mark) 420 412 421 413 SafeConstructor.add_constructor( 422 u'tag:yaml.org,2002:null',414 'tag:yaml.org,2002:null', 423 415 SafeConstructor.construct_yaml_null) 424 416 425 417 SafeConstructor.add_constructor( 426 u'tag:yaml.org,2002:bool',418 'tag:yaml.org,2002:bool', 427 419 SafeConstructor.construct_yaml_bool) 428 420 429 421 SafeConstructor.add_constructor( 430 u'tag:yaml.org,2002:int',422 'tag:yaml.org,2002:int', 431 423 SafeConstructor.construct_yaml_int) 432 424 433 425 SafeConstructor.add_constructor( 434 u'tag:yaml.org,2002:float',426 'tag:yaml.org,2002:float', 435 427 SafeConstructor.construct_yaml_float) 436 428 437 429 SafeConstructor.add_constructor( 438 u'tag:yaml.org,2002:binary',430 'tag:yaml.org,2002:binary', 439 431 SafeConstructor.construct_yaml_binary) 440 432 441 433 SafeConstructor.add_constructor( 442 u'tag:yaml.org,2002:timestamp',434 'tag:yaml.org,2002:timestamp', 443 435 SafeConstructor.construct_yaml_timestamp) 444 436 445 437 SafeConstructor.add_constructor( 446 u'tag:yaml.org,2002:omap',438 'tag:yaml.org,2002:omap', 447 439 SafeConstructor.construct_yaml_omap) 448 440 449 441 SafeConstructor.add_constructor( 450 u'tag:yaml.org,2002:pairs',442 'tag:yaml.org,2002:pairs', 451 443 SafeConstructor.construct_yaml_pairs) 452 444 453 445 SafeConstructor.add_constructor( 454 u'tag:yaml.org,2002:set',446 'tag:yaml.org,2002:set', 455 447 SafeConstructor.construct_yaml_set) 456 448 457 449 SafeConstructor.add_constructor( 458 u'tag:yaml.org,2002:str',450 'tag:yaml.org,2002:str', 459 451 SafeConstructor.construct_yaml_str) 460 452 461 453 SafeConstructor.add_constructor( 462 u'tag:yaml.org,2002:seq',454 'tag:yaml.org,2002:seq', 463 455 SafeConstructor.construct_yaml_seq) 464 456 465 457 SafeConstructor.add_constructor( 466 u'tag:yaml.org,2002:map',458 'tag:yaml.org,2002:map', 467 459 SafeConstructor.construct_yaml_map) 468 460 … … 473 465 474 466 def construct_python_str(self, node): 475 return self.construct_scalar(node) .encode('utf-8')467 return self.construct_scalar(node) 476 468 477 469 def construct_python_unicode(self, node): 478 470 return self.construct_scalar(node) 479 471 472 def construct_python_bytes(self, node): 473 try: 474 value = self.construct_scalar(node).encode('ascii') 475 except UnicodeEncodeError as exc: 476 raise ConstructorError(None, None, 477 "failed to convert base64 data into ascii: %s" % exc, 478 node.start_mark) 479 try: 480 return base64.decodestring(value) 481 except binascii.Error as exc: 482 raise ConstructorError(None, None, 483 "failed to decode base64 data: %s" % exc, node.start_mark) 484 480 485 def construct_python_long(self, node): 481 return long(self.construct_yaml_int(node))486 return self.construct_yaml_int(node) 482 487 483 488 def construct_python_complex(self, node): … … 493 498 try: 494 499 __import__(name) 495 except ImportError ,exc:500 except ImportError as exc: 496 501 raise ConstructorError("while constructing a Python module", mark, 497 "cannot find module %r (%s)" % (name .encode('utf-8'), exc), mark)502 "cannot find module %r (%s)" % (name, exc), mark) 498 503 return sys.modules[name] 499 504 … … 502 507 raise ConstructorError("while constructing a Python object", mark, 503 508 "expected non-empty name appended to the tag", mark) 504 if u'.' in name: 505 # Python 2.4 only 506 #module_name, object_name = name.rsplit('.', 1) 507 items = name.split('.') 508 object_name = items.pop() 509 module_name = '.'.join(items) 509 if '.' in name: 510 module_name, object_name = name.rsplit('.', 1) 510 511 else: 511 512 module_name = '__builtin__' … … 513 514 try: 514 515 __import__(module_name) 515 except ImportError ,exc:516 except ImportError as exc: 516 517 raise ConstructorError("while constructing a Python object", mark, 517 "cannot find module %r (%s)" % (module_name .encode('utf-8'), exc), mark)518 "cannot find module %r (%s)" % (module_name, exc), mark) 518 519 module = sys.modules[module_name] 519 520 if not hasattr(module, object_name): 520 521 raise ConstructorError("while constructing a Python object", mark, 521 "cannot find %r in the module %r" % (object_name.encode('utf-8'),522 module.__name__), mark)522 "cannot find %r in the module %r" 523 % (object_name, module.__name__), mark) 523 524 return getattr(module, object_name) 524 525 … … 527 528 if value: 528 529 raise ConstructorError("while constructing a Python name", node.start_mark, 529 "expected the empty value, but found %r" % value.encode('utf-8'), 530 node.start_mark) 530 "expected the empty value, but found %r" % value, node.start_mark) 531 531 return self.find_python_name(suffix, node.start_mark) 532 532 … … 535 535 if value: 536 536 raise ConstructorError("while constructing a Python module", node.start_mark, 537 "expected the empty value, but found %r" % value.encode('utf-8'), 538 node.start_mark) 537 "expected the empty value, but found %r" % value, node.start_mark) 539 538 return self.find_python_module(suffix, node.start_mark) 540 541 class classobj: pass542 539 543 540 def make_python_instance(self, suffix, node, … … 548 545 kwds = {} 549 546 cls = self.find_python_name(suffix, node.start_mark) 550 if newobj and isinstance(cls, type(self.classobj)) \ 551 and not args and not kwds: 552 instance = self.classobj() 553 instance.__class__ = cls 554 return instance 555 elif newobj and isinstance(cls, type): 547 if newobj and isinstance(cls, type): 556 548 return cls.__new__(cls, *args, **kwds) 557 549 else: … … 620 612 621 613 Constructor.add_constructor( 622 u'tag:yaml.org,2002:python/none',614 'tag:yaml.org,2002:python/none', 623 615 Constructor.construct_yaml_null) 624 616 625 617 Constructor.add_constructor( 626 u'tag:yaml.org,2002:python/bool',618 'tag:yaml.org,2002:python/bool', 627 619 Constructor.construct_yaml_bool) 628 620 629 621 Constructor.add_constructor( 630 u'tag:yaml.org,2002:python/str',622 'tag:yaml.org,2002:python/str', 631 623 Constructor.construct_python_str) 632 624 633 625 Constructor.add_constructor( 634 u'tag:yaml.org,2002:python/unicode',626 'tag:yaml.org,2002:python/unicode', 635 627 Constructor.construct_python_unicode) 636 628 637 629 Constructor.add_constructor( 638 u'tag:yaml.org,2002:python/int', 630 'tag:yaml.org,2002:python/bytes', 631 Constructor.construct_python_bytes) 632 633 Constructor.add_constructor( 634 'tag:yaml.org,2002:python/int', 639 635 Constructor.construct_yaml_int) 640 636 641 637 Constructor.add_constructor( 642 u'tag:yaml.org,2002:python/long',638 'tag:yaml.org,2002:python/long', 643 639 Constructor.construct_python_long) 644 640 645 641 Constructor.add_constructor( 646 u'tag:yaml.org,2002:python/float',642 'tag:yaml.org,2002:python/float', 647 643 Constructor.construct_yaml_float) 648 644 649 645 Constructor.add_constructor( 650 u'tag:yaml.org,2002:python/complex',646 'tag:yaml.org,2002:python/complex', 651 647 Constructor.construct_python_complex) 652 648 653 649 Constructor.add_constructor( 654 u'tag:yaml.org,2002:python/list',650 'tag:yaml.org,2002:python/list', 655 651 Constructor.construct_yaml_seq) 656 652 657 653 Constructor.add_constructor( 658 u'tag:yaml.org,2002:python/tuple',654 'tag:yaml.org,2002:python/tuple', 659 655 Constructor.construct_python_tuple) 660 656 661 657 Constructor.add_constructor( 662 u'tag:yaml.org,2002:python/dict',658 'tag:yaml.org,2002:python/dict', 663 659 Constructor.construct_yaml_map) 664 660 665 661 Constructor.add_multi_constructor( 666 u'tag:yaml.org,2002:python/name:',662 'tag:yaml.org,2002:python/name:', 667 663 Constructor.construct_python_name) 668 664 669 665 Constructor.add_multi_constructor( 670 u'tag:yaml.org,2002:python/module:',666 'tag:yaml.org,2002:python/module:', 671 667 Constructor.construct_python_module) 672 668 673 669 Constructor.add_multi_constructor( 674 u'tag:yaml.org,2002:python/object:',670 'tag:yaml.org,2002:python/object:', 675 671 Constructor.construct_python_object) 676 672 677 673 Constructor.add_multi_constructor( 678 u'tag:yaml.org,2002:python/object/apply:',674 'tag:yaml.org,2002:python/object/apply:', 679 675 Constructor.construct_python_object_apply) 680 676 681 677 Constructor.add_multi_constructor( 682 u'tag:yaml.org,2002:python/object/new:',678 'tag:yaml.org,2002:python/object/new:', 683 679 Constructor.construct_python_object_new) 684 680 -
pyyaml/trunk/lib3/yaml/dumper.py
r307 r328 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 .emitter import * 5 from .serializer import * 6 from .representer import * 7 from .resolver import * 8 8 9 9 class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver): -
pyyaml/trunk/lib3/yaml/emitter.py
r313 r328 9 9 __all__ = ['Emitter', 'EmitterError'] 10 10 11 from error import YAMLError12 from events import *11 from .error import YAMLError 12 from .events import * 13 13 14 14 class EmitterError(YAMLError): 15 15 pass 16 16 17 class ScalarAnalysis (object):17 class ScalarAnalysis: 18 18 def __init__(self, scalar, empty, multiline, 19 19 allow_flow_plain, allow_block_plain, … … 29 29 self.allow_block = allow_block 30 30 31 class Emitter (object):31 class Emitter: 32 32 33 33 DEFAULT_TAG_PREFIXES = { 34 u'!' : u'!',35 u'tag:yaml.org,2002:' : u'!!',34 '!' : '!', 35 'tag:yaml.org,2002:' : '!!', 36 36 } 37 37 … … 89 89 if width and width > self.best_indent*2: 90 90 self.best_width = width 91 self.best_line_break = u'\n'92 if line_break in [ u'\r', u'\n', u'\r\n']:91 self.best_line_break = '\n' 92 if line_break in ['\r', '\n', '\r\n']: 93 93 self.best_line_break = line_break 94 94 … … 155 155 def expect_stream_start(self): 156 156 if isinstance(self.event, StreamStartEvent): 157 if self.event.encoding :157 if self.event.encoding and not hasattr(self.stream, 'encoding'): 158 158 self.encoding = self.event.encoding 159 159 self.write_stream_start() … … 174 174 if isinstance(self.event, DocumentStartEvent): 175 175 if (self.event.version or self.event.tags) and self.open_ended: 176 self.write_indicator( u'...', True)176 self.write_indicator('...', True) 177 177 self.write_indent() 178 178 if self.event.version: … … 194 194 if not implicit: 195 195 self.write_indent() 196 self.write_indicator( u'---', True)196 self.write_indicator('---', True) 197 197 if self.canonical: 198 198 self.write_indent() … … 200 200 elif isinstance(self.event, StreamEndEvent): 201 201 if self.open_ended: 202 self.write_indicator( u'...', True)202 self.write_indicator('...', True) 203 203 self.write_indent() 204 204 self.write_stream_end() … … 212 212 self.write_indent() 213 213 if self.event.explicit: 214 self.write_indicator( u'...', True)214 self.write_indicator('...', True) 215 215 self.write_indent() 216 216 self.flush_stream() … … 235 235 self.expect_alias() 236 236 elif isinstance(self.event, (ScalarEvent, CollectionStartEvent)): 237 self.process_anchor( u'&')237 self.process_anchor('&') 238 238 self.process_tag() 239 239 if isinstance(self.event, ScalarEvent): … … 257 257 if self.event.anchor is None: 258 258 raise EmitterError("anchor is not specified for alias") 259 self.process_anchor( u'*')259 self.process_anchor('*') 260 260 self.state = self.states.pop() 261 261 … … 269 269 270 270 def expect_flow_sequence(self): 271 self.write_indicator( u'[', True, whitespace=True)271 self.write_indicator('[', True, whitespace=True) 272 272 self.flow_level += 1 273 273 self.increase_indent(flow=True) … … 278 278 self.indent = self.indents.pop() 279 279 self.flow_level -= 1 280 self.write_indicator( u']', False)280 self.write_indicator(']', False) 281 281 self.state = self.states.pop() 282 282 else: … … 291 291 self.flow_level -= 1 292 292 if self.canonical: 293 self.write_indicator( u',', False)293 self.write_indicator(',', False) 294 294 self.write_indent() 295 self.write_indicator( u']', False)295 self.write_indicator(']', False) 296 296 self.state = self.states.pop() 297 297 else: 298 self.write_indicator( u',', False)298 self.write_indicator(',', False) 299 299 if self.canonical or self.column > self.best_width: 300 300 self.write_indent() … … 305 305 306 306 def expect_flow_mapping(self): 307 self.write_indicator( u'{', True, whitespace=True)307 self.write_indicator('{', True, whitespace=True) 308 308 self.flow_level += 1 309 309 self.increase_indent(flow=True) … … 314 314 self.indent = self.indents.pop() 315 315 self.flow_level -= 1 316 self.write_indicator( u'}', False)316 self.write_indicator('}', False) 317 317 self.state = self.states.pop() 318 318 else: … … 323 323 self.expect_node(mapping=True, simple_key=True) 324 324 else: 325 self.write_indicator( u'?', True)325 self.write_indicator('?', True) 326 326 self.states.append(self.expect_flow_mapping_value) 327 327 self.expect_node(mapping=True) … … 332 332 self.flow_level -= 1 333 333 if self.canonical: 334 self.write_indicator( u',', False)334 self.write_indicator(',', False) 335 335 self.write_indent() 336 self.write_indicator( u'}', False)336 self.write_indicator('}', False) 337 337 self.state = self.states.pop() 338 338 else: 339 self.write_indicator( u',', False)339 self.write_indicator(',', False) 340 340 if self.canonical or self.column > self.best_width: 341 341 self.write_indent() … … 344 344 self.expect_node(mapping=True, simple_key=True) 345 345 else: 346 self.write_indicator( u'?', True)346 self.write_indicator('?', True) 347 347 self.states.append(self.expect_flow_mapping_value) 348 348 self.expect_node(mapping=True) 349 349 350 350 def expect_flow_mapping_simple_value(self): 351 self.write_indicator( u':', False)351 self.write_indicator(':', False) 352 352 self.states.append(self.expect_flow_mapping_key) 353 353 self.expect_node(mapping=True) … … 356 356 if self.canonical or self.column > self.best_width: 357 357 self.write_indent() 358 self.write_indicator( u':', True)358 self.write_indicator(':', True) 359 359 self.states.append(self.expect_flow_mapping_key) 360 360 self.expect_node(mapping=True) … … 376 376 else: 377 377 self.write_indent() 378 self.write_indicator( u'-', True, indention=True)378 self.write_indicator('-', True, indention=True) 379 379 self.states.append(self.expect_block_sequence_item) 380 380 self.expect_node(sequence=True) … … 399 399 self.expect_node(mapping=True, simple_key=True) 400 400 else: 401 self.write_indicator( u'?', True, indention=True)401 self.write_indicator('?', True, indention=True) 402 402 self.states.append(self.expect_block_mapping_value) 403 403 self.expect_node(mapping=True) 404 404 405 405 def expect_block_mapping_simple_value(self): 406 self.write_indicator( u':', False)406 self.write_indicator(':', False) 407 407 self.states.append(self.expect_block_mapping_key) 408 408 self.expect_node(mapping=True) … … 410 410 def expect_block_mapping_value(self): 411 411 self.write_indent() 412 self.write_indicator( u':', True, indention=True)412 self.write_indicator(':', True, indention=True) 413 413 self.states.append(self.expect_block_mapping_key) 414 414 self.expect_node(mapping=True) … … 429 429 event = self.events[0] 430 430 return (isinstance(event, ScalarEvent) and event.anchor is None 431 and event.tag is None and event.implicit and event.value == u'')431 and event.tag is None and event.implicit and event.value == '') 432 432 433 433 def check_simple_key(self): … … 474 474 return 475 475 if self.event.implicit[0] and tag is None: 476 tag = u'!'476 tag = '!' 477 477 self.prepared_tag = None 478 478 else: … … 537 537 if major != 1: 538 538 raise EmitterError("unsupported YAML version: %d.%d" % (major, minor)) 539 return u'%d.%d' % (major, minor)539 return '%d.%d' % (major, minor) 540 540 541 541 def prepare_tag_handle(self, handle): 542 542 if not handle: 543 543 raise EmitterError("tag handle must not be empty") 544 if handle[0] != u'!' or handle[-1] != u'!': 545 raise EmitterError("tag handle must start and end with '!': %r" 546 % (handle.encode('utf-8'))) 544 if handle[0] != '!' or handle[-1] != '!': 545 raise EmitterError("tag handle must start and end with '!': %r" % handle) 547 546 for ch in handle[1:-1]: 548 if not ( u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \549 or ch in u'-_'):547 if not ('0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 548 or ch in '-_'): 550 549 raise EmitterError("invalid character %r in the tag handle: %r" 551 % (ch .encode('utf-8'), handle.encode('utf-8')))550 % (ch, handle)) 552 551 return handle 553 552 … … 557 556 chunks = [] 558 557 start = end = 0 559 if prefix[0] == u'!':558 if prefix[0] == '!': 560 559 end = 1 561 560 while end < len(prefix): 562 561 ch = prefix[end] 563 if u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\564 or ch in u'-;/?!:@&=+$,_.~*\'()[]':562 if '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 563 or ch in '-;/?!:@&=+$,_.~*\'()[]': 565 564 end += 1 566 565 else: … … 570 569 data = ch.encode('utf-8') 571 570 for ch in data: 572 chunks.append( u'%%%02X' % ord(ch))571 chunks.append('%%%02X' % ord(ch)) 573 572 if start < end: 574 573 chunks.append(prefix[start:end]) 575 return u''.join(chunks)574 return ''.join(chunks) 576 575 577 576 def prepare_tag(self, tag): 578 577 if not tag: 579 578 raise EmitterError("tag must not be empty") 580 if tag == u'!':579 if tag == '!': 581 580 return tag 582 581 handle = None … … 584 583 for prefix in self.tag_prefixes: 585 584 if tag.startswith(prefix) \ 586 and (prefix == u'!' or len(prefix) < len(tag)):585 and (prefix == '!' or len(prefix) < len(tag)): 587 586 handle = self.tag_prefixes[prefix] 588 587 suffix = tag[len(prefix):] … … 591 590 while end < len(suffix): 592 591 ch = suffix[end] 593 if u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z'\594 or ch in u'-;/?:@&=+$,_.~*\'()[]' \595 or (ch == u'!' and handle != u'!'):592 if '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 593 or ch in '-;/?:@&=+$,_.~*\'()[]' \ 594 or (ch == '!' and handle != '!'): 596 595 end += 1 597 596 else: … … 601 600 data = ch.encode('utf-8') 602 601 for ch in data: 603 chunks.append( u'%%%02X' % ord(ch))602 chunks.append('%%%02X' % ord(ch)) 604 603 if start < end: 605 604 chunks.append(suffix[start:end]) 606 suffix_text = u''.join(chunks)605 suffix_text = ''.join(chunks) 607 606 if handle: 608 return u'%s%s' % (handle, suffix_text)609 else: 610 return u'!<%s>' % suffix_text607 return '%s%s' % (handle, suffix_text) 608 else: 609 return '!<%s>' % suffix_text 611 610 612 611 def prepare_anchor(self, anchor): … … 614 613 raise EmitterError("anchor must not be empty") 615 614 for ch in anchor: 616 if not ( u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \617 or ch in u'-_'):615 if not ('0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 616 or ch in '-_'): 618 617 raise EmitterError("invalid character %r in the anchor: %r" 619 % (ch .encode('utf-8'), anchor.encode('utf-8')))618 % (ch, anchor)) 620 619 return anchor 621 620 … … 644 643 645 644 # Check document indicators. 646 if scalar.startswith( u'---') or scalar.startswith(u'...'):645 if scalar.startswith('---') or scalar.startswith('...'): 647 646 block_indicators = True 648 647 flow_indicators = True … … 653 652 # Last character or followed by a whitespace. 654 653 followed_by_whitespace = (len(scalar) == 1 or 655 scalar[1] in u'\0 \t\r\n\x85\u2028\u2029')654 scalar[1] in '\0 \t\r\n\x85\u2028\u2029') 656 655 657 656 # The previous character is a space. … … 668 667 if index == 0: 669 668 # Leading indicators are special characters. 670 if ch in u'#,[]{}&*!|>\'\"%@`':669 if ch in '#,[]{}&*!|>\'\"%@`': 671 670 flow_indicators = True 672 671 block_indicators = True 673 if ch in u'?:':672 if ch in '?:': 674 673 flow_indicators = True 675 674 if followed_by_whitespace: 676 675 block_indicators = True 677 if ch == u'-' and followed_by_whitespace:676 if ch == '-' and followed_by_whitespace: 678 677 flow_indicators = True 679 678 block_indicators = True 680 679 else: 681 680 # Some indicators cannot appear within a scalar as well. 682 if ch in u',?[]{}':681 if ch in ',?[]{}': 683 682 flow_indicators = True 684 if ch == u':':683 if ch == ':': 685 684 flow_indicators = True 686 685 if followed_by_whitespace: 687 686 block_indicators = True 688 if ch == u'#' and preceeded_by_whitespace:687 if ch == '#' and preceeded_by_whitespace: 689 688 flow_indicators = True 690 689 block_indicators = True 691 690 692 691 # Check for line breaks, special, and unicode characters. 693 if ch in u'\n\x85\u2028\u2029':692 if ch in '\n\x85\u2028\u2029': 694 693 line_breaks = True 695 if not (ch == u'\n' or u'\x20' <= ch <= u'\x7E'):696 if (ch == u'\x85' or u'\xA0' <= ch <= u'\uD7FF'697 or u'\uE000' <= ch <= u'\uFFFD') and ch != u'\uFEFF':694 if not (ch == '\n' or '\x20' <= ch <= '\x7E'): 695 if (ch == '\x85' or '\xA0' <= ch <= '\uD7FF' 696 or '\uE000' <= ch <= '\uFFFD') and ch != '\uFEFF': 698 697 unicode_characters = True 699 698 if not self.allow_unicode: … … 703 702 704 703 # Detect important whitespace combinations. 705 if ch == u' ':704 if ch == ' ': 706 705 if index == 0: 707 706 leading_space = True … … 712 711 previous_space = True 713 712 previous_break = False 714 elif ch in u'\n\x85\u2028\u2029':713 elif ch in '\n\x85\u2028\u2029': 715 714 if index == 0: 716 715 leading_break = True … … 727 726 # Prepare for the next character. 728 727 index += 1 729 preceeded_by_whitespace = (ch in u'\0 \t\r\n\x85\u2028\u2029')728 preceeded_by_whitespace = (ch in '\0 \t\r\n\x85\u2028\u2029') 730 729 followed_by_whitespace = (index+1 >= len(scalar) or 731 scalar[index+1] in u'\0 \t\r\n\x85\u2028\u2029')730 scalar[index+1] in '\0 \t\r\n\x85\u2028\u2029') 732 731 733 732 # Let's decide what styles are allowed. … … 788 787 # Write BOM if needed. 789 788 if self.encoding and self.encoding.startswith('utf-16'): 790 self.stream.write( u'\xFF\xFE'.encode(self.encoding))789 self.stream.write('\xFF\xFE'.encode(self.encoding)) 791 790 792 791 def write_stream_end(self): … … 798 797 data = indicator 799 798 else: 800 data = u' '+indicator799 data = ' '+indicator 801 800 self.whitespace = whitespace 802 801 self.indention = self.indention and indention … … 814 813 if self.column < indent: 815 814 self.whitespace = True 816 data = u' '*(indent-self.column)815 data = ' '*(indent-self.column) 817 816 self.column = indent 818 817 if self.encoding: … … 832 831 833 832 def write_version_directive(self, version_text): 834 data = u'%%YAML %s' % version_text833 data = '%%YAML %s' % version_text 835 834 if self.encoding: 836 835 data = data.encode(self.encoding) … … 839 838 840 839 def write_tag_directive(self, handle_text, prefix_text): 841 data = u'%%TAG %s %s' % (handle_text, prefix_text)840 data = '%%TAG %s %s' % (handle_text, prefix_text) 842 841 if self.encoding: 843 842 data = data.encode(self.encoding) … … 848 847 849 848 def write_single_quoted(self, text, split=True): 850 self.write_indicator( u'\'', True)849 self.write_indicator('\'', True) 851 850 spaces = False 852 851 breaks = False … … 857 856 ch = text[end] 858 857 if spaces: 859 if ch is None or ch != u' ':858 if ch is None or ch != ' ': 860 859 if start+1 == end and self.column > self.best_width and split \ 861 860 and start != 0 and end != len(text): … … 869 868 start = end 870 869 elif breaks: 871 if ch is None or ch not in u'\n\x85\u2028\u2029':872 if text[start] == u'\n':870 if ch is None or ch not in '\n\x85\u2028\u2029': 871 if text[start] == '\n': 873 872 self.write_line_break() 874 873 for br in text[start:end]: 875 if br == u'\n':874 if br == '\n': 876 875 self.write_line_break() 877 876 else: … … 880 879 start = end 881 880 else: 882 if ch is None or ch in u' \n\x85\u2028\u2029' or ch == u'\'':881 if ch is None or ch in ' \n\x85\u2028\u2029' or ch == '\'': 883 882 if start < end: 884 883 data = text[start:end] … … 888 887 self.stream.write(data) 889 888 start = end 890 if ch == u'\'':891 data = u'\'\''889 if ch == '\'': 890 data = '\'\'' 892 891 self.column += 2 893 892 if self.encoding: … … 896 895 start = end + 1 897 896 if ch is not None: 898 spaces = (ch == u' ')899 breaks = (ch in u'\n\x85\u2028\u2029')897 spaces = (ch == ' ') 898 breaks = (ch in '\n\x85\u2028\u2029') 900 899 end += 1 901 self.write_indicator( u'\'', False)900 self.write_indicator('\'', False) 902 901 903 902 ESCAPE_REPLACEMENTS = { 904 u'\0': u'0',905 u'\x07': u'a',906 u'\x08': u'b',907 u'\x09': u't',908 u'\x0A': u'n',909 u'\x0B': u'v',910 u'\x0C': u'f',911 u'\x0D': u'r',912 u'\x1B': u'e',913 u'\"': u'\"',914 u'\\': u'\\',915 u'\x85': u'N',916 u'\xA0': u'_',917 u'\u2028': u'L',918 u'\u2029': u'P',903 '\0': '0', 904 '\x07': 'a', 905 '\x08': 'b', 906 '\x09': 't', 907 '\x0A': 'n', 908 '\x0B': 'v', 909 '\x0C': 'f', 910 '\x0D': 'r', 911 '\x1B': 'e', 912 '\"': '\"', 913 '\\': '\\', 914 '\x85': 'N', 915 '\xA0': '_', 916 '\u2028': 'L', 917 '\u2029': 'P', 919 918 } 920 919 921 920 def write_double_quoted(self, text, split=True): 922 self.write_indicator( u'"', True)921 self.write_indicator('"', True) 923 922 start = end = 0 924 923 while end <= len(text): … … 926 925 if end < len(text): 927 926 ch = text[end] 928 if ch is None or ch in u'"\\\x85\u2028\u2029\uFEFF' \929 or not ( u'\x20' <= ch <= u'\x7E'927 if ch is None or ch in '"\\\x85\u2028\u2029\uFEFF' \ 928 or not ('\x20' <= ch <= '\x7E' 930 929 or (self.allow_unicode 931 and ( u'\xA0' <= ch <= u'\uD7FF'932 or u'\uE000' <= ch <= u'\uFFFD'))):930 and ('\xA0' <= ch <= '\uD7FF' 931 or '\uE000' <= ch <= '\uFFFD'))): 933 932 if start < end: 934 933 data = text[start:end] … … 940 939 if ch is not None: 941 940 if ch in self.ESCAPE_REPLACEMENTS: 942 data = u'\\'+self.ESCAPE_REPLACEMENTS[ch]943 elif ch <= u'\xFF':944 data = u'\\x%02X' % ord(ch)945 elif ch <= u'\uFFFF':946 data = u'\\u%04X' % ord(ch)941 data = '\\'+self.ESCAPE_REPLACEMENTS[ch] 942 elif ch <= '\xFF': 943 data = '\\x%02X' % ord(ch) 944 elif ch <= '\uFFFF': 945 data = '\\u%04X' % ord(ch) 947 946 else: 948 data = u'\\U%08X' % ord(ch)947 data = '\\U%08X' % ord(ch) 949 948 self.column += len(data) 950 949 if self.encoding: … … 952 951 self.stream.write(data) 953 952 start = end+1 954 if 0 < end < len(text)-1 and (ch == u' ' or start >= end)\953 if 0 < end < len(text)-1 and (ch == ' ' or start >= end) \ 955 954 and self.column+(end-start) > self.best_width and split: 956 data = text[start:end]+ u'\\'955 data = text[start:end]+'\\' 957 956 if start < end: 958 957 start = end … … 964 963 self.whitespace = False 965 964 self.indention = False 966 if text[start] == u' ':967 data = u'\\'965 if text[start] == ' ': 966 data = '\\' 968 967 self.column += len(data) 969 968 if self.encoding: … … 971 970 self.stream.write(data) 972 971 end += 1 973 self.write_indicator( u'"', False)972 self.write_indicator('"', False) 974 973 975 974 def determine_block_hints(self, text): 976 hints = u''975 hints = '' 977 976 if text: 978 if text[0] in u' \n\x85\u2028\u2029':979 hints += unicode(self.best_indent)980 if text[-1] not in u'\n\x85\u2028\u2029':981 hints += u'-'982 elif len(text) == 1 or text[-2] in u'\n\x85\u2028\u2029':983 hints += u'+'977 if text[0] in ' \n\x85\u2028\u2029': 978 hints += str(self.best_indent) 979 if text[-1] not in '\n\x85\u2028\u2029': 980 hints += '-' 981 elif len(text) == 1 or text[-2] in '\n\x85\u2028\u2029': 982 hints += '+' 984 983 return hints 985 984 986 985 def write_folded(self, text): 987 986 hints = self.determine_block_hints(text) 988 self.write_indicator( u'>'+hints, True)989 if hints[-1:] == u'+':987 self.write_indicator('>'+hints, True) 988 if hints[-1:] == '+': 990 989 self.open_ended = True 991 990 self.write_line_break() … … 999 998 ch = text[end] 1000 999 if breaks: 1001 if ch is None or ch not in u'\n\x85\u2028\u2029':1002 if not leading_space and ch is not None and ch != u' '\1003 and text[start] == u'\n':1000 if ch is None or ch not in '\n\x85\u2028\u2029': 1001 if not leading_space and ch is not None and ch != ' ' \ 1002 and text[start] == '\n': 1004 1003 self.write_line_break() 1005 leading_space = (ch == u' ')1004 leading_space = (ch == ' ') 1006 1005 for br in text[start:end]: 1007 if br == u'\n':1006 if br == '\n': 1008 1007 self.write_line_break() 1009 1008 else: … … 1013 1012 start = end 1014 1013 elif spaces: 1015 if ch != u' ':1014 if ch != ' ': 1016 1015 if start+1 == end and self.column > self.best_width: 1017 1016 self.write_indent() … … 1024 1023 start = end 1025 1024 else: 1026 if ch is None or ch in u' \n\x85\u2028\u2029':1025 if ch is None or ch in ' \n\x85\u2028\u2029': 1027 1026 data = text[start:end] 1028 1027 if self.encoding: … … 1033 1032 start = end 1034 1033 if ch is not None: 1035 breaks = (ch in u'\n\x85\u2028\u2029')1036 spaces = (ch == u' ')1034 breaks = (ch in '\n\x85\u2028\u2029') 1035 spaces = (ch == ' ') 1037 1036 end += 1 1038 1037 1039 1038 def write_literal(self, text): 1040 1039 hints = self.determine_block_hints(text) 1041 self.write_indicator( u'|'+hints, True)1042 if hints[-1:] == u'+':1040 self.write_indicator('|'+hints, True) 1041 if hints[-1:] == '+': 1043 1042 self.open_ended = True 1044 1043 self.write_line_break() … … 1050 1049 ch = text[end] 1051 1050 if breaks: 1052 if ch is None or ch not in u'\n\x85\u2028\u2029':1051 if ch is None or ch not in '\n\x85\u2028\u2029': 1053 1052 for br in text[start:end]: 1054 if br == u'\n':1053 if br == '\n': 1055 1054 self.write_line_break() 1056 1055 else: … … 1060 1059 start = end 1061 1060 else: 1062 if ch is None or ch in u'\n\x85\u2028\u2029':1061 if ch is None or ch in '\n\x85\u2028\u2029': 1063 1062 data = text[start:end] 1064 1063 if self.encoding: … … 1069 1068 start = end 1070 1069 if ch is not None: 1071 breaks = (ch in u'\n\x85\u2028\u2029')1070 breaks = (ch in '\n\x85\u2028\u2029') 1072 1071 end += 1 1073 1072 … … 1078 1077 return 1079 1078 if not self.whitespace: 1080 data = u' '1079 data = ' ' 1081 1080 self.column += len(data) 1082 1081 if self.encoding: … … 1093 1092 ch = text[end] 1094 1093 if spaces: 1095 if ch != u' ':1094 if ch != ' ': 1096 1095 if start+1 == end and self.column > self.best_width and split: 1097 1096 self.write_indent() … … 1106 1105 start = end 1107 1106 elif breaks: 1108 if ch not in u'\n\x85\u2028\u2029':1109 if text[start] == u'\n':1107 if ch not in '\n\x85\u2028\u2029': 1108 if text[start] == '\n': 1110 1109 self.write_line_break() 1111 1110 for br in text[start:end]: 1112 if br == u'\n':1111 if br == '\n': 1113 1112 self.write_line_break() 1114 1113 else: … … 1119 1118 start = end 1120 1119 else: 1121 if ch is None or ch in u' \n\x85\u2028\u2029':1120 if ch is None or ch in ' \n\x85\u2028\u2029': 1122 1121 data = text[start:end] 1123 1122 self.column += len(data) … … 1127 1126 start = end 1128 1127 if ch is not None: 1129 spaces = (ch == u' ')1130 breaks = (ch in u'\n\x85\u2028\u2029')1128 spaces = (ch == ' ') 1129 breaks = (ch in '\n\x85\u2028\u2029') 1131 1130 end += 1 1132 1131 -
pyyaml/trunk/lib3/yaml/error.py
r222 r328 2 2 __all__ = ['Mark', 'YAMLError', 'MarkedYAMLError'] 3 3 4 class Mark (object):4 class Mark: 5 5 6 6 def __init__(self, name, index, line, column, buffer, pointer): … … 17 17 head = '' 18 18 start = self.pointer 19 while start > 0 and self.buffer[start-1] not in u'\0\r\n\x85\u2028\u2029':19 while start > 0 and self.buffer[start-1] not in '\0\r\n\x85\u2028\u2029': 20 20 start -= 1 21 21 if self.pointer-start > max_length/2-1: … … 25 25 tail = '' 26 26 end = self.pointer 27 while end < len(self.buffer) and self.buffer[end] not in u'\0\r\n\x85\u2028\u2029':27 while end < len(self.buffer) and self.buffer[end] not in '\0\r\n\x85\u2028\u2029': 28 28 end += 1 29 29 if end-self.pointer > max_length/2-1: … … 31 31 end -= 5 32 32 break 33 snippet = self.buffer[start:end] .encode('utf-8')33 snippet = self.buffer[start:end] 34 34 return ' '*indent + head + snippet + tail + '\n' \ 35 35 + ' '*(indent+self.pointer-start+len(head)) + '^' -
pyyaml/trunk/lib3/yaml/loader.py
r137 r328 2 2 __all__ = ['BaseLoader', 'SafeLoader', 'Loader'] 3 3 4 from reader import *5 from scanner import *6 from parser import *7 from composer import *8 from constructor import *9 from resolver import *4 from .reader import * 5 from .scanner import * 6 from .parser import * 7 from .composer import * 8 from .constructor import * 9 from .resolver import * 10 10 11 11 class BaseLoader(Reader, Scanner, Parser, Composer, BaseConstructor, BaseResolver): -
pyyaml/trunk/lib3/yaml/parser.py
r302 r328 62 62 __all__ = ['Parser', 'ParserError'] 63 63 64 from error import MarkedYAMLError65 from tokens import *66 from events import *67 from scanner import *64 from .error import MarkedYAMLError 65 from .tokens import * 66 from .events import * 67 from .scanner import * 68 68 69 69 class ParserError(MarkedYAMLError): 70 70 pass 71 71 72 class Parser (object):72 class Parser: 73 73 # Since writing a recursive-descendant parser is a straightforward task, we 74 74 # do not give many comments here. 75 75 76 76 DEFAULT_TAGS = { 77 u'!': u'!',78 u'!!': u'tag:yaml.org,2002:',77 '!': '!', 78 '!!': 'tag:yaml.org,2002:', 79 79 } 80 80 … … 215 215 while self.check_token(DirectiveToken): 216 216 token = self.get_token() 217 if token.name == u'YAML':217 if token.name == 'YAML': 218 218 if self.yaml_version is not None: 219 219 raise ParserError(None, None, … … 225 225 token.start_mark) 226 226 self.yaml_version = token.value 227 elif token.name == u'TAG':227 elif token.name == 'TAG': 228 228 handle, prefix = token.value 229 229 if handle in self.tag_handles: 230 230 raise ParserError(None, None, 231 "duplicate tag handle %r" % handle .encode('utf-8'),231 "duplicate tag handle %r" % handle, 232 232 token.start_mark) 233 233 self.tag_handles[handle] = prefix … … 299 299 if handle not in self.tag_handles: 300 300 raise ParserError("while parsing a node", start_mark, 301 "found undefined tag handle %r" % handle .encode('utf-8'),301 "found undefined tag handle %r" % handle, 302 302 tag_mark) 303 303 tag = self.tag_handles[handle]+suffix 304 304 else: 305 305 tag = suffix 306 #if tag == u'!':306 #if tag == '!': 307 307 # raise ParserError("while parsing a node", start_mark, 308 308 # "found non-specific tag '!'", tag_mark, … … 311 311 start_mark = end_mark = self.peek_token().start_mark 312 312 event = None 313 implicit = (tag is None or tag == u'!')313 implicit = (tag is None or tag == '!') 314 314 if indentless_sequence and self.check_token(BlockEntryToken): 315 315 end_mark = self.peek_token().end_mark … … 321 321 token = self.get_token() 322 322 end_mark = token.end_mark 323 if (token.plain and tag is None) or tag == u'!':323 if (token.plain and tag is None) or tag == '!': 324 324 implicit = (True, False) 325 325 elif tag is None: … … 353 353 # Empty scalars are allowed even if a tag or an anchor is 354 354 # specified. 355 event = ScalarEvent(anchor, tag, (implicit, False), u'',355 event = ScalarEvent(anchor, tag, (implicit, False), '', 356 356 start_mark, end_mark) 357 357 self.state = self.states.pop() … … 581 581 582 582 def process_empty_scalar(self, mark): 583 return ScalarEvent(None, None, (True, False), u'', mark, mark)584 583 return ScalarEvent(None, None, (True, False), '', mark, mark) 584 -
pyyaml/trunk/lib3/yaml/reader.py
r323 r328 18 18 __all__ = ['Reader', 'ReaderError'] 19 19 20 from error import YAMLError, Mark20 from .error import YAMLError, Mark 21 21 22 22 import codecs, re 23 24 # Unfortunately, codec functions in Python 2.3 does not support the `finish`25 # arguments, so we have to write our own wrappers.26 27 try:28 codecs.utf_8_decode('', 'strict', False)29 from codecs import utf_8_decode, utf_16_le_decode, utf_16_be_decode30 31 except TypeError:32 33 def utf_16_le_decode(data, errors, finish=False):34 if not finish and len(data) % 2 == 1:35 data = data[:-1]36 return codecs.utf_16_le_decode(data, errors)37 38 def utf_16_be_decode(data, errors, finish=False):39 if not finish and len(data) % 2 == 1:40 data = data[:-1]41 return codecs.utf_16_be_decode(data, errors)42 43 def utf_8_decode(data, errors, finish=False):44 if not finish:45 # We are trying to remove a possible incomplete multibyte character46 # from the suffix of the data.47 # The first byte of a multi-byte sequence is in the range 0xc0 to 0xfd.48 # All further bytes are in the range 0x80 to 0xbf.49 # UTF-8 encoded UCS characters may be up to six bytes long.50 count = 051 while count < 5 and count < len(data) \52 and '\x80' <= data[-count-1] <= '\xBF':53 count -= 154 if count < 5 and count < len(data) \55 and '\xC0' <= data[-count-1] <= '\xFD':56 data = data[:-count-1]57 return codecs.utf_8_decode(data, errors)58 23 59 24 class ReaderError(YAMLError): … … 67 32 68 33 def __str__(self): 69 if isinstance(self.character, str):34 if isinstance(self.character, bytes): 70 35 return "'%s' codec can't decode byte #x%02x: %s\n" \ 71 36 " in \"%s\", position %d" \ … … 80 45 class Reader(object): 81 46 # Reader: 82 # - determines the data encoding and converts it to unicode,47 # - determines the data encoding and converts it to a unicode string, 83 48 # - checks if characters are in allowed range, 84 49 # - adds '\0' to the end. 85 50 86 51 # Reader accepts 52 # - a `bytes` object, 87 53 # - a `str` object, 88 # - a `unicode` object,89 54 # - a file-like object with its `read` method returning `str`, 90 55 # - a file-like object with its `read` method returning `unicode`. … … 97 62 self.stream_pointer = 0 98 63 self.eof = True 99 self.buffer = u''64 self.buffer = '' 100 65 self.pointer = 0 101 66 self.raw_buffer = None … … 105 70 self.line = 0 106 71 self.column = 0 107 if isinstance(stream, unicode):72 if isinstance(stream, str): 108 73 self.name = "<unicode string>" 109 74 self.check_printable(stream) 110 self.buffer = stream+ u'\0'111 elif isinstance(stream, str):112 self.name = "< string>"75 self.buffer = stream+'\0' 76 elif isinstance(stream, bytes): 77 self.name = "<byte string>" 113 78 self.raw_buffer = stream 114 79 self.determine_encoding() … … 117 82 self.name = getattr(stream, 'name', "<file>") 118 83 self.eof = False 119 self.raw_buffer = ''84 self.raw_buffer = None 120 85 self.determine_encoding() 121 86 … … 139 104 self.pointer += 1 140 105 self.index += 1 141 if ch in u'\n\x85\u2028\u2029' \142 or (ch == u'\r' and self.buffer[self.pointer] != u'\n'):106 if ch in '\n\x85\u2028\u2029' \ 107 or (ch == '\r' and self.buffer[self.pointer] != '\n'): 143 108 self.line += 1 144 109 self.column = 0 145 elif ch != u'\uFEFF':110 elif ch != '\uFEFF': 146 111 self.column += 1 147 112 length -= 1 … … 156 121 157 122 def determine_encoding(self): 158 while not self.eof and len(self.raw_buffer) < 2:123 while not self.eof and (self.raw_buffer is None or len(self.raw_buffer) < 2): 159 124 self.update_raw() 160 if not isinstance(self.raw_buffer, unicode):125 if isinstance(self.raw_buffer, bytes): 161 126 if self.raw_buffer.startswith(codecs.BOM_UTF16_LE): 162 self.raw_decode = utf_16_le_decode127 self.raw_decode = codecs.utf_16_le_decode 163 128 self.encoding = 'utf-16-le' 164 129 elif self.raw_buffer.startswith(codecs.BOM_UTF16_BE): 165 self.raw_decode = utf_16_be_decode130 self.raw_decode = codecs.utf_16_be_decode 166 131 self.encoding = 'utf-16-be' 167 132 else: 168 self.raw_decode = utf_8_decode133 self.raw_decode = codecs.utf_8_decode 169 134 self.encoding = 'utf-8' 170 135 self.update(1) 171 136 172 NON_PRINTABLE = re.compile( u'[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD]')137 NON_PRINTABLE = re.compile('[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD]') 173 138 def check_printable(self, data): 174 139 match = self.NON_PRINTABLE.search(data) … … 191 156 data, converted = self.raw_decode(self.raw_buffer, 192 157 'strict', self.eof) 193 except UnicodeDecodeError ,exc:158 except UnicodeDecodeError as exc: 194 159 character = exc.object[exc.start] 195 160 if self.stream is not None: … … 206 171 self.raw_buffer = self.raw_buffer[converted:] 207 172 if self.eof: 208 self.buffer += u'\0'173 self.buffer += '\0' 209 174 self.raw_buffer = None 210 175 break 211 176 212 def update_raw(self, size= 1024):177 def update_raw(self, size=4096): 213 178 data = self.stream.read(size) 214 if data: 179 if self.raw_buffer is None: 180 self.raw_buffer = data 181 else: 215 182 self.raw_buffer += data 216 self.stream_pointer += len(data)217 else:183 self.stream_pointer += len(data) 184 if not data: 218 185 self.eof = True 219 186 -
pyyaml/trunk/lib3/yaml/representer.py
r248 r328 3 3 'RepresenterError'] 4 4 5 from error import * 6 from nodes import * 7 8 import datetime 9 10 try: 11 set 12 except NameError: 13 from sets import Set as set 14 15 import sys, copy_reg, types 5 from .error import * 6 from .nodes import * 7 8 import datetime, sys, copyreg, types, base64 16 9 17 10 class RepresenterError(YAMLError): 18 11 pass 19 12 20 class BaseRepresenter (object):13 class BaseRepresenter: 21 14 22 15 yaml_representers = {} … … 36 29 self.object_keeper = [] 37 30 self.alias_key = None 38 39 def get_classobj_bases(self, cls):40 bases = [cls]41 for base in cls.__bases__:42 bases.extend(self.get_classobj_bases(base))43 return bases44 31 45 32 def represent_data(self, data): … … 57 44 self.object_keeper.append(data) 58 45 data_types = type(data).__mro__ 59 if type(data) is types.InstanceType:60 data_types = self.get_classobj_bases(data.__class__)+list(data_types)61 46 if data_types[0] in self.yaml_representers: 62 47 node = self.yaml_representers[data_types[0]](self, data) … … 72 57 node = self.yaml_representers[None](self, data) 73 58 else: 74 node = ScalarNode(None, unicode(data))59 node = ScalarNode(None, str(data)) 75 60 #if alias_key is not None: 76 61 # self.represented_objects[alias_key] = node 77 62 return node 78 63 64 @classmethod 79 65 def add_representer(cls, data_type, representer): 80 66 if not 'yaml_representers' in cls.__dict__: 81 67 cls.yaml_representers = cls.yaml_representers.copy() 82 68 cls.yaml_representers[data_type] = representer 83 add_representer = classmethod(add_representer) 84 69 70 @classmethod 85 71 def add_multi_representer(cls, data_type, representer): 86 72 if not 'yaml_multi_representers' in cls.__dict__: 87 73 cls.yaml_multi_representers = cls.yaml_multi_representers.copy() 88 74 cls.yaml_multi_representers[data_type] = representer 89 add_multi_representer = classmethod(add_multi_representer)90 75 91 76 def represent_scalar(self, tag, value, style=None): … … 122 107 best_style = True 123 108 if hasattr(mapping, 'items'): 124 mapping = mapping.items() 125 mapping.sort() 109 mapping = list(mapping.items()) 110 try: 111 mapping = sorted(mapping) 112 except TypeError: 113 pass 126 114 for item_key, item_value in mapping: 127 115 node_key = self.represent_data(item_key) … … 147 135 if data in [None, ()]: 148 136 return True 149 if isinstance(data, (str, unicode, bool, int, float)):137 if isinstance(data, (str, bytes, bool, int, float)): 150 138 return True 151 139 152 140 def represent_none(self, data): 153 return self.represent_scalar(u'tag:yaml.org,2002:null', 154 u'null') 141 return self.represent_scalar('tag:yaml.org,2002:null', 'null') 155 142 156 143 def represent_str(self, data): 157 tag = None 158 style = None 159 try: 160 data = unicode(data, 'ascii') 161 tag = u'tag:yaml.org,2002:str' 162 except UnicodeDecodeError: 163 try: 164 data = unicode(data, 'utf-8') 165 tag = u'tag:yaml.org,2002:str' 166 except UnicodeDecodeError: 167 data = data.encode('base64') 168 tag = u'tag:yaml.org,2002:binary' 169 style = '|' 170 return self.represent_scalar(tag, data, style=style) 171 172 def represent_unicode(self, data): 173 return self.represent_scalar(u'tag:yaml.org,2002:str', data) 144 return self.represent_scalar('tag:yaml.org,2002:str', data) 145 146 def represent_binary(self, data): 147 data = base64.encodestring(data).decode('ascii') 148 return self.represent_scalar('tag:yaml.org,2002:binary', data, style='|') 174 149 175 150 def represent_bool(self, data): 176 151 if data: 177 value = u'true'178 else: 179 value = u'false'180 return self.represent_scalar( u'tag:yaml.org,2002:bool', value)152 value = 'true' 153 else: 154 value = 'false' 155 return self.represent_scalar('tag:yaml.org,2002:bool', value) 181 156 182 157 def represent_int(self, data): 183 return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data)) 184 185 def represent_long(self, data): 186 return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data)) 158 return self.represent_scalar('tag:yaml.org,2002:int', str(data)) 187 159 188 160 inf_value = 1e300 … … 192 164 def represent_float(self, data): 193 165 if data != data or (data == 0.0 and data == 1.0): 194 value = u'.nan'166 value = '.nan' 195 167 elif data == self.inf_value: 196 value = u'.inf'168 value = '.inf' 197 169 elif data == -self.inf_value: 198 value = u'-.inf'199 else: 200 value = unicode(repr(data)).lower()170 value = '-.inf' 171 else: 172 value = repr(data).lower() 201 173 # Note that in some cases `repr(data)` represents a float number 202 174 # without the decimal parts. For instance: … … 206 178 # to the definition of the `!!float` tag. We fix this by adding 207 179 # '.0' before the 'e' symbol. 208 if u'.' not in value and u'e' in value:209 value = value.replace( u'e', u'.0e', 1)210 return self.represent_scalar( u'tag:yaml.org,2002:float', value)180 if '.' not in value and 'e' in value: 181 value = value.replace('e', '.0e', 1) 182 return self.represent_scalar('tag:yaml.org,2002:float', value) 211 183 212 184 def represent_list(self, data): … … 218 190 # break 219 191 #if not pairs: 220 return self.represent_sequence( u'tag:yaml.org,2002:seq', data)192 return self.represent_sequence('tag:yaml.org,2002:seq', data) 221 193 #value = [] 222 194 #for item_key, item_value in data: … … 226 198 227 199 def represent_dict(self, data): 228 return self.represent_mapping( u'tag:yaml.org,2002:map', data)200 return self.represent_mapping('tag:yaml.org,2002:map', data) 229 201 230 202 def represent_set(self, data): … … 232 204 for key in data: 233 205 value[key] = None 234 return self.represent_mapping( u'tag:yaml.org,2002:set', value)206 return self.represent_mapping('tag:yaml.org,2002:set', value) 235 207 236 208 def represent_date(self, data): 237 value = unicode(data.isoformat())238 return self.represent_scalar( u'tag:yaml.org,2002:timestamp', value)209 value = data.isoformat() 210 return self.represent_scalar('tag:yaml.org,2002:timestamp', value) 239 211 240 212 def represent_datetime(self, data): 241 value = unicode(data.isoformat(' '))242 return self.represent_scalar( u'tag:yaml.org,2002:timestamp', value)213 value = data.isoformat(' ') 214 return self.represent_scalar('tag:yaml.org,2002:timestamp', value) 243 215 244 216 def represent_yaml_object(self, tag, data, cls, flow_style=None): … … 258 230 SafeRepresenter.represent_str) 259 231 260 SafeRepresenter.add_representer( unicode,261 SafeRepresenter.represent_ unicode)232 SafeRepresenter.add_representer(bytes, 233 SafeRepresenter.represent_binary) 262 234 263 235 SafeRepresenter.add_representer(bool, … … 267 239 SafeRepresenter.represent_int) 268 240 269 SafeRepresenter.add_representer(long,270 SafeRepresenter.represent_long)271 272 241 SafeRepresenter.add_representer(float, 273 242 SafeRepresenter.represent_float) … … 287 256 SafeRepresenter.add_representer(datetime.date, 288 257 SafeRepresenter.represent_date) 258 289 259 SafeRepresenter.add_representer(datetime.datetime, 290 260 SafeRepresenter.represent_datetime) … … 295 265 class Representer(SafeRepresenter): 296 266 297 def represent_str(self, data):298 tag = None299 style = None300 try:301 data = unicode(data, 'ascii')302 tag = u'tag:yaml.org,2002:str'303 except UnicodeDecodeError:304 try:305 data = unicode(data, 'utf-8')306 tag = u'tag:yaml.org,2002:python/str'307 except UnicodeDecodeError:308 data = data.encode('base64')309 tag = u'tag:yaml.org,2002:binary'310 style = '|'311 return self.represent_scalar(tag, data, style=style)312 313 def represent_unicode(self, data):314 tag = None315 try:316 data.encode('ascii')317 tag = u'tag:yaml.org,2002:python/unicode'318 except UnicodeEncodeError:319 tag = u'tag:yaml.org,2002:str'320 return self.represent_scalar(tag, data)321 322 def represent_long(self, data):323 tag = u'tag:yaml.org,2002:int'324 if int(data) is not data:325 tag = u'tag:yaml.org,2002:python/long'326 return self.represent_scalar(tag, unicode(data))327 328 267 def represent_complex(self, data): 329 268 if data.imag == 0.0: 330 data = u'%r' % data.real269 data = '%r' % data.real 331 270 elif data.real == 0.0: 332 data = u'%rj' % data.imag271 data = '%rj' % data.imag 333 272 elif data.imag > 0: 334 data = u'%r+%rj' % (data.real, data.imag)335 else: 336 data = u'%r%rj' % (data.real, data.imag)337 return self.represent_scalar( u'tag:yaml.org,2002:python/complex', data)273 data = '%r+%rj' % (data.real, data.imag) 274 else: 275 data = '%r%rj' % (data.real, data.imag) 276 return self.represent_scalar('tag:yaml.org,2002:python/complex', data) 338 277 339 278 def represent_tuple(self, data): 340 return self.represent_sequence( u'tag:yaml.org,2002:python/tuple', data)279 return self.represent_sequence('tag:yaml.org,2002:python/tuple', data) 341 280 342 281 def represent_name(self, data): 343 name = u'%s.%s' % (data.__module__, data.__name__)344 return self.represent_scalar( u'tag:yaml.org,2002:python/name:'+name, u'')282 name = '%s.%s' % (data.__module__, data.__name__) 283 return self.represent_scalar('tag:yaml.org,2002:python/name:'+name, '') 345 284 346 285 def represent_module(self, data): 347 286 return self.represent_scalar( 348 u'tag:yaml.org,2002:python/module:'+data.__name__, u'') 349 350 def represent_instance(self, data): 351 # For instances of classic classes, we use __getinitargs__ and 352 # __getstate__ to serialize the data. 353 354 # If data.__getinitargs__ exists, the object must be reconstructed by 355 # calling cls(**args), where args is a tuple returned by 356 # __getinitargs__. Otherwise, the cls.__init__ method should never be 357 # called and the class instance is created by instantiating a trivial 358 # class and assigning to the instance's __class__ variable. 359 360 # If data.__getstate__ exists, it returns the state of the object. 361 # Otherwise, the state of the object is data.__dict__. 362 363 # We produce either a !!python/object or !!python/object/new node. 364 # If data.__getinitargs__ does not exist and state is a dictionary, we 365 # produce a !!python/object node . Otherwise we produce a 366 # !!python/object/new node. 367 368 cls = data.__class__ 369 class_name = u'%s.%s' % (cls.__module__, cls.__name__) 370 args = None 371 state = None 372 if hasattr(data, '__getinitargs__'): 373 args = list(data.__getinitargs__()) 374 if hasattr(data, '__getstate__'): 375 state = data.__getstate__() 376 else: 377 state = data.__dict__ 378 if args is None and isinstance(state, dict): 379 return self.represent_mapping( 380 u'tag:yaml.org,2002:python/object:'+class_name, state) 381 if isinstance(state, dict) and not state: 382 return self.represent_sequence( 383 u'tag:yaml.org,2002:python/object/new:'+class_name, args) 384 value = {} 385 if args: 386 value['args'] = args 387 value['state'] = state 388 return self.represent_mapping( 389 u'tag:yaml.org,2002:python/object/new:'+class_name, value) 287 'tag:yaml.org,2002:python/module:'+data.__name__, '') 390 288 391 289 def represent_object(self, data): … … 407 305 408 306 cls = type(data) 409 if cls in copy _reg.dispatch_table:410 reduce = copy _reg.dispatch_table[cls](data)307 if cls in copyreg.dispatch_table: 308 reduce = copyreg.dispatch_table[cls](data) 411 309 elif hasattr(data, '__reduce_ex__'): 412 310 reduce = data.__reduce_ex__(2) … … 427 325 function = args[0] 428 326 args = args[1:] 429 tag = u'tag:yaml.org,2002:python/object/new:'327 tag = 'tag:yaml.org,2002:python/object/new:' 430 328 newobj = True 431 329 else: 432 tag = u'tag:yaml.org,2002:python/object/apply:'330 tag = 'tag:yaml.org,2002:python/object/apply:' 433 331 newobj = False 434 function_name = u'%s.%s' % (function.__module__, function.__name__)332 function_name = '%s.%s' % (function.__module__, function.__name__) 435 333 if not args and not listitems and not dictitems \ 436 334 and isinstance(state, dict) and newobj: 437 335 return self.represent_mapping( 438 u'tag:yaml.org,2002:python/object:'+function_name, state)336 'tag:yaml.org,2002:python/object:'+function_name, state) 439 337 if not listitems and not dictitems \ 440 338 and isinstance(state, dict) and not state: … … 451 349 return self.represent_mapping(tag+function_name, value) 452 350 453 Representer.add_representer(str,454 Representer.represent_str)455 456 Representer.add_representer(unicode,457 Representer.represent_unicode)458 459 Representer.add_representer(long,460 Representer.represent_long)461 462 351 Representer.add_representer(complex, 463 352 Representer.represent_complex) … … 469 358 Representer.represent_name) 470 359 471 Representer.add_representer(types.ClassType,472 Representer.represent_name)473 474 360 Representer.add_representer(types.FunctionType, 475 361 Representer.represent_name) … … 481 367 Representer.represent_module) 482 368 483 Representer.add_multi_representer(types.InstanceType,484 Representer.represent_instance)485 486 369 Representer.add_multi_representer(object, 487 370 Representer.represent_object) -
pyyaml/trunk/lib3/yaml/resolver.py
r260 r328 2 2 __all__ = ['BaseResolver', 'Resolver'] 3 3 4 from error import *5 from nodes import *4 from .error import * 5 from .nodes import * 6 6 7 7 import re … … 10 10 pass 11 11 12 class BaseResolver (object):13 14 DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'15 DEFAULT_SEQUENCE_TAG = u'tag:yaml.org,2002:seq'16 DEFAULT_MAPPING_TAG = u'tag:yaml.org,2002:map'12 class BaseResolver: 13 14 DEFAULT_SCALAR_TAG = 'tag:yaml.org,2002:str' 15 DEFAULT_SEQUENCE_TAG = 'tag:yaml.org,2002:seq' 16 DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map' 17 17 18 18 yaml_implicit_resolvers = {} … … 23 23 self.resolver_prefix_paths = [] 24 24 25 @classmethod 25 26 def add_implicit_resolver(cls, tag, regexp, first): 26 27 if not 'yaml_implicit_resolvers' in cls.__dict__: … … 30 31 for ch in first: 31 32 cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp)) 32 add_implicit_resolver = classmethod(add_implicit_resolver) 33 33 34 @classmethod 34 35 def add_path_resolver(cls, tag, path, kind=None): 35 36 # Note: `add_path_resolver` is experimental. The API could be changed. … … 67 68 node_check = MappingNode 68 69 elif node_check not in [ScalarNode, SequenceNode, MappingNode] \ 69 and not isinstance(node_check, basestring)\70 and not isinstance(node_check, str) \ 70 71 and node_check is not None: 71 72 raise ResolverError("Invalid node checker: %s" % node_check) 72 if not isinstance(index_check, ( basestring, int))\73 if not isinstance(index_check, (str, int)) \ 73 74 and index_check is not None: 74 75 raise ResolverError("Invalid index checker: %s" % index_check) … … 84 85 raise ResolverError("Invalid node kind: %s" % kind) 85 86 cls.yaml_path_resolvers[tuple(new_path), kind] = tag 86 add_path_resolver = classmethod(add_path_resolver)87 87 88 88 def descend_resolver(self, current_node, current_index): … … 118 118 current_node, current_index): 119 119 node_check, index_check = path[depth-1] 120 if isinstance(node_check, basestring):120 if isinstance(node_check, str): 121 121 if current_node.tag != node_check: 122 122 return … … 129 129 and current_index is None: 130 130 return 131 if isinstance(index_check, basestring):131 if isinstance(index_check, str): 132 132 if not (isinstance(current_index, ScalarNode) 133 133 and index_check == current_index.value): … … 140 140 def resolve(self, kind, value, implicit): 141 141 if kind is ScalarNode and implicit[0]: 142 if value == u'':143 resolvers = self.yaml_implicit_resolvers.get( u'', [])142 if value == '': 143 resolvers = self.yaml_implicit_resolvers.get('', []) 144 144 else: 145 145 resolvers = self.yaml_implicit_resolvers.get(value[0], []) … … 166 166 167 167 Resolver.add_implicit_resolver( 168 u'tag:yaml.org,2002:bool',169 re.compile( ur'''^(?:yes|Yes|YES|no|No|NO168 'tag:yaml.org,2002:bool', 169 re.compile(r'''^(?:yes|Yes|YES|no|No|NO 170 170 |true|True|TRUE|false|False|FALSE 171 171 |on|On|ON|off|Off|OFF)$''', re.X), 172 list( u'yYnNtTfFoO'))173 174 Resolver.add_implicit_resolver( 175 u'tag:yaml.org,2002:float',176 re.compile( ur'''^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)?172 list('yYnNtTfFoO')) 173 174 Resolver.add_implicit_resolver( 175 'tag:yaml.org,2002:float', 176 re.compile(r'''^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)? 177 177 |\.[0-9_]+(?:[eE][-+][0-9]+)? 178 178 |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]* 179 179 |[-+]?\.(?:inf|Inf|INF) 180 180 |\.(?:nan|NaN|NAN))$''', re.X), 181 list( u'-+0123456789.'))182 183 Resolver.add_implicit_resolver( 184 u'tag:yaml.org,2002:int',185 re.compile( ur'''^(?:[-+]?0b[0-1_]+181 list('-+0123456789.')) 182 183 Resolver.add_implicit_resolver( 184 'tag:yaml.org,2002:int', 185 re.compile(r'''^(?:[-+]?0b[0-1_]+ 186 186 |[-+]?0[0-7_]+ 187 187 |[-+]?(?:0|[1-9][0-9_]*) 188 188 |[-+]?0x[0-9a-fA-F_]+ 189 189 |[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X), 190 list( u'-+0123456789'))191 192 Resolver.add_implicit_resolver( 193 u'tag:yaml.org,2002:merge',194 re.compile( ur'^(?:<<)$'),190 list('-+0123456789')) 191 192 Resolver.add_implicit_resolver( 193 'tag:yaml.org,2002:merge', 194 re.compile(r'^(?:<<)$'), 195 195 ['<']) 196 196 197 197 Resolver.add_implicit_resolver( 198 u'tag:yaml.org,2002:null',199 re.compile( ur'''^(?: ~198 'tag:yaml.org,2002:null', 199 re.compile(r'''^(?: ~ 200 200 |null|Null|NULL 201 201 | )$''', re.X), 202 [ u'~', u'n', u'N', u''])203 204 Resolver.add_implicit_resolver( 205 u'tag:yaml.org,2002:timestamp',206 re.compile( ur'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]202 ['~', 'n', 'N', '']) 203 204 Resolver.add_implicit_resolver( 205 'tag:yaml.org,2002:timestamp', 206 re.compile(r'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] 207 207 |[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]? 208 208 (?:[Tt]|[ \t]+)[0-9][0-9]? 209 209 :[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)? 210 210 (?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X), 211 list( u'0123456789'))212 213 Resolver.add_implicit_resolver( 214 u'tag:yaml.org,2002:value',215 re.compile( ur'^(?:=)$'),211 list('0123456789')) 212 213 Resolver.add_implicit_resolver( 214 'tag:yaml.org,2002:value', 215 re.compile(r'^(?:=)$'), 216 216 ['=']) 217 217 … … 219 219 # because plain scalars cannot start with '!', '&', or '*'. 220 220 Resolver.add_implicit_resolver( 221 u'tag:yaml.org,2002:yaml',222 re.compile( ur'^(?:!|&|\*)$'),223 list( u'!&*'))224 221 'tag:yaml.org,2002:yaml', 222 re.compile(r'^(?:!|&|\*)$'), 223 list('!&*')) 224 -
pyyaml/trunk/lib3/yaml/scanner.py
r222 r328 27 27 __all__ = ['Scanner', 'ScannerError'] 28 28 29 from error import MarkedYAMLError30 from tokens import *29 from .error import MarkedYAMLError 30 from .tokens import * 31 31 32 32 class ScannerError(MarkedYAMLError): 33 33 pass 34 34 35 class SimpleKey (object):35 class SimpleKey: 36 36 # See below simple keys treatment. 37 37 … … 44 44 self.mark = mark 45 45 46 class Scanner (object):46 class Scanner: 47 47 48 48 def __init__(self): … … 167 167 168 168 # Is it the end of stream? 169 if ch == u'\0':169 if ch == '\0': 170 170 return self.fetch_stream_end() 171 171 172 172 # Is it a directive? 173 if ch == u'%' and self.check_directive():173 if ch == '%' and self.check_directive(): 174 174 return self.fetch_directive() 175 175 176 176 # Is it the document start? 177 if ch == u'-' and self.check_document_start():177 if ch == '-' and self.check_document_start(): 178 178 return self.fetch_document_start() 179 179 180 180 # Is it the document end? 181 if ch == u'.' and self.check_document_end():181 if ch == '.' and self.check_document_end(): 182 182 return self.fetch_document_end() 183 183 184 184 # TODO: support for BOM within a stream. 185 #if ch == u'\uFEFF':185 #if ch == '\uFEFF': 186 186 # return self.fetch_bom() <-- issue BOMToken 187 187 … … 189 189 190 190 # Is it the flow sequence start indicator? 191 if ch == u'[':191 if ch == '[': 192 192 return self.fetch_flow_sequence_start() 193 193 194 194 # Is it the flow mapping start indicator? 195 if ch == u'{':195 if ch == '{': 196 196 return self.fetch_flow_mapping_start() 197 197 198 198 # Is it the flow sequence end indicator? 199 if ch == u']':199 if ch == ']': 200 200 return self.fetch_flow_sequence_end() 201 201 202 202 # Is it the flow mapping end indicator? 203 if ch == u'}':203 if ch == '}': 204 204 return self.fetch_flow_mapping_end() 205 205 206 206 # Is it the flow entry indicator? 207 if ch == u',':207 if ch == ',': 208 208 return self.fetch_flow_entry() 209 209 210 210 # Is it the block entry indicator? 211 if ch == u'-' and self.check_block_entry():211 if ch == '-' and self.check_block_entry(): 212 212 return self.fetch_block_entry() 213 213 214 214 # Is it the key indicator? 215 if ch == u'?' and self.check_key():215 if ch == '?' and self.check_key(): 216 216 return self.fetch_key() 217 217 218 218 # Is it the value indicator? 219 if ch == u':' and self.check_value():219 if ch == ':' and self.check_value(): 220 220 return self.fetch_value() 221 221 222 222 # Is it an alias? 223 if ch == u'*':223 if ch == '*': 224 224 return self.fetch_alias() 225 225 226 226 # Is it an anchor? 227 if ch == u'&':227 if ch == '&': 228 228 return self.fetch_anchor() 229 229 230 230 # Is it a tag? 231 if ch == u'!':231 if ch == '!': 232 232 return self.fetch_tag() 233 233 234 234 # Is it a literal scalar? 235 if ch == u'|' and not self.flow_level:235 if ch == '|' and not self.flow_level: 236 236 return self.fetch_literal() 237 237 238 238 # Is it a folded scalar? 239 if ch == u'>' and not self.flow_level:239 if ch == '>' and not self.flow_level: 240 240 return self.fetch_folded() 241 241 242 242 # Is it a single quoted scalar? 243 if ch == u'\'':243 if ch == '\'': 244 244 return self.fetch_single() 245 245 246 246 # Is it a double quoted scalar? 247 if ch == u'\"':247 if ch == '\"': 248 248 return self.fetch_double() 249 249 … … 254 254 # No? It's an error. Let's produce a nice error message. 255 255 raise ScannerError("while scanning for the next token", None, 256 "found character %r that cannot start any token" 257 % ch.encode('utf-8'),self.get_mark())256 "found character %r that cannot start any token" % ch, 257 self.get_mark()) 258 258 259 259 # Simple keys treatment. … … 281 281 # Disabling this procedure will allow simple keys of any length and 282 282 # height (may cause problems if indentation is broken though). 283 for level in self.possible_simple_keys.keys():283 for level in list(self.possible_simple_keys): 284 284 key = self.possible_simple_keys[level] 285 285 if key.line != self.line \ … … 692 692 # DOCUMENT-START: ^ '---' (' '|'\n') 693 693 if self.column == 0: 694 if self.prefix(3) == u'---' \695 and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':694 if self.prefix(3) == '---' \ 695 and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029': 696 696 return True 697 697 … … 700 700 # DOCUMENT-END: ^ '...' (' '|'\n') 701 701 if self.column == 0: 702 if self.prefix(3) == u'...' \703 and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':702 if self.prefix(3) == '...' \ 703 and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029': 704 704 return True 705 705 … … 707 707 708 708 # BLOCK-ENTRY: '-' (' '|'\n') 709 return self.peek(1) in u'\0 \t\r\n\x85\u2028\u2029'709 return self.peek(1) in '\0 \t\r\n\x85\u2028\u2029' 710 710 711 711 def check_key(self): … … 717 717 # KEY(block context): '?' (' '|'\n') 718 718 else: 719 return self.peek(1) in u'\0 \t\r\n\x85\u2028\u2029'719 return self.peek(1) in '\0 \t\r\n\x85\u2028\u2029' 720 720 721 721 def check_value(self): … … 727 727 # VALUE(block context): ':' (' '|'\n') 728 728 else: 729 return self.peek(1) in u'\0 \t\r\n\x85\u2028\u2029'729 return self.peek(1) in '\0 \t\r\n\x85\u2028\u2029' 730 730 731 731 def check_plain(self): … … 744 744 # independent. 745 745 ch = self.peek() 746 return ch not in u'\0 \t\r\n\x85\u2028\u2029-?:,[]{}#&*!|>\'\"%@`' \747 or (self.peek(1) not in u'\0 \t\r\n\x85\u2028\u2029'748 and (ch == u'-' or (not self.flow_level and ch in u'?:')))746 return ch not in '\0 \t\r\n\x85\u2028\u2029-?:,[]{}#&*!|>\'\"%@`' \ 747 or (self.peek(1) not in '\0 \t\r\n\x85\u2028\u2029' 748 and (ch == '-' or (not self.flow_level and ch in '?:'))) 749 749 750 750 # Scanners. … … 770 770 # Scanners for block, flow, and plain scalars need to be modified. 771 771 772 if self.index == 0 and self.peek() == u'\uFEFF':772 if self.index == 0 and self.peek() == '\uFEFF': 773 773 self.forward() 774 774 found = False 775 775 while not found: 776 while self.peek() == u' ':776 while self.peek() == ' ': 777 777 self.forward() 778 if self.peek() == u'#':779 while self.peek() not in u'\0\r\n\x85\u2028\u2029':778 if self.peek() == '#': 779 while self.peek() not in '\0\r\n\x85\u2028\u2029': 780 780 self.forward() 781 781 if self.scan_line_break(): … … 791 791 name = self.scan_directive_name(start_mark) 792 792 value = None 793 if name == u'YAML':793 if name == 'YAML': 794 794 value = self.scan_yaml_directive_value(start_mark) 795 795 end_mark = self.get_mark() 796 elif name == u'TAG':796 elif name == 'TAG': 797 797 value = self.scan_tag_directive_value(start_mark) 798 798 end_mark = self.get_mark() 799 799 else: 800 800 end_mark = self.get_mark() 801 while self.peek() not in u'\0\r\n\x85\u2028\u2029':801 while self.peek() not in '\0\r\n\x85\u2028\u2029': 802 802 self.forward() 803 803 self.scan_directive_ignored_line(start_mark) … … 808 808 length = 0 809 809 ch = self.peek(length) 810 while u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \811 or ch in u'-_':810 while '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 811 or ch in '-_': 812 812 length += 1 813 813 ch = self.peek(length) … … 815 815 raise ScannerError("while scanning a directive", start_mark, 816 816 "expected alphabetic or numeric character, but found %r" 817 % ch .encode('utf-8'), self.get_mark())817 % ch, self.get_mark()) 818 818 value = self.prefix(length) 819 819 self.forward(length) 820 820 ch = self.peek() 821 if ch not in u'\0 \r\n\x85\u2028\u2029':821 if ch not in '\0 \r\n\x85\u2028\u2029': 822 822 raise ScannerError("while scanning a directive", start_mark, 823 823 "expected alphabetic or numeric character, but found %r" 824 % ch .encode('utf-8'), self.get_mark())824 % ch, self.get_mark()) 825 825 return value 826 826 827 827 def scan_yaml_directive_value(self, start_mark): 828 828 # See the specification for details. 829 while self.peek() == u' ':829 while self.peek() == ' ': 830 830 self.forward() 831 831 major = self.scan_yaml_directive_number(start_mark) 832 832 if self.peek() != '.': 833 833 raise ScannerError("while scanning a directive", start_mark, 834 "expected a digit or '.', but found %r" 835 % self.peek().encode('utf-8'), 834 "expected a digit or '.', but found %r" % self.peek(), 836 835 self.get_mark()) 837 836 self.forward() 838 837 minor = self.scan_yaml_directive_number(start_mark) 839 if self.peek() not in u'\0 \r\n\x85\u2028\u2029':838 if self.peek() not in '\0 \r\n\x85\u2028\u2029': 840 839 raise ScannerError("while scanning a directive", start_mark, 841 "expected a digit or ' ', but found %r" 842 % self.peek().encode('utf-8'), 840 "expected a digit or ' ', but found %r" % self.peek(), 843 841 self.get_mark()) 844 842 return (major, minor) … … 847 845 # See the specification for details. 848 846 ch = self.peek() 849 if not ( u'0' <= ch <= '9'):847 if not ('0' <= ch <= '9'): 850 848 raise ScannerError("while scanning a directive", start_mark, 851 "expected a digit, but found %r" % ch.encode('utf-8'), 852 self.get_mark()) 849 "expected a digit, but found %r" % ch, self.get_mark()) 853 850 length = 0 854 while u'0' <= self.peek(length) <= u'9':851 while '0' <= self.peek(length) <= '9': 855 852 length += 1 856 853 value = int(self.prefix(length)) … … 860 857 def scan_tag_directive_value(self, start_mark): 861 858 # See the specification for details. 862 while self.peek() == u' ':859 while self.peek() == ' ': 863 860 self.forward() 864 861 handle = self.scan_tag_directive_handle(start_mark) 865 while self.peek() == u' ':862 while self.peek() == ' ': 866 863 self.forward() 867 864 prefix = self.scan_tag_directive_prefix(start_mark) … … 872 869 value = self.scan_tag_handle('directive', start_mark) 873 870 ch = self.peek() 874 if ch != u' ':871 if ch != ' ': 875 872 raise ScannerError("while scanning a directive", start_mark, 876 "expected ' ', but found %r" % ch.encode('utf-8'), 877 self.get_mark()) 873 "expected ' ', but found %r" % ch, self.get_mark()) 878 874 return value 879 875 … … 882 878 value = self.scan_tag_uri('directive', start_mark) 883 879 ch = self.peek() 884 if ch not in u'\0 \r\n\x85\u2028\u2029':880 if ch not in '\0 \r\n\x85\u2028\u2029': 885 881 raise ScannerError("while scanning a directive", start_mark, 886 "expected ' ', but found %r" % ch.encode('utf-8'), 887 self.get_mark()) 882 "expected ' ', but found %r" % ch, self.get_mark()) 888 883 return value 889 884 890 885 def scan_directive_ignored_line(self, start_mark): 891 886 # See the specification for details. 892 while self.peek() == u' ':887 while self.peek() == ' ': 893 888 self.forward() 894 if self.peek() == u'#':895 while self.peek() not in u'\0\r\n\x85\u2028\u2029':889 if self.peek() == '#': 890 while self.peek() not in '\0\r\n\x85\u2028\u2029': 896 891 self.forward() 897 892 ch = self.peek() 898 if ch not in u'\0\r\n\x85\u2028\u2029':893 if ch not in '\0\r\n\x85\u2028\u2029': 899 894 raise ScannerError("while scanning a directive", start_mark, 900 895 "expected a comment or a line break, but found %r" 901 % ch .encode('utf-8'), self.get_mark())896 % ch, self.get_mark()) 902 897 self.scan_line_break() 903 898 … … 920 915 length = 0 921 916 ch = self.peek(length) 922 while u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \923 or ch in u'-_':917 while '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 918 or ch in '-_': 924 919 length += 1 925 920 ch = self.peek(length) … … 927 922 raise ScannerError("while scanning an %s" % name, start_mark, 928 923 "expected alphabetic or numeric character, but found %r" 929 % ch .encode('utf-8'), self.get_mark())924 % ch, self.get_mark()) 930 925 value = self.prefix(length) 931 926 self.forward(length) 932 927 ch = self.peek() 933 if ch not in u'\0 \t\r\n\x85\u2028\u2029?:,]}%@`':928 if ch not in '\0 \t\r\n\x85\u2028\u2029?:,]}%@`': 934 929 raise ScannerError("while scanning an %s" % name, start_mark, 935 930 "expected alphabetic or numeric character, but found %r" 936 % ch .encode('utf-8'), self.get_mark())931 % ch, self.get_mark()) 937 932 end_mark = self.get_mark() 938 933 return TokenClass(value, start_mark, end_mark) … … 942 937 start_mark = self.get_mark() 943 938 ch = self.peek(1) 944 if ch == u'<':939 if ch == '<': 945 940 handle = None 946 941 self.forward(2) 947 942 suffix = self.scan_tag_uri('tag', start_mark) 948 if self.peek() != u'>':943 if self.peek() != '>': 949 944 raise ScannerError("while parsing a tag", start_mark, 950 "expected '>', but found %r" % self.peek() .encode('utf-8'),945 "expected '>', but found %r" % self.peek(), 951 946 self.get_mark()) 952 947 self.forward() 953 elif ch in u'\0 \t\r\n\x85\u2028\u2029':948 elif ch in '\0 \t\r\n\x85\u2028\u2029': 954 949 handle = None 955 suffix = u'!'950 suffix = '!' 956 951 self.forward() 957 952 else: 958 953 length = 1 959 954 use_handle = False 960 while ch not in u'\0 \r\n\x85\u2028\u2029':961 if ch == u'!':955 while ch not in '\0 \r\n\x85\u2028\u2029': 956 if ch == '!': 962 957 use_handle = True 963 958 break 964 959 length += 1 965 960 ch = self.peek(length) 966 handle = u'!'961 handle = '!' 967 962 if use_handle: 968 963 handle = self.scan_tag_handle('tag', start_mark) 969 964 else: 970 handle = u'!'965 handle = '!' 971 966 self.forward() 972 967 suffix = self.scan_tag_uri('tag', start_mark) 973 968 ch = self.peek() 974 if ch not in u'\0 \r\n\x85\u2028\u2029':969 if ch not in '\0 \r\n\x85\u2028\u2029': 975 970 raise ScannerError("while scanning a tag", start_mark, 976 "expected ' ', but found %r" % ch.encode('utf-8'), 977 self.get_mark()) 971 "expected ' ', but found %r" % ch, self.get_mark()) 978 972 value = (handle, suffix) 979 973 end_mark = self.get_mark() … … 1006 1000 indent = min_indent+increment-1 1007 1001 breaks, end_mark = self.scan_block_scalar_breaks(indent) 1008 line_break = u''1002 line_break = '' 1009 1003 1010 1004 # Scan the inner part of the block scalar. 1011 while self.column == indent and self.peek() != u'\0':1005 while self.column == indent and self.peek() != '\0': 1012 1006 chunks.extend(breaks) 1013 leading_non_space = self.peek() not in u' \t'1007 leading_non_space = self.peek() not in ' \t' 1014 1008 length = 0 1015 while self.peek(length) not in u'\0\r\n\x85\u2028\u2029':1009 while self.peek(length) not in '\0\r\n\x85\u2028\u2029': 1016 1010 length += 1 1017 1011 chunks.append(self.prefix(length)) … … 1019 1013 line_break = self.scan_line_break() 1020 1014 breaks, end_mark = self.scan_block_scalar_breaks(indent) 1021 if self.column == indent and self.peek() != u'\0':1015 if self.column == indent and self.peek() != '\0': 1022 1016 1023 1017 # Unfortunately, folding rules are ambiguous. … … 1025 1019 # This is the folding according to the specification: 1026 1020 1027 if folded and line_break == u'\n'\1028 and leading_non_space and self.peek() not in u' \t':1021 if folded and line_break == '\n' \ 1022 and leading_non_space and self.peek() not in ' \t': 1029 1023 if not breaks: 1030 chunks.append( u' ')1024 chunks.append(' ') 1031 1025 else: 1032 1026 chunks.append(line_break) … … 1035 1029 # examples): 1036 1030 # 1037 #if folded and line_break == u'\n':1031 #if folded and line_break == '\n': 1038 1032 # if not breaks: 1039 1033 # if self.peek() not in ' \t': 1040 # chunks.append( u' ')1034 # chunks.append(' ') 1041 1035 # else: 1042 1036 # chunks.append(line_break) … … 1053 1047 1054 1048 # We are done. 1055 return ScalarToken( u''.join(chunks), False, start_mark, end_mark,1049 return ScalarToken(''.join(chunks), False, start_mark, end_mark, 1056 1050 style) 1057 1051 … … 1061 1055 increment = None 1062 1056 ch = self.peek() 1063 if ch in u'+-':1057 if ch in '+-': 1064 1058 if ch == '+': 1065 1059 chomping = True … … 1068 1062 self.forward() 1069 1063 ch = self.peek() 1070 if ch in u'0123456789':1064 if ch in '0123456789': 1071 1065 increment = int(ch) 1072 1066 if increment == 0: … … 1075 1069 self.get_mark()) 1076 1070 self.forward() 1077 elif ch in u'0123456789':1071 elif ch in '0123456789': 1078 1072 increment = int(ch) 1079 1073 if increment == 0: … … 1083 1077 self.forward() 1084 1078 ch = self.peek() 1085 if ch in u'+-':1079 if ch in '+-': 1086 1080 if ch == '+': 1087 1081 chomping = True … … 1090 1084 self.forward() 1091 1085 ch = self.peek() 1092 if ch not in u'\0 \r\n\x85\u2028\u2029':1086 if ch not in '\0 \r\n\x85\u2028\u2029': 1093 1087 raise ScannerError("while scanning a block scalar", start_mark, 1094 1088 "expected chomping or indentation indicators, but found %r" 1095 % ch.encode('utf-8'), self.get_mark())1089 % ch, self.get_mark()) 1096 1090 return chomping, increment 1097 1091 1098 1092 def scan_block_scalar_ignored_line(self, start_mark): 1099 1093 # See the specification for details. 1100 while self.peek() == u' ':1094 while self.peek() == ' ': 1101 1095 self.forward() 1102 if self.peek() == u'#':1103 while self.peek() not in u'\0\r\n\x85\u2028\u2029':1096 if self.peek() == '#': 1097 while self.peek() not in '\0\r\n\x85\u2028\u2029': 1104 1098 self.forward() 1105 1099 ch = self.peek() 1106 if ch not in u'\0\r\n\x85\u2028\u2029':1100 if ch not in '\0\r\n\x85\u2028\u2029': 1107 1101 raise ScannerError("while scanning a block scalar", start_mark, 1108 "expected a comment or a line break, but found %r" 1109 % ch.encode('utf-8'),self.get_mark())1102 "expected a comment or a line break, but found %r" % ch, 1103 self.get_mark()) 1110 1104 self.scan_line_break() 1111 1105 … … 1115 1109 max_indent = 0 1116 1110 end_mark = self.get_mark() 1117 while self.peek() in u' \r\n\x85\u2028\u2029':1118 if self.peek() != u' ':1111 while self.peek() in ' \r\n\x85\u2028\u2029': 1112 if self.peek() != ' ': 1119 1113 chunks.append(self.scan_line_break()) 1120 1114 end_mark = self.get_mark() … … 1129 1123 chunks = [] 1130 1124 end_mark = self.get_mark() 1131 while self.column < indent and self.peek() == u' ':1125 while self.column < indent and self.peek() == ' ': 1132 1126 self.forward() 1133 while self.peek() in u'\r\n\x85\u2028\u2029':1127 while self.peek() in '\r\n\x85\u2028\u2029': 1134 1128 chunks.append(self.scan_line_break()) 1135 1129 end_mark = self.get_mark() 1136 while self.column < indent and self.peek() == u' ':1130 while self.column < indent and self.peek() == ' ': 1137 1131 self.forward() 1138 1132 return chunks, end_mark … … 1159 1153 self.forward() 1160 1154 end_mark = self.get_mark() 1161 return ScalarToken( u''.join(chunks), False, start_mark, end_mark,1155 return ScalarToken(''.join(chunks), False, start_mark, end_mark, 1162 1156 style) 1163 1157 1164 1158 ESCAPE_REPLACEMENTS = { 1165 u'0': u'\0',1166 u'a': u'\x07',1167 u'b': u'\x08',1168 u't': u'\x09',1169 u'\t': u'\x09',1170 u'n': u'\x0A',1171 u'v': u'\x0B',1172 u'f': u'\x0C',1173 u'r': u'\x0D',1174 u'e': u'\x1B',1175 u' ': u'\x20',1176 u'\"': u'\"',1177 u'\\': u'\\',1178 u'N': u'\x85',1179 u'_': u'\xA0',1180 u'L': u'\u2028',1181 u'P': u'\u2029',1159 '0': '\0', 1160 'a': '\x07', 1161 'b': '\x08', 1162 't': '\x09', 1163 '\t': '\x09', 1164 'n': '\x0A', 1165 'v': '\x0B', 1166 'f': '\x0C', 1167 'r': '\x0D', 1168 'e': '\x1B', 1169 ' ': '\x20', 1170 '\"': '\"', 1171 '\\': '\\', 1172 'N': '\x85', 1173 '_': '\xA0', 1174 'L': '\u2028', 1175 'P': '\u2029', 1182 1176 } 1183 1177 1184 1178 ESCAPE_CODES = { 1185 u'x':2,1186 u'u':4,1187 u'U':8,1179 'x': 2, 1180 'u': 4, 1181 'U': 8, 1188 1182 } 1189 1183 … … 1193 1187 while True: 1194 1188 length = 0 1195 while self.peek(length) not in u'\'\"\\\0 \t\r\n\x85\u2028\u2029':1189 while self.peek(length) not in '\'\"\\\0 \t\r\n\x85\u2028\u2029': 1196 1190 length += 1 1197 1191 if length: … … 1199 1193 self.forward(length) 1200 1194 ch = self.peek() 1201 if not double and ch == u'\'' and self.peek(1) == u'\'':1202 chunks.append( u'\'')1195 if not double and ch == '\'' and self.peek(1) == '\'': 1196 chunks.append('\'') 1203 1197 self.forward(2) 1204 elif (double and ch == u'\'') or (not double and ch in u'\"\\'):1198 elif (double and ch == '\'') or (not double and ch in '\"\\'): 1205 1199 chunks.append(ch) 1206 1200 self.forward() 1207 elif double and ch == u'\\':1201 elif double and ch == '\\': 1208 1202 self.forward() 1209 1203 ch = self.peek() … … 1215 1209 self.forward() 1216 1210 for k in range(length): 1217 if self.peek(k) not in u'0123456789ABCDEFabcdef':1211 if self.peek(k) not in '0123456789ABCDEFabcdef': 1218 1212 raise ScannerError("while scanning a double-quoted scalar", start_mark, 1219 1213 "expected escape sequence of %d hexdecimal numbers, but found %r" % 1220 (length, self.peek(k) .encode('utf-8')), self.get_mark())1214 (length, self.peek(k)), self.get_mark()) 1221 1215 code = int(self.prefix(length), 16) 1222 chunks.append( unichr(code))1216 chunks.append(chr(code)) 1223 1217 self.forward(length) 1224 elif ch in u'\r\n\x85\u2028\u2029':1218 elif ch in '\r\n\x85\u2028\u2029': 1225 1219 self.scan_line_break() 1226 1220 chunks.extend(self.scan_flow_scalar_breaks(double, start_mark)) 1227 1221 else: 1228 1222 raise ScannerError("while scanning a double-quoted scalar", start_mark, 1229 "found unknown escape character %r" % ch .encode('utf-8'), self.get_mark())1223 "found unknown escape character %r" % ch, self.get_mark()) 1230 1224 else: 1231 1225 return chunks … … 1235 1229 chunks = [] 1236 1230 length = 0 1237 while self.peek(length) in u' \t':1231 while self.peek(length) in ' \t': 1238 1232 length += 1 1239 1233 whitespaces = self.prefix(length) 1240 1234 self.forward(length) 1241 1235 ch = self.peek() 1242 if ch == u'\0':1236 if ch == '\0': 1243 1237 raise ScannerError("while scanning a quoted scalar", start_mark, 1244 1238 "found unexpected end of stream", self.get_mark()) 1245 elif ch in u'\r\n\x85\u2028\u2029':1239 elif ch in '\r\n\x85\u2028\u2029': 1246 1240 line_break = self.scan_line_break() 1247 1241 breaks = self.scan_flow_scalar_breaks(double, start_mark) 1248 if line_break != u'\n':1242 if line_break != '\n': 1249 1243 chunks.append(line_break) 1250 1244 elif not breaks: 1251 chunks.append( u' ')1245 chunks.append(' ') 1252 1246 chunks.extend(breaks) 1253 1247 else: … … 1262 1256 # separators. 1263 1257 prefix = self.prefix(3) 1264 if (prefix == u'---' or prefix == u'...') \1265 and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':1258 if (prefix == '---' or prefix == '...') \ 1259 and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029': 1266 1260 raise ScannerError("while scanning a quoted scalar", start_mark, 1267 1261 "found unexpected document separator", self.get_mark()) 1268 while self.peek() in u' \t':1262 while self.peek() in ' \t': 1269 1263 self.forward() 1270 if self.peek() in u'\r\n\x85\u2028\u2029':1264 if self.peek() in '\r\n\x85\u2028\u2029': 1271 1265 chunks.append(self.scan_line_break()) 1272 1266 else: … … 1290 1284 while True: 1291 1285 length = 0 1292 if self.peek() == u'#':1286 if self.peek() == '#': 1293 1287 break 1294 1288 while True: 1295 1289 ch = self.peek(length) 1296 if ch in u'\0 \t\r\n\x85\u2028\u2029'\1297 or (not self.flow_level and ch == u':' and1298 self.peek(length+1) in u'\0 \t\r\n\x85\u2028\u2029') \1299 or (self.flow_level and ch in u',:?[]{}'):1290 if ch in '\0 \t\r\n\x85\u2028\u2029' \ 1291 or (not self.flow_level and ch == ':' and 1292 self.peek(length+1) in '\0 \t\r\n\x85\u2028\u2029') \ 1293 or (self.flow_level and ch in ',:?[]{}'): 1300 1294 break 1301 1295 length += 1 1302 1296 # It's not clear what we should do with ':' in the flow context. 1303 if (self.flow_level and ch == u':'1304 and self.peek(length+1) not in u'\0 \t\r\n\x85\u2028\u2029,[]{}'):1297 if (self.flow_level and ch == ':' 1298 and self.peek(length+1) not in '\0 \t\r\n\x85\u2028\u2029,[]{}'): 1305 1299 self.forward(length) 1306 1300 raise ScannerError("while scanning a plain scalar", start_mark, … … 1315 1309 end_mark = self.get_mark() 1316 1310 spaces = self.scan_plain_spaces(indent, start_mark) 1317 if not spaces or self.peek() == u'#' \1311 if not spaces or self.peek() == '#' \ 1318 1312 or (not self.flow_level and self.column < indent): 1319 1313 break 1320 return ScalarToken( u''.join(chunks), True, start_mark, end_mark)1314 return ScalarToken(''.join(chunks), True, start_mark, end_mark) 1321 1315 1322 1316 def scan_plain_spaces(self, indent, start_mark): … … 1326 1320 chunks = [] 1327 1321 length = 0 1328 while self.peek(length) in u' ':1322 while self.peek(length) in ' ': 1329 1323 length += 1 1330 1324 whitespaces = self.prefix(length) 1331 1325 self.forward(length) 1332 1326 ch = self.peek() 1333 if ch in u'\r\n\x85\u2028\u2029':1327 if ch in '\r\n\x85\u2028\u2029': 1334 1328 line_break = self.scan_line_break() 1335 1329 self.allow_simple_key = True 1336 1330 prefix = self.prefix(3) 1337 if (prefix == u'---' or prefix == u'...') \1338 and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':1331 if (prefix == '---' or prefix == '...') \ 1332 and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029': 1339 1333 return 1340 1334 breaks = [] 1341 while self.peek() in u' \r\n\x85\u2028\u2029':1335 while self.peek() in ' \r\n\x85\u2028\u2029': 1342 1336 if self.peek() == ' ': 1343 1337 self.forward() … … 1345 1339 breaks.append(self.scan_line_break()) 1346 1340 prefix = self.prefix(3) 1347 if (prefix == u'---' or prefix == u'...') \1348 and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':1341 if (prefix == '---' or prefix == '...') \ 1342 and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029': 1349 1343 return 1350 if line_break != u'\n':1344 if line_break != '\n': 1351 1345 chunks.append(line_break) 1352 1346 elif not breaks: 1353 chunks.append( u' ')1347 chunks.append(' ') 1354 1348 chunks.extend(breaks) 1355 1349 elif whitespaces: … … 1362 1356 # tag handles. I have allowed it anyway. 1363 1357 ch = self.peek() 1364 if ch != u'!':1358 if ch != '!': 1365 1359 raise ScannerError("while scanning a %s" % name, start_mark, 1366 "expected '!', but found %r" % ch.encode('utf-8'), 1367 self.get_mark()) 1360 "expected '!', but found %r" % ch, self.get_mark()) 1368 1361 length = 1 1369 1362 ch = self.peek(length) 1370 if ch != u' ':1371 while u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \1372 or ch in u'-_':1363 if ch != ' ': 1364 while '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 1365 or ch in '-_': 1373 1366 length += 1 1374 1367 ch = self.peek(length) 1375 if ch != u'!':1368 if ch != '!': 1376 1369 self.forward(length) 1377 1370 raise ScannerError("while scanning a %s" % name, start_mark, 1378 "expected '!', but found %r" % ch.encode('utf-8'), 1379 self.get_mark()) 1371 "expected '!', but found %r" % ch, self.get_mark()) 1380 1372 length += 1 1381 1373 value = self.prefix(length) … … 1389 1381 length = 0 1390 1382 ch = self.peek(length) 1391 while u'0' <= ch <= u'9' or u'A' <= ch <= 'Z' or u'a' <= ch <= 'z' \1392 or ch in u'-;/?:@&=+$,_.!~*\'()[]%':1393 if ch == u'%':1383 while '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' \ 1384 or ch in '-;/?:@&=+$,_.!~*\'()[]%': 1385 if ch == '%': 1394 1386 chunks.append(self.prefix(length)) 1395 1387 self.forward(length) … … 1405 1397 if not chunks: 1406 1398 raise ScannerError("while parsing a %s" % name, start_mark, 1407 "expected URI, but found %r" % ch.encode('utf-8'), 1408 self.get_mark()) 1409 return u''.join(chunks) 1399 "expected URI, but found %r" % ch, self.get_mark()) 1400 return ''.join(chunks) 1410 1401 1411 1402 def scan_uri_escapes(self, name, start_mark): 1412 1403 # See the specification for details. 1413 bytes = []1404 codes = [] 1414 1405 mark = self.get_mark() 1415 while self.peek() == u'%':1406 while self.peek() == '%': 1416 1407 self.forward() 1417 1408 for k in range(2): 1418 if self.peek(k) not in u'0123456789ABCDEFabcdef':1409 if self.peek(k) not in '0123456789ABCDEFabcdef': 1419 1410 raise ScannerError("while scanning a %s" % name, start_mark, 1420 "expected URI escape sequence of 2 hexdecimal numbers, but found %r" %1421 (self.peek(k).encode('utf-8')), self.get_mark())1422 bytes.append(chr(int(self.prefix(2), 16)))1411 "expected URI escape sequence of 2 hexdecimal numbers, but found %r" 1412 % self.peek(k), self.get_mark()) 1413 codes.append(int(self.prefix(2), 16)) 1423 1414 self.forward(2) 1424 1415 try: 1425 value = unicode(''.join(bytes),'utf-8')1426 except UnicodeDecodeError ,exc:1416 value = bytes(codes).decode('utf-8') 1417 except UnicodeDecodeError as exc: 1427 1418 raise ScannerError("while scanning a %s" % name, start_mark, str(exc), mark) 1428 1419 return value … … 1438 1429 # default : '' 1439 1430 ch = self.peek() 1440 if ch in u'\r\n\x85':1441 if self.prefix(2) == u'\r\n':1431 if ch in '\r\n\x85': 1432 if self.prefix(2) == '\r\n': 1442 1433 self.forward(2) 1443 1434 else: 1444 1435 self.forward() 1445 return u'\n'1446 elif ch in u'\u2028\u2029':1436 return '\n' 1437 elif ch in '\u2028\u2029': 1447 1438 self.forward() 1448 1439 return ch 1449 return u''1440 return '' 1450 1441 1451 1442 #try: -
pyyaml/trunk/lib3/yaml/serializer.py
r222 r328 2 2 __all__ = ['Serializer', 'SerializerError'] 3 3 4 from error import YAMLError5 from events import *6 from nodes import *4 from .error import YAMLError 5 from .events import * 6 from .nodes import * 7 7 8 8 class SerializerError(YAMLError): 9 9 pass 10 10 11 class Serializer (object):11 class Serializer: 12 12 13 ANCHOR_TEMPLATE = u'id%03d'13 ANCHOR_TEMPLATE = 'id%03d' 14 14 15 15 def __init__(self, encoding=None, -
pyyaml/trunk/setup.py
r326 r328 32 32 "Programming Language :: Python :: 2.5", 33 33 "Programming Language :: Python :: 2.6", 34 # Python 3.0 is not yet supported, but see http://pyyaml.org/ticket/74 35 # "Programming Language :: Python :: 3", 36 # "Programming Language :: Python :: 3.0", 34 "Programming Language :: Python :: 3", 35 "Programming Language :: Python :: 3.0", 37 36 "Topic :: Software Development :: Libraries :: Python Modules", 38 37 "Topic :: Text Processing :: Markup", … … 157 156 try: 158 157 _build_ext.run(self) 159 except DistutilsPlatformError, exc: 158 except DistutilsPlatformError: 159 exc = sys.exc_info()[1] 160 160 if optional: 161 161 log.warn(str(exc)) … … 288 288 build_cmd.run() 289 289 sys.path.insert(0, build_cmd.build_lib) 290 sys.path.insert(0, 'tests') 290 if sys.version_info[0] < 3: 291 sys.path.insert(0, 'tests') 292 else: 293 sys.path.insert(0, 'tests3') 291 294 import test_all 292 295 test_all.main([]) … … 295 298 if __name__ == '__main__': 296 299 297 setup( 298 name=NAME, 299 version=VERSION, 300 description=DESCRIPTION, 301 long_description=LONG_DESCRIPTION, 302 author=AUTHOR, 303 author_email=AUTHOR_EMAIL, 304 license=LICENSE, 305 platforms=PLATFORMS, 306 url=URL, 307 download_url=DOWNLOAD_URL, 308 classifiers=CLASSIFIERS, 309 310 package_dir={'': 'lib'}, 311 packages=['yaml'], 312 ext_modules=[ 313 Extension('_yaml', ['ext/_yaml.pyx'], 314 'libyaml', "LibYAML bindings", LIBYAML_CHECK, 315 libraries=['yaml']), 316 ], 317 318 distclass=Distribution, 319 cmdclass={ 320 'build_ext': build_ext, 321 'bdist_rpm': bdist_rpm, 322 'test': test, 323 }, 324 ) 325 300 if sys.version_info[0] < 3: 301 302 setup( 303 name=NAME, 304 version=VERSION, 305 description=DESCRIPTION, 306 long_description=LONG_DESCRIPTION, 307 author=AUTHOR, 308 author_email=AUTHOR_EMAIL, 309 license=LICENSE, 310 platforms=PLATFORMS, 311 url=URL, 312 download_url=DOWNLOAD_URL, 313 classifiers=CLASSIFIERS, 314 315 package_dir={'': 'lib'}, 316 packages=['yaml'], 317 ext_modules=[ 318 Extension('_yaml', ['ext/_yaml.pyx'], 319 'libyaml', "LibYAML bindings", LIBYAML_CHECK, 320 libraries=['yaml']), 321 ], 322 323 distclass=Distribution, 324 cmdclass={ 325 'build_ext': build_ext, 326 'bdist_rpm': bdist_rpm, 327 'test': test, 328 }, 329 ) 330 331 else: 332 333 setup( 334 name=NAME, 335 version=VERSION, 336 description=DESCRIPTION, 337 long_description=LONG_DESCRIPTION, 338 author=AUTHOR, 339 author_email=AUTHOR_EMAIL, 340 license=LICENSE, 341 platforms=PLATFORMS, 342 url=URL, 343 download_url=DOWNLOAD_URL, 344 classifiers=CLASSIFIERS, 345 346 package_dir={'': 'lib3'}, 347 packages=['yaml'], 348 349 cmdclass={ 350 'test': test, 351 }, 352 ) 353 -
pyyaml/trunk/tests3/canonical.py
r322 r328 8 8 9 9 def __init__(self, data): 10 try: 11 self.data = unicode(data, 'utf-8')+u'\0' 12 except UnicodeDecodeError: 13 raise CanonicalError("utf-8 stream is expected") 10 if isinstance(data, bytes): 11 try: 12 data = data.decode('utf-8') 13 except UnicodeDecodeError: 14 raise CanonicalError("utf-8 stream is expected") 15 self.data = data+'\0' 14 16 self.index = 0 15 17 self.tokens = [] … … 50 52 self.find_token() 51 53 ch = self.data[self.index] 52 if ch == u'\0':54 if ch == '\0': 53 55 self.tokens.append(yaml.StreamEndToken(None, None)) 54 56 break 55 elif ch == u'%':57 elif ch == '%': 56 58 self.tokens.append(self.scan_directive()) 57 elif ch == u'-' and self.data[self.index:self.index+3] == u'---':59 elif ch == '-' and self.data[self.index:self.index+3] == '---': 58 60 self.index += 3 59 61 self.tokens.append(yaml.DocumentStartToken(None, None)) 60 elif ch == u'[':62 elif ch == '[': 61 63 self.index += 1 62 64 self.tokens.append(yaml.FlowSequenceStartToken(None, None)) 63 elif ch == u'{':65 elif ch == '{': 64 66 self.index += 1 65 67 self.tokens.append(yaml.FlowMappingStartToken(None, None)) 66 elif ch == u']':68 elif ch == ']': 67 69 self.index += 1 68 70 self.tokens.append(yaml.FlowSequenceEndToken(None, None)) 69 elif ch == u'}':71 elif ch == '}': 70 72 self.index += 1 71 73 self.tokens.append(yaml.FlowMappingEndToken(None, None)) 72 elif ch == u'?':74 elif ch == '?': 73 75 self.index += 1 74 76 self.tokens.append(yaml.KeyToken(None, None)) 75 elif ch == u':':77 elif ch == ':': 76 78 self.index += 1 77 79 self.tokens.append(yaml.ValueToken(None, None)) 78 elif ch == u',':80 elif ch == ',': 79 81 self.index += 1 80 82 self.tokens.append(yaml.FlowEntryToken(None, None)) 81 elif ch == u'*' or ch == u'&':83 elif ch == '*' or ch == '&': 82 84 self.tokens.append(self.scan_alias()) 83 elif ch == u'!':85 elif ch == '!': 84 86 self.tokens.append(self.scan_tag()) 85 elif ch == u'"':87 elif ch == '"': 86 88 self.tokens.append(self.scan_scalar()) 87 89 else: … … 89 91 self.scanned = True 90 92 91 DIRECTIVE = u'%YAML 1.1'93 DIRECTIVE = '%YAML 1.1' 92 94 93 95 def scan_directive(self): 94 96 if self.data[self.index:self.index+len(self.DIRECTIVE)] == self.DIRECTIVE and \ 95 self.data[self.index+len(self.DIRECTIVE)] in u' \n\0':97 self.data[self.index+len(self.DIRECTIVE)] in ' \n\0': 96 98 self.index += len(self.DIRECTIVE) 97 99 return yaml.DirectiveToken('YAML', (1, 1), None, None) … … 100 102 101 103 def scan_alias(self): 102 if self.data[self.index] == u'*':104 if self.data[self.index] == '*': 103 105 TokenClass = yaml.AliasToken 104 106 else: … … 106 108 self.index += 1 107 109 start = self.index 108 while self.data[self.index] not in u', \n\0':110 while self.data[self.index] not in ', \n\0': 109 111 self.index += 1 110 112 value = self.data[start:self.index] … … 114 116 self.index += 1 115 117 start = self.index 116 while self.data[self.index] not in u' \n\0':118 while self.data[self.index] not in ' \n\0': 117 119 self.index += 1 118 120 value = self.data[start:self.index] 119 121 if not value: 120 value = u'!'121 elif value[0] == u'!':122 value = '!' 123 elif value[0] == '!': 122 124 value = 'tag:yaml.org,2002:'+value[1:] 123 elif value[0] == u'<' and value[-1] == u'>':125 elif value[0] == '<' and value[-1] == '>': 124 126 value = value[1:-1] 125 127 else: 126 value = u'!'+value128 value = '!'+value 127 129 return yaml.TagToken(value, None, None) 128 130 … … 134 136 135 137 QUOTE_REPLACES = { 136 u'\\': u'\\', 137 u'\"': u'\"', 138 u' ': u' ', 139 u'a': u'\x07', 140 u'b': u'\x08', 141 u'e': u'\x1B', 142 u'f': u'\x0C', 143 u'n': u'\x0A', 144 u'r': u'\x0D', 145 u't': u'\x09', 146 u'v': u'\x0B', 147 u'N': u'\u0085', 148 u'L': u'\u2028', 149 u'P': u'\u2029', 150 u'_': u'_', 151 u'0': u'\x00', 152 138 '\\': '\\', 139 '\"': '\"', 140 ' ': ' ', 141 'a': '\x07', 142 'b': '\x08', 143 'e': '\x1B', 144 'f': '\x0C', 145 'n': '\x0A', 146 'r': '\x0D', 147 't': '\x09', 148 'v': '\x0B', 149 'N': '\u0085', 150 'L': '\u2028', 151 'P': '\u2029', 152 '_': '_', 153 '0': '\x00', 153 154 } 154 155 … … 158 159 start = self.index 159 160 ignore_spaces = False 160 while self.data[self.index] != u'"':161 if self.data[self.index] == u'\\':161 while self.data[self.index] != '"': 162 if self.data[self.index] == '\\': 162 163 ignore_spaces = False 163 164 chunks.append(self.data[start:self.index]) … … 165 166 ch = self.data[self.index] 166 167 self.index += 1 167 if ch == u'\n':168 if ch == '\n': 168 169 ignore_spaces = True 169 170 elif ch in self.QUOTE_CODES: 170 171 length = self.QUOTE_CODES[ch] 171 172 code = int(self.data[self.index:self.index+length], 16) 172 chunks.append( unichr(code))173 chunks.append(chr(code)) 173 174 self.index += length 174 175 else: … … 177 178 chunks.append(self.QUOTE_REPLACES[ch]) 178 179 start = self.index 179 elif self.data[self.index] == u'\n':180 elif self.data[self.index] == '\n': 180 181 chunks.append(self.data[start:self.index]) 181 chunks.append( u' ')182 chunks.append(' ') 182 183 self.index += 1 183 184 start = self.index 184 185 ignore_spaces = True 185 elif ignore_spaces and self.data[self.index] == u' ':186 elif ignore_spaces and self.data[self.index] == ' ': 186 187 self.index += 1 187 188 start = self.index … … 191 192 chunks.append(self.data[start:self.index]) 192 193 self.index += 1 193 return yaml.ScalarToken( u''.join(chunks), False, None, None)194 return yaml.ScalarToken(''.join(chunks), False, None, None) 194 195 195 196 def find_token(self): 196 197 found = False 197 198 while not found: 198 while self.data[self.index] in u' \t':199 self.index += 1 200 if self.data[self.index] == u'#':201 while self.data[self.index] != u'\n':199 while self.data[self.index] in ' \t': 200 self.index += 1 201 if self.data[self.index] == '#': 202 while self.data[self.index] != '\n': 202 203 self.index += 1 203 if self.data[self.index] == u'\n':204 if self.data[self.index] == '\n': 204 205 self.index += 1 205 206 else: -
pyyaml/trunk/tests3/data/construct-binary.code
r58 r328 1 1 { 2 2 "canonical": 3 "GIF89a\x0c\x00\x0c\x00\x84\x00\x00\xff\xff\xf7\xf5\xf5\xee\xe9\xe9\xe5fff\x00\x00\x00\xe7\xe7\xe7^^^\xf3\xf3\xed\x8e\x8e\x8e\xe0\xe0\xe0\x9f\x9f\x9f\x93\x93\x93\xa7\xa7\xa7\x9e\x9e\x9eiiiccc\xa3\xa3\xa3\x84\x84\x84\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9!\xfe\x0eMade with GIMP\x00,\x00\x00\x00\x00\x0c\x00\x0c\x00\x00\x05, \x8e\x810\x9e\xe3@\x14\xe8i\x10\xc4\xd1\x8a\x08\x1c\xcf\x80M$z\xef\xff0\x85p\xb8\xb01f\r\x1b\xce\x01\xc3\x01\x1e\x10' \x82\n\x01\x00;",3 b"GIF89a\x0c\x00\x0c\x00\x84\x00\x00\xff\xff\xf7\xf5\xf5\xee\xe9\xe9\xe5fff\x00\x00\x00\xe7\xe7\xe7^^^\xf3\xf3\xed\x8e\x8e\x8e\xe0\xe0\xe0\x9f\x9f\x9f\x93\x93\x93\xa7\xa7\xa7\x9e\x9e\x9eiiiccc\xa3\xa3\xa3\x84\x84\x84\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9!\xfe\x0eMade with GIMP\x00,\x00\x00\x00\x00\x0c\x00\x0c\x00\x00\x05, \x8e\x810\x9e\xe3@\x14\xe8i\x10\xc4\xd1\x8a\x08\x1c\xcf\x80M$z\xef\xff0\x85p\xb8\xb01f\r\x1b\xce\x01\xc3\x01\x1e\x10' \x82\n\x01\x00;", 4 4 "generic": 5 "GIF89a\x0c\x00\x0c\x00\x84\x00\x00\xff\xff\xf7\xf5\xf5\xee\xe9\xe9\xe5fff\x00\x00\x00\xe7\xe7\xe7^^^\xf3\xf3\xed\x8e\x8e\x8e\xe0\xe0\xe0\x9f\x9f\x9f\x93\x93\x93\xa7\xa7\xa7\x9e\x9e\x9eiiiccc\xa3\xa3\xa3\x84\x84\x84\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9!\xfe\x0eMade with GIMP\x00,\x00\x00\x00\x00\x0c\x00\x0c\x00\x00\x05, \x8e\x810\x9e\xe3@\x14\xe8i\x10\xc4\xd1\x8a\x08\x1c\xcf\x80M$z\xef\xff0\x85p\xb8\xb01f\r\x1b\xce\x01\xc3\x01\x1e\x10' \x82\n\x01\x00;",5 b"GIF89a\x0c\x00\x0c\x00\x84\x00\x00\xff\xff\xf7\xf5\xf5\xee\xe9\xe9\xe5fff\x00\x00\x00\xe7\xe7\xe7^^^\xf3\xf3\xed\x8e\x8e\x8e\xe0\xe0\xe0\x9f\x9f\x9f\x93\x93\x93\xa7\xa7\xa7\x9e\x9e\x9eiiiccc\xa3\xa3\xa3\x84\x84\x84\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9!\xfe\x0eMade with GIMP\x00,\x00\x00\x00\x00\x0c\x00\x0c\x00\x00\x05, \x8e\x810\x9e\xe3@\x14\xe8i\x10\xc4\xd1\x8a\x08\x1c\xcf\x80M$z\xef\xff0\x85p\xb8\xb01f\r\x1b\xce\x01\xc3\x01\x1e\x10' \x82\n\x01\x00;", 6 6 "description": "The binary value above is a tiny arrow encoded as a gif image.", 7 7 } -
pyyaml/trunk/tests3/data/emitting-unacceptable-unicode-character-bug.code
r153 r328 1 u"\udd00"1 "\udd00" -
pyyaml/trunk/tests3/data/serializer-is-already-opened.dumper-error
r322 r328 1 dumper = yaml.Dumper( StringIO.StringIO())1 dumper = yaml.Dumper(io.StringIO()) 2 2 dumper.open() 3 3 dumper.open() -
pyyaml/trunk/tests3/data/serializer-is-closed-1.dumper-error
r322 r328 1 dumper = yaml.Dumper( StringIO.StringIO())1 dumper = yaml.Dumper(io.StringIO()) 2 2 dumper.open() 3 3 dumper.close() -
pyyaml/trunk/tests3/data/serializer-is-closed-2.dumper-error
r322 r328 1 dumper = yaml.Dumper( StringIO.StringIO())1 dumper = yaml.Dumper(io.StringIO()) 2 2 dumper.open() 3 3 dumper.close() -
pyyaml/trunk/tests3/data/serializer-is-not-opened-1.dumper-error
r322 r328 1 dumper = yaml.Dumper( StringIO.StringIO())1 dumper = yaml.Dumper(io.StringIO()) 2 2 dumper.close() -
pyyaml/trunk/tests3/data/serializer-is-not-opened-2.dumper-error
r322 r328 1 dumper = yaml.Dumper( StringIO.StringIO())1 dumper = yaml.Dumper(io.StringIO()) 2 2 dumper.serialize(yaml.ScalarNode(tag='!foo', value='bar')) -
pyyaml/trunk/tests3/test_appliance.py
r325 r328 2 2 import sys, os, os.path, types, traceback, pprint 3 3 4 DATA = 'tests /data'4 DATA = 'tests3/data' 5 5 6 6 def find_test_functions(collections): … … 11 11 if not isinstance(collection, dict): 12 12 collection = vars(collection) 13 keys = collection.keys() 14 keys.sort() 15 for key in keys: 13 for key in sorted(collection): 16 14 value = collection[key] 17 15 if isinstance(value, types.FunctionType) and hasattr(value, 'unittest'): … … 25 23 base, ext = os.path.splitext(filename) 26 24 filenames.setdefault(base, []).append(ext) 27 filenames = filenames.items() 28 filenames.sort() 25 filenames = sorted(filenames.items()) 29 26 return filenames 30 27 … … 52 49 53 50 def execute(function, filenames, verbose): 54 if hasattr(function, 'unittest_name'): 55 name = function.unittest_name 56 else: 57 name = function.func_name 51 name = function.__name__ 58 52 if verbose: 59 53 sys.stdout.write('='*75+'\n') … … 61 55 try: 62 56 function(verbose=verbose, *filenames) 63 except Exception ,exc:57 except Exception as exc: 64 58 info = sys.exc_info() 65 59 if isinstance(exc, AssertionError): … … 107 101 sys.stdout.write('-'*75+'\n') 108 102 sys.stdout.write('%s:\n' % filename) 109 data = open(filename, 'r b').read()103 data = open(filename, 'r', errors='replace').read() 110 104 sys.stdout.write(data) 111 105 if data and data[-1] != '\n': … … 124 118 results = [] 125 119 for function in test_functions: 126 if include_functions and function. func_namenot in include_functions:120 if include_functions and function.__name__ not in include_functions: 127 121 continue 128 122 if function.unittest: -
pyyaml/trunk/tests3/test_canonical.py
r322 r328 8 8 if verbose: 9 9 for token in tokens: 10 print token10 print(token) 11 11 12 12 test_canonical_scanner.unittest = ['.canonical'] … … 18 18 if verbose: 19 19 for event in events: 20 print event20 print(event) 21 21 22 22 test_canonical_parser.unittest = ['.canonical'] … … 26 26 try: 27 27 output = list(yaml.canonical_load_all(data)) 28 except yaml.YAMLError ,exc:28 except yaml.YAMLError as exc: 29 29 if verbose: 30 print exc30 print(exc) 31 31 else: 32 32 raise AssertionError("expected an exception") -
pyyaml/trunk/tests3/test_constructor.py
r325 r328 4 4 5 5 import datetime 6 try:7 set8 except NameError:9 from sets import Set as set10 6 import yaml.tokens 11 7 12 8 def execute(code): 13 exec code9 exec(code) 14 10 return value 15 11 … … 230 226 elif isinstance(data, datetime.datetime): 231 227 return repr(data.utctimetuple()) 232 elif isinstance(data, unicode):233 return data.encode('utf-8')234 228 elif isinstance(data, float) and data != data: 235 229 return '?' … … 252 246 pass 253 247 if verbose: 254 print "SERIALIZED NATIVE1:"255 print _serialize_value(native1)256 print "SERIALIZED NATIVE2:"257 print _serialize_value(native2)248 print("SERIALIZED NATIVE1:") 249 print(_serialize_value(native1)) 250 print("SERIALIZED NATIVE2:") 251 print(_serialize_value(native2)) 258 252 assert _serialize_value(native1) == _serialize_value(native2), (native1, native2) 259 253 finally: 260 254 if verbose: 261 print "NATIVE1:"255 print("NATIVE1:") 262 256 pprint.pprint(native1) 263 print "NATIVE2:"257 print("NATIVE2:") 264 258 pprint.pprint(native2) 265 259 -
pyyaml/trunk/tests3/test_emitter.py
r322 r328 19 19 output = yaml.emit(events) 20 20 if verbose: 21 print "OUTPUT:"22 print output21 print("OUTPUT:") 22 print(output) 23 23 new_events = list(yaml.parse(output)) 24 24 _compare_events(events, new_events) … … 31 31 output = yaml.emit(events, canonical=canonical) 32 32 if verbose: 33 print "OUTPUT (canonical=%s):" % canonical34 print output33 print("OUTPUT (canonical=%s):" % canonical) 34 print(output) 35 35 new_events = list(yaml.parse(output)) 36 36 _compare_events(events, new_events) … … 57 57 output = yaml.emit(styled_events) 58 58 if verbose: 59 print "OUTPUT (filename=%r, flow_style=%r, style=%r)" % (filename, flow_style, style)60 print output59 print("OUTPUT (filename=%r, flow_style=%r, style=%r)" % (filename, flow_style, style)) 60 print(output) 61 61 new_events = list(yaml.parse(output)) 62 62 _compare_events(events, new_events) … … 90 90 output = yaml.emit(events) 91 91 if verbose: 92 print "OUTPUT:"93 print output92 print("OUTPUT:") 93 print(output) 94 94 new_events = list(yaml.parse(output)) 95 95 _compare_events(events, new_events) -
pyyaml/trunk/tests3/test_errors.py
r322 r328 5 5 try: 6 6 list(yaml.load_all(open(error_filename, 'rb'))) 7 except yaml.YAMLError ,exc:7 except yaml.YAMLError as exc: 8 8 if verbose: 9 print "%s:" % exc.__class__.__name__, exc9 print("%s:" % exc.__class__.__name__, exc) 10 10 else: 11 11 raise AssertionError("expected an exception") … … 16 16 try: 17 17 list(yaml.load_all(open(error_filename, 'rb').read())) 18 except yaml.YAMLError ,exc:18 except yaml.YAMLError as exc: 19 19 if verbose: 20 print "%s:" % exc.__class__.__name__, exc20 print("%s:" % exc.__class__.__name__, exc) 21 21 else: 22 22 raise AssertionError("expected an exception") … … 27 27 try: 28 28 yaml.load(open(error_filename, 'rb').read()) 29 except yaml.YAMLError ,exc:29 except yaml.YAMLError as exc: 30 30 if verbose: 31 print "%s:" % exc.__class__.__name__, exc31 print("%s:" % exc.__class__.__name__, exc) 32 32 else: 33 33 raise AssertionError("expected an exception") … … 40 40 try: 41 41 yaml.emit(events) 42 except yaml.YAMLError ,exc:42 except yaml.YAMLError as exc: 43 43 if verbose: 44 print "%s:" % exc.__class__.__name__, exc44 print("%s:" % exc.__class__.__name__, exc) 45 45 else: 46 46 raise AssertionError("expected an exception") … … 51 51 code = open(error_filename, 'rb').read() 52 52 try: 53 import yaml, StringIO54 exec code55 except yaml.YAMLError ,exc:53 import yaml, io 54 exec(code) 55 except yaml.YAMLError as exc: 56 56 if verbose: 57 print "%s:" % exc.__class__.__name__, exc57 print("%s:" % exc.__class__.__name__, exc) 58 58 else: 59 59 raise AssertionError("expected an exception") -
pyyaml/trunk/tests3/test_mark.py
r322 r328 3 3 4 4 def test_marks(marks_filename, verbose=False): 5 inputs = open(marks_filename, 'r b').read().split('---\n')[1:]5 inputs = open(marks_filename, 'r').read().split('---\n')[1:] 6 6 for input in inputs: 7 7 index = 0 … … 15 15 column += 1 16 16 index += 1 17 mark = yaml.Mark(marks_filename, index, line, column, unicode(input), index)17 mark = yaml.Mark(marks_filename, index, line, column, input, index) 18 18 snippet = mark.get_snippet(indent=2, max_length=79) 19 19 if verbose: 20 print snippet20 print(snippet) 21 21 assert isinstance(snippet, str), type(snippet) 22 22 assert snippet.count('\n') == 1, snippet.count('\n') -
pyyaml/trunk/tests3/test_reader.py
r322 r328 1 1 2 2 import yaml.reader 3 import codecs4 3 5 4 def _run_reader(data, verbose): 6 5 try: 7 6 stream = yaml.reader.Reader(data) 8 while stream.peek() != u'\0':7 while stream.peek() != '\0': 9 8 stream.forward() 10 except yaml.reader.ReaderError ,exc:9 except yaml.reader.ReaderError as exc: 11 10 if verbose: 12 print exc11 print(exc) 13 12 else: 14 13 raise AssertionError("expected an exception") … … 19 18 for encoding in ['utf-8', 'utf-16-le', 'utf-16-be']: 20 19 try: 21 data = unicode(open(error_filename, 'rb').read(),encoding)20 data = open(error_filename, 'rb').read().decode(encoding) 22 21 break 23 22 except UnicodeDecodeError: … … 26 25 return 27 26 _run_reader(data, verbose) 28 _run_reader( codecs.open(error_filename, encoding=encoding), verbose)27 _run_reader(open(error_filename, encoding=encoding), verbose) 29 28 30 29 test_stream_error.unittest = ['.stream-error'] -
pyyaml/trunk/tests3/test_recursive.py
r325 r328 24 24 25 25 def test_recursive(recursive_filename, verbose=False): 26 exec open(recursive_filename, 'rb').read()26 exec(open(recursive_filename, 'rb').read()) 27 27 value1 = value 28 28 output1 = None … … 36 36 finally: 37 37 if verbose: 38 #print "VALUE1:", value139 #print "VALUE2:", value240 print "OUTPUT1:"41 print output142 print "OUTPUT2:"43 print output238 print("VALUE1:", value1) 39 print("VALUE2:", value2) 40 print("OUTPUT1:") 41 print(output1) 42 print("OUTPUT2:") 43 print(output2) 44 44 45 45 test_recursive.unittest = ['.recursive'] -
pyyaml/trunk/tests3/test_representer.py
r322 r328 21 21 value2 = test_constructor._serialize_value(native2) 22 22 if verbose: 23 print "SERIALIZED NATIVE1:"24 print value125 print "SERIALIZED NATIVE2:"26 print value223 print("SERIALIZED NATIVE1:") 24 print(value1) 25 print("SERIALIZED NATIVE2:") 26 print(value2) 27 27 assert value1 == value2, (native1, native2) 28 28 finally: 29 29 if verbose: 30 print "NATIVE1:"30 print("NATIVE1:") 31 31 pprint.pprint(native1) 32 print "NATIVE2:"32 print("NATIVE2:") 33 33 pprint.pprint(native2) 34 print "OUTPUT:"35 print output34 print("OUTPUT:") 35 print(output) 36 36 37 37 test_representer_types.unittest = ['.code'] -
pyyaml/trunk/tests3/test_resolver.py
r322 r328 7 7 node = None 8 8 try: 9 correct_tag = open(detect_filename, 'r b').read().strip()9 correct_tag = open(detect_filename, 'r').read().strip() 10 10 node = yaml.compose(open(data_filename, 'rb')) 11 11 assert isinstance(node, yaml.SequenceNode), node … … 15 15 finally: 16 16 if verbose: 17 print "CORRECT TAG:", correct_tag17 print("CORRECT TAG:", correct_tag) 18 18 if hasattr(node, 'value'): 19 print "CHILDREN:"19 print("CHILDREN:") 20 20 pprint.pprint(node.value) 21 21 … … 30 30 pass 31 31 32 yaml.add_path_resolver( u'!root', [],32 yaml.add_path_resolver('!root', [], 33 33 Loader=MyLoader, Dumper=MyDumper) 34 yaml.add_path_resolver( u'!root/scalar', [], str,34 yaml.add_path_resolver('!root/scalar', [], str, 35 35 Loader=MyLoader, Dumper=MyDumper) 36 yaml.add_path_resolver( u'!root/key11/key12/*', ['key11', 'key12'],36 yaml.add_path_resolver('!root/key11/key12/*', ['key11', 'key12'], 37 37 Loader=MyLoader, Dumper=MyDumper) 38 yaml.add_path_resolver( u'!root/key21/1/*', ['key21', 1],38 yaml.add_path_resolver('!root/key21/1/*', ['key21', 1], 39 39 Loader=MyLoader, Dumper=MyDumper) 40 yaml.add_path_resolver( u'!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict,40 yaml.add_path_resolver('!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict, 41 41 Loader=MyLoader, Dumper=MyDumper) 42 42 … … 68 68 finally: 69 69 if verbose: 70 print yaml.serialize_all(nodes1)70 print(yaml.serialize_all(nodes1)) 71 71 72 72 test_path_resolver_loader.unittest = ['.data', '.path'] … … 77 77 output = yaml.serialize_all(yaml.compose_all(open(filename, 'rb')), Dumper=MyDumper) 78 78 if verbose: 79 print output79 print(output) 80 80 nodes1 = yaml.compose_all(output) 81 81 nodes2 = yaml.compose_all(open(data_filename, 'rb')) -
pyyaml/trunk/tests3/test_structure.py
r322 r328 35 35 def test_structure(data_filename, structure_filename, verbose=False): 36 36 nodes1 = [] 37 nodes2 = eval(open(structure_filename, 'r b').read())37 nodes2 = eval(open(structure_filename, 'r').read()) 38 38 try: 39 39 loader = yaml.Loader(open(data_filename, 'rb')) … … 49 49 finally: 50 50 if verbose: 51 print "NODES1:"51 print("NODES1:") 52 52 pprint.pprint(nodes1) 53 print "NODES2:"53 print("NODES2:") 54 54 pprint.pprint(nodes2) 55 55 … … 63 63 assert event1.anchor == event2.anchor, (event1, event2) 64 64 if isinstance(event1, (yaml.ScalarEvent, yaml.CollectionStartEvent)): 65 if (event1.tag not in [None, u'!'] and event2.tag not in [None, u'!']) or full:65 if (event1.tag not in [None, '!'] and event2.tag not in [None, '!']) or full: 66 66 assert event1.tag == event2.tag, (event1, event2) 67 67 if isinstance(event1, yaml.ScalarEvent): … … 77 77 finally: 78 78 if verbose: 79 print "EVENTS1:"79 print("EVENTS1:") 80 80 pprint.pprint(events1) 81 print "EVENTS2:"81 print("EVENTS2:") 82 82 pprint.pprint(events2) 83 83 … … 93 93 finally: 94 94 if verbose: 95 print "EVENTS1:"95 print("EVENTS1:") 96 96 pprint.pprint(events1) 97 print "EVENTS2:"97 print("EVENTS2:") 98 98 pprint.pprint(events2) 99 99 … … 125 125 finally: 126 126 if verbose: 127 print "NODES1:"127 print("NODES1:") 128 128 pprint.pprint(nodes1) 129 print "NODES2:"129 print("NODES2:") 130 130 pprint.pprint(nodes2) 131 131 … … 145 145 return self.construct_scalar(node) 146 146 147 MyLoader.add_constructor( u'tag:yaml.org,2002:map', MyLoader.construct_mapping)147 MyLoader.add_constructor('tag:yaml.org,2002:map', MyLoader.construct_mapping) 148 148 MyLoader.add_constructor(None, MyLoader.construct_undefined) 149 149 … … 161 161 return self.construct_scalar(node) 162 162 163 MyCanonicalLoader.add_constructor( u'tag:yaml.org,2002:map', MyCanonicalLoader.construct_mapping)163 MyCanonicalLoader.add_constructor('tag:yaml.org,2002:map', MyCanonicalLoader.construct_mapping) 164 164 MyCanonicalLoader.add_constructor(None, MyCanonicalLoader.construct_undefined) 165 165 … … 175 175 finally: 176 176 if verbose: 177 print "NATIVE1:"177 print("NATIVE1:") 178 178 pprint.pprint(native1) 179 print "NATIVE2:"179 print("NATIVE2:") 180 180 pprint.pprint(native2) 181 181 -
pyyaml/trunk/tests3/test_tokens.py
r322 r328 45 45 def test_tokens(data_filename, tokens_filename, verbose=False): 46 46 tokens1 = [] 47 tokens2 = open(tokens_filename, 'r b').read().split()47 tokens2 = open(tokens_filename, 'r').read().split() 48 48 try: 49 49 for token in yaml.scan(open(data_filename, 'rb')): … … 52 52 finally: 53 53 if verbose: 54 print "TOKENS1:", ' '.join(tokens1)55 print "TOKENS2:", ' '.join(tokens2)54 print("TOKENS1:", ' '.join(tokens1)) 55 print("TOKENS2:", ' '.join(tokens2)) 56 56 assert len(tokens1) == len(tokens2), (tokens1, tokens2) 57 57 for token1, token2 in zip(tokens1, tokens2): -
pyyaml/trunk/tests3/test_yaml_ext.py
r325 r328 118 118 def test_c_version(verbose=False): 119 119 if verbose: 120 print _yaml.get_version()121 print _yaml.get_version_string()120 print(_yaml.get_version()) 121 print(_yaml.get_version_string()) 122 122 assert ("%s.%s.%s" % _yaml.get_version()) == _yaml.get_version_string(), \ 123 123 (_yaml.get_version(), _yaml.get_version_string()) … … 144 144 finally: 145 145 if verbose: 146 print "PY_TOKENS:"146 print("PY_TOKENS:") 147 147 pprint.pprint(py_tokens) 148 print "C_TOKENS:"148 print("C_TOKENS:") 149 149 pprint.pprint(c_tokens) 150 150 … … 177 177 finally: 178 178 if verbose: 179 print "PY_EVENTS:"179 print("PY_EVENTS:") 180 180 pprint.pprint(py_events) 181 print "C_EVENTS:"181 print("C_EVENTS:") 182 182 pprint.pprint(c_events) 183 183 … … 199 199 c_data = yaml.emit(events, Dumper=yaml.CDumper) 200 200 if verbose: 201 print c_data201 print(c_data) 202 202 py_events = list(yaml.parse(c_data, Loader=yaml.PyLoader)) 203 203 c_events = list(yaml.parse(c_data, Loader=yaml.CLoader)) … … 211 211 py_value = getattr(py_event, attribute, None) 212 212 c_value = getattr(c_event, attribute, None) 213 if attribute == 'tag' and value in [None, u'!'] \214 and py_value in [None, u'!'] and c_value in [None, u'!']:213 if attribute == 'tag' and value in [None, '!'] \ 214 and py_value in [None, '!'] and c_value in [None, '!']: 215 215 continue 216 216 if attribute == 'explicit' and (py_value or c_value): … … 220 220 finally: 221 221 if verbose: 222 print "EVENTS:"222 print("EVENTS:") 223 223 pprint.pprint(events) 224 print "PY_EVENTS:"224 print("PY_EVENTS:") 225 225 pprint.pprint(py_events) 226 print "C_EVENTS:"226 print("C_EVENTS:") 227 227 pprint.pprint(c_events) 228 228 … … 241 241 finally: 242 242 _tear_down() 243 try: 244 wrapper.func_name = '%s_ext' % function.func_name 245 except TypeError: 246 pass 247 wrapper.unittest_name = '%s_ext' % function.func_name 243 wrapper.__name__ = '%s_ext' % function.__name__ 248 244 wrapper.unittest = function.unittest 249 245 wrapper.skip = getattr(function, 'skip', [])+['.skip-ext']
Note: See TracChangeset
for help on using the changeset viewer.
