diff options
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r-- | Objects/bytesobject.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 8e09849..d8fb96b 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -3175,11 +3175,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return PyBytes_FromStringAndSize(NULL, 0); } - if (PyUnicode_Check(x)) { + if (encoding != NULL) { /* Encode via the codec registry */ - if (encoding == NULL) { + if (!PyUnicode_Check(x)) { PyErr_SetString(PyExc_TypeError, - "string argument without an encoding"); + "encoding without a string argument"); return NULL; } new = PyUnicode_AsEncodedString(x, encoding, errors); @@ -3189,10 +3189,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return new; } - /* If it's not unicode, there can't be encoding or errors */ - if (encoding != NULL || errors != NULL) { + if (errors != NULL) { PyErr_SetString(PyExc_TypeError, - "encoding or errors without a string argument"); + PyUnicode_Check(x) ? + "string argument without an encoding" : + "errors without a string argument"); return NULL; } @@ -3217,6 +3218,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) else if (PyErr_Occurred()) return NULL; + if (PyUnicode_Check(x)) { + PyErr_SetString(PyExc_TypeError, + "string argument without an encoding"); + return NULL; + } /* Is it an integer? */ size = PyNumber_AsSsize_t(x, PyExc_OverflowError); if (size == -1 && PyErr_Occurred()) { |