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
d25c6504
Commit
d25c6504
authored
Jul 23, 2004
by
Marc-André Lemburg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
parent
fe080838
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
10 deletions
+20
-10
Lib/test/test_unicode.py
Lib/test/test_unicode.py
+8
-0
Objects/unicodeobject.c
Objects/unicodeobject.c
+12
-10
No files found.
Lib/test/test_unicode.py
View file @
d25c6504
...
...
@@ -438,6 +438,14 @@ class UnicodeTest(
self
.
assertEqual
(
unicode
(
o
),
u'unicode(obj) is compatible to str()'
)
self
.
assertEqual
(
str
(
o
),
'unicode(obj) is compatible to str()'
)
# %-formatting and .__unicode__()
self
.
assertEqual
(
u'%s'
%
UnicodeCompat
(
u"u'%s' % obj uses obj.__unicode__()"
),
u"u'%s' % obj uses obj.__unicode__()"
)
self
.
assertEqual
(
u'%s'
%
UnicodeCompat
(
u"u'%s' % obj falls back to obj.__str__()"
),
u"u'%s' % obj falls back to obj.__str__()"
)
for
obj
in
(
123
,
123.45
,
123L
):
self
.
assertEqual
(
unicode
(
obj
),
unicode
(
str
(
obj
)))
...
...
Objects/unicodeobject.c
View file @
d25c6504
...
...
@@ -6883,20 +6883,15 @@ PyObject *PyUnicode_Format(PyObject *format,
else
{
PyObject
*
unicode
;
if
(
c
==
's'
)
temp
=
PyObject_
Str
(
v
);
temp
=
PyObject_
Unicode
(
v
);
else
temp
=
PyObject_Repr
(
v
);
if
(
temp
==
NULL
)
goto
onError
;
if
(
!
PyString_Check
(
temp
))
{
/* XXX Note: this should never happen, since
PyObject_Repr() and PyObject_Str() assure
this */
Py_DECREF
(
temp
);
PyErr_SetString
(
PyExc_TypeError
,
"%s argument has non-string str()"
);
goto
onError
;
}
if
(
PyUnicode_Check
(
temp
))
/* nothing to do */
;
else
if
(
PyString_Check
(
temp
))
{
/* convert to string to Unicode */
unicode
=
PyUnicode_Decode
(
PyString_AS_STRING
(
temp
),
PyString_GET_SIZE
(
temp
),
NULL
,
...
...
@@ -6905,6 +6900,13 @@ PyObject *PyUnicode_Format(PyObject *format,
temp
=
unicode
;
if
(
temp
==
NULL
)
goto
onError
;
}
else
{
Py_DECREF
(
temp
);
PyErr_SetString
(
PyExc_TypeError
,
"%s argument has non-string str()"
);
goto
onError
;
}
}
pbuf
=
PyUnicode_AS_UNICODE
(
temp
);
len
=
PyUnicode_GET_SIZE
(
temp
);
...
...
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