Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Modules/zlibmodule.c

    r2 r388  
    2929
    3030#define ENTER_ZLIB \
    31         Py_BEGIN_ALLOW_THREADS \
    32         PyThread_acquire_lock(zlib_lock, 1); \
    33         Py_END_ALLOW_THREADS
     31        Py_BEGIN_ALLOW_THREADS \
     32        PyThread_acquire_lock(zlib_lock, 1); \
     33        Py_END_ALLOW_THREADS
    3434
    3535#define LEAVE_ZLIB \
    36         PyThread_release_lock(zlib_lock);
     36        PyThread_release_lock(zlib_lock);
    3737
    3838#else
     
    7373zlib_error(z_stream zst, int err, char *msg)
    7474{
    75     if (zst.msg == Z_NULL)
    76         PyErr_Format(ZlibError, "Error %d %s", err, msg);
     75    const char *zmsg = Z_NULL;
     76    /* In case of a version mismatch, zst.msg won't be initialized.
     77       Check for this case first, before looking at zst.msg. */
     78    if (err == Z_VERSION_ERROR)
     79        zmsg = "library version mismatch";
     80    if (zmsg == Z_NULL)
     81        zmsg = zst.msg;
     82    if (zmsg == Z_NULL) {
     83        switch (err) {
     84        case Z_BUF_ERROR:
     85            zmsg = "incomplete or truncated stream";
     86            break;
     87        case Z_STREAM_ERROR:
     88            zmsg = "inconsistent stream state";
     89            break;
     90        case Z_DATA_ERROR:
     91            zmsg = "invalid input data";
     92            break;
     93        }
     94    }
     95    if (zmsg == Z_NULL)
     96        PyErr_Format(ZlibError, "Error %d %s", err, msg);
    7797    else
    78         PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
     98        PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
    7999}
    80100
     
    82102"compressobj([level]) -- Return a compressor object.\n"
    83103"\n"
    84 "Optional arg level is the compression level, in 1-9.");
     104"Optional arg level is the compression level, in 0-9.");
    85105
    86106PyDoc_STRVAR(decompressobj__doc__,
     
    95115    self = PyObject_New(compobject, type);
    96116    if (self == NULL)
    97         return NULL;
     117        return NULL;
    98118    self->is_initialised = 0;
    99119    self->unused_data = PyString_FromString("");
    100120    if (self->unused_data == NULL) {
    101         Py_DECREF(self);
    102         return NULL;
     121        Py_DECREF(self);
     122        return NULL;
    103123    }
    104124    self->unconsumed_tail = PyString_FromString("");
    105125    if (self->unconsumed_tail == NULL) {
    106         Py_DECREF(self);
    107         return NULL;
     126        Py_DECREF(self);
     127        return NULL;
    108128    }
    109129    return self;
     
    113133"compress(string[, level]) -- Returned compressed string.\n"
    114134"\n"
    115 "Optional arg level is the compression level, in 1-9.");
     135"Optional arg level is the compression level, in 0-9.");
    116136
    117137static PyObject *
     
    125145    /* require Python string object, optional 'level' arg */
    126146    if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
    127         return NULL;
     147        return NULL;
    128148
    129149    zst.avail_out = length + length/1000 + 12 + 1;
     
    131151    output = (Byte*)malloc(zst.avail_out);
    132152    if (output == NULL) {
    133         PyErr_SetString(PyExc_MemoryError,
    134                         "Can't allocate memory to compress data");
    135         return NULL;
     153        PyErr_SetString(PyExc_MemoryError,
     154                        "Can't allocate memory to compress data");
     155        return NULL;
    136156    }
    137157
     
    148168    switch(err) {
    149169    case(Z_OK):
    150         break;
     170        break;
    151171    case(Z_MEM_ERROR):
    152         PyErr_SetString(PyExc_MemoryError,
    153                         "Out of memory while compressing data");
    154         goto error;
     172        PyErr_SetString(PyExc_MemoryError,
     173                        "Out of memory while compressing data");
     174        goto error;
    155175    case(Z_STREAM_ERROR):
    156         PyErr_SetString(ZlibError,
    157                         "Bad compression level");
    158         goto error;
     176        PyErr_SetString(ZlibError,
     177                        "Bad compression level");
     178        goto error;
    159179    default:
    160180        deflateEnd(&zst);
    161         zlib_error(zst, err, "while compressing data");
    162         goto error;
     181        zlib_error(zst, err, "while compressing data");
     182        goto error;
    163183    }
    164184
     
    168188
    169189    if (err != Z_STREAM_END) {
    170         zlib_error(zst, err, "while compressing data");
    171         deflateEnd(&zst);
    172         goto error;
     190        zlib_error(zst, err, "while compressing data");
     191        deflateEnd(&zst);
     192        goto error;
    173193    }
    174194
    175195    err=deflateEnd(&zst);
    176196    if (err == Z_OK)
    177         ReturnVal = PyString_FromStringAndSize((char *)output,
    178                                                zst.total_out);
     197        ReturnVal = PyString_FromStringAndSize((char *)output,
     198                                               zst.total_out);
    179199    else
    180         zlib_error(zst, err, "while finishing compression");
     200        zlib_error(zst, err, "while finishing compression");
    181201
    182202 error:
     
    203223
    204224    if (!PyArg_ParseTuple(args, "s#|in:decompress",
    205                           &input, &length, &wsize, &r_strlen))
    206         return NULL;
     225                          &input, &length, &wsize, &r_strlen))
     226        return NULL;
    207227
    208228    if (r_strlen <= 0)
    209         r_strlen = 1;
     229        r_strlen = 1;
    210230
    211231    zst.avail_in = length;
     
    213233
    214234    if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
    215         return NULL;
     235        return NULL;
    216236
    217237    zst.zalloc = (alloc_func)NULL;
     
    223243    switch(err) {
    224244    case(Z_OK):
    225         break;
     245        break;
    226246    case(Z_MEM_ERROR):
    227         PyErr_SetString(PyExc_MemoryError,
    228                         "Out of memory while decompressing data");
    229         goto error;
     247        PyErr_SetString(PyExc_MemoryError,
     248                        "Out of memory while decompressing data");
     249        goto error;
    230250    default:
    231251        inflateEnd(&zst);
    232         zlib_error(zst, err, "while preparing to decompress data");
    233         goto error;
     252        zlib_error(zst, err, "while preparing to decompress data");
     253        goto error;
    234254    }
    235255
    236256    do {
    237         Py_BEGIN_ALLOW_THREADS
    238         err=inflate(&zst, Z_FINISH);
    239         Py_END_ALLOW_THREADS
    240 
    241         switch(err) {
    242         case(Z_STREAM_END):
    243             break;
    244         case(Z_BUF_ERROR):
    245             /*
    246              * If there is at least 1 byte of room according to zst.avail_out
    247              * and we get this error, assume that it means zlib cannot
    248              * process the inflate call() due to an error in the data.
    249              */
    250             if (zst.avail_out > 0) {
    251                 PyErr_Format(ZlibError, "Error %i while decompressing data",
    252                              err);
    253                 inflateEnd(&zst);
    254                 goto error;
    255             }
    256             /* fall through */
    257         case(Z_OK):
    258             /* need more memory */
    259             if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
    260                 inflateEnd(&zst);
    261                 goto error;
    262             }
    263             zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
    264                 + r_strlen;
    265             zst.avail_out = r_strlen;
    266             r_strlen = r_strlen << 1;
    267             break;
    268         default:
    269             inflateEnd(&zst);
    270             zlib_error(zst, err, "while decompressing data");
    271             goto error;
    272         }
     257        Py_BEGIN_ALLOW_THREADS
     258        err=inflate(&zst, Z_FINISH);
     259        Py_END_ALLOW_THREADS
     260
     261        switch(err) {
     262        case(Z_STREAM_END):
     263            break;
     264        case(Z_BUF_ERROR):
     265            /*
     266             * If there is at least 1 byte of room according to zst.avail_out
     267             * and we get this error, assume that it means zlib cannot
     268             * process the inflate call() due to an error in the data.
     269             */
     270            if (zst.avail_out > 0) {
     271                zlib_error(zst, err, "while decompressing data");
     272                inflateEnd(&zst);
     273                goto error;
     274            }
     275            /* fall through */
     276        case(Z_OK):
     277            /* need more memory */
     278            if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
     279                inflateEnd(&zst);
     280                goto error;
     281            }
     282            zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
     283                + r_strlen;
     284            zst.avail_out = r_strlen;
     285            r_strlen = r_strlen << 1;
     286            break;
     287        default:
     288            inflateEnd(&zst);
     289            zlib_error(zst, err, "while decompressing data");
     290            goto error;
     291        }
    273292    } while (err != Z_STREAM_END);
    274293
    275294    err = inflateEnd(&zst);
    276295    if (err != Z_OK) {
    277         zlib_error(zst, err, "while finishing data decompression");
    278         goto error;
     296        zlib_error(zst, err, "while finishing data decompression");
     297        goto error;
    279298    }
    280299
     
    295314
    296315    if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
    297                           &memLevel, &strategy))
    298         return NULL;
     316                          &memLevel, &strategy))
     317        return NULL;
    299318
    300319    self = newcompobject(&Comptype);
    301320    if (self==NULL)
    302         return(NULL);
     321        return(NULL);
    303322    self->zst.zalloc = (alloc_func)NULL;
    304323    self->zst.zfree = (free_func)Z_NULL;
     
    308327    switch(err) {
    309328    case (Z_OK):
    310         self->is_initialised = 1;
    311         return (PyObject*)self;
     329        self->is_initialised = 1;
     330        return (PyObject*)self;
    312331    case (Z_MEM_ERROR):
    313         Py_DECREF(self);
    314         PyErr_SetString(PyExc_MemoryError,
    315                         "Can't allocate memory for compression object");
    316         return NULL;
     332        Py_DECREF(self);
     333        PyErr_SetString(PyExc_MemoryError,
     334                        "Can't allocate memory for compression object");
     335        return NULL;
    317336    case(Z_STREAM_ERROR):
    318         Py_DECREF(self);
    319         PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
    320         return NULL;
     337        Py_DECREF(self);
     338        PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
     339        return NULL;
    321340    default:
    322         zlib_error(self->zst, err, "while creating compression object");
     341        zlib_error(self->zst, err, "while creating compression object");
    323342        Py_DECREF(self);
    324         return NULL;
     343        return NULL;
    325344    }
    326345}
     
    332351    compobject *self;
    333352    if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
    334         return NULL;
     353        return NULL;
    335354
    336355    self = newcompobject(&Decomptype);
    337356    if (self == NULL)
    338         return(NULL);
     357        return(NULL);
    339358    self->zst.zalloc = (alloc_func)NULL;
    340359    self->zst.zfree = (free_func)Z_NULL;
     
    344363    switch(err) {
    345364    case (Z_OK):
    346         self->is_initialised = 1;
    347         return (PyObject*)self;
     365        self->is_initialised = 1;
     366        return (PyObject*)self;
    348367    case(Z_STREAM_ERROR):
    349         Py_DECREF(self);
    350         PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
    351         return NULL;
     368        Py_DECREF(self);
     369        PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
     370        return NULL;
    352371    case (Z_MEM_ERROR):
    353         Py_DECREF(self);
    354         PyErr_SetString(PyExc_MemoryError,
    355                         "Can't allocate memory for decompression object");
    356         return NULL;
     372        Py_DECREF(self);
     373        PyErr_SetString(PyExc_MemoryError,
     374                        "Can't allocate memory for decompression object");
     375        return NULL;
    357376    default:
    358         zlib_error(self->zst, err, "while creating decompression object");
     377        zlib_error(self->zst, err, "while creating decompression object");
    359378        Py_DECREF(self);
    360         return NULL;
     379        return NULL;
    361380    }
    362381}
     
    366385{
    367386    if (self->is_initialised)
    368         deflateEnd(&self->zst);
     387        deflateEnd(&self->zst);
    369388    Py_XDECREF(self->unused_data);
    370389    Py_XDECREF(self->unconsumed_tail);
     
    376395{
    377396    if (self->is_initialised)
    378         inflateEnd(&self->zst);
     397        inflateEnd(&self->zst);
    379398    Py_XDECREF(self->unused_data);
    380399    Py_XDECREF(self->unconsumed_tail);
     
    393412PyZlib_objcompress(compobject *self, PyObject *args)
    394413{
    395     int err, inplen, length = DEFAULTALLOC;
     414    int err, inplen;
     415    Py_ssize_t length = DEFAULTALLOC;
    396416    PyObject *RetVal;
    397417    Byte *input;
     
    399419
    400420    if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
    401         return NULL;
     421        return NULL;
    402422
    403423    if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
    404         return NULL;
     424        return NULL;
    405425
    406426    ENTER_ZLIB
     
    419439       so extend the output buffer and try again */
    420440    while (err == Z_OK && self->zst.avail_out == 0) {
    421         if (_PyString_Resize(&RetVal, length << 1) < 0)
    422             goto error;
    423         self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
    424             + length;
    425         self->zst.avail_out = length;
    426         length = length << 1;
    427 
    428         Py_BEGIN_ALLOW_THREADS
    429         err = deflate(&(self->zst), Z_NO_FLUSH);
    430         Py_END_ALLOW_THREADS
     441        if (_PyString_Resize(&RetVal, length << 1) < 0)
     442            goto error;
     443        self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
     444            + length;
     445        self->zst.avail_out = length;
     446        length = length << 1;
     447
     448        Py_BEGIN_ALLOW_THREADS
     449        err = deflate(&(self->zst), Z_NO_FLUSH);
     450        Py_END_ALLOW_THREADS
    431451    }
    432452    /* We will only get Z_BUF_ERROR if the output buffer was full but
     
    436456
    437457    if (err != Z_OK && err != Z_BUF_ERROR) {
    438         zlib_error(self->zst, err, "while compressing");
    439         Py_DECREF(RetVal);
    440         RetVal = NULL;
    441         goto error;
     458        zlib_error(self->zst, err, "while compressing");
     459        Py_DECREF(RetVal);
     460        RetVal = NULL;
     461        goto error;
    442462    }
    443463    _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
     
    446466    LEAVE_ZLIB
    447467    return RetVal;
     468}
     469
     470/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
     471   self->unused_data or self->unconsumed_tail, as appropriate. */
     472static int
     473save_unconsumed_input(compobject *self, int err)
     474{
     475    if (err == Z_STREAM_END) {
     476        /* The end of the compressed data has been reached. Store the leftover
     477           input data in self->unused_data. */
     478        if (self->zst.avail_in > 0) {
     479            Py_ssize_t old_size = PyString_GET_SIZE(self->unused_data);
     480            Py_ssize_t new_size;
     481            PyObject *new_data;
     482            if (self->zst.avail_in > PY_SSIZE_T_MAX - old_size) {
     483                PyErr_NoMemory();
     484                return -1;
     485            }
     486            new_size = old_size + self->zst.avail_in;
     487            new_data = PyString_FromStringAndSize(NULL, new_size);
     488            if (new_data == NULL)
     489                return -1;
     490            Py_MEMCPY(PyString_AS_STRING(new_data),
     491                      PyString_AS_STRING(self->unused_data), old_size);
     492            Py_MEMCPY(PyString_AS_STRING(new_data) + old_size,
     493                      self->zst.next_in, self->zst.avail_in);
     494            Py_DECREF(self->unused_data);
     495            self->unused_data = new_data;
     496            self->zst.avail_in = 0;
     497        }
     498    }
     499    if (self->zst.avail_in > 0 || PyString_GET_SIZE(self->unconsumed_tail)) {
     500        /* This code handles two distinct cases:
     501           1. Output limit was reached. Save leftover input in unconsumed_tail.
     502           2. All input data was consumed. Clear unconsumed_tail. */
     503        PyObject *new_data = PyString_FromStringAndSize(
     504                (char *)self->zst.next_in, self->zst.avail_in);
     505        if (new_data == NULL)
     506            return -1;
     507        Py_DECREF(self->unconsumed_tail);
     508        self->unconsumed_tail = new_data;
     509    }
     510    return 0;
    448511}
    449512
     
    462525PyZlib_objdecompress(compobject *self, PyObject *args)
    463526{
    464     int err, inplen, old_length, length = DEFAULTALLOC;
    465     int max_length = 0;
     527    int err, inplen, max_length = 0;
     528    Py_ssize_t old_length, length = DEFAULTALLOC;
    466529    PyObject *RetVal;
    467530    Byte *input;
     
    469532
    470533    if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
    471                           &inplen, &max_length))
    472         return NULL;
     534                          &inplen, &max_length))
     535        return NULL;
    473536    if (max_length < 0) {
    474         PyErr_SetString(PyExc_ValueError,
    475                         "max_length must be greater than zero");
    476         return NULL;
     537        PyErr_SetString(PyExc_ValueError,
     538                        "max_length must be greater than zero");
     539        return NULL;
    477540    }
    478541
    479542    /* limit amount of data allocated to max_length */
    480543    if (max_length && length > max_length)
    481         length = max_length;
     544        length = max_length;
    482545    if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
    483         return NULL;
     546        return NULL;
    484547
    485548    ENTER_ZLIB
     
    499562    */
    500563    while (err == Z_OK && self->zst.avail_out == 0) {
    501         /* If max_length set, don't continue decompressing if we've already
    502            reached the limit.
    503         */
    504         if (max_length && length >= max_length)
    505             break;
    506 
    507         /* otherwise, ... */
    508         old_length = length;
    509         length = length << 1;
    510         if (max_length && length > max_length)
    511             length = max_length;
    512 
    513         if (_PyString_Resize(&RetVal, length) < 0)
    514             goto error;
    515         self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
    516             + old_length;
    517         self->zst.avail_out = length - old_length;
    518 
    519         Py_BEGIN_ALLOW_THREADS
    520         err = inflate(&(self->zst), Z_SYNC_FLUSH);
    521         Py_END_ALLOW_THREADS
    522     }
    523 
    524     /* Not all of the compressed data could be accommodated in the output buffer
    525        of specified size. Return the unconsumed tail in an attribute.*/
    526     if(max_length) {
    527         Py_DECREF(self->unconsumed_tail);
    528         self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in,
    529                                                            self->zst.avail_in);
    530         if(!self->unconsumed_tail) {
    531             Py_DECREF(RetVal);
    532             RetVal = NULL;
    533             goto error;
    534         }
    535     }
    536 
    537     /* The end of the compressed data has been reached, so set the
    538        unused_data attribute to a string containing the remainder of the
    539        data in the string.  Note that this is also a logical place to call
    540        inflateEnd, but the old behaviour of only calling it on flush() is
    541        preserved.
    542     */
    543     if (err == Z_STREAM_END) {
    544         Py_XDECREF(self->unused_data);  /* Free original empty string */
    545         self->unused_data = PyString_FromStringAndSize(
    546             (char *)self->zst.next_in, self->zst.avail_in);
    547         if (self->unused_data == NULL) {
    548             Py_DECREF(RetVal);
    549             goto error;
    550         }
    551         /* We will only get Z_BUF_ERROR if the output buffer was full
    552            but there wasn't more output when we tried again, so it is
    553            not an error condition.
    554         */
    555     } else if (err != Z_OK && err != Z_BUF_ERROR) {
    556         zlib_error(self->zst, err, "while decompressing");
    557         Py_DECREF(RetVal);
    558         RetVal = NULL;
    559         goto error;
     564        /* If max_length set, don't continue decompressing if we've already
     565           reached the limit.
     566        */
     567        if (max_length && length >= max_length)
     568            break;
     569
     570        /* otherwise, ... */
     571        old_length = length;
     572        length = length << 1;
     573        if (max_length && length > max_length)
     574            length = max_length;
     575
     576        if (_PyString_Resize(&RetVal, length) < 0)
     577            goto error;
     578        self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
     579            + old_length;
     580        self->zst.avail_out = length - old_length;
     581
     582        Py_BEGIN_ALLOW_THREADS
     583        err = inflate(&(self->zst), Z_SYNC_FLUSH);
     584        Py_END_ALLOW_THREADS
     585    }
     586
     587    if (save_unconsumed_input(self, err) < 0) {
     588        Py_DECREF(RetVal);
     589        RetVal = NULL;
     590        goto error;
     591    }
     592
     593    /* This is the logical place to call inflateEnd, but the old behaviour of
     594       only calling it on flush() is preserved. */
     595
     596    if (err != Z_STREAM_END && err != Z_OK && err != Z_BUF_ERROR) {
     597        /* We will only get Z_BUF_ERROR if the output buffer was full
     598           but there wasn't more output when we tried again, so it is
     599           not an error condition.
     600        */
     601        zlib_error(self->zst, err, "while decompressing");
     602        Py_DECREF(RetVal);
     603        RetVal = NULL;
     604        goto error;
    560605    }
    561606
     
    585630
    586631    if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
    587         return NULL;
     632        return NULL;
    588633
    589634    /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
    590635       doing any work at all; just return an empty string. */
    591636    if (flushmode == Z_NO_FLUSH) {
    592         return PyString_FromStringAndSize(NULL, 0);
     637        return PyString_FromStringAndSize(NULL, 0);
    593638    }
    594639
    595640    if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
    596         return NULL;
     641        return NULL;
    597642
    598643    ENTER_ZLIB
     
    610655       so extend the output buffer and try again */
    611656    while (err == Z_OK && self->zst.avail_out == 0) {
    612         if (_PyString_Resize(&RetVal, length << 1) < 0)
    613             goto error;
    614         self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
    615             + length;
    616         self->zst.avail_out = length;
    617         length = length << 1;
    618 
    619         Py_BEGIN_ALLOW_THREADS
    620         err = deflate(&(self->zst), flushmode);
    621         Py_END_ALLOW_THREADS
     657        if (_PyString_Resize(&RetVal, length << 1) < 0)
     658            goto error;
     659        self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
     660            + length;
     661        self->zst.avail_out = length;
     662        length = length << 1;
     663
     664        Py_BEGIN_ALLOW_THREADS
     665        err = deflate(&(self->zst), flushmode);
     666        Py_END_ALLOW_THREADS
    622667    }
    623668
     
    626671       flushmode is Z_FINISH, but checking both for safety*/
    627672    if (err == Z_STREAM_END && flushmode == Z_FINISH) {
    628         err = deflateEnd(&(self->zst));
    629         if (err != Z_OK) {
    630             zlib_error(self->zst, err, "from deflateEnd()");
    631             Py_DECREF(RetVal);
    632             RetVal = NULL;
    633             goto error;
    634         }
    635         else
    636             self->is_initialised = 0;
    637 
    638         /* We will only get Z_BUF_ERROR if the output buffer was full
    639            but there wasn't more output when we tried again, so it is
    640            not an error condition.
    641         */
     673        err = deflateEnd(&(self->zst));
     674        if (err != Z_OK) {
     675            zlib_error(self->zst, err, "from deflateEnd()");
     676            Py_DECREF(RetVal);
     677            RetVal = NULL;
     678            goto error;
     679        }
     680        else
     681            self->is_initialised = 0;
     682
     683        /* We will only get Z_BUF_ERROR if the output buffer was full
     684           but there wasn't more output when we tried again, so it is
     685           not an error condition.
     686        */
    642687    } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
    643         zlib_error(self->zst, err, "while flushing");
    644         Py_DECREF(RetVal);
    645         RetVal = NULL;
    646         goto error;
     688        zlib_error(self->zst, err, "while flushing");
     689        Py_DECREF(RetVal);
     690        RetVal = NULL;
     691        goto error;
    647692    }
    648693
     
    774819
    775820    if (!PyArg_ParseTuple(args, "|i:flush", &length))
    776         return NULL;
     821        return NULL;
    777822    if (length <= 0) {
    778         PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
    779         return NULL;
     823        PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
     824        return NULL;
    780825    }
    781826    if (!(retval = PyString_FromStringAndSize(NULL, length)))
    782         return NULL;
     827        return NULL;
    783828
    784829
     
    786831
    787832    start_total_out = self->zst.total_out;
     833    self->zst.avail_in = PyString_GET_SIZE(self->unconsumed_tail);
     834    self->zst.next_in = (Byte *)PyString_AS_STRING(self->unconsumed_tail);
    788835    self->zst.avail_out = length;
    789836    self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
     
    796843       so extend the output buffer and try again */
    797844    while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
    798         if (_PyString_Resize(&retval, length << 1) < 0)
    799             goto error;
    800         self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
    801         self->zst.avail_out = length;
    802         length = length << 1;
    803 
    804         Py_BEGIN_ALLOW_THREADS
    805         err = inflate(&(self->zst), Z_FINISH);
    806         Py_END_ALLOW_THREADS
     845        if (_PyString_Resize(&retval, length << 1) < 0)
     846            goto error;
     847        self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
     848        self->zst.avail_out = length;
     849        length = length << 1;
     850
     851        Py_BEGIN_ALLOW_THREADS
     852        err = inflate(&(self->zst), Z_FINISH);
     853        Py_END_ALLOW_THREADS
     854    }
     855
     856    if (save_unconsumed_input(self, err) < 0) {
     857        Py_DECREF(retval);
     858        retval = NULL;
     859        goto error;
    807860    }
    808861
     
    811864       flushmode is Z_FINISH */
    812865    if (err == Z_STREAM_END) {
    813         err = inflateEnd(&(self->zst));
     866        err = inflateEnd(&(self->zst));
    814867        self->is_initialised = 0;
    815         if (err != Z_OK) {
    816             zlib_error(self->zst, err, "from inflateEnd()");
    817             Py_DECREF(retval);
    818             retval = NULL;
    819             goto error;
    820         }
    821     }
     868        if (err != Z_OK) {
     869            zlib_error(self->zst, err, "from inflateEnd()");
     870            Py_DECREF(retval);
     871            retval = NULL;
     872            goto error;
     873        }
     874    }
     875
    822876    _PyString_Resize(&retval, self->zst.total_out - start_total_out);
    823877
     
    872926
    873927    if (strcmp(name, "unused_data") == 0) {
    874         Py_INCREF(self->unused_data);
    875         retval = self->unused_data;
     928        Py_INCREF(self->unused_data);
     929        retval = self->unused_data;
    876930    } else if (strcmp(name, "unconsumed_tail") == 0) {
    877         Py_INCREF(self->unconsumed_tail);
    878         retval = self->unconsumed_tail;
     931        Py_INCREF(self->unconsumed_tail);
     932        retval = self->unconsumed_tail;
    879933    } else
    880         retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
     934        retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
    881935
    882936    LEAVE_ZLIB
     
    899953
    900954    if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
    901         return NULL;
     955        return NULL;
    902956    /* In Python 2.x we return a signed integer regardless of native platform
    903957     * long size (the 32bit unsigned long is treated as 32-bit signed and sign
     
    922976
    923977    if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
    924         return NULL;
     978        return NULL;
    925979    /* In Python 2.x we return a signed integer regardless of native platform
    926980     * long size (the 32bit unsigned long is treated as 32-bit signed and sign
     
    9861040"\n"
    9871041"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
    988 "compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
     1042"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
    9891043"compressobj([level]) -- Return a compressor object.\n"
    9901044"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
     
    10031057    Py_TYPE(&Decomptype) = &PyType_Type;
    10041058    m = Py_InitModule4("zlib", zlib_methods,
    1005                        zlib_module_documentation,
    1006                        (PyObject*)NULL,PYTHON_API_VERSION);
     1059                       zlib_module_documentation,
     1060                       (PyObject*)NULL,PYTHON_API_VERSION);
    10071061    if (m == NULL)
    1008         return;
     1062        return;
    10091063
    10101064    ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
    10111065    if (ZlibError != NULL) {
    10121066        Py_INCREF(ZlibError);
    1013         PyModule_AddObject(m, "error", ZlibError);
     1067        PyModule_AddObject(m, "error", ZlibError);
    10141068    }
    10151069    PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
     
    10301084    ver = PyString_FromString(ZLIB_VERSION);
    10311085    if (ver != NULL)
    1032         PyModule_AddObject(m, "ZLIB_VERSION", ver);
     1086        PyModule_AddObject(m, "ZLIB_VERSION", ver);
    10331087
    10341088    PyModule_AddStringConstant(m, "__version__", "1.0");
Note: See TracChangeset for help on using the changeset viewer.