Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Lib/test/test_codeccallbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,33 @@ def custom_handler(exc):
self.assertTrue(_codecs_unregister_error(custom_name))
self.assertRaises(LookupError, codecs.lookup_error, custom_name)

def test_unicode_error_args(self):
def handler_decode(exc):
self.assertEqual(exc.args[0], exc.encoding)
self.assertEqual(exc.args[1], exc.object)
self.assertEqual(exc.args[2], exc.start)
self.assertEqual(exc.args[3], exc.end)
self.assertEqual(exc.args[4], exc.reason)
return '?', exc.end

codecs.register_error('test_args', handler_decode)
result = b'\x80\xd0'.decode('utf-8', 'test_args')
self.assertEqual(result, '??')
self.assertTrue(_codecs_unregister_error('test_args'))

def handler_encode(exc):
self.assertEqual(exc.args[0], exc.encoding)
self.assertEqual(exc.args[1], exc.object)
self.assertEqual(exc.args[2], exc.start)
self.assertEqual(exc.args[3], exc.end)
self.assertEqual(exc.args[4], exc.reason)
return b'?', exc.end

codecs.register_error('test_args', handler_encode)
result = '\u1111 \u2222'.encode('ascii', 'test_args')
self.assertEqual(result, b'? ?')
self.assertTrue(_codecs_unregister_error('test_args'))

def test_unregister_custom_unknown_error_handler(self):
unknown_name = 'test.test_unregister_custom_unknown_error_handler'
self.assertRaises(LookupError, codecs.lookup_error, unknown_name)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def testAttributes(self):
'start' : 0, 'reason' : 'ordinal not in range'}),
(UnicodeDecodeError, ('ascii', bytearray(b'\xff'), 0, 1,
'ordinal not in range'), {},
{'args' : ('ascii', bytearray(b'\xff'), 0, 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is setting off some alarm bells for me. If we're changing tests like this, we're probably breaking something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a bug fix, there is a mismatch:

>>> exc = UnicodeDecodeError('ascii', bytearray(b'\xff'), 0, 1,'ordinal not in range')
>>> exc.args # 1 is the object
('ascii', bytearray(b'\xff'), 0, 1, 'ordinal not in range')
>>> exc.object
b'\xff'
>>> 

{'args' : ('ascii', b'\xff', 0, 1,
'ordinal not in range'),
'encoding' : 'ascii', 'object' : b'\xff',
'start' : 0, 'reason' : 'ordinal not in range'}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:exc:`UnicodeDecodeError` and :exc:`UnicodeEncodeError` now update the ``args``
tuple.
21 changes: 19 additions & 2 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3223,6 +3223,23 @@ unicode_error_adjust_end(Py_ssize_t end, Py_ssize_t objlen)
#define PyUnicodeErrorObject_CAST(op) \
(assert(PyUnicodeError_Check(op)), ((PyUnicodeErrorObject *)(op)))

static PyObject *
UnicodeError_args_get(PyObject *op, void *context)
{
PyUnicodeErrorObject *self = PyUnicodeErrorObject_CAST(op);
return Py_BuildValue("(OOnnO)",
self->encoding ? self->encoding : Py_None,
self->object ? self->object : Py_None,
self->start,
self->end,
self->reason ? self->reason : Py_None);
}

static PyGetSetDef UnicodeError_getset[] = {
{"args", UnicodeError_args_get, NULL, NULL},
{NULL}
};

/* Assert some properties of the adjusted 'end' value. */
#ifndef NDEBUG
static void
Expand Down Expand Up @@ -3734,7 +3751,7 @@ static PyTypeObject _PyExc_UnicodeEncodeError = {
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
PyDoc_STR("Unicode encoding error."), UnicodeError_traverse,
UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
UnicodeError_getset, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
UnicodeEncodeError_init, 0, BaseException_new,
};
PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
Expand Down Expand Up @@ -3847,7 +3864,7 @@ static PyTypeObject _PyExc_UnicodeDecodeError = {
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
PyDoc_STR("Unicode decoding error."), UnicodeError_traverse,
UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
UnicodeError_getset, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
UnicodeDecodeError_init, 0, BaseException_new,
};
PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
Expand Down
Loading