From 972e53865b79abddded73de352bf57d8ad0581ed Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 14 Oct 2018 19:37:27 +0300 Subject: [PATCH] bpo-34984: Improve error messages for bytes and bytearray constructors. --- Lib/test/test_bytes.py | 2 ++ Objects/bytearrayobject.c | 24 ++++++++++++++++++++---- Objects/bytesobject.c | 5 +++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index b7b48bfe17f6ee..4fadf1113b9e2b 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -152,6 +152,8 @@ class C: self.assertRaises(TypeError, self.type2test, [0.0]) self.assertRaises(TypeError, self.type2test, [None]) self.assertRaises(TypeError, self.type2test, [C()]) + self.assertRaises(TypeError, self.type2test, encoding='ascii') + self.assertRaises(TypeError, self.type2test, errors='ignore') self.assertRaises(TypeError, self.type2test, 0, 'ascii') self.assertRaises(TypeError, self.type2test, b'', 'ascii') self.assertRaises(TypeError, self.type2test, 0, errors='ignore') diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 4e2bb603151b92..3072c7d046ec1b 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -784,7 +784,9 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) if (arg == NULL) { if (encoding != NULL || errors != NULL) { PyErr_SetString(PyExc_TypeError, - "encoding or errors without sequence argument"); + encoding != NULL ? + "encoding without a string argument" : + "errors without a string argument"); return -1; } return 0; @@ -813,7 +815,9 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) /* If it's not unicode, there can't be encoding or errors */ if (encoding != NULL || errors != NULL) { PyErr_SetString(PyExc_TypeError, - "encoding or errors without a string argument"); + encoding != NULL ? + "encoding without a string argument" : + "errors without a string argument"); return -1; } @@ -861,8 +865,14 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) /* Get the iterator */ it = PyObject_GetIter(arg); - if (it == NULL) + if (it == NULL) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) { + PyErr_Format(PyExc_TypeError, + "cannot convert '%.200s' object to bytearray", + arg->ob_type->tp_name); + } return -1; + } iternext = *Py_TYPE(it)->tp_iternext; /* Run the iterator to exhaustion */ @@ -1627,8 +1637,14 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) } it = PyObject_GetIter(iterable_of_ints); - if (it == NULL) + if (it == NULL) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) { + PyErr_Format(PyExc_TypeError, + "can't extend bytearray with %.100s", + iterable_of_ints->ob_type->tp_name); + } return NULL; + } /* Try to determine the length of the argument. 32 is arbitrary. */ buf_size = PyObject_LengthHint(iterable_of_ints, 32); diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index d51d1ba023c385..4aa78a08c28c4e 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -2537,8 +2537,9 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (x == NULL) { if (encoding != NULL || errors != NULL) { PyErr_SetString(PyExc_TypeError, - "encoding or errors without sequence " - "argument"); + encoding != NULL ? + "encoding without a string argument" : + "errors without a string argument"); return NULL; } return PyBytes_FromStringAndSize(NULL, 0);