Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1e91d8eb
Commit
1e91d8eb
authored
Apr 19, 2003
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make _strptime escape regex syntax in format string to prevent use in internal regex.
parent
482c5f7e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
1 deletion
+24
-1
Lib/_strptime.py
Lib/_strptime.py
+10
-1
Lib/test/test_strptime.py
Lib/test/test_strptime.py
+14
-0
No files found.
Lib/_strptime.py
View file @
1e91d8eb
...
...
@@ -373,8 +373,17 @@ class TimeRE(dict):
return
'%s)'
%
regex
def
pattern
(
self
,
format
):
"""Return re pattern for the format string."""
"""Return re pattern for the format string.
Need to make sure that any characters that might be interpreted as
regex syntax is escaped.
"""
processed_format
=
''
# The sub() call escapes all characters that might be misconstrued
# as regex syntax.
regex_chars
=
re_compile
(
r"([\\.^$*+?{}\
[
\]|])"
)
format
=
regex_chars
.
sub
(
r"\\\1"
,
format
)
whitespace_replacement
=
re_compile
(
'
\
s+
'
)
format = whitespace_replacement.sub('
\
s
*
', format)
while format.find('
%
') != -1:
...
...
Lib/test/test_strptime.py
View file @
1e91d8eb
...
...
@@ -168,6 +168,14 @@ class TimeRETests(unittest.TestCase):
"did not find 'd' directive pattern string '%s'"
%
pattern_string
)
def
test_pattern_escaping
(
self
):
# Make sure any characters in the format string that might be taken as
# regex syntax is escaped.
pattern_string
=
self
.
time_re
.
pattern
(
"
\
d+
"
)
self.failUnless(r"
\\
d
\
+
" in pattern_string,
"
%
s
does
not
have
re
characters
escaped
properly
" %
pattern_string)
def test_compile(self):
# Check that compiled regex is correct
found = self.time_re.compile(r"
%
A
").match(self.locale_time.f_weekday[6])
...
...
@@ -201,6 +209,12 @@ class TimeRETests(unittest.TestCase):
self.failUnless(_strptime.TimeRE(test_locale).pattern("
%
Z
") == '',
"
with
timezone
==
(
''
,
''
),
TimeRE
().
pattern
(
'%Z'
)
!=
''")
def test_matching_with_escapes(self):
# Make sure a format that requires escaping of characters works
compiled_re = self.time_re.compile("
\
w
+
%
m
")
found = compiled_re.match("
\
w
+
10
")
self.failUnless(found, "
Escaping
failed
of
format
'
\
w+
10
'")
class StrptimeTests(unittest.TestCase):
"""Tests for _strptime.strptime."""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment