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
ca7fecb0
Commit
ca7fecb0
authored
May 18, 2015
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #24102: Fixed exception type checking in standard error handlers.
parent
0a29e898
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
24 deletions
+43
-24
Lib/test/test_codeccallbacks.py
Lib/test/test_codeccallbacks.py
+23
-0
Misc/NEWS
Misc/NEWS
+2
-0
Python/codecs.c
Python/codecs.c
+18
-24
No files found.
Lib/test/test_codeccallbacks.py
View file @
ca7fecb0
...
...
@@ -961,6 +961,29 @@ class CodecCallbackTest(unittest.TestCase):
with
self
.
assertRaises
(
TypeError
):
data
.
decode
(
encoding
,
"test.replacing"
)
def
test_fake_error_class
(
self
):
handlers
=
[
codecs
.
strict_errors
,
codecs
.
ignore_errors
,
codecs
.
replace_errors
,
codecs
.
backslashreplace_errors
,
codecs
.
xmlcharrefreplace_errors
,
codecs
.
lookup_error
(
'surrogateescape'
),
codecs
.
lookup_error
(
'surrogatepass'
),
]
for
cls
in
UnicodeEncodeError
,
UnicodeDecodeError
,
UnicodeTranslateError
:
class
FakeUnicodeError
(
str
):
__class__
=
cls
for
handler
in
handlers
:
with
self
.
subTest
(
handler
=
handler
,
error_class
=
cls
):
self
.
assertRaises
(
TypeError
,
handler
,
FakeUnicodeError
())
class
FakeUnicodeError
(
Exception
):
__class__
=
cls
for
handler
in
handlers
:
with
self
.
subTest
(
handler
=
handler
,
error_class
=
cls
):
with
self
.
assertRaises
((
TypeError
,
FakeUnicodeError
)):
handler
(
FakeUnicodeError
())
if
__name__
==
"__main__"
:
unittest
.
main
()
Misc/NEWS
View file @
ca7fecb0
...
...
@@ -10,6 +10,8 @@ Release date: tba
Core and Builtins
-----------------
- Issue #24102: Fixed exception type checking in standard error handlers.
- Issue #20274: Remove ignored and erroneous "kwargs" parameters from three
METH_VARARGS methods on _sqlite.Connection.
...
...
Python/codecs.c
View file @
ca7fecb0
...
...
@@ -661,18 +661,9 @@ PyObject *PyCodec_LookupError(const char *name)
static
void
wrong_exception_type
(
PyObject
*
exc
)
{
_Py_IDENTIFIER
(
__class__
);
_Py_IDENTIFIER
(
__name__
);
PyObject
*
type
=
_PyObject_GetAttrId
(
exc
,
&
PyId___class__
);
if
(
type
!=
NULL
)
{
PyObject
*
name
=
_PyObject_GetAttrId
(
type
,
&
PyId___name__
);
Py_DECREF
(
type
);
if
(
name
!=
NULL
)
{
PyErr_Format
(
PyExc_TypeError
,
"don't know how to handle %S in error callback"
,
name
);
Py_DECREF
(
name
);
}
}
PyErr_Format
(
PyExc_TypeError
,
"don't know how to handle %.200s in error callback"
,
exc
->
ob_type
->
tp_name
);
}
PyObject
*
PyCodec_StrictErrors
(
PyObject
*
exc
)
...
...
@@ -688,15 +679,16 @@ PyObject *PyCodec_StrictErrors(PyObject *exc)
PyObject
*
PyCodec_IgnoreErrors
(
PyObject
*
exc
)
{
Py_ssize_t
end
;
if
(
PyObject_IsInstance
(
exc
,
PyExc_UnicodeEncodeError
))
{
if
(
PyObject_TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeEncodeError
))
{
if
(
PyUnicodeEncodeError_GetEnd
(
exc
,
&
end
))
return
NULL
;
}
else
if
(
PyObject_
IsInstance
(
exc
,
PyExc_UnicodeDecodeError
))
{
else
if
(
PyObject_
TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeDecodeError
))
{
if
(
PyUnicodeDecodeError_GetEnd
(
exc
,
&
end
))
return
NULL
;
}
else
if
(
PyObject_
IsInstance
(
exc
,
PyExc_UnicodeTranslateError
))
{
else
if
(
PyObject_
TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeTranslateError
))
{
if
(
PyUnicodeTranslateError_GetEnd
(
exc
,
&
end
))
return
NULL
;
}
...
...
@@ -712,7 +704,7 @@ PyObject *PyCodec_ReplaceErrors(PyObject *exc)
{
Py_ssize_t
start
,
end
,
i
,
len
;
if
(
PyObject_
IsInstance
(
exc
,
PyExc_UnicodeEncodeError
))
{
if
(
PyObject_
TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeEncodeError
))
{
PyObject
*
res
;
int
kind
;
void
*
data
;
...
...
@@ -731,14 +723,14 @@ PyObject *PyCodec_ReplaceErrors(PyObject *exc)
assert
(
_PyUnicode_CheckConsistency
(
res
,
1
));
return
Py_BuildValue
(
"(Nn)"
,
res
,
end
);
}
else
if
(
PyObject_
IsInstance
(
exc
,
PyExc_UnicodeDecodeError
))
{
else
if
(
PyObject_
TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeDecodeError
))
{
if
(
PyUnicodeDecodeError_GetEnd
(
exc
,
&
end
))
return
NULL
;
return
Py_BuildValue
(
"(Cn)"
,
(
int
)
Py_UNICODE_REPLACEMENT_CHARACTER
,
end
);
}
else
if
(
PyObject_
IsInstance
(
exc
,
PyExc_UnicodeTranslateError
))
{
else
if
(
PyObject_
TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeTranslateError
))
{
PyObject
*
res
;
int
kind
;
void
*
data
;
...
...
@@ -765,7 +757,7 @@ PyObject *PyCodec_ReplaceErrors(PyObject *exc)
PyObject
*
PyCodec_XMLCharRefReplaceErrors
(
PyObject
*
exc
)
{
if
(
PyObject_
IsInstance
(
exc
,
PyExc_UnicodeEncodeError
))
{
if
(
PyObject_
TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeEncodeError
))
{
PyObject
*
restuple
;
PyObject
*
object
;
Py_ssize_t
i
;
...
...
@@ -863,7 +855,7 @@ PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
PyObject
*
PyCodec_BackslashReplaceErrors
(
PyObject
*
exc
)
{
if
(
PyObject_
IsInstance
(
exc
,
PyExc_UnicodeEncodeError
))
{
if
(
PyObject_
TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeEncodeError
))
{
PyObject
*
restuple
;
PyObject
*
object
;
Py_ssize_t
i
;
...
...
@@ -1007,7 +999,8 @@ PyCodec_SurrogatePassErrors(PyObject *exc)
Py_ssize_t
start
;
Py_ssize_t
end
;
PyObject
*
res
;
if
(
PyObject_IsInstance
(
exc
,
PyExc_UnicodeEncodeError
))
{
if
(
PyObject_TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeEncodeError
))
{
unsigned
char
*
outp
;
if
(
PyUnicodeEncodeError_GetStart
(
exc
,
&
start
))
return
NULL
;
...
...
@@ -1078,7 +1071,7 @@ PyCodec_SurrogatePassErrors(PyObject *exc)
Py_DECREF
(
object
);
return
restuple
;
}
else
if
(
PyObject_
IsInstance
(
exc
,
PyExc_UnicodeDecodeError
))
{
else
if
(
PyObject_
TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeDecodeError
))
{
unsigned
char
*
p
;
Py_UCS4
ch
=
0
;
if
(
PyUnicodeDecodeError_GetStart
(
exc
,
&
start
))
...
...
@@ -1157,7 +1150,8 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
Py_ssize_t
start
;
Py_ssize_t
end
;
PyObject
*
res
;
if
(
PyObject_IsInstance
(
exc
,
PyExc_UnicodeEncodeError
))
{
if
(
PyObject_TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeEncodeError
))
{
char
*
outp
;
if
(
PyUnicodeEncodeError_GetStart
(
exc
,
&
start
))
return
NULL
;
...
...
@@ -1188,7 +1182,7 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
Py_DECREF
(
object
);
return
restuple
;
}
else
if
(
PyObject_
IsInstance
(
exc
,
PyExc_UnicodeDecodeError
))
{
else
if
(
PyObject_
TypeCheck
(
exc
,
(
PyTypeObject
*
)
PyExc_UnicodeDecodeError
))
{
PyObject
*
str
;
unsigned
char
*
p
;
Py_UCS2
ch
[
4
];
/* decode up to 4 bad bytes. */
...
...
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