@@ -2889,6 +2889,79 @@ def test_flags_repr(self):
28892889 "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0xffe01" )
28902890
28912891
2892+ class TemplateTests (unittest .TestCase ):
2893+ def test_literal (self ):
2894+ p = re .compile (r'\w' )
2895+ t = p .compile_template ('a' )
2896+ self .assertIsInstance (t , re .Template )
2897+ self .assertEqual (re .sub (p , t , 'x-yz' ), 'a-aa' )
2898+ self .assertEqual (p .sub (t , 'x-yz' ), 'a-aa' )
2899+ self .assertEqual (re .subn (p , t , 'x-yz' , count = 2 ), ('a-az' , 2 ))
2900+ self .assertEqual (p .subn (t , 'x-yz' , 2 ), ('a-az' , 2 ))
2901+
2902+ p = re .compile (br'\w' )
2903+ t = p .compile_template (b'a' )
2904+ self .assertIsInstance (t , re .Template )
2905+ self .assertEqual (re .sub (p , t , b'x-yz' ), b'a-aa' )
2906+ self .assertEqual (p .sub (t , b'x-yz' ), b'a-aa' )
2907+ self .assertEqual (re .subn (p , t , b'x-yz' , count = 2 ), (b'a-az' , 2 ))
2908+ self .assertEqual (p .subn (t , b'x-yz' , 2 ), (b'a-az' , 2 ))
2909+
2910+ def test_group_refs (self ):
2911+ p = re .compile (r'(\w)(\w)' )
2912+ t = p .compile_template (r'[\2-\1]' )
2913+ self .assertIsInstance (t , re .Template )
2914+ self .assertEqual (re .sub (p , t , 'xyzt' ), '[y-x][t-z]' )
2915+ self .assertEqual (p .sub (t , 'xyzt' ), '[y-x][t-z]' )
2916+
2917+ p = re .compile (br'(\w)(\w)' )
2918+ t = p .compile_template (br'[\2-\1]' )
2919+ self .assertIsInstance (t , re .Template )
2920+ self .assertEqual (re .sub (p , t , b'xyzt' ), b'[y-x][t-z]' )
2921+ self .assertEqual (p .sub (t , b'xyzt' ), b'[y-x][t-z]' )
2922+
2923+ def test_group_refs_emplty_literals (self ):
2924+ p = re .compile (r'(\w)(\w)' )
2925+ t = p .compile_template (r'\2\1' )
2926+ self .assertIsInstance (t , re .Template )
2927+ self .assertEqual (re .sub (p , t , 'xyzt' ), 'yxtz' )
2928+ self .assertEqual (p .sub (t , 'xyzt' ), 'yxtz' )
2929+
2930+ p = re .compile (br'(\w)(\w)' )
2931+ t = p .compile_template (br'\2\1' )
2932+ self .assertIsInstance (t , re .Template )
2933+ self .assertEqual (re .sub (p , t , b'xyzt' ), b'yxtz' )
2934+ self .assertEqual (p .sub (t , b'xyzt' ), b'yxtz' )
2935+
2936+ def test_symbolic_group_refs (self ):
2937+ p = re .compile (r'(?P<a>\w)(?P<b>\w)' )
2938+ t = p .compile_template (r'[\g<b>-\g<a>]' )
2939+ self .assertIsInstance (t , re .Template )
2940+ self .assertEqual (re .sub (p , t , 'xyzt' ), '[y-x][t-z]' )
2941+ self .assertEqual (p .sub (t , 'xyzt' ), '[y-x][t-z]' )
2942+
2943+ p = re .compile (br'(?P<a>\w)(?P<b>\w)' )
2944+ t = p .compile_template (br'[\g<b>-\g<a>]' )
2945+ self .assertIsInstance (t , re .Template )
2946+ self .assertEqual (re .sub (p , t , b'xyzt' ), b'[y-x][t-z]' )
2947+ self .assertEqual (p .sub (t , b'xyzt' ), b'[y-x][t-z]' )
2948+
2949+ def test_call (self ):
2950+ p = re .compile (r'(\w)(\w)' )
2951+ t = p .compile_template (r'[\2-\1]' )
2952+ m = p .search (' xy ' )
2953+ self .assertEqual (t (m ), '[y-x]' )
2954+ self .assertRaises (TypeError , t , None )
2955+ self .assertRaises (TypeError , t , {})
2956+
2957+ p = re .compile (br'(\w)(\w)' )
2958+ t = p .compile_template (br'[\2-\1]' )
2959+ m = p .search (b' xy ' )
2960+ self .assertEqual (t (m ), b'[y-x]' )
2961+ self .assertRaises (TypeError , t , None )
2962+ self .assertRaises (TypeError , t , {})
2963+
2964+
28922965class ImplementationTest (unittest .TestCase ):
28932966 """
28942967 Test implementation details of the re module.
@@ -2901,6 +2974,8 @@ def test_immutable(self):
29012974 re .Match .foo = 1
29022975 with self .assertRaises (TypeError ):
29032976 re .Pattern .foo = 1
2977+ with self .assertRaises (TypeError ):
2978+ re .Template .foo = 1
29042979 with self .assertRaises (TypeError ):
29052980 pat = re .compile ("" )
29062981 tp = type (pat .scanner ("" ))
@@ -2924,6 +2999,7 @@ def test_disallow_instantiation(self):
29242999 # Ensure that the type disallows instantiation (bpo-43916)
29253000 check_disallow_instantiation (self , re .Match )
29263001 check_disallow_instantiation (self , re .Pattern )
3002+ check_disallow_instantiation (self , re .Template )
29273003 pat = re .compile ("" )
29283004 check_disallow_instantiation (self , type (pat .scanner ("" )))
29293005
0 commit comments