summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2008-09-06 20:13:06 (GMT)
committerGregory P. Smith <greg@mad-scientist.com>2008-09-06 20:13:06 (GMT)
commit693fc4604f9dd251949638228e282a0c8757b4ca (patch)
tree629b595fbfce66cfb0743a9d22fb6c4107380740
parent35e661c7115256290e7abbf62f9d0bc602dfeac3 (diff)
downloadcpython-693fc4604f9dd251949638228e282a0c8757b4ca.zip
cpython-693fc4604f9dd251949638228e282a0c8757b4ca.tar.gz
cpython-693fc4604f9dd251949638228e282a0c8757b4ca.tar.bz2
Fixes release blocker issue #3492 and #3790.
Make zlib and zipimport to return bytes instead of bytearray and use bytes rather than bytearray for their internal leftover data storages.
-rw-r--r--Lib/test/test_zlib.py10
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/zipimport.c22
-rw-r--r--Modules/zlibmodule.c62
4 files changed, 55 insertions, 43 deletions
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index b5f3fe5..f52aa5e 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -110,6 +110,8 @@ class CompressObjectTestCase(unittest.TestCase):
y1 = dco.decompress(x1 + x2)
y2 = dco.flush()
self.assertEqual(data, y1 + y2)
+ self.assert_(isinstance(dco.unconsumed_tail, bytes))
+ self.assert_(isinstance(dco.unused_data, bytes))
def test_compressoptions(self):
# specify lots of options to compressobj()
@@ -152,7 +154,11 @@ class CompressObjectTestCase(unittest.TestCase):
bufs.append(co.flush())
combuf = b''.join(bufs)
- self.assertEqual(data, zlib.decompress(combuf))
+ decombuf = zlib.decompress(combuf)
+ # Test type of return value
+ self.assert_(isinstance(decombuf, bytes))
+
+ self.assertEqual(data, decombuf)
dco = zlib.decompressobj()
bufs = []
@@ -348,6 +354,8 @@ class CompressObjectTestCase(unittest.TestCase):
# Test copying a decompression object
data = HAMLET_SCENE
comp = zlib.compress(data)
+ # Test type of return value
+ self.assert_(isinstance(comp, bytes))
d0 = zlib.decompressobj()
bufs0 = []
diff --git a/Misc/NEWS b/Misc/NEWS
index bc6ede7..cbbe447 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -137,6 +137,10 @@ Extension Modules
- The _bytesio and _stringio modules are now compiled into the python binary.
+- Issue #3492 and #3790: Fixed the zlib module and zipimport module uses of
+ mutable bytearray objects where they should have been using immutable bytes.
+
+
Tools/Demos
-----------
diff --git a/Modules/zipimport.c b/Modules/zipimport.c
index 336859d..000e987 100644
--- a/Modules/zipimport.c
+++ b/Modules/zipimport.c
@@ -467,7 +467,7 @@ zipimporter_get_source(PyObject *obj, PyObject *args)
toc_entry = PyDict_GetItemString(self->files, path);
if (toc_entry != NULL) {
PyObject *bytes = get_data(_PyUnicode_AsString(self->archive), toc_entry);
- PyObject *res = PyUnicode_FromString(PyByteArray_AsString(bytes));
+ PyObject *res = PyUnicode_FromString(PyBytes_AsString(bytes));
Py_XDECREF(bytes);
return res;
}
@@ -836,13 +836,13 @@ get_data(char *archive, PyObject *toc_entry)
bytes_size = compress == 0 ? data_size : data_size + 1;
if (bytes_size == 0)
bytes_size++;
- raw_data = PyByteArray_FromStringAndSize((char *)NULL, bytes_size);
+ raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
if (raw_data == NULL) {
fclose(fp);
return NULL;
}
- buf = PyByteArray_AsString(raw_data);
+ buf = PyBytes_AsString(raw_data);
err = fseek(fp, file_offset, 0);
if (err == 0)
@@ -862,7 +862,7 @@ get_data(char *archive, PyObject *toc_entry)
buf[data_size] = '\0';
if (compress == 0) { /* data is not compressed */
- data = PyByteArray_FromStringAndSize(buf, data_size);
+ data = PyBytes_FromStringAndSize(buf, data_size);
Py_DECREF(raw_data);
return data;
}
@@ -903,8 +903,8 @@ static PyObject *
unmarshal_code(char *pathname, PyObject *data, time_t mtime)
{
PyObject *code;
- char *buf = PyByteArray_AsString(data);
- Py_ssize_t size = PyByteArray_Size(data);
+ char *buf = PyBytes_AsString(data);
+ Py_ssize_t size = PyBytes_Size(data);
if (size <= 9) {
PyErr_SetString(ZipImportError,
@@ -949,16 +949,16 @@ unmarshal_code(char *pathname, PyObject *data, time_t mtime)
static PyObject *
normalize_line_endings(PyObject *source)
{
- char *buf, *q, *p = PyByteArray_AsString(source);
+ char *buf, *q, *p = PyBytes_AsString(source);
PyObject *fixed_source;
int len = 0;
if (!p) {
- return PyByteArray_FromStringAndSize("\n\0", 2);
+ return PyBytes_FromStringAndSize("\n\0", 2);
}
/* one char extra for trailing \n and one for terminating \0 */
- buf = (char *)PyMem_Malloc(PyByteArray_Size(source) + 2);
+ buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
if (buf == NULL) {
PyErr_SetString(PyExc_MemoryError,
"zipimport: no memory to allocate "
@@ -978,7 +978,7 @@ normalize_line_endings(PyObject *source)
}
*q++ = '\n'; /* add trailing \n */
*q = '\0';
- fixed_source = PyByteArray_FromStringAndSize(buf, len + 2);
+ fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
PyMem_Free(buf);
return fixed_source;
}
@@ -994,7 +994,7 @@ compile_source(char *pathname, PyObject *source)
if (fixed_source == NULL)
return NULL;
- code = Py_CompileString(PyByteArray_AsString(fixed_source), pathname,
+ code = Py_CompileString(PyBytes_AsString(fixed_source), pathname,
Py_file_input);
Py_DECREF(fixed_source);
return code;
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 3fa84d9..35f7bbb 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -96,12 +96,12 @@ newcompobject(PyTypeObject *type)
if (self == NULL)
return NULL;
self->is_initialised = 0;
- self->unused_data = PyByteArray_FromStringAndSize("", 0);
+ self->unused_data = PyBytes_FromStringAndSize("", 0);
if (self->unused_data == NULL) {
Py_DECREF(self);
return NULL;
}
- self->unconsumed_tail = PyByteArray_FromStringAndSize("", 0);
+ self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
if (self->unconsumed_tail == NULL) {
Py_DECREF(self);
return NULL;
@@ -178,7 +178,7 @@ PyZlib_compress(PyObject *self, PyObject *args)
err=deflateEnd(&zst);
if (err == Z_OK)
- ReturnVal = PyByteArray_FromStringAndSize((char *)output,
+ ReturnVal = PyBytes_FromStringAndSize((char *)output,
zst.total_out);
else
zlib_error(zst, err, "while finishing compression");
@@ -219,14 +219,14 @@ PyZlib_decompress(PyObject *self, PyObject *args)
zst.avail_in = length;
zst.avail_out = r_strlen;
- if (!(result_str = PyByteArray_FromStringAndSize(NULL, r_strlen))) {
+ if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
PyBuffer_Release(&pinput);
return NULL;
}
zst.zalloc = (alloc_func)NULL;
zst.zfree = (free_func)Z_NULL;
- zst.next_out = (Byte *)PyByteArray_AS_STRING(result_str);
+ zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
zst.next_in = (Byte *)input;
err = inflateInit2(&zst, wsize);
@@ -266,12 +266,12 @@ PyZlib_decompress(PyObject *self, PyObject *args)
/* fall through */
case(Z_OK):
/* need more memory */
- if (PyByteArray_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 *)PyByteArray_AS_STRING(result_str) + r_strlen;
+ (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
zst.avail_out = r_strlen;
r_strlen = r_strlen << 1;
break;
@@ -288,7 +288,7 @@ PyZlib_decompress(PyObject *self, PyObject *args)
goto error;
}
- if (PyByteArray_Resize(result_str, zst.total_out) < 0)
+ if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
goto error;
PyBuffer_Release(&pinput);
@@ -417,7 +417,7 @@ PyZlib_objcompress(compobject *self, PyObject *args)
input = pinput.buf;
inplen = pinput.len;
- if (!(RetVal = PyByteArray_FromStringAndSize(NULL, length))) {
+ if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
PyBuffer_Release(&pinput);
return NULL;
}
@@ -428,7 +428,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 *)PyByteArray_AS_STRING(RetVal);
+ self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), Z_NO_FLUSH);
@@ -437,13 +437,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 (PyByteArray_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 *)PyByteArray_AS_STRING(RetVal) + length;
+ (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
self->zst.avail_out = length;
length = length << 1;
@@ -462,7 +462,7 @@ PyZlib_objcompress(compobject *self, PyObject *args)
RetVal = NULL;
goto error;
}
- if (PyByteArray_Resize(RetVal, self->zst.total_out - start_total_out) < 0) {
+ if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
}
@@ -509,7 +509,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 = PyByteArray_FromStringAndSize(NULL, length))) {
+ if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
PyBuffer_Release(&pinput);
return NULL;
}
@@ -520,7 +520,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 *)PyByteArray_AS_STRING(RetVal);
+ self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Py_BEGIN_ALLOW_THREADS
err = inflate(&(self->zst), Z_SYNC_FLUSH);
@@ -542,13 +542,13 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
if (max_length && length > max_length)
length = max_length;
- if (PyByteArray_Resize(RetVal, length) < 0) {
+ if (_PyBytes_Resize(&RetVal, length) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
goto error;
}
self->zst.next_out =
- (unsigned char *)PyByteArray_AS_STRING(RetVal) + old_length;
+ (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
self->zst.avail_out = length - old_length;
Py_BEGIN_ALLOW_THREADS
@@ -560,7 +560,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 = PyByteArray_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);
@@ -577,7 +577,7 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
*/
if (err == Z_STREAM_END) {
Py_XDECREF(self->unused_data); /* Free original empty string */
- self->unused_data = PyByteArray_FromStringAndSize(
+ self->unused_data = PyBytes_FromStringAndSize(
(char *)self->zst.next_in, self->zst.avail_in);
if (self->unused_data == NULL) {
Py_DECREF(RetVal);
@@ -594,7 +594,7 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
goto error;
}
- if (PyByteArray_Resize(RetVal, self->zst.total_out - start_total_out) < 0) {
+ if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
}
@@ -627,10 +627,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 PyByteArray_FromStringAndSize(NULL, 0);
+ return PyBytes_FromStringAndSize(NULL, 0);
}
- if (!(RetVal = PyByteArray_FromStringAndSize(NULL, length)))
+ if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
return NULL;
ENTER_ZLIB
@@ -638,7 +638,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 *)PyByteArray_AS_STRING(RetVal);
+ self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), flushmode);
@@ -647,13 +647,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 (PyByteArray_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 *)PyByteArray_AS_STRING(RetVal) + length;
+ (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
self->zst.avail_out = length;
length = length << 1;
@@ -687,7 +687,7 @@ PyZlib_flush(compobject *self, PyObject *args)
goto error;
}
- if (PyByteArray_Resize(RetVal, self->zst.total_out - start_total_out) < 0) {
+ if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
}
@@ -822,7 +822,7 @@ PyZlib_unflush(compobject *self, PyObject *args)
PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
return NULL;
}
- if (!(retval = PyByteArray_FromStringAndSize(NULL, length)))
+ if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
return NULL;
@@ -830,7 +830,7 @@ PyZlib_unflush(compobject *self, PyObject *args)
start_total_out = self->zst.total_out;
self->zst.avail_out = length;
- self->zst.next_out = (Byte *)PyByteArray_AS_STRING(retval);
+ self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Py_BEGIN_ALLOW_THREADS
err = inflate(&(self->zst), Z_FINISH);
@@ -839,12 +839,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 (PyByteArray_Resize(retval, length << 1) < 0) {
+ if (_PyBytes_Resize(&retval, length << 1) < 0) {
Py_DECREF(retval);
retval = NULL;
goto error;
}
- self->zst.next_out = (Byte *)PyByteArray_AS_STRING(retval) + length;
+ self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
self->zst.avail_out = length;
length = length << 1;
@@ -866,7 +866,7 @@ PyZlib_unflush(compobject *self, PyObject *args)
goto error;
}
}
- if (PyByteArray_Resize(retval, self->zst.total_out - start_total_out) < 0) {
+ if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(retval);
retval = NULL;
}