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
da2c7ebd
Commit
da2c7ebd
authored
Mar 23, 2013
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow any type with __getitem__ to be a mapping for the purposes of % (#15801)
parent
7e2f197a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
15 additions
and
4 deletions
+15
-4
Lib/test/string_tests.py
Lib/test/string_tests.py
+4
-0
Misc/NEWS
Misc/NEWS
+7
-0
Objects/stringobject.c
Objects/stringobject.c
+2
-2
Objects/unicodeobject.c
Objects/unicodeobject.c
+2
-2
No files found.
Lib/test/string_tests.py
View file @
da2c7ebd
...
...
@@ -1130,6 +1130,10 @@ class MixinStrUnicodeUserStringTest:
class
X
(
object
):
pass
self
.
checkraises
(
TypeError
,
'abc'
,
'__mod__'
,
X
())
class
X
(
Exception
):
def
__getitem__
(
self
,
k
):
return
k
self
.
checkequal
(
'melon apple'
,
'%(melon)s %(apple)s'
,
'__mod__'
,
X
())
def
test_floatformatting
(
self
):
# float formatting
...
...
Misc/NEWS
View file @
da2c7ebd
...
...
@@ -6,6 +6,13 @@ What's New in Python 2.7.4?
*Release date: XXXX-XX-XX*
Core and Builtins
-----------------
- Issue #15801 (again): With string % formatting, relax the type check for a
mapping such that any type with a __getitem__ can be used on the right hand
side.
Library
-------
...
...
Objects/stringobject.c
View file @
da2c7ebd
...
...
@@ -4257,8 +4257,8 @@ PyString_Format(PyObject *format, PyObject *args)
arglen
=
-
1
;
argidx
=
-
2
;
}
if
(
Py
Mapping_Check
(
args
)
&&
!
PyTuple_Check
(
args
)
&&
!
PyObject_TypeCheck
(
args
,
&
PyBaseString_Type
))
if
(
Py
_TYPE
(
args
)
->
tp_as_mapping
&&
Py_TYPE
(
args
)
->
tp_as_mapping
->
mp_subscript
&&
!
Py
Tuple_Check
(
args
)
&&
!
Py
Object_TypeCheck
(
args
,
&
PyBaseString_Type
))
dict
=
args
;
while
(
--
fmtcnt
>=
0
)
{
if
(
*
fmt
!=
'%'
)
{
...
...
Objects/unicodeobject.c
View file @
da2c7ebd
...
...
@@ -8287,8 +8287,8 @@ PyObject *PyUnicode_Format(PyObject *format,
arglen
=
-
1
;
argidx
=
-
2
;
}
if
(
Py
Mapping_Check
(
args
)
&&
!
PyTuple_Check
(
args
)
&&
!
PyObject_TypeCheck
(
args
,
&
PyBaseString_Type
))
if
(
Py
_TYPE
(
args
)
->
tp_as_mapping
&&
Py_TYPE
(
args
)
->
tp_as_mapping
->
mp_subscript
&&
!
Py
Tuple_Check
(
args
)
&&
!
Py
Object_TypeCheck
(
args
,
&
PyBaseString_Type
))
dict
=
args
;
while
(
--
fmtcnt
>=
0
)
{
...
...
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