1
1
2
- __all__ = ['BaseConstructor' , 'SafeConstructor' , 'Constructor' ,
3
- 'ConstructorError' ]
2
+ __all__ = [
3
+ 'BaseConstructor' ,
4
+ 'SafeConstructor' ,
5
+ 'FullConstructor' ,
6
+ 'UnsafeConstructor' ,
7
+ 'Constructor' ,
8
+ 'ConstructorError'
9
+ ]
4
10
5
11
from .error import *
6
12
from .nodes import *
@@ -464,7 +470,7 @@ def construct_undefined(self, node):
464
470
SafeConstructor .add_constructor (None ,
465
471
SafeConstructor .construct_undefined )
466
472
467
- class Constructor (SafeConstructor ):
473
+ class FullConstructor (SafeConstructor ):
468
474
469
475
def construct_python_str (self , node ):
470
476
return self .construct_scalar (node )
@@ -497,18 +503,22 @@ def construct_python_complex(self, node):
497
503
def construct_python_tuple (self , node ):
498
504
return tuple (self .construct_sequence (node ))
499
505
500
- def find_python_module (self , name , mark ):
506
+ def find_python_module (self , name , mark , unsafe = False ):
501
507
if not name :
502
508
raise ConstructorError ("while constructing a Python module" , mark ,
503
509
"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 :
507
517
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 )
509
519
return sys .modules [name ]
510
520
511
- def find_python_name (self , name , mark ):
521
+ def find_python_name (self , name , mark , unsafe = False ):
512
522
if not name :
513
523
raise ConstructorError ("while constructing a Python object" , mark ,
514
524
"expected non-empty name appended to the tag" , mark )
@@ -517,11 +527,15 @@ def find_python_name(self, name, mark):
517
527
else :
518
528
module_name = 'builtins'
519
529
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 :
523
537
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 )
525
539
module = sys .modules [module_name ]
526
540
if not hasattr (module , object_name ):
527
541
raise ConstructorError ("while constructing a Python object" , mark ,
@@ -544,12 +558,16 @@ def construct_python_module(self, suffix, node):
544
558
return self .find_python_module (suffix , node .start_mark )
545
559
546
560
def make_python_instance (self , suffix , node ,
547
- args = None , kwds = None , newobj = False ):
561
+ args = None , kwds = None , newobj = False , unsafe = False ):
548
562
if not args :
549
563
args = []
550
564
if not kwds :
551
565
kwds = {}
552
566
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 )
553
571
if newobj and isinstance (cls , type ):
554
572
return cls .__new__ (cls , * args , ** kwds )
555
573
else :
@@ -616,71 +634,87 @@ def construct_python_object_apply(self, suffix, node, newobj=False):
616
634
def construct_python_object_new (self , suffix , node ):
617
635
return self .construct_python_object_apply (suffix , node , newobj = True )
618
636
619
- Constructor .add_constructor (
637
+ FullConstructor .add_constructor (
620
638
'tag:yaml.org,2002:python/none' ,
621
- Constructor .construct_yaml_null )
639
+ FullConstructor .construct_yaml_null )
622
640
623
- Constructor .add_constructor (
641
+ FullConstructor .add_constructor (
624
642
'tag:yaml.org,2002:python/bool' ,
625
- Constructor .construct_yaml_bool )
643
+ FullConstructor .construct_yaml_bool )
626
644
627
- Constructor .add_constructor (
645
+ FullConstructor .add_constructor (
628
646
'tag:yaml.org,2002:python/str' ,
629
- Constructor .construct_python_str )
647
+ FullConstructor .construct_python_str )
630
648
631
- Constructor .add_constructor (
649
+ FullConstructor .add_constructor (
632
650
'tag:yaml.org,2002:python/unicode' ,
633
- Constructor .construct_python_unicode )
651
+ FullConstructor .construct_python_unicode )
634
652
635
- Constructor .add_constructor (
653
+ FullConstructor .add_constructor (
636
654
'tag:yaml.org,2002:python/bytes' ,
637
- Constructor .construct_python_bytes )
655
+ FullConstructor .construct_python_bytes )
638
656
639
- Constructor .add_constructor (
657
+ FullConstructor .add_constructor (
640
658
'tag:yaml.org,2002:python/int' ,
641
- Constructor .construct_yaml_int )
659
+ FullConstructor .construct_yaml_int )
642
660
643
- Constructor .add_constructor (
661
+ FullConstructor .add_constructor (
644
662
'tag:yaml.org,2002:python/long' ,
645
- Constructor .construct_python_long )
663
+ FullConstructor .construct_python_long )
646
664
647
- Constructor .add_constructor (
665
+ FullConstructor .add_constructor (
648
666
'tag:yaml.org,2002:python/float' ,
649
- Constructor .construct_yaml_float )
667
+ FullConstructor .construct_yaml_float )
650
668
651
- Constructor .add_constructor (
669
+ FullConstructor .add_constructor (
652
670
'tag:yaml.org,2002:python/complex' ,
653
- Constructor .construct_python_complex )
671
+ FullConstructor .construct_python_complex )
654
672
655
- Constructor .add_constructor (
673
+ FullConstructor .add_constructor (
656
674
'tag:yaml.org,2002:python/list' ,
657
- Constructor .construct_yaml_seq )
675
+ FullConstructor .construct_yaml_seq )
658
676
659
- Constructor .add_constructor (
677
+ FullConstructor .add_constructor (
660
678
'tag:yaml.org,2002:python/tuple' ,
661
- Constructor .construct_python_tuple )
679
+ FullConstructor .construct_python_tuple )
662
680
663
- Constructor .add_constructor (
681
+ FullConstructor .add_constructor (
664
682
'tag:yaml.org,2002:python/dict' ,
665
- Constructor .construct_yaml_map )
683
+ FullConstructor .construct_yaml_map )
666
684
667
- Constructor .add_multi_constructor (
685
+ FullConstructor .add_multi_constructor (
668
686
'tag:yaml.org,2002:python/name:' ,
669
- Constructor .construct_python_name )
687
+ FullConstructor .construct_python_name )
670
688
671
- Constructor .add_multi_constructor (
689
+ FullConstructor .add_multi_constructor (
672
690
'tag:yaml.org,2002:python/module:' ,
673
- Constructor .construct_python_module )
691
+ FullConstructor .construct_python_module )
674
692
675
- Constructor .add_multi_constructor (
693
+ FullConstructor .add_multi_constructor (
676
694
'tag:yaml.org,2002:python/object:' ,
677
- Constructor .construct_python_object )
695
+ FullConstructor .construct_python_object )
678
696
679
- Constructor .add_multi_constructor (
697
+ FullConstructor .add_multi_constructor (
680
698
'tag:yaml.org,2002:python/object/apply:' ,
681
- Constructor .construct_python_object_apply )
699
+ FullConstructor .construct_python_object_apply )
682
700
683
- Constructor .add_multi_constructor (
701
+ FullConstructor .add_multi_constructor (
684
702
'tag:yaml.org,2002:python/object/new:' ,
685
- Constructor .construct_python_object_new )
703
+ FullConstructor .construct_python_object_new )
704
+
705
+ class UnsafeConstructor (FullConstructor ):
686
706
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