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
4ff33af2
Commit
4ff33af2
authored
Apr 05, 2014
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #21118: Add unit test for invalid character replacement (code point higher than U+10ffff)
parent
89a76abf
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
10 deletions
+18
-10
Lib/test/test_unicode.py
Lib/test/test_unicode.py
+8
-0
Objects/unicodeobject.c
Objects/unicodeobject.c
+10
-10
No files found.
Lib/test/test_unicode.py
View file @
4ff33af2
...
...
@@ -280,6 +280,14 @@ class UnicodeTest(string_tests.CommonTest,
self
.
assertEqual
(
"[
\
xe9
]"
.
translate
(
str
.
maketrans
({
'
\
xe9
'
:
None
})),
"[]"
)
# invalid Unicode characters
invalid_char
=
0x10ffff
+
1
for
before
in
"a
\
xe9
\
u20ac
\
U0010ffff
"
:
mapping
=
str
.
maketrans
({
before
:
invalid_char
})
text
=
"[%s]"
%
before
self
.
assertRaises
(
ValueError
,
text
.
translate
,
mapping
)
# errors
self
.
assertRaises
(
TypeError
,
self
.
type2test
.
maketrans
)
self
.
assertRaises
(
ValueError
,
self
.
type2test
.
maketrans
,
'abc'
,
'defg'
)
self
.
assertRaises
(
TypeError
,
self
.
type2test
.
maketrans
,
2
,
'def'
)
...
...
Objects/unicodeobject.c
View file @
4ff33af2
...
...
@@ -8473,10 +8473,10 @@ charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result)
}
else
if
(
PyLong_Check
(
x
))
{
long
value
=
PyLong_AS_LONG
(
x
);
long
max
=
PyUnicode_GetMax
();
if
(
value
<
0
||
value
>
max
)
{
PyErr_Format
(
PyExc_TypeError
,
"character mapping must be in range(0x%x)"
,
max
+
1
);
if
(
value
<
0
||
value
>
MAX_UNICODE
)
{
PyErr_Format
(
PyExc_ValueError
,
"character mapping must be in range(0x%x)"
,
MAX_UNICODE
+
1
);
Py_DECREF
(
x
);
return
-
1
;
}
...
...
@@ -8522,7 +8522,9 @@ charmaptranslate_output(Py_UCS4 ch, PyObject *mapping,
}
if
(
PyLong_Check
(
item
))
{
Py_UCS4
ch
=
(
Py_UCS4
)
PyLong_AS_LONG
(
item
);
long
ch
=
(
Py_UCS4
)
PyLong_AS_LONG
(
item
);
/* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already
used it */
if
(
_PyUnicodeWriter_WriteCharInline
(
writer
,
ch
)
<
0
)
{
Py_DECREF
(
item
);
return
-
1
;
...
...
@@ -8570,11 +8572,9 @@ unicode_fast_translate_lookup(PyObject *mapping, Py_UCS1 ch,
if
(
PyLong_Check
(
item
))
{
long
replace
=
(
Py_UCS4
)
PyLong_AS_LONG
(
item
);
if
(
replace
==
-
1
)
{
Py_DECREF
(
item
);
return
-
1
;
}
if
(
replace
<
0
||
127
<
replace
)
{
/* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already
used it */
if
(
127
<
replace
)
{
/* invalid character or character outside ASCII:
skip the fast translate */
goto
exit
;
...
...
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