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
bc32fee0
Commit
bc32fee0
authored
Feb 18, 2008
by
Eric Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added code to correct combining str and unicode in ''.format(). Added test case.
parent
5299935b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
0 deletions
+25
-0
Lib/test/test_unicode.py
Lib/test/test_unicode.py
+9
-0
Objects/stringlib/string_format.h
Objects/stringlib/string_format.h
+16
-0
No files found.
Lib/test/test_unicode.py
View file @
bc32fee0
...
@@ -1088,6 +1088,15 @@ class UnicodeTest(
...
@@ -1088,6 +1088,15 @@ class UnicodeTest(
self
.
assertRaises
(
ValueError
,
format
,
""
,
"-"
)
self
.
assertRaises
(
ValueError
,
format
,
""
,
"-"
)
self
.
assertRaises
(
ValueError
,
"{0:=s}"
.
format
,
''
)
self
.
assertRaises
(
ValueError
,
"{0:=s}"
.
format
,
''
)
# test combining string and unicode
self
.
assertEqual
(
u"foo{0}"
.
format
(
'bar'
),
u'foobar'
)
# This will try to convert the argument from unicode to str, which
# will succeed
self
.
assertEqual
(
"foo{0}"
.
format
(
u'bar'
),
'foobar'
)
# This will try to convert the argument from unicode to str, which
# will fail
self
.
assertRaises
(
UnicodeEncodeError
,
"foo{0}"
.
format
,
u'
\
u1000
bar'
)
def
test_main
():
def
test_main
():
test_support
.
run_unittest
(
__name__
)
test_support
.
run_unittest
(
__name__
)
...
...
Objects/stringlib/string_format.h
View file @
bc32fee0
...
@@ -493,6 +493,22 @@ render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
...
@@ -493,6 +493,22 @@ render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
if
(
result
==
NULL
)
if
(
result
==
NULL
)
goto
done
;
goto
done
;
#if PY_VERSION_HEX >= 0x03000000
assert
(
PyString_Check
(
result
));
#else
assert
(
PyString_Check
(
result
)
||
PyUnicode_Check
(
result
));
/* Convert result to our type. We could be str, and result could
be unicode */
{
PyObject
*
tmp
=
STRINGLIB_TOSTR
(
result
);
if
(
tmp
==
NULL
)
goto
done
;
Py_DECREF
(
result
);
result
=
tmp
;
}
#endif
ok
=
output_data
(
output
,
ok
=
output_data
(
output
,
STRINGLIB_STR
(
result
),
STRINGLIB_LEN
(
result
));
STRINGLIB_STR
(
result
),
STRINGLIB_LEN
(
result
));
done:
done:
...
...
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