Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r89409:ee9a45377582
Date: 2017-01-07 10:56 +0100
http://bitbucket.org/pypy/pypy/changeset/ee9a45377582/

Log:    zlib: CPython issue27164

diff --git a/pypy/module/zlib/interp_zlib.py b/pypy/module/zlib/interp_zlib.py
--- a/pypy/module/zlib/interp_zlib.py
+++ b/pypy/module/zlib/interp_zlib.py
@@ -122,9 +122,7 @@
         ZLibObject.__init__(self, space)
         try:
             self.stream = rzlib.deflateInit(level, method, wbits,
-                                            memLevel, strategy)
-            if zdict is not None:
-                rzlib.deflateSetDictionary(self.stream, zdict)
+                                            memLevel, strategy, zdict=zdict)
         except rzlib.RZlibError as e:
             raise zlib_error(space, e.msg)
         except ValueError:
@@ -242,7 +240,7 @@
         self.unconsumed_tail = ''
         self.eof = False
         try:
-            self.stream = rzlib.inflateInit(wbits)
+            self.stream = rzlib.inflateInit(wbits, zdict=zdict)
         except rzlib.RZlibError as e:
             raise zlib_error(space, e.msg)
         except ValueError:
diff --git a/pypy/module/zlib/test/test_zlib.py 
b/pypy/module/zlib/test/test_zlib.py
--- a/pypy/module/zlib/test/test_zlib.py
+++ b/pypy/module/zlib/test/test_zlib.py
@@ -320,3 +320,13 @@
     def test_version(self):
         zlib = self.zlib
         assert zlib.ZLIB_VERSION[0] == zlib.ZLIB_RUNTIME_VERSION[0]
+
+    # CPython issue27164
+    def test_decompress_raw_with_dictionary(self):
+        zlib = self.zlib
+        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()
+        assert zdict == uncomp
diff --git a/rpython/rlib/rzlib.py b/rpython/rlib/rzlib.py
--- a/rpython/rlib/rzlib.py
+++ b/rpython/rlib/rzlib.py
@@ -51,6 +51,7 @@
     voidpf = rffi_platform.SimpleType('voidpf', rffi.VOIDP)
 
     ZLIB_VERSION = rffi_platform.DefinedConstantString('ZLIB_VERSION')
+    ZLIB_VERNUM = rffi_platform.DefinedConstantInteger('ZLIB_VERNUM')
 
 for _name in constantnames:
     setattr(SimpleCConfig, _name, rffi_platform.ConstantInteger(_name))
@@ -63,6 +64,7 @@
 Bytefp = lltype.Ptr(lltype.Array(Bytef, hints={'nolength': True}))
 
 ZLIB_VERSION = config['ZLIB_VERSION']
+ZLIB_VERNUM = config['ZLIB_VERNUM']
 
 for _name in constantnames:
     globals()[_name] = config[_name]
@@ -261,7 +263,7 @@
 
 def deflateInit(level=Z_DEFAULT_COMPRESSION, method=Z_DEFLATED,
                 wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL,
-                strategy=Z_DEFAULT_STRATEGY):
+                strategy=Z_DEFAULT_STRATEGY, zdict=None):
     """
     Allocate and return an opaque 'stream' object that can be used to
     compress data.
@@ -270,6 +272,12 @@
     rgc.add_memory_pressure(rffi.sizeof(z_stream))
     err = _deflateInit2(stream, level, method, wbits, memLevel, strategy)
     if err == Z_OK:
+        if zdict is not None:
+            try:
+                deflateSetDictionary(stream, zdict)
+            except:
+                lltype.free(stream, flavor='raw')
+                raise
         return stream
     else:
         try:
@@ -290,7 +298,7 @@
     lltype.free(stream, flavor='raw')
 
 
-def inflateInit(wbits=MAX_WBITS):
+def inflateInit(wbits=MAX_WBITS, zdict=None):
     """
     Allocate and return an opaque 'stream' object that can be used to
     decompress data.
@@ -299,6 +307,17 @@
     rgc.add_memory_pressure(rffi.sizeof(z_stream))
     err = _inflateInit2(stream, wbits)
     if err == Z_OK:
+        if zdict is not None and wbits < 0:
+            try:
+                if ZLIB_VERNUM is None or ZLIB_VERNUM < 0x1221:
+                    raise RZlibError("zlib version %s does not allow raw "
+                                     "inflate with dictionary" %
+                                       ZLIB_VERSION if ZLIB_VERSION is not None
+                                       else "<unknown>")
+                inflateSetDictionary(stream, zdict)
+            except:
+                lltype.free(stream, flavor='raw')
+                raise
         return stream
     else:
         try:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to