Stream functions like read() are supposed to return bytes, not buffer.

Now multibytecodec directly works with PyStrings, and disallow PyBytes.
This commit is contained in:
Amaury Forgeot d'Arc 2007-11-22 21:33:52 +00:00
parent af59346f1a
commit 3ee05afa53

View File

@ -1230,15 +1230,7 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
if (cres == NULL) if (cres == NULL)
goto errorexit; goto errorexit;
if (PyString_Check(cres)) { if (!PyString_Check(cres)) {
PyObject *cres2 = PyBytes_FromObject(cres);
if (cres2 == NULL)
return NULL;
Py_DECREF(cres);
cres = cres2;
}
if (!PyBytes_Check(cres)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"stream function returned a " "stream function returned a "
"non-bytes object (%.100s)", "non-bytes object (%.100s)",
@ -1246,28 +1238,28 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
goto errorexit; goto errorexit;
} }
endoffile = (PyBytes_GET_SIZE(cres) == 0); endoffile = (PyString_GET_SIZE(cres) == 0);
if (self->pendingsize > 0) { if (self->pendingsize > 0) {
PyObject *ctr; PyObject *ctr;
char *ctrdata; char *ctrdata;
rsize = PyBytes_GET_SIZE(cres) + self->pendingsize; rsize = PyString_GET_SIZE(cres) + self->pendingsize;
ctr = PyBytes_FromStringAndSize(NULL, rsize); ctr = PyString_FromStringAndSize(NULL, rsize);
if (ctr == NULL) if (ctr == NULL)
goto errorexit; goto errorexit;
ctrdata = PyBytes_AS_STRING(ctr); ctrdata = PyString_AS_STRING(ctr);
memcpy(ctrdata, self->pending, self->pendingsize); memcpy(ctrdata, self->pending, self->pendingsize);
memcpy(ctrdata + self->pendingsize, memcpy(ctrdata + self->pendingsize,
PyBytes_AS_STRING(cres), PyString_AS_STRING(cres),
PyBytes_GET_SIZE(cres)); PyString_GET_SIZE(cres));
Py_DECREF(cres); Py_DECREF(cres);
cres = ctr; cres = ctr;
self->pendingsize = 0; self->pendingsize = 0;
} }
rsize = PyBytes_GET_SIZE(cres); rsize = PyString_GET_SIZE(cres);
if (decoder_prepare_buffer(&buf, PyBytes_AS_STRING(cres), if (decoder_prepare_buffer(&buf, PyString_AS_STRING(cres),
rsize) != 0) rsize) != 0)
goto errorexit; goto errorexit;