Changeset 328 for pyyaml/trunk/lib3/yaml/constructor.py
- Timestamp:
- 12/29/08 12:24:05 (3 years ago)
- Location:
- pyyaml/trunk/lib3
- Files:
-
- 1 modified
- 1 copied
-
. (copied) (copied from pyyaml/trunk/lib)
-
yaml/constructor.py (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
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
