Changeset 139 for pyyaml/trunk/lib/yaml/constructor.py
- Timestamp:
- 04/18/06 10:35:28 (7 years ago)
- File:
-
- 1 edited
-
pyyaml/trunk/lib/yaml/constructor.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pyyaml/trunk/lib/yaml/constructor.py
r136 r139 18 18 from sets import Set as set 19 19 20 import binascii, re 20 import binascii, re, sys 21 21 22 22 class ConstructorError(MarkedYAMLError): … … 62 62 constructor = lambda node: \ 63 63 self.yaml_multi_constructors[tag_prefix](self, tag_suffix, node) 64 break64 break 65 65 else: 66 66 if None in self.yaml_multi_constructors: … … 76 76 elif isinstance(node, MappingNode): 77 77 constructor = self.construct_mapping 78 else: 79 print node.tag 78 80 data = constructor(node) 79 81 self.constructed_objects[node] = data … … 350 352 351 353 def construct_yaml_object(self, node, cls): 352 mapping = self.construct_mapping(node) 353 state = {} 354 for key in mapping: 355 state[key.replace('-', '_')] = mapping[key] 354 state = self.construct_mapping(node) 356 355 data = cls.__new__(cls) 357 356 if hasattr(data, '__setstate__'): 358 data.__setstate__( mapping)357 data.__setstate__(state) 359 358 else: 360 data.__dict__.update( mapping)359 data.__dict__.update(state) 361 360 return data 362 361 … … 419 418 420 419 class Constructor(SafeConstructor): 421 pass 422 420 421 def construct_python_str(self, node): 422 return self.construct_scalar(node).encode('utf-8') 423 424 def construct_python_unicode(self, node): 425 return self.construct_scalar(node) 426 427 def construct_python_long(self, node): 428 return long(self.construct_yaml_int(node)) 429 430 def construct_python_complex(self, node): 431 return complex(self.construct_scalar(node)) 432 433 def construct_python_tuple(self, node): 434 return tuple(self.construct_yaml_seq(node)) 435 436 def find_python_module(self, name, mark): 437 if not name: 438 raise ConstructorError("while constructing a Python module", mark, 439 "expected non-empty name appended to the tag", mark) 440 try: 441 __import__(name) 442 except ImportError, exc: 443 raise ConstructorError("while constructing a Python module", mark, 444 "cannot find module %r (%s)" % (name.encode('utf-8'), exc), mark) 445 return sys.modules[name] 446 447 def find_python_name(self, name, mark): 448 if not name: 449 raise ConstructorError("while constructing a Python object", mark, 450 "expected non-empty name appended to the tag", mark) 451 if u'.' in name: 452 module_name, object_name = name.rsplit('.', 1) 453 else: 454 module_name = '__builtin__' 455 object_name = name 456 try: 457 __import__(module_name) 458 except ImportError, exc: 459 raise ConstructorError("while constructing a Python object", mark, 460 "cannot find module %r (%s)" % (module_name.encode('utf-8'), exc), mark) 461 module = sys.modules[module_name] 462 if not hasattr(module, object_name): 463 raise ConstructorError("while constructing a Python object", mark, 464 "cannot find %r in the module %r" % (object_name.encode('utf-8'), 465 module.__name__), mark) 466 return getattr(module, object_name) 467 468 def construct_python_name(self, suffix, node): 469 value = self.construct_scalar(node) 470 if value: 471 raise ConstructorError("while constructing a Python name", node.start_mark, 472 "expected the empty value, but found %r" % value.encode('utf-8'), 473 node.start_mark) 474 return self.find_python_name(suffix, node.start_mark) 475 476 def construct_python_module(self, suffix, node): 477 value = self.construct_scalar(node) 478 if value: 479 raise ConstructorError("while constructing a Python module", node.start_mark, 480 "expected the empty value, but found %r" % value.encode('utf-8'), 481 node.start_mark) 482 return self.find_python_module(suffix, node.start_mark) 483 484 Constructor.add_constructor( 485 u'tag:yaml.org,2002:python/none', 486 Constructor.construct_yaml_null) 487 488 Constructor.add_constructor( 489 u'tag:yaml.org,2002:python/bool', 490 Constructor.construct_yaml_bool) 491 492 Constructor.add_constructor( 493 u'tag:yaml.org,2002:python/str', 494 Constructor.construct_python_str) 495 496 Constructor.add_constructor( 497 u'tag:yaml.org,2002:python/unicode', 498 Constructor.construct_python_unicode) 499 500 Constructor.add_constructor( 501 u'tag:yaml.org,2002:python/int', 502 Constructor.construct_yaml_int) 503 504 Constructor.add_constructor( 505 u'tag:yaml.org,2002:python/long', 506 Constructor.construct_python_long) 507 508 Constructor.add_constructor( 509 u'tag:yaml.org,2002:python/float', 510 Constructor.construct_yaml_float) 511 512 Constructor.add_constructor( 513 u'tag:yaml.org,2002:python/complex', 514 Constructor.construct_python_complex) 515 516 Constructor.add_constructor( 517 u'tag:yaml.org,2002:python/list', 518 Constructor.construct_yaml_seq) 519 520 Constructor.add_constructor( 521 u'tag:yaml.org,2002:python/tuple', 522 Constructor.construct_python_tuple) 523 524 Constructor.add_constructor( 525 u'tag:yaml.org,2002:python/dict', 526 Constructor.construct_yaml_map) 527 528 Constructor.add_multi_constructor( 529 u'tag:yaml.org,2002:python/name:', 530 Constructor.construct_python_name) 531 532 Constructor.add_multi_constructor( 533 u'tag:yaml.org,2002:python/module:', 534 Constructor.construct_python_module) 535
Note: See TracChangeset
for help on using the changeset viewer.
