@@ -904,7 +904,7 @@ def test_search_coverage(self):
904904 self .assertEqual (re .search (r"a\s" , "a " ).group (0 ), "a " )
905905
906906 def assertMatch (self , pattern , text , match = None , span = None ,
907- matcher = re .match ):
907+ matcher = re .fullmatch ):
908908 if match is None and span is None :
909909 # the pattern matches the whole text
910910 match = text
@@ -917,45 +917,46 @@ def assertMatch(self, pattern, text, match=None, span=None,
917917 self .assertEqual (m .group (), match )
918918 self .assertEqual (m .span (), span )
919919
920+ LITERAL_CHARS = string .ascii_letters + string .digits + '!"%&\' ,/:;<=>@_`~'
921+
920922 def test_re_escape (self ):
921- alnum_chars = string .ascii_letters + string .digits + '_'
922923 p = '' .join (chr (i ) for i in range (256 ))
923924 for c in p :
924- if c in alnum_chars :
925- self .assertEqual (re .escape (c ), c )
926- elif c == '\x00 ' :
927- self .assertEqual (re .escape (c ), '\\ 000' )
928- else :
929- self .assertEqual (re .escape (c ), '\\ ' + c )
930925 self .assertMatch (re .escape (c ), c )
926+ self .assertMatch ('[' + re .escape (c ) + ']' , c )
927+ self .assertMatch ('(?x)' + re .escape (c ), c )
931928 self .assertMatch (re .escape (p ), p )
929+ for c in '-.]{}' :
930+ self .assertEqual (re .escape (c )[:1 ], '\\ ' )
931+ literal_chars = self .LITERAL_CHARS
932+ self .assertEqual (re .escape (literal_chars ), literal_chars )
932933
933- def test_re_escape_byte (self ):
934- alnum_chars = (string .ascii_letters + string .digits + '_' ).encode ('ascii' )
934+ def test_re_escape_bytes (self ):
935935 p = bytes (range (256 ))
936936 for i in p :
937937 b = bytes ([i ])
938- if b in alnum_chars :
939- self .assertEqual (re .escape (b ), b )
940- elif i == 0 :
941- self .assertEqual (re .escape (b ), b'\\ 000' )
942- else :
943- self .assertEqual (re .escape (b ), b'\\ ' + b )
944938 self .assertMatch (re .escape (b ), b )
939+ self .assertMatch (b'[' + re .escape (b ) + b']' , b )
940+ self .assertMatch (b'(?x)' + re .escape (b ), b )
945941 self .assertMatch (re .escape (p ), p )
942+ for i in b'-.]{}' :
943+ b = bytes ([i ])
944+ self .assertEqual (re .escape (b )[:1 ], b'\\ ' )
945+ literal_chars = self .LITERAL_CHARS .encode ('ascii' )
946+ self .assertEqual (re .escape (literal_chars ), literal_chars )
946947
947948 def test_re_escape_non_ascii (self ):
948949 s = 'xxx\u2620 \u2620 \u2620 xxx'
949950 s_escaped = re .escape (s )
950- self .assertEqual (s_escaped , 'xxx \\ \u2620 \\ \u2620 \\ \u2620 xxx' )
951+ self .assertEqual (s_escaped , s )
951952 self .assertMatch (s_escaped , s )
952953 self .assertMatch ('.%s+.' % re .escape ('\u2620 ' ), s ,
953954 'x\u2620 \u2620 \u2620 x' , (2 , 7 ), re .search )
954955
955956 def test_re_escape_non_ascii_bytes (self ):
956957 b = 'y\u2620 y\u2620 y' .encode ('utf-8' )
957958 b_escaped = re .escape (b )
958- self .assertEqual (b_escaped , b'y \\ \xe2 \\ \x98 \\ \xa0 y \\ \xe2 \\ \x98 \\ \xa0 y' )
959+ self .assertEqual (b_escaped , b )
959960 self .assertMatch (b_escaped , b )
960961 res = re .findall (re .escape ('\u2620 ' .encode ('utf-8' )), b )
961962 self .assertEqual (len (res ), 2 )
0 commit comments