diff options
author | Alexandre Vassalotti <alexandre@peadrop.com> | 2009-12-31 03:56:09 (GMT) |
---|---|---|
committer | Alexandre Vassalotti <alexandre@peadrop.com> | 2009-12-31 03:56:09 (GMT) |
commit | eb6f8de8bf105b4d6e9e21898e76c5647a48a3c9 (patch) | |
tree | 8ea05c9c5d2af1b3c65c15a1c9c8dfbb0063576c /Objects/bytesobject.c | |
parent | b05e73d9c86872c3f0ebc7d6a157860ebc28ca2f (diff) | |
download | cpython-eb6f8de8bf105b4d6e9e21898e76c5647a48a3c9.zip cpython-eb6f8de8bf105b4d6e9e21898e76c5647a48a3c9.tar.gz cpython-eb6f8de8bf105b4d6e9e21898e76c5647a48a3c9.tar.bz2 |
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r-- | Objects/bytesobject.c | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 41eee40..cb63448 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -2884,6 +2884,7 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) const char *encoding = NULL; const char *errors = NULL; PyObject *new = NULL; + Py_ssize_t size; static char *kwlist[] = {"source", "encoding", "errors", 0}; if (type != &PyBytes_Type) @@ -2914,6 +2915,25 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) assert(PyBytes_Check(new)); return new; } + /* Is it an integer? */ + size = PyNumber_AsSsize_t(x, PyExc_ValueError); + if (size == -1 && PyErr_Occurred()) { + PyErr_Clear(); + } + else { + if (size < 0) { + PyErr_SetString(PyExc_ValueError, "negative count"); + return NULL; + } + new = PyBytes_FromStringAndSize(NULL, size); + if (new == NULL) { + return NULL; + } + if (size > 0) { + memset(((PyBytesObject*)new)->ob_sval, 0, size); + } + return new; + } /* If it's not unicode, there can't be encoding or errors */ if (encoding != NULL || errors != NULL) { @@ -2934,27 +2954,6 @@ PyBytes_FromObject(PyObject *x) PyErr_BadInternalCall(); return NULL; } - - /* Is it an int? */ - size = PyNumber_AsSsize_t(x, PyExc_ValueError); - if (size == -1 && PyErr_Occurred()) { - PyErr_Clear(); - } - else { - if (size < 0) { - PyErr_SetString(PyExc_ValueError, "negative count"); - return NULL; - } - new = PyBytes_FromStringAndSize(NULL, size); - if (new == NULL) { - return NULL; - } - if (size > 0) { - memset(((PyBytesObject*)new)->ob_sval, 0, size); - } - return new; - } - /* Use the modern buffer interface */ if (PyObject_CheckBuffer(x)) { Py_buffer view; @@ -2974,6 +2973,11 @@ PyBytes_FromObject(PyObject *x) PyBuffer_Release(&view); return NULL; } + if (PyUnicode_Check(x)) { + PyErr_SetString(PyExc_TypeError, + "cannot convert unicode object to bytes"); + return NULL; + } /* For iterator version, create a string object and resize as needed */ /* XXX(gb): is 64 a good value? also, optimize if length is known */ |