@@ -127,8 +127,10 @@ def test_basic_re_sub(self):
127127 self .assertEqual (re .sub ("(?i)b+" , "x" , "bbbb BBBB" ), 'x x' )
128128 self .assertEqual (re .sub (r'\d+' , self .bump_num , '08.2 -2 23x99y' ),
129129 '9.3 -3 24x100y' )
130- self .assertEqual (re .sub (r'\d+' , self .bump_num , '08.2 -2 23x99y' , 3 ),
131- '9.3 -3 23x99y' )
130+ with self .assertWarns (DeprecationWarning ) as w :
131+ self .assertEqual (re .sub (r'\d+' , self .bump_num , '08.2 -2 23x99y' , 3 ),
132+ '9.3 -3 23x99y' )
133+ self .assertEqual (w .filename , __file__ )
132134 self .assertEqual (re .sub (r'\d+' , self .bump_num , '08.2 -2 23x99y' , count = 3 ),
133135 '9.3 -3 23x99y' )
134136
@@ -235,9 +237,42 @@ def test_sub_template_numeric_escape(self):
235237
236238 def test_qualified_re_sub (self ):
237239 self .assertEqual (re .sub ('a' , 'b' , 'aaaaa' ), 'bbbbb' )
238- self .assertEqual (re .sub ('a' , 'b' , 'aaaaa' , 1 ), 'baaaa' )
240+ with self .assertWarns (DeprecationWarning ) as w :
241+ self .assertEqual (re .sub ('a' , 'b' , 'aaaaa' , 1 ), 'baaaa' )
242+ self .assertEqual (w .filename , __file__ )
239243 self .assertEqual (re .sub ('a' , 'b' , 'aaaaa' , count = 1 ), 'baaaa' )
240244
245+ with self .assertRaisesRegex (TypeError ,
246+ r"sub\(\) got multiple values for argument 'count'" ):
247+ re .sub ('a' , 'b' , 'aaaaa' , 1 , count = 1 )
248+ with self .assertRaisesRegex (TypeError ,
249+ r"sub\(\) got multiple values for argument 'flags'" ):
250+ re .sub ('a' , 'b' , 'aaaaa' , 1 , 0 , flags = 0 )
251+ with self .assertRaisesRegex (TypeError ,
252+ r"sub\(\) takes from 3 to 5 positional arguments but 6 "
253+ r"were given" ):
254+ re .sub ('a' , 'b' , 'aaaaa' , 1 , 0 , 0 )
255+
256+ def test_misuse_flags (self ):
257+ with self .assertWarns (DeprecationWarning ) as w :
258+ result = re .sub ('a' , 'b' , 'aaaaa' , re .I )
259+ self .assertEqual (result , re .sub ('a' , 'b' , 'aaaaa' , count = int (re .I )))
260+ self .assertEqual (str (w .warning ),
261+ "'count' is passed as positional argument" )
262+ self .assertEqual (w .filename , __file__ )
263+ with self .assertWarns (DeprecationWarning ) as w :
264+ result = re .subn ("b*" , "x" , "xyz" , re .I )
265+ self .assertEqual (result , re .subn ("b*" , "x" , "xyz" , count = int (re .I )))
266+ self .assertEqual (str (w .warning ),
267+ "'count' is passed as positional argument" )
268+ self .assertEqual (w .filename , __file__ )
269+ with self .assertWarns (DeprecationWarning ) as w :
270+ result = re .split (":" , ":a:b::c" , re .I )
271+ self .assertEqual (result , re .split (":" , ":a:b::c" , maxsplit = int (re .I )))
272+ self .assertEqual (str (w .warning ),
273+ "'maxsplit' is passed as positional argument" )
274+ self .assertEqual (w .filename , __file__ )
275+
241276 def test_bug_114660 (self ):
242277 self .assertEqual (re .sub (r'(\S)\s+(\S)' , r'\1 \2' , 'hello there' ),
243278 'hello there' )
@@ -344,9 +379,22 @@ def test_re_subn(self):
344379 self .assertEqual (re .subn ("b+" , "x" , "bbbb BBBB" ), ('x BBBB' , 1 ))
345380 self .assertEqual (re .subn ("b+" , "x" , "xyz" ), ('xyz' , 0 ))
346381 self .assertEqual (re .subn ("b*" , "x" , "xyz" ), ('xxxyxzx' , 4 ))
347- self .assertEqual (re .subn ("b*" , "x" , "xyz" , 2 ), ('xxxyz' , 2 ))
382+ with self .assertWarns (DeprecationWarning ) as w :
383+ self .assertEqual (re .subn ("b*" , "x" , "xyz" , 2 ), ('xxxyz' , 2 ))
384+ self .assertEqual (w .filename , __file__ )
348385 self .assertEqual (re .subn ("b*" , "x" , "xyz" , count = 2 ), ('xxxyz' , 2 ))
349386
387+ with self .assertRaisesRegex (TypeError ,
388+ r"subn\(\) got multiple values for argument 'count'" ):
389+ re .subn ('a' , 'b' , 'aaaaa' , 1 , count = 1 )
390+ with self .assertRaisesRegex (TypeError ,
391+ r"subn\(\) got multiple values for argument 'flags'" ):
392+ re .subn ('a' , 'b' , 'aaaaa' , 1 , 0 , flags = 0 )
393+ with self .assertRaisesRegex (TypeError ,
394+ r"subn\(\) takes from 3 to 5 positional arguments but 6 "
395+ r"were given" ):
396+ re .subn ('a' , 'b' , 'aaaaa' , 1 , 0 , 0 )
397+
350398 def test_re_split (self ):
351399 for string in ":a:b::c" , S (":a:b::c" ):
352400 self .assertTypedEqual (re .split (":" , string ),
@@ -401,7 +449,9 @@ def test_re_split(self):
401449 self .assertTypedEqual (re .split (sep , ':a:b::c' ), expected )
402450
403451 def test_qualified_re_split (self ):
404- self .assertEqual (re .split (":" , ":a:b::c" , 2 ), ['' , 'a' , 'b::c' ])
452+ with self .assertWarns (DeprecationWarning ) as w :
453+ self .assertEqual (re .split (":" , ":a:b::c" , 2 ), ['' , 'a' , 'b::c' ])
454+ self .assertEqual (w .filename , __file__ )
405455 self .assertEqual (re .split (":" , ":a:b::c" , maxsplit = 2 ), ['' , 'a' , 'b::c' ])
406456 self .assertEqual (re .split (':' , 'a:b:c:d' , maxsplit = 2 ), ['a' , 'b' , 'c:d' ])
407457 self .assertEqual (re .split ("(:)" , ":a:b::c" , maxsplit = 2 ),
@@ -411,6 +461,17 @@ def test_qualified_re_split(self):
411461 self .assertEqual (re .split ("(:*)" , ":a:b::c" , maxsplit = 2 ),
412462 ['' , ':' , '' , '' , 'a:b::c' ])
413463
464+ with self .assertRaisesRegex (TypeError ,
465+ r"split\(\) got multiple values for argument 'maxsplit'" ):
466+ re .split (":" , ":a:b::c" , 2 , maxsplit = 2 )
467+ with self .assertRaisesRegex (TypeError ,
468+ r"split\(\) got multiple values for argument 'flags'" ):
469+ re .split (":" , ":a:b::c" , 2 , 0 , flags = 0 )
470+ with self .assertRaisesRegex (TypeError ,
471+ r"split\(\) takes from 2 to 4 positional arguments but 5 "
472+ r"were given" ):
473+ re .split (":" , ":a:b::c" , 2 , 0 , 0 )
474+
414475 def test_re_findall (self ):
415476 self .assertEqual (re .findall (":+" , "abc" ), [])
416477 for string in "a:b::c:::d" , S ("a:b::c:::d" ):
0 commit comments