Issue #20335: bytes constructor now raises TypeError when encoding or errors

is specified with non-string argument.  Based on patch by Renaud Blanch.
This commit is contained in:
Serhiy Storchaka 2014-12-02 09:24:06 +02:00
parent 9d98c9bf42
commit 83cf99d733
3 changed files with 18 additions and 7 deletions

View file

@ -2503,6 +2503,13 @@ 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) {
PyErr_SetString(PyExc_TypeError,
"encoding or errors without a string argument");
return NULL;
}
/* We'd like to call PyObject_Bytes here, but we need to check for an
integer argument before deferring to PyBytes_FromObject, something
PyObject_Bytes doesn't do. */
@ -2544,13 +2551,6 @@ 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) {
PyErr_SetString(PyExc_TypeError,
"encoding or errors without a string argument");
return NULL;
}
return PyBytes_FromObject(x);
}