1717
1818import unittest
1919
20+ class S (str ):
21+ def __getitem__ (self , index ):
22+ return S (super ().__getitem__ (index ))
23+
24+ class B (bytes ):
25+ def __getitem__ (self , index ):
26+ return B (super ().__getitem__ (index ))
27+
2028class ReTests (unittest .TestCase ):
2129
30+ def assertTypedEqual (self , actual , expect , msg = None ):
31+ self .assertEqual (actual , expect , msg )
32+ def recurse (actual , expect ):
33+ if isinstance (expect , (tuple , list )):
34+ for x , y in zip (actual , expect ):
35+ recurse (x , y )
36+ else :
37+ self .assertIs (type (actual ), type (expect ), msg )
38+ recurse (actual , expect )
39+
2240 def test_keep_buffer (self ):
2341 # See bug 14212
2442 b = bytearray (b'x' )
@@ -53,6 +71,13 @@ def bump_num(self, matchobj):
5371 return str (int_value + 1 )
5472
5573 def test_basic_re_sub (self ):
74+ self .assertTypedEqual (re .sub ('y' , 'a' , 'xyz' ), 'xaz' )
75+ self .assertTypedEqual (re .sub ('y' , S ('a' ), S ('xyz' )), 'xaz' )
76+ self .assertTypedEqual (re .sub (b'y' , b'a' , b'xyz' ), b'xaz' )
77+ self .assertTypedEqual (re .sub (b'y' , B (b'a' ), B (b'xyz' )), b'xaz' )
78+ self .assertTypedEqual (re .sub (b'y' , bytearray (b'a' ), bytearray (b'xyz' )), b'xaz' )
79+ self .assertTypedEqual (re .sub (b'y' , memoryview (b'a' ), memoryview (b'xyz' )), b'xaz' )
80+
5681 self .assertEqual (re .sub ("(?i)b+" , "x" , "bbbb BBBB" ), 'x x' )
5782 self .assertEqual (re .sub (r'\d+' , self .bump_num , '08.2 -2 23x99y' ),
5883 '9.3 -3 24x100y' )
@@ -210,10 +235,22 @@ def test_re_subn(self):
210235 self .assertEqual (re .subn ("b*" , "x" , "xyz" , 2 ), ('xxxyz' , 2 ))
211236
212237 def test_re_split (self ):
213- self .assertEqual (re .split (":" , ":a:b::c" ), ['' , 'a' , 'b' , '' , 'c' ])
214- self .assertEqual (re .split (":*" , ":a:b::c" ), ['' , 'a' , 'b' , 'c' ])
215- self .assertEqual (re .split ("(:*)" , ":a:b::c" ),
216- ['' , ':' , 'a' , ':' , 'b' , '::' , 'c' ])
238+ for string in ":a:b::c" , S (":a:b::c" ):
239+ self .assertTypedEqual (re .split (":" , string ),
240+ ['' , 'a' , 'b' , '' , 'c' ])
241+ self .assertTypedEqual (re .split (":*" , string ),
242+ ['' , 'a' , 'b' , 'c' ])
243+ self .assertTypedEqual (re .split ("(:*)" , string ),
244+ ['' , ':' , 'a' , ':' , 'b' , '::' , 'c' ])
245+ for string in (b":a:b::c" , B (b":a:b::c" ), bytearray (b":a:b::c" ),
246+ memoryview (b":a:b::c" )):
247+ self .assertTypedEqual (re .split (b":" , string ),
248+ [b'' , b'a' , b'b' , b'' , b'c' ])
249+ self .assertTypedEqual (re .split (b":*" , string ),
250+ [b'' , b'a' , b'b' , b'c' ])
251+ self .assertTypedEqual (re .split (b"(:*)" , string ),
252+ [b'' , b':' , b'a' , b':' , b'b' , b'::' , b'c' ])
253+
217254 self .assertEqual (re .split ("(?::*)" , ":a:b::c" ), ['' , 'a' , 'b' , 'c' ])
218255 self .assertEqual (re .split ("(:)*" , ":a:b::c" ),
219256 ['' , ':' , 'a' , ':' , 'b' , ':' , 'c' ])
@@ -235,22 +272,39 @@ def test_qualified_re_split(self):
235272
236273 def test_re_findall (self ):
237274 self .assertEqual (re .findall (":+" , "abc" ), [])
238- self .assertEqual (re .findall (":+" , "a:b::c:::d" ), [":" , "::" , ":::" ])
239- self .assertEqual (re .findall ("(:+)" , "a:b::c:::d" ), [":" , "::" , ":::" ])
240- self .assertEqual (re .findall ("(:)(:*)" , "a:b::c:::d" ), [(":" , "" ),
241- (":" , ":" ),
242- (":" , "::" )])
275+ for string in "a:b::c:::d" , S ("a:b::c:::d" ):
276+ self .assertTypedEqual (re .findall (":+" , string ),
277+ [":" , "::" , ":::" ])
278+ self .assertTypedEqual (re .findall ("(:+)" , string ),
279+ [":" , "::" , ":::" ])
280+ self .assertTypedEqual (re .findall ("(:)(:*)" , string ),
281+ [(":" , "" ), (":" , ":" ), (":" , "::" )])
282+ for string in (b"a:b::c:::d" , B (b"a:b::c:::d" ), bytearray (b"a:b::c:::d" ),
283+ memoryview (b"a:b::c:::d" )):
284+ self .assertTypedEqual (re .findall (b":+" , string ),
285+ [b":" , b"::" , b":::" ])
286+ self .assertTypedEqual (re .findall (b"(:+)" , string ),
287+ [b":" , b"::" , b":::" ])
288+ self .assertTypedEqual (re .findall (b"(:)(:*)" , string ),
289+ [(b":" , b"" ), (b":" , b":" ), (b":" , b"::" )])
243290
244291 def test_bug_117612 (self ):
245292 self .assertEqual (re .findall (r"(a|(b))" , "aba" ),
246293 [("a" , "" ),("b" , "b" ),("a" , "" )])
247294
248295 def test_re_match (self ):
249- self .assertEqual (re .match ('a' , 'a' ).groups (), ())
250- self .assertEqual (re .match ('(a)' , 'a' ).groups (), ('a' ,))
251- self .assertEqual (re .match (r'(a)' , 'a' ).group (0 ), 'a' )
252- self .assertEqual (re .match (r'(a)' , 'a' ).group (1 ), 'a' )
253- self .assertEqual (re .match (r'(a)' , 'a' ).group (1 , 1 ), ('a' , 'a' ))
296+ for string in 'a' , S ('a' ):
297+ self .assertEqual (re .match ('a' , string ).groups (), ())
298+ self .assertEqual (re .match ('(a)' , string ).groups (), ('a' ,))
299+ self .assertEqual (re .match ('(a)' , string ).group (0 ), 'a' )
300+ self .assertEqual (re .match ('(a)' , string ).group (1 ), 'a' )
301+ self .assertEqual (re .match ('(a)' , string ).group (1 , 1 ), ('a' , 'a' ))
302+ for string in b'a' , B (b'a' ), bytearray (b'a' ), memoryview (b'a' ):
303+ self .assertEqual (re .match (b'a' , string ).groups (), ())
304+ self .assertEqual (re .match (b'(a)' , string ).groups (), (b'a' ,))
305+ self .assertEqual (re .match (b'(a)' , string ).group (0 ), b'a' )
306+ self .assertEqual (re .match (b'(a)' , string ).group (1 ), b'a' )
307+ self .assertEqual (re .match (b'(a)' , string ).group (1 , 1 ), (b'a' , b'a' ))
254308
255309 pat = re .compile ('((a)|(b))(c)?' )
256310 self .assertEqual (pat .match ('a' ).groups (), ('a' , 'a' , None , None ))
0 commit comments