~pythonregexp2.7/python/issue2636-11

« back to all changes in this revision

Viewing changes to Modules/bz2module.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-21 13:47:31 UTC
  • mfrom: (39021.1.404 Regexp-2.7)
  • mto: This revision was merged to the branch mainline in revision 39030.
  • Revision ID: darklord@timehorse.com-20080921134731-rudomuzeh1b2tz1y
Merged in changes from the latest python source snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
416
416
                return 0;
417
417
        }
418
418
        if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
 
419
                PyErr_NoMemory();
419
420
                return -1;
420
421
        }
421
422
        Py_BEGIN_ALLOW_THREADS
791
792
BZ2File_write(BZ2FileObject *self, PyObject *args)
792
793
{
793
794
        PyObject *ret = NULL;
 
795
        Py_buffer pbuf;
794
796
        char *buf;
795
797
        int len;
796
798
        int bzerror;
797
799
 
798
 
        if (!PyArg_ParseTuple(args, "s#:write", &buf, &len))
 
800
        if (!PyArg_ParseTuple(args, "s*:write", &pbuf))
799
801
                return NULL;
 
802
        buf = pbuf.buf;
 
803
        len = pbuf.len;
800
804
 
801
805
        ACQUIRE_LOCK(self);
802
806
        switch (self->mode) {
830
834
        ret = Py_None;
831
835
 
832
836
cleanup:
 
837
        PyBuffer_Release(&pbuf);
833
838
        RELEASE_LOCK(self);
834
839
        return ret;
835
840
}
1451
1456
        PyStringObject* ret;
1452
1457
        ACQUIRE_LOCK(self);
1453
1458
        if (self->mode == MODE_CLOSED) {
 
1459
                RELEASE_LOCK(self);
1454
1460
                PyErr_SetString(PyExc_ValueError,
1455
1461
                                "I/O operation on closed file");
1456
1462
                return NULL;
1547
1553
static PyObject *
1548
1554
BZ2Comp_compress(BZ2CompObject *self, PyObject *args)
1549
1555
{
 
1556
        Py_buffer pdata;
1550
1557
        char *data;
1551
1558
        int datasize;
1552
1559
        int bufsize = SMALLCHUNK;
1555
1562
        bz_stream *bzs = &self->bzs;
1556
1563
        int bzerror;
1557
1564
 
1558
 
        if (!PyArg_ParseTuple(args, "s#:compress", &data, &datasize))
 
1565
        if (!PyArg_ParseTuple(args, "s*:compress", &pdata))
1559
1566
                return NULL;
 
1567
        data = pdata.buf;
 
1568
        datasize = pdata.len;
1560
1569
 
1561
 
        if (datasize == 0)
 
1570
        if (datasize == 0) {
 
1571
                PyBuffer_Release(&pdata);
1562
1572
                return PyString_FromString("");
 
1573
        }
1563
1574
 
1564
1575
        ACQUIRE_LOCK(self);
1565
1576
        if (!self->running) {
1604
1615
        _PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout));
1605
1616
 
1606
1617
        RELEASE_LOCK(self);
 
1618
        PyBuffer_Release(&pdata);
1607
1619
        return ret;
1608
1620
 
1609
1621
error:
1610
1622
        RELEASE_LOCK(self);
 
1623
        PyBuffer_Release(&pdata);
1611
1624
        Py_XDECREF(ret);
1612
1625
        return NULL;
1613
1626
}
1831
1844
static PyObject *
1832
1845
BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args)
1833
1846
{
 
1847
        Py_buffer pdata;
1834
1848
        char *data;
1835
1849
        int datasize;
1836
1850
        int bufsize = SMALLCHUNK;
1839
1853
        bz_stream *bzs = &self->bzs;
1840
1854
        int bzerror;
1841
1855
 
1842
 
        if (!PyArg_ParseTuple(args, "s#:decompress", &data, &datasize))
 
1856
        if (!PyArg_ParseTuple(args, "s*:decompress", &pdata))
1843
1857
                return NULL;
 
1858
        data = pdata.buf;
 
1859
        datasize = pdata.len;
1844
1860
 
1845
1861
        ACQUIRE_LOCK(self);
1846
1862
        if (!self->running) {
1897
1913
                _PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout));
1898
1914
 
1899
1915
        RELEASE_LOCK(self);
 
1916
        PyBuffer_Release(&pdata);
1900
1917
        return ret;
1901
1918
 
1902
1919
error:
1903
1920
        RELEASE_LOCK(self);
 
1921
        PyBuffer_Release(&pdata);
1904
1922
        Py_XDECREF(ret);
1905
1923
        return NULL;
1906
1924
}
2039
2057
bz2_compress(PyObject *self, PyObject *args, PyObject *kwargs)
2040
2058
{
2041
2059
        int compresslevel=9;
 
2060
        Py_buffer pdata;
2042
2061
        char *data;
2043
2062
        int datasize;
2044
2063
        int bufsize;
2048
2067
        int bzerror;
2049
2068
        static char *kwlist[] = {"data", "compresslevel", 0};
2050
2069
 
2051
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i",
2052
 
                                         kwlist, &data, &datasize,
 
2070
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|i",
 
2071
                                         kwlist, &pdata,
2053
2072
                                         &compresslevel))
2054
2073
                return NULL;
 
2074
        data = pdata.buf;
 
