diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-10-21 15:29:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-21 15:29:12 +0300 |
commit | 2c2044e789875ea736ec42e216fdbe61816fbe28 (patch) | |
tree | 0dd0b52b1167f07473c0c5c13f2a519ac8c06afb /Objects/bytearrayobject.c | |
parent | 914f9a078f997e58cfcfabcbb30fafdd1f277bef (diff) | |
download | cpython-git-2c2044e789875ea736ec42e216fdbe61816fbe28.tar.gz |
bpo-34984: Improve error messages for bytes and bytearray constructors. (GH-9874)
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r-- | Objects/bytearrayobject.c | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 58fe53bcca..782e275697 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -783,7 +783,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; @@ -812,7 +814,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; } @@ -860,8 +864,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 */ @@ -1626,8 +1636,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); |