11
2- __all__ = ['BaseConstructor' , 'SafeConstructor' , 'Constructor' ,
3- 'ConstructorError' ]
2+ __all__ = [
3+ 'BaseConstructor' ,
4+ 'SafeConstructor' ,
5+ 'FullConstructor' ,
6+ 'UnsafeConstructor' ,
7+ 'Constructor' ,
8+ 'ConstructorError'
9+ ]
410
511from .error import *
612from .nodes import *
@@ -464,7 +470,7 @@ def construct_undefined(self, node):
464470SafeConstructor .add_constructor (None ,
465471 SafeConstructor .construct_undefined )
466472
467- class Constructor (SafeConstructor ):
473+ class FullConstructor (SafeConstructor ):
468474
469475 def construct_python_str (self , node ):
470476 return self .construct_scalar (node )
@@ -497,18 +503,22 @@ def construct_python_complex(self, node):
497503 def construct_python_tuple (self , node ):
498504 return tuple (self .construct_sequence (node ))
499505
500- def find_python_module (self , name , mark ):
506+ def find_python_module (self , name , mark , unsafe = False ):
501507 if not name :
502508 raise ConstructorError ("while constructing a Python module" , mark ,
503509 "expected non-empty name appended to the tag" , mark )
504- try :
505- __import__ (name )
506- except ImportError as exc :
510+ if unsafe :
511+ try :
512+ __import__ (name )
513+ except ImportError as exc :
514+ raise ConstructorError ("while constructing a Python module" , mark ,
515+ "cannot find module %r (%s)" % (name , exc ), mark )
516+ if not name in sys .modules :
507517 raise ConstructorError ("while constructing a Python module" , mark ,
508- "cannot find module %r (%s) " % ( name , exc ) , mark )
518+ "module %r is not imported " % name , mark )
509519 return sys .modules [name ]
510520
511- def find_python_name (self , name , mark ):
521+ def find_python_name (self , name , mark , unsafe = False ):
512522 if not name :
513523 raise ConstructorError ("while constructing a Python object" , mark ,
514524 "expected non-empty name appended to the tag" , mark )
@@ -517,11 +527,15 @@ def find_python_name(self, name, mark):
517527 else :
518528 module_name = 'builtins'
519529 object_name = name
520- try :
521- __import__ (module_name )
522- except ImportError as exc :
530+ if unsafe :
531+ try :
532+ __import__ (module_name )
533+ except ImportError as exc :
534+ raise ConstructorError ("while constructing a Python object" , mark ,
535+ "cannot find module %r (%s)" % (module_name , exc ), mark )
536+ if not module_name in sys .modules :
523537 raise ConstructorError ("while constructing a Python object" , mark ,
524- "cannot find module %r (%s) " % ( module_name , exc ) , mark )
538+ "module %r is not imported " % module_name , mark )
525539 module = sys .modules [module_name ]
526540 if not hasattr (module , object_name ):
527541 raise ConstructorError ("while constructing a Python object" , mark ,
@@ -544,12 +558,16 @@ def construct_python_module(self, suffix, node):
544558 return self .find_python_module (suffix , node .start_mark )
545559
546560 def make_python_instance (self , suffix , node ,
547- args = None , kwds = None , newobj = False ):
561+ args = None , kwds = None , newobj = False , unsafe = False ):
548562 if not args :
549563 args = []
550564 if not kwds :
551565 kwds = {}
552566 cls = self .find_python_name (suffix , node .start_mark )
567+ if not (unsafe or isinstance (cls , type )):
568+ raise ConstructorError ("while constructing a Python instance" , node .start_mark ,
569+ "expected a class, but found %r" % type (cls ),
570+ node .start_mark )
553571 if newobj and isinstance (cls , type ):
554572 return cls .__new__ (cls , * args , ** kwds )
555573 else :
@@ -616,71 +634,87 @@ def construct_python_object_apply(self, suffix, node, newobj=False):
616634 def construct_python_object_new (self , suffix , node ):
617635 return self .construct_python_object_apply (suffix , node , newobj = True )
618636
619- Constructor .add_constructor (
637+ FullConstructor .add_constructor (
620638 'tag:yaml.org,2002:python/none' ,
621- Constructor .construct_yaml_null )
639+ FullConstructor .construct_yaml_null )
622640
623- Constructor .add_constructor (
641+ FullConstructor .add_constructor (
624642 'tag:yaml.org,2002:python/bool' ,
625- Constructor .construct_yaml_bool )
643+ FullConstructor .construct_yaml_bool )
626644
627- Constructor .add_constructor (
645+ FullConstructor .add_constructor (
628646 'tag:yaml.org,2002:python/str' ,
629- Constructor .construct_python_str )
647+ FullConstructor .construct_python_str )
630648
631- Constructor .add_constructor (
649+ FullConstructor .add_constructor (
632650 'tag:yaml.org,2002:python/unicode' ,
633- Constructor .construct_python_unicode )
651+ FullConstructor .construct_python_unicode )
634652
635- Constructor .add_constructor (
653+ FullConstructor .add_constructor (
636654 'tag:yaml.org,2002:python/bytes' ,
637- Constructor .construct_python_bytes )
655+ FullConstructor .construct_python_bytes )
638656
639- Constructor .add_constructor (
657+ FullConstructor .add_constructor (
640658 'tag:yaml.org,2002:python/int' ,
641- Constructor .construct_yaml_int )
659+ FullConstructor .construct_yaml_int )
642660
643- Constructor .add_constructor (
661+ FullConstructor .add_constructor (
644662 'tag:yaml.org,2002:python/long' ,
645- Constructor .construct_python_long )
663+ FullConstructor .construct_python_long )
646664
647- Constructor .add_constructor (
665+ FullConstructor .add_constructor (
648666 'tag:yaml.org,2002:python/float' ,
649- Constructor .construct_yaml_float )
667+ FullConstructor .construct_yaml_float )
650668
651- Constructor .add_constructor (
669+ FullConstructor .add_constructor (
652670 'tag:yaml.org,2002:python/complex' ,
653- Constructor .construct_python_complex )
671+ FullConstructor .construct_python_complex )
654672
655- Constructor .add_constructor (
673+ FullConstructor .add_constructor (
656674 'tag:yaml.org,2002:python/list' ,
657- Constructor .construct_yaml_seq )
675+ FullConstructor .construct_yaml_seq )
658676
659- Constructor .add_constructor (
677+ FullConstructor .add_constructor (
660678 'tag:yaml.org,2002:python/tuple' ,
661- Constructor .construct_python_tuple )
679+ FullConstructor .construct_python_tuple )
662680
663- Constructor .add_constructor (
681+ FullConstructor .add_constructor (
664682 'tag:yaml.org,2002:python/dict' ,
665- Constructor .construct_yaml_map )
683+ FullConstructor .construct_yaml_map )
666684
667- Constructor .add_multi_constructor (
685+ FullConstructor .add_multi_constructor (
668686 'tag:yaml.org,2002:python/name:' ,
669- Constructor .construct_python_name )
687+ FullConstructor .construct_python_name )
670688
671- Constructor .add_multi_constructor (
689+ FullConstructor .add_multi_constructor (
672690 'tag:yaml.org,2002:python/module:' ,
673- Constructor .construct_python_module )
691+ FullConstructor .construct_python_module )
674692
675- Constructor .add_multi_constructor (
693+ FullConstructor .add_multi_constructor (
676694 'tag:yaml.org,2002:python/object:' ,
677- Constructor .construct_python_object )
695+ FullConstructor .construct_python_object )
678696
679- Constructor .add_multi_constructor (
697+ FullConstructor .add_multi_constructor (
680698 'tag:yaml.org,2002:python/object/apply:' ,
681- Constructor .construct_python_object_apply )
699+ FullConstructor .construct_python_object_apply )
682700
683- Constructor .add_multi_constructor (
701+ FullConstructor .add_multi_constructor (
684702 'tag:yaml.org,2002:python/object/new:' ,
685- Constructor .construct_python_object_new )
703+ FullConstructor .construct_python_object_new )
704+
705+ class UnsafeConstructor (FullConstructor ):
686706
707+ def find_python_module (self , name , mark ):
708+ return super (UnsafeConstructor , self ).find_python_module (name , mark , unsafe = True )
709+
710+ def find_python_name (self , name , mark ):
711+ return super (UnsafeConstructor , self ).find_python_name (name , mark , unsafe = True )
712+
713+ def make_python_instance (self , suffix , node , args = None , kwds = None , newobj = False ):
714+ return super (UnsafeConstructor , self ).make_python_instance (
715+ suffix , node , args , kwds , newobj , unsafe = True )
716+
717+ # Constructor is same as UnsafeConstructor. Need to leave this in place in case
718+ # people have extended it directly.
719+ class Constructor (UnsafeConstructor ):
720+ pass
0 commit comments