@@ -783,7 +783,9 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
783783 if (arg == NULL ) {
784784 if (encoding != NULL || errors != NULL ) {
785785 PyErr_SetString (PyExc_TypeError ,
786- "encoding or errors without sequence argument" );
786+ encoding != NULL ?
787+ "encoding without a string argument" :
788+ "errors without a string argument" );
787789 return -1 ;
788790 }
789791 return 0 ;
@@ -812,7 +814,9 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
812814 /* If it's not unicode, there can't be encoding or errors */
813815 if (encoding != NULL || errors != NULL ) {
814816 PyErr_SetString (PyExc_TypeError ,
815- "encoding or errors without a string argument" );
817+ encoding != NULL ?
818+ "encoding without a string argument" :
819+ "errors without a string argument" );
816820 return -1 ;
817821 }
818822
@@ -860,8 +864,14 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
860864
861865 /* Get the iterator */
862866 it = PyObject_GetIter (arg );
863- if (it == NULL )
867+ if (it == NULL ) {
868+ if (PyErr_ExceptionMatches (PyExc_TypeError )) {
869+ PyErr_Format (PyExc_TypeError ,
870+ "cannot convert '%.200s' object to bytearray" ,
871+ arg -> ob_type -> tp_name );
872+ }
864873 return -1 ;
874+ }
865875 iternext = * Py_TYPE (it )-> tp_iternext ;
866876
867877 /* Run the iterator to exhaustion */
@@ -1626,8 +1636,14 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
16261636 }
16271637
16281638 it = PyObject_GetIter (iterable_of_ints );
1629- if (it == NULL )
1639+ if (it == NULL ) {
1640+ if (PyErr_ExceptionMatches (PyExc_TypeError )) {
1641+ PyErr_Format (PyExc_TypeError ,
1642+ "can't extend bytearray with %.100s" ,
1643+ iterable_of_ints -> ob_type -> tp_name );
1644+ }
16301645 return NULL ;
1646+ }
16311647
16321648 /* Try to determine the length of the argument. 32 is arbitrary. */
16331649 buf_size = PyObject_LengthHint (iterable_of_ints , 32 );
0 commit comments