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
ff05e522
Commit
ff05e522
authored
Sep 18, 2010
by
Florent Xicluna
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #1686: Fix string.Template when overriding the pattern attribute.
parent
71ede504
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
9 deletions
+39
-9
Lib/string.py
Lib/string.py
+3
-9
Lib/test/test_pep292.py
Lib/test/test_pep292.py
+34
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Lib/string.py
View file @
ff05e522
...
@@ -182,24 +182,18 @@ class Template:
...
@@ -182,24 +182,18 @@ class Template:
mapping = args[0]
mapping = args[0]
# Helper function for .sub()
# Helper function for .sub()
def convert(mo):
def convert(mo):
named = mo.group('named')
named = mo.group('named')
or mo.group('braced')
if named is not None:
if named is not None:
try:
try:
# We use this idiom instead of str() because the latter
# We use this idiom instead of str() because the latter
# will fail if val is a Unicode containing non-ASCII
# will fail if val is a Unicode containing non-ASCII
return '%s' % (mapping[named],)
return '%s' % (mapping[named],)
except KeyError:
except KeyError:
return self.delimiter + named
return mo.group()
braced = mo.group('braced')
if braced is not None:
try:
return '%s' % (mapping[braced],)
except KeyError:
return self.delimiter + '{' + braced + '}'
if mo.group('escaped') is not None:
if mo.group('escaped') is not None:
return self.delimiter
return self.delimiter
if mo.group('invalid') is not None:
if mo.group('invalid') is not None:
return
self.delimiter
return
mo.group()
raise ValueError('Unrecognized named group in pattern',
raise ValueError('Unrecognized named group in pattern',
self.pattern)
self.pattern)
return self.pattern.sub(convert, self.template)
return self.pattern.sub(convert, self.template)
...
...
Lib/test/test_pep292.py
View file @
ff05e522
...
@@ -125,6 +125,40 @@ class TestTemplate(unittest.TestCase):
...
@@ -125,6 +125,40 @@ class TestTemplate(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
s
.
substitute
,
{})
self
.
assertRaises
(
ValueError
,
s
.
substitute
,
{})
self
.
assertRaises
(
ValueError
,
s
.
safe_substitute
,
{})
self
.
assertRaises
(
ValueError
,
s
.
safe_substitute
,
{})
def
test_braced_override
(
self
):
class
MyTemplate
(
Template
):
pattern
=
r"""
\
$(?:
(?P<escaped>$) |
(?P<named>[_a-z][_a-z0-9]*) |
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |
(?P<invalid>) |
)
"""
tmpl
=
'PyCon in $@@location@@'
t
=
MyTemplate
(
tmpl
)
self
.
assertRaises
(
KeyError
,
t
.
substitute
,
{})
val
=
t
.
substitute
({
'location'
:
'Cleveland'
})
self
.
assertEqual
(
val
,
'PyCon in Cleveland'
)
def
test_braced_override_safe
(
self
):
class
MyTemplate
(
Template
):
pattern
=
r"""
\
$(?:
(?P<escaped>$) |
(?P<named>[_a-z][_a-z0-9]*) |
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |
(?P<invalid>) |
)
"""
tmpl
=
'PyCon in $@@location@@'
t
=
MyTemplate
(
tmpl
)
self
.
assertEqual
(
t
.
safe_substitute
(),
tmpl
)
val
=
t
.
safe_substitute
({
'location'
:
'Cleveland'
})
self
.
assertEqual
(
val
,
'PyCon in Cleveland'
)
def
test_unicode_values
(
self
):
def
test_unicode_values
(
self
):
s
=
Template
(
'$who likes $what'
)
s
=
Template
(
'$who likes $what'
)
d
=
dict
(
who
=
u't
\
xff
m'
,
what
=
u'f
\
xfe
\
f
ed'
)
d
=
dict
(
who
=
u't
\
xff
m'
,
what
=
u'f
\
xfe
\
f
ed'
)
...
...
Misc/NEWS
View file @
ff05e522
...
@@ -34,6 +34,8 @@ Core and Builtins
...
@@ -34,6 +34,8 @@ Core and Builtins
Library
Library
-------
-------
-
Issue
#
1686
:
Fix
string
.
Template
when
overriding
the
pattern
attribute
.
-
Issue
#
11866
:
Eliminated
race
condition
in
the
computation
of
names
-
Issue
#
11866
:
Eliminated
race
condition
in
the
computation
of
names
for
new
threads
.
for
new
threads
.
...
...
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