2075
        datasize = pdata.len;
2055
2076
 
2056
2077
        if (compresslevel < 1 || compresslevel > 9) {
2057
2078
                PyErr_SetString(PyExc_ValueError,
2058
2079
                                "compresslevel must be between 1 and 9");
 
2080
                PyBuffer_Release(&pdata);
2059
2081
                return NULL;
2060
2082
        }
2061
2083
 
2064
2086
        bufsize = datasize + (datasize/100+1) + 600;
2065
2087
 
2066
2088
        ret = PyString_FromStringAndSize(NULL, bufsize);
2067
 
        if (!ret)
 
2089
        if (!ret) {
 
2090
                PyBuffer_Release(&pdata);
2068
2091
                return NULL;
 
2092
        }
2069
2093
 
2070
2094
        memset(bzs, 0, sizeof(bz_stream));
2071
2095
 
2077
2101
        bzerror = BZ2_bzCompressInit(bzs, compresslevel, 0, 0);
2078
2102
        if (bzerror != BZ_OK) {
2079
2103
                Util_CatchBZ2Error(bzerror);
 
2104
                PyBuffer_Release(&pdata);
2080
2105
                Py_DECREF(ret);
2081
2106
                return NULL;
2082
2107
        }
2090
2115
                } else if (bzerror != BZ_FINISH_OK) {
2091
2116
                        BZ2_bzCompressEnd(bzs);
2092
2117
                        Util_CatchBZ2Error(bzerror);
 
2118
                        PyBuffer_Release(&pdata);
2093
2119
                        Py_DECREF(ret);
2094
2120
                        return NULL;
2095
2121
                }
2097
2123
                        bufsize = Util_NewBufferSize(bufsize);
2098
2124
                        if (_PyString_Resize(&ret, bufsize) < 0) {
2099
2125
                                BZ2_bzCompressEnd(bzs);
 
2126
                                PyBuffer_Release(&pdata);
2100
2127
                                Py_DECREF(ret);
2101
2128
                                return NULL;
2102
2129
                        }
2109
2136
                _PyString_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs));
2110
2137
        BZ2_bzCompressEnd(bzs);
2111
2138
 
 
2139
        PyBuffer_Release(&pdata);
2112
2140
        return ret;
2113
2141
}
2114
2142
 
2122
2150
static PyObject *
2123
2151
bz2_decompress(PyObject *self, PyObject *args)
2124
2152
{
 
2153
        Py_buffer pdata;
2125
2154
        char *data;
2126
2155
        int datasize;
2127
2156
        int bufsize = SMALLCHUNK;
2130
2159
        bz_stream *bzs = &_bzs;
2131
2160
        int bzerror;
2132
2161
 
2133
 
        if (!PyArg_ParseTuple(args, "s#:decompress", &data, &datasize))
 
2162
        if (!PyArg_ParseTuple(args, "s*:decompress", &pdata))
2134
2163
                return NULL;
 
2164
        data = pdata.buf;
 
2165
        datasize = pdata.len;
2135
2166
 
2136
 
        if (datasize == 0)
 
2167
        if (datasize == 0) {
 
2168
                PyBuffer_Release(&pdata);
2137
2169
                return PyString_FromString("");
 
2170
        }
2138
2171
 
2139
2172
        ret = PyString_FromStringAndSize(NULL, bufsize);
2140
 
        if (!ret)
 
2173
        if (!ret) {
 
2174
                PyBuffer_Release(&pdata);
2141
2175
                return NULL;
 
2176
        }
2142
2177
 
2143
2178
        memset(bzs, 0, sizeof(bz_stream));
2144
2179
 
2151
2186
        if (bzerror != BZ_OK) {
2152
2187
                Util_CatchBZ2Error(bzerror);
2153
2188
                Py_DECREF(ret);
 
2189
                PyBuffer_Release(&pdata);
2154
2190
                return NULL;
2155
2191
        }
2156
2192
 
2163
2199
                } else if (bzerror != BZ_OK) {
2164
2200
                        BZ2_bzDecompressEnd(bzs);
2165
2201
                        Util_CatchBZ2Error(bzerror);
 
2202
                        PyBuffer_Release(&pdata);
2166
2203
                        Py_DECREF(ret);
2167
2204
                        return NULL;
2168
2205
                }
2170
2207
                        BZ2_bzDecompressEnd(bzs);
2171
2208
                        PyErr_SetString(PyExc_ValueError,
2172
2209
                                        "couldn't find end of stream");
 
2210
                        PyBuffer_Release(&pdata);
2173
2211
                        Py_DECREF(ret);
2174
2212
                        return NULL;
2175
2213
                }
2177
2215
                        bufsize = Util_NewBufferSize(bufsize);
2178
2216
                        if (_PyString_Resize(&ret, bufsize) < 0) {
2179
2217
                                BZ2_bzDecompressEnd(bzs);
 
2218
                                PyBuffer_Release(&pdata);
2180
2219
                                Py_DECREF(ret);
2181
2220
                                return NULL;
2182
2221
                        }
2188
2227
        if (bzs->avail_out != 0)
2189
2228
                _PyString_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs));
2190
2229
        BZ2_bzDecompressEnd(bzs);
 
2230
        PyBuffer_Release(&pdata);
2191
2231
 
2192
2232
        return ret;
2193
2233
}