diff -r 3a57eafd8401 Lib/test/test_zlib.py --- a/Lib/test/test_zlib.py Tue May 24 09:15:14 2016 +0300 +++ b/Lib/test/test_zlib.py Wed Jun 01 22:59:05 2016 +0800 @@ -557,6 +557,15 @@ self.assertEqual(dco.unconsumed_tail, b'') self.assertEqual(dco.unused_data, remainder) + # issue27164 + def test_decompress_raw_with_dictionary(self): + zdict = b'abcdefghijklmnopqrstuvwxyz' + co = zlib.compressobj(wbits=-zlib.MAX_WBITS, zdict=zdict) + comp = co.compress(zdict) + co.flush() + dco = zlib.decompressobj(wbits=-zlib.MAX_WBITS, zdict=zdict) + uncomp = dco.decompress(comp) + dco.flush() + self.assertEqual(zdict, uncomp) + def test_flush_with_freed_input(self): # Issue #16411: decompressor accesses input to last decompress() call # in flush(), even if this object has been freed in the meanwhile. diff -r 3a57eafd8401 Modules/zlibmodule.c --- a/Modules/zlibmodule.c Tue May 24 09:15:14 2016 +0300 +++ b/Modules/zlibmodule.c Wed Jun 01 22:59:05 2016 +0800 @@ -22,6 +22,10 @@ #define LEAVE_ZLIB(obj) #endif +#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221 +#define AT_LEAST_ZLIB_1_2_2_1 +#endif + /* The following parameters are copied from zutil.h, version 0.95 */ #define DEFLATED 8 #if MAX_MEM_LEVEL >= 8 @@ -511,6 +515,36 @@ switch(err) { case (Z_OK): self->is_initialised = 1; + if (self->zdict != NULL && wbits < 0) { +#ifdef AT_LEAST_ZLIB_1_2_2_1 + Py_buffer zdict_buf; + if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) { + Py_DECREF(self); + return NULL; + } + if ((size_t)zdict_buf.len > UINT_MAX) { + PyErr_SetString(PyExc_OverflowError, + "zdict length does not fit in an unsigned int"); + PyBuffer_Release(&zdict_buf); + Py_DECREF(self); + return NULL; + } + err = inflateSetDictionary(&(self->zst), + zdict_buf.buf, (unsigned int)zdict_buf.len); + PyBuffer_Release(&zdict_buf); + if (err != Z_OK) { + zlib_error(self->zst, err, "while setting zdict"); + Py_DECREF(self); + return NULL; + } +#else + PyErr_Format(ZlibError, + "zlib version %s does not allow raw inflate with dictionary", + ZLIB_VERSION); + Py_DECREF(self); + return NULL; +#endif + } return (PyObject*)self; case(Z_STREAM_ERROR): Py_DECREF(self);