File tree 2 files changed +24
-1
lines changed
2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -373,8 +373,17 @@ def sorter(a, b):
373
373
return '%s)' % regex
374
374
375
375
def pattern (self , format ):
376
- """Return re pattern for the format string."""
376
+ """Return re pattern for the format string.
377
+
378
+ Need to make sure that any characters that might be interpreted as
379
+ regex syntax is escaped.
380
+
381
+ """
377
382
processed_format = ''
383
+ # The sub() call escapes all characters that might be misconstrued
384
+ # as regex syntax.
385
+ regex_chars = re_compile (r"([\\.^$*+?{}\[\]|])" )
386
+ format = regex_chars .sub (r"\\\1" , format )
378
387
whitespace_replacement = re_compile ('\s+' )
379
388
format = whitespace_replacement .sub ('\s*' , format )
380
389
while format .find ('%' ) != - 1 :
Original file line number Diff line number Diff line change @@ -168,6 +168,14 @@ def test_pattern(self):
168
168
"did not find 'd' directive pattern string '%s'" %
169
169
pattern_string )
170
170
171
+ def test_pattern_escaping (self ):
172
+ # Make sure any characters in the format string that might be taken as
173
+ # regex syntax is escaped.
174
+ pattern_string = self .time_re .pattern ("\d+" )
175
+ self .failUnless (r"\\d\+" in pattern_string ,
176
+ "%s does not have re characters escaped properly" %
177
+ pattern_string )
178
+
171
179
def test_compile (self ):
172
180
# Check that compiled regex is correct
173
181
found = self .time_re .compile (r"%A" ).match (self .locale_time .f_weekday [6 ])
@@ -201,6 +209,12 @@ def test_blankpattern(self):
201
209
self .failUnless (_strptime .TimeRE (test_locale ).pattern ("%Z" ) == '' ,
202
210
"with timezone == ('',''), TimeRE().pattern('%Z') != ''" )
203
211
212
+ def test_matching_with_escapes (self ):
213
+ # Make sure a format that requires escaping of characters works
214
+ compiled_re = self .time_re .compile ("\w+ %m" )
215
+ found = compiled_re .match ("\w+ 10" )
216
+ self .failUnless (found , "Escaping failed of format '\w+ 10'" )
217
+
204
218
class StrptimeTests (unittest .TestCase ):
205
219
"""Tests for _strptime.strptime."""
206
220
You can’t perform that action at this time.
0 commit comments