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
975134e2
Commit
975134e2
authored
Nov 22, 2011
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #13093: Fix error handling on PyUnicode_EncodeDecimal()
Add tests for PyUnicode_EncodeDecimal()
parent
5aa7df32
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
4 deletions
+64
-4
Lib/test/test_unicode.py
Lib/test/test_unicode.py
+25
-0
Modules/_testcapimodule.c
Modules/_testcapimodule.c
+36
-0
Objects/unicodeobject.c
Objects/unicodeobject.c
+3
-4
No files found.
Lib/test/test_unicode.py
View file @
975134e2
...
...
@@ -1606,6 +1606,31 @@ class UnicodeTest(
self
.
assertEqual
(
"%s"
%
u
,
u'__unicode__ overridden'
)
self
.
assertEqual
(
"{}"
.
format
(
u
),
'__unicode__ overridden'
)
def
test_encode_decimal
(
self
):
from
_testcapi
import
unicode_encodedecimal
self
.
assertEqual
(
unicode_encodedecimal
(
u'123'
),
b'123'
)
self
.
assertEqual
(
unicode_encodedecimal
(
u'
\
u0663
.
\
u0661
\
u0664
'
),
b'3.14'
)
self
.
assertEqual
(
unicode_encodedecimal
(
u"
\
N{EM SPACE}
3.14
\
N{EN SPACE}
"
),
b' 3.14 '
)
self
.
assertRaises
(
UnicodeEncodeError
,
unicode_encodedecimal
,
u"123
\
u20ac
"
,
"strict"
)
self
.
assertEqual
(
unicode_encodedecimal
(
u"123
\
u20ac
"
,
"replace"
),
b'123?'
)
self
.
assertEqual
(
unicode_encodedecimal
(
u"123
\
u20ac
"
,
"ignore"
),
b'123'
)
self
.
assertEqual
(
unicode_encodedecimal
(
u"123
\
u20ac
"
,
"xmlcharrefreplace"
),
b'123€'
)
self
.
assertEqual
(
unicode_encodedecimal
(
u"123
\
u20ac
"
,
"backslashreplace"
),
b'123
\
\
u20ac'
)
self
.
assertEqual
(
unicode_encodedecimal
(
u"123
\
u20ac
\
N{EM SPACE}
"
,
"replace"
),
b'123? '
)
self
.
assertEqual
(
unicode_encodedecimal
(
u"123
\
u20ac
\
u20ac
"
,
"replace"
),
b'123??'
)
self
.
assertEqual
(
unicode_encodedecimal
(
u"123
\
u20ac
\
u0660
"
,
"replace"
),
b'123?0'
)
def
test_main
():
test_support
.
run_unittest
(
__name__
)
...
...
Modules/_testcapimodule.c
View file @
975134e2
...
...
@@ -1105,6 +1105,41 @@ test_widechar(PyObject *self)
Py_RETURN_NONE
;
}
static
PyObject
*
unicode_encodedecimal
(
PyObject
*
self
,
PyObject
*
args
)
{
Py_UNICODE
*
unicode
;
Py_ssize_t
length
;
char
*
errors
=
NULL
;
PyObject
*
decimal
;
Py_ssize_t
decimal_length
,
new_length
;
int
res
;
if
(
!
PyArg_ParseTuple
(
args
,
"u#|s"
,
&
unicode
,
&
length
,
&
errors
))
return
NULL
;
decimal_length
=
length
*
7
;
/* len('€') */
decimal
=
PyBytes_FromStringAndSize
(
NULL
,
decimal_length
);
if
(
decimal
==
NULL
)
return
NULL
;
res
=
PyUnicode_EncodeDecimal
(
unicode
,
length
,
PyBytes_AS_STRING
(
decimal
),
errors
);
if
(
res
<
0
)
{
Py_DECREF
(
decimal
);
return
NULL
;
}
new_length
=
strlen
(
PyBytes_AS_STRING
(
decimal
));
assert
(
new_length
<=
decimal_length
);
res
=
_PyBytes_Resize
(
&
decimal
,
new_length
);
if
(
res
<
0
)
return
NULL
;
return
decimal
;
}
static
PyObject
*
test_empty_argparse
(
PyObject
*
self
)
{
...
...
@@ -1698,6 +1733,7 @@ static PyMethodDef TestMethods[] = {
#ifdef Py_USING_UNICODE
{
"test_u_code"
,
(
PyCFunction
)
test_u_code
,
METH_NOARGS
},
{
"test_widechar"
,
(
PyCFunction
)
test_widechar
,
METH_NOARGS
},
{
"unicode_encodedecimal"
,
unicode_encodedecimal
,
METH_VARARGS
},
#endif
#ifdef WITH_THREAD
{
"_test_thread_state"
,
test_thread_state
,
METH_VARARGS
},
...
...
Objects/unicodeobject.c
View file @
975134e2
...
...
@@ -5160,11 +5160,10 @@ int PyUnicode_EncodeDecimal(Py_UNICODE *s,
}
/* All other characters are considered unencodable */
collstart
=
p
;
collend
=
p
+
1
;
while
(
collend
<
end
)
{
for
(
collend
=
p
+
1
;
collend
<
end
;
collend
++
)
{
if
((
0
<
*
collend
&&
*
collend
<
256
)
||
!
Py_UNICODE_ISSPACE
(
*
collend
)
||
Py_UNICODE_TODECIMAL
(
*
collend
))
Py_UNICODE_ISSPACE
(
*
collend
)
||
0
<=
Py_UNICODE_TODECIMAL
(
*
collend
))
break
;
}
/* cache callback name lookup
...
...
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