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
184252ad
Commit
184252ad
authored
Jun 16, 2012
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix "%f" format of str%args if the result is not an ASCII or latin1 string
parent
9a77770a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
17 deletions
+21
-17
Lib/test/test_format.py
Lib/test/test_format.py
+2
-0
Objects/unicodeobject.c
Objects/unicodeobject.c
+19
-17
No files found.
Lib/test/test_format.py
View file @
184252ad
...
...
@@ -265,6 +265,8 @@ class FormatTest(unittest.TestCase):
raise
TestFailed
(
'"%*d"%(maxsize, -127) should fail'
)
def
test_non_ascii
(
self
):
testformat
(
"
\
u20ac
=%f"
,
(
1.0
,),
"
\
u20ac
=1.000000"
)
self
.
assertEqual
(
format
(
"abc"
,
"
\
u2007
<5"
),
"abc
\
u2007
\
u2007
"
)
self
.
assertEqual
(
format
(
123
,
"
\
u2007
<5"
),
"123
\
u2007
\
u2007
"
)
self
.
assertEqual
(
format
(
12.3
,
"
\
u2007
<6"
),
"12.3
\
u2007
\
u2007
"
)
...
...
Objects/unicodeobject.c
View file @
184252ad
...
...
@@ -1660,34 +1660,34 @@ unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos,
}
/* Copy a ASCII or latin1 char* string into a Python Unicode string.
Return the length of the input string.
WARNING: The function doesn't copy the terminating null character and
doesn't check the maximum character (may write a latin1 character in an
ASCII string). */
static
Py_ssize_t
unicode_write_cstr
(
PyObject
*
unicode
,
Py_ssize_t
index
,
const
char
*
str
)
static
void
unicode_write_cstr
(
PyObject
*
unicode
,
Py_ssize_t
index
,
const
char
*
str
,
Py_ssize_t
len
)
{
enum
PyUnicode_Kind
kind
=
PyUnicode_KIND
(
unicode
);
void
*
data
=
PyUnicode_DATA
(
unicode
);
const
char
*
end
=
str
+
len
;
switch
(
kind
)
{
case
PyUnicode_1BYTE_KIND
:
{
Py_ssize_t
len
=
strlen
(
str
);
assert
(
index
+
len
<=
PyUnicode_GET_LENGTH
(
unicode
));
memcpy
((
char
*
)
data
+
index
,
str
,
len
);
return
len
;
break
;
}
case
PyUnicode_2BYTE_KIND
:
{
Py_UCS2
*
start
=
(
Py_UCS2
*
)
data
+
index
;
Py_UCS2
*
ucs2
=
start
;
assert
(
index
<=
PyUnicode_GET_LENGTH
(
unicode
));
for
(;
*
str
;
++
ucs2
,
++
str
)
for
(;
str
<
end
;
++
ucs2
,
++
str
)
*
ucs2
=
(
Py_UCS2
)
*
str
;
assert
((
ucs2
-
start
)
<=
PyUnicode_GET_LENGTH
(
unicode
));
return
ucs2
-
start
;
break
;
}
default:
{
Py_UCS4
*
start
=
(
Py_UCS4
*
)
data
+
index
;
...
...
@@ -1695,11 +1695,10 @@ unicode_write_cstr(PyObject *unicode, Py_ssize_t index, const char *str)
assert
(
kind
==
PyUnicode_4BYTE_KIND
);
assert
(
index
<=
PyUnicode_GET_LENGTH
(
unicode
));
for
(;
*
str
;
++
ucs4
,
++
str
)
for
(;
str
<
end
;
++
ucs4
,
++
str
)
*
ucs4
=
(
Py_UCS4
)
*
str
;
assert
((
ucs4
-
start
)
<=
PyUnicode_GET_LENGTH
(
unicode
));
return
ucs4
-
start
;
}
}
}
...
...
@@ -2730,17 +2729,18 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
case
'x'
:
case
'p'
:
{
Py_ssize_t
writt
en
;
Py_ssize_t
l
en
;
/* unused, since we already have the result */
if
(
*
f
==
'p'
)
(
void
)
va_arg
(
vargs
,
void
*
);
else
(
void
)
va_arg
(
vargs
,
int
);
/* extract the result from numberresults and append. */
written
=
unicode_write_cstr
(
string
,
i
,
numberresult
);
len
=
strlen
(
numberresult
);
unicode_write_cstr
(
string
,
i
,
numberresult
,
len
);
/* skip over the separating '\0' */
i
+=
writt
en
;
numberresult
+=
writt
en
;
i
+=
l
en
;
numberresult
+=
l
en
;
assert
(
*
numberresult
==
'\0'
);
numberresult
++
;
assert
(
numberresult
<=
numberresults
+
numbersize
);
...
...
@@ -2812,10 +2812,14 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
PyUnicode_WRITE
(
kind
,
data
,
i
++
,
'%'
);
break
;
default:
i
+=
unicode_write_cstr
(
string
,
i
,
p
);
{
Py_ssize_t
len
=
strlen
(
p
);
unicode_write_cstr
(
string
,
i
,
p
,
len
);
i
+=
len
;
assert
(
i
==
PyUnicode_GET_LENGTH
(
string
));
goto
end
;
}
}
}
else
{
assert
(
i
<
PyUnicode_GET_LENGTH
(
string
));
...
...
@@ -13211,9 +13215,7 @@ formatfloat(PyObject *v, int flags, int prec, int type,
if
(
writer
)
{
if
(
_PyUnicodeWriter_Prepare
(
writer
,
len
,
127
)
==
-
1
)
return
-
1
;
memcpy
((
char
*
)
writer
->
data
+
writer
->
pos
*
writer
->
kind
,
p
,
len
);
unicode_write_cstr
(
writer
->
buffer
,
writer
->
pos
,
p
,
len
);
writer
->
pos
+=
len
;
}
else
...
...
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