summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-05-04 05:00:04 (GMT)
committerGuido van Rossum <guido@python.org>2007-05-04 05:00:04 (GMT)
commit4355a47903f3242222b5807c71ec3dda4a8c8d5c (patch)
tree958b5582fa088b1595b9625371c11344bfa6d14a /Objects
parent6c1e6741f8e3f44fbf7dc05e3ec79a1290c63c11 (diff)
downloadcpython-4355a47903f3242222b5807c71ec3dda4a8c8d5c.zip
cpython-4355a47903f3242222b5807c71ec3dda4a8c8d5c.tar.gz
cpython-4355a47903f3242222b5807c71ec3dda4a8c8d5c.tar.bz2
Make all of test_bytes pass (except pickling, which is too badly busted).
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytesobject.c30
-rw-r--r--Objects/unicodeobject.c6
2 files changed, 17 insertions, 19 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 987a3c5..cb830e3 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -218,6 +218,7 @@ bytes_iconcat(PyBytesObject *self, PyObject *other)
Py_ssize_t mysize;
Py_ssize_t size;
+ /* XXX What if other == self? */
osize = _getbuffer(other, &optr);
if (osize < 0) {
PyErr_Format(PyExc_TypeError,
@@ -698,34 +699,25 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
if (PyUnicode_Check(arg)) {
/* Encode via the codec registry */
- PyObject *encoded;
- char *bytes;
- Py_ssize_t size;
+ PyObject *encoded, *new;
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
encoded = PyCodec_Encode(arg, encoding, errors);
if (encoded == NULL)
return -1;
- if (!PyString_Check(encoded)) {
+ if (!PyBytes_Check(encoded) && !PyString_Check(encoded)) {
PyErr_Format(PyExc_TypeError,
- "encoder did not return a string object (type=%.400s)",
+ "encoder did not return a str8 or bytes object (type=%.400s)",
encoded->ob_type->tp_name);
Py_DECREF(encoded);
return -1;
}
- bytes = PyString_AS_STRING(encoded);
- size = PyString_GET_SIZE(encoded);
- if (size < self->ob_alloc) {
- self->ob_size = size;
- self->ob_bytes[self->ob_size] = '\0'; /* Trailing null byte */
- }
- else if (PyBytes_Resize((PyObject *)self, size) < 0) {
- Py_DECREF(encoded);
- return -1;
- }
- memcpy(self->ob_bytes, bytes, size);
- Py_DECREF(encoded);
- return 0;
+ new = bytes_iconcat(self, encoded);
+ Py_DECREF(encoded);
+ if (new == NULL)
+ return -1;
+ Py_DECREF(new);
+ return 0;
}
/* If it's not unicode, there can't be encoding or errors */
@@ -2689,7 +2681,7 @@ bytes_fromhex(PyObject *cls, PyObject *args)
return NULL;
buf = PyBytes_AS_STRING(newbytes);
- for (i = j = 0; ; i += 2) {
+ for (i = j = 0; i < len; i += 2) {
/* skip over spaces in the input */
while (Py_CHARMASK(hex[i]) == ' ')
i++;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 26d6fc6..d4a17ce 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -5634,6 +5634,12 @@ unicode_encode(PyUnicodeObject *self, PyObject *args)
if (v == NULL)
goto onError;
if (!PyBytes_Check(v)) {
+ if (PyString_Check(v)) {
+ /* Old codec, turn it into bytes */
+ PyObject *b = PyBytes_FromObject(v);
+ Py_DECREF(v);
+ return b;
+ }
PyErr_Format(PyExc_TypeError,
"encoder did not return a bytes object "
"(type=%.400s)",