zlib now uses bytes everywhere, and the test passes.

This commit is contained in:
Guido van Rossum 2007-05-22 22:44:07 +00:00
parent dcee3c0d1f
commit 776152b601
2 changed files with 104 additions and 78 deletions

View file

@ -96,12 +96,12 @@ newcompobject(PyTypeObject *type)
if (self == NULL)
return NULL;
self->is_initialised = 0;
self->unused_data = PyString_FromString("");
self->unused_data = PyBytes_FromStringAndSize("", 0);
if (self->unused_data == NULL) {
Py_DECREF(self);
return NULL;
}
self->unconsumed_tail = PyString_FromString("");
self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
if (self->unconsumed_tail == NULL) {
Py_DECREF(self);
return NULL;
@ -174,8 +174,8 @@ PyZlib_compress(PyObject *self, PyObject *args)
err=deflateEnd(&zst);
if (err == Z_OK)
ReturnVal = PyString_FromStringAndSize((char *)output,
zst.total_out);
ReturnVal = PyBytes_FromStringAndSize((char *)output,
zst.total_out);
else
zlib_error(zst, err, "while finishing compression");
@ -210,12 +210,12 @@ PyZlib_decompress(PyObject *self, PyObject *args)
zst.avail_in = length;
zst.avail_out = r_strlen;
if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
return NULL;
zst.zalloc = (alloc_func)NULL;
zst.zfree = (free_func)Z_NULL;
zst.next_out = (Byte *)PyString_AS_STRING(result_str);
zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
zst.next_in = (Byte *)input;
err = inflateInit2(&zst, wsize);
@ -255,12 +255,12 @@ PyZlib_decompress(PyObject *self, PyObject *args)
/* fall through */
case(Z_OK):
/* need more memory */
if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
if (PyBytes_Resize(result_str, r_strlen << 1) < 0) {
inflateEnd(&zst);
goto error;
}
zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
+ r_strlen;
zst.next_out =
(unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
zst.avail_out = r_strlen;
r_strlen = r_strlen << 1;
break;
@ -277,7 +277,9 @@ PyZlib_decompress(PyObject *self, PyObject *args)
goto error;
}
_PyString_Resize(&result_str, zst.total_out);
if (PyBytes_Resize(result_str, zst.total_out) < 0)
goto error;
return result_str;
error:
@ -399,7 +401,7 @@ PyZlib_objcompress(compobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
return NULL;
if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
return NULL;
ENTER_ZLIB
@ -408,7 +410,7 @@ PyZlib_objcompress(compobject *self, PyObject *args)
self->zst.avail_in = inplen;
self->zst.next_in = input;
self->zst.avail_out = length;
self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), Z_NO_FLUSH);
@ -417,10 +419,13 @@ PyZlib_objcompress(compobject *self, PyObject *args)
/* while Z_OK and the output buffer is full, there might be more output,
so extend the output buffer and try again */
while (err == Z_OK && self->zst.avail_out == 0) {
if (_PyString_Resize(&RetVal, length << 1) < 0)
if (PyBytes_Resize(RetVal, length << 1) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
goto error;
self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
+ length;
}
self->zst.next_out =
(unsigned char *)PyBytes_AS_STRING(RetVal) + length;
self->zst.avail_out = length;
length = length << 1;
@ -439,7 +444,10 @@ PyZlib_objcompress(compobject *self, PyObject *args)
RetVal = NULL;
goto error;
}
_PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
if (PyBytes_Resize(RetVal, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
}
error:
LEAVE_ZLIB
@ -478,7 +486,7 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
/* limit amount of data allocated to max_length */
if (max_length && length > max_length)
length = max_length;
if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
return NULL;
ENTER_ZLIB
@ -487,7 +495,7 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
self->zst.avail_in = inplen;
self->zst.next_in = input;
self->zst.avail_out = length;
self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Py_BEGIN_ALLOW_THREADS
err = inflate(&(self->zst), Z_SYNC_FLUSH);
@ -509,10 +517,13 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
if (max_length && length > max_length)
length = max_length;
if (_PyString_Resize(&RetVal, length) < 0)
if (PyBytes_Resize(RetVal, length) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
goto error;
self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
+ old_length;
}
self->zst.next_out =
(unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
self->zst.avail_out = length - old_length;
Py_BEGIN_ALLOW_THREADS
@ -524,7 +535,7 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
of specified size. Return the unconsumed tail in an attribute.*/
if(max_length) {
Py_DECREF(self->unconsumed_tail);
self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in,
self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
self->zst.avail_in);
if(!self->unconsumed_tail) {
Py_DECREF(RetVal);
@ -541,7 +552,7 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
*/
if (err == Z_STREAM_END) {
Py_XDECREF(self->unused_data); /* Free original empty string */
self->unused_data = PyString_FromStringAndSize(
self->unused_data = PyBytes_FromStringAndSize(
(char *)self->zst.next_in, self->zst.avail_in);
if (self->unused_data == NULL) {
Py_DECREF(RetVal);
@ -558,7 +569,10 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
goto error;
}
_PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
if (PyBytes_Resize(RetVal, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
}
error:
LEAVE_ZLIB
@ -588,10 +602,10 @@ PyZlib_flush(compobject *self, PyObject *args)
/* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
doing any work at all; just return an empty string. */
if (flushmode == Z_NO_FLUSH) {
return PyString_FromStringAndSize(NULL, 0);
return PyBytes_FromStringAndSize(NULL, 0);
}
if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
return NULL;
ENTER_ZLIB
@ -599,7 +613,7 @@ PyZlib_flush(compobject *self, PyObject *args)
start_total_out = self->zst.total_out;
self->zst.avail_in = 0;
self->zst.avail_out = length;
self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), flushmode);
@ -608,10 +622,13 @@ PyZlib_flush(compobject *self, PyObject *args)
/* while Z_OK and the output buffer is full, there might be more output,
so extend the output buffer and try again */
while (err == Z_OK && self->zst.avail_out == 0) {
if (_PyString_Resize(&RetVal, length << 1) < 0)
if (PyBytes_Resize(RetVal, length << 1) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
goto error;
self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
+ length;
}
self->zst.next_out =
(unsigned char *)PyBytes_AS_STRING(RetVal) + length;
self->zst.avail_out = length;
length = length << 1;
@ -645,7 +662,10 @@ PyZlib_flush(compobject *self, PyObject *args)
goto error;
}
_PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
if (PyBytes_Resize(RetVal, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
}
error:
LEAVE_ZLIB
@ -773,7 +793,7 @@ PyZlib_unflush(compobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "|i:flush", &length))
return NULL;
if (!(retval = PyString_FromStringAndSize(NULL, length)))
if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
return NULL;
@ -781,7 +801,7 @@ PyZlib_unflush(compobject *self, PyObject *args)
start_total_out = self->zst.total_out;
self->zst.avail_out = length;
self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Py_BEGIN_ALLOW_THREADS
err = inflate(&(self->zst), Z_FINISH);
@ -790,9 +810,12 @@ PyZlib_unflush(compobject *self, PyObject *args)
/* while Z_OK and the output buffer is full, there might be more output,
so extend the output buffer and try again */
while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
if (_PyString_Resize(&retval, length << 1) < 0)
if (PyBytes_Resize(retval, length << 1) < 0) {
Py_DECREF(retval);
retval = NULL;
goto error;
self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
}
self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
self->zst.avail_out = length;
length = length << 1;
@ -814,7 +837,10 @@ PyZlib_unflush(compobject *self, PyObject *args)
goto error;
}
}
_PyString_Resize(&retval, self->zst.total_out - start_total_out);
if (PyBytes_Resize(retval, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(retval);
retval = NULL;
}
error: