[pypy-commit] pypy py3k: zlib only deals with bytes, not str

amauryfa noreply at buildbot.pypy.org
Mon Nov 7 21:22:39 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r48879:0aaed69faf04
Date: 2011-11-07 21:02 +0100
http://bitbucket.org/pypy/pypy/changeset/0aaed69faf04/

Log:	zlib only deals with bytes, not str

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
@@ -1,7 +1,7 @@
 import sys
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.baseobjspace import Wrappable
-from pypy.interpreter.typedef import TypeDef, interp_attrproperty
+from pypy.interpreter.typedef import TypeDef, interp_attrproperty_bytes
 from pypy.interpreter.error import OperationError
 from pypy.rlib.rarithmetic import intmask, r_uint
 from pypy.rlib.objectmodel import keepalive_until_here
@@ -84,7 +84,7 @@
             rzlib.deflateEnd(stream)
     except rzlib.RZlibError, e:
         raise zlib_error(space, e.msg)
-    return space.wrap(result)
+    return space.wrapbytes(result)
 
 
 @unwrap_spec(string='bufferstr', wbits=int, bufsize=int)
@@ -106,7 +106,7 @@
             rzlib.inflateEnd(stream)
     except rzlib.RZlibError, e:
         raise zlib_error(space, e.msg)
-    return space.wrap(result)
+    return space.wrapbytes(result)
 
 
 class ZLibObject(Wrappable):
@@ -179,7 +179,7 @@
                 self.unlock()
         except rzlib.RZlibError, e:
             raise zlib_error(self.space, e.msg)
-        return self.space.wrap(result)
+        return self.space.wrapbytes(result)
 
 
     @unwrap_spec(mode=int)
@@ -209,7 +209,7 @@
                 self.unlock()
         except rzlib.RZlibError, e:
             raise zlib_error(self.space, e.msg)
-        return self.space.wrap(result)
+        return self.space.wrapbytes(result)
 
 
 @unwrap_spec(level=int, method=int, wbits=int, memLevel=int, strategy=int)
@@ -302,11 +302,11 @@
         assert unused_start >= 0
         tail = data[unused_start:]
         if finished:
-            self.unconsumed_tail = ''
+            self.unconsumed_tail = b''
             self.unused_data = tail
         else:
             self.unconsumed_tail = tail
-        return self.space.wrap(string)
+        return self.space.wrapbytes(string)
 
 
     @unwrap_spec(length=int)
@@ -324,7 +324,7 @@
         # however CPython's zlib module does not behave like that.
         # I could not figure out a case in which flush() in CPython
         # doesn't simply return an empty string without complaining.
-        return self.space.wrap("")
+        return self.space.wrapbytes("")
 
 
 @unwrap_spec(wbits=int)
@@ -343,8 +343,8 @@
     __new__ = interp2app(Decompress___new__),
     decompress = interp2app(Decompress.decompress),
     flush = interp2app(Decompress.flush),
-    unused_data = interp_attrproperty('unused_data', Decompress),
-    unconsumed_tail = interp_attrproperty('unconsumed_tail', Decompress),
+    unused_data = interp_attrproperty_bytes('unused_data', Decompress),
+    unconsumed_tail = interp_attrproperty_bytes('unconsumed_tail', Decompress),
     __doc__ = """decompressobj([wbits]) -- Return a decompressor object.
 
 Optional arg wbits is the window buffer size.
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
@@ -34,9 +34,9 @@
             import zlib
             return zlib
         """)
-        expanded = 'some bytes which will be compressed'
-        cls.w_expanded = cls.space.wrap(expanded)
-        cls.w_compressed = cls.space.wrap(zlib.compress(expanded))
+        expanded = b'some bytes which will be compressed'
+        cls.w_expanded = cls.space.wrapbytes(expanded)
+        cls.w_compressed = cls.space.wrapbytes(zlib.compress(expanded))
 
 
     def test_error(self):
@@ -52,9 +52,9 @@
         return it as a signed 32 bit integer.  On 64-bit machines too
         (it is a bug in CPython < 2.6 to return unsigned values in this case).
         """
-        assert self.zlib.crc32('') == 0
-        assert self.zlib.crc32('\0') == -771559539
-        assert self.zlib.crc32('hello, world.') == -936931198
+        assert self.zlib.crc32(b'') == 0
+        assert self.zlib.crc32(b'\0') == -771559539
+        assert self.zlib.crc32(b'hello, world.') == -936931198
 
 
     def test_crc32_start_value(self):
@@ -62,29 +62,29 @@
         When called with a string and an integer, zlib.crc32 should compute the
         CRC32 of the string using the integer as the starting value.
         """
-        assert self.zlib.crc32('', 42) == 42
-        assert self.zlib.crc32('\0', 42) == 163128923
-        assert self.zlib.crc32('hello, world.', 42) == 1090960721
-        hello = 'hello, '
+        assert self.zlib.crc32(b'', 42) == 42
+        assert self.zlib.crc32(b'\0', 42) == 163128923
+        assert self.zlib.crc32(b'hello, world.', 42) == 1090960721
+        hello = b'hello, '
         hellocrc = self.zlib.crc32(hello)
-        world = 'world.'
+        world = b'world.'
         helloworldcrc = self.zlib.crc32(world, hellocrc)
         assert helloworldcrc == self.zlib.crc32(hello + world)
 
     def test_crc32_negative_start(self):
-        v = self.zlib.crc32('', -1)
+        v = self.zlib.crc32(b'', -1)
         assert v == -1
 
     def test_crc32_negative_long_start(self):
-        v = self.zlib.crc32('', -1L)
+        v = self.zlib.crc32(b'', -1L)
         assert v == -1
-        assert self.zlib.crc32('foo', -99999999999999999999999) == 1611238463
+        assert self.zlib.crc32(b'foo', -99999999999999999999999) == 1611238463
 
     def test_crc32_long_start(self):
         import sys
-        v = self.zlib.crc32('', sys.maxint*2)
+        v = self.zlib.crc32(b'', sys.maxint*2)
         assert v == -2
-        assert self.zlib.crc32('foo', 99999999999999999999999) == 1635107045
+        assert self.zlib.crc32(b'foo', 99999999999999999999999) == 1635107045
 
     def test_adler32(self):
         """
@@ -93,10 +93,10 @@
         On 64-bit machines too
         (it is a bug in CPython < 2.6 to return unsigned values in this case).
         """
-        assert self.zlib.adler32('') == 1
-        assert self.zlib.adler32('\0') == 65537
-        assert self.zlib.adler32('hello, world.') == 571147447
-        assert self.zlib.adler32('x' * 23) == -2122904887
+        assert self.zlib.adler32(b'') == 1
+        assert self.zlib.adler32(b'\0') == 65537
+        assert self.zlib.adler32(b'hello, world.') == 571147447
+        assert self.zlib.adler32(b'x' * 23) == -2122904887
 
 
     def test_adler32_start_value(self):
@@ -105,18 +105,18 @@
         the adler 32 checksum of the string using the integer as the starting
         value.
         """
-        assert self.zlib.adler32('', 42) == 42
-        assert self.zlib.adler32('\0', 42) == 2752554
-        assert self.zlib.adler32('hello, world.', 42) == 606078176
-        assert self.zlib.adler32('x' * 23, 42) == -2061104398
-        hello = 'hello, '
+        assert self.zlib.adler32(b'', 42) == 42
+        assert self.zlib.adler32(b'\0', 42) == 2752554
+        assert self.zlib.adler32(b'hello, world.', 42) == 606078176
+        assert self.zlib.adler32(b'x' * 23, 42) == -2061104398
+        hello = b'hello, '
         hellosum = self.zlib.adler32(hello)
-        world = 'world.'
+        world = b'world.'
         helloworldsum = self.zlib.adler32(world, hellosum)
         assert helloworldsum == self.zlib.adler32(hello + world)
 
-        assert self.zlib.adler32('foo', -1) == 45547858
-        assert self.zlib.adler32('foo', 99999999999999999999999) == -114818734
+        assert self.zlib.adler32(b'foo', -1) == 45547858
+        assert self.zlib.adler32(b'foo', 99999999999999999999999) == -114818734
 
 
     def test_invalidLevel(self):
@@ -171,7 +171,7 @@
         Try to feed garbage to zlib.decompress().
         """
         raises(self.zlib.error, self.zlib.decompress, self.compressed[:-2])
-        raises(self.zlib.error, self.zlib.decompress, 'foobar')
+        raises(self.zlib.error, self.zlib.decompress, b'foobar')
 
 
     def test_unused_data(self):
@@ -180,21 +180,21 @@
         It should show up in the unused_data attribute.
         """
         d = self.zlib.decompressobj()
-        s = d.decompress(self.compressed + 'extrastuff')
+        s = d.decompress(self.compressed + b'extrastuff')
         assert s == self.expanded
-        assert d.unused_data == 'extrastuff'
+        assert d.unused_data == b'extrastuff'
         # try again with several decompression steps
         d = self.zlib.decompressobj()
         s1 = d.decompress(self.compressed[:10])
-        assert d.unused_data == ''
+        assert d.unused_data == b''
         s2 = d.decompress(self.compressed[10:-3])
-        assert d.unused_data == ''
-        s3 = d.decompress(self.compressed[-3:] + 'spam' * 100)
-        assert d.unused_data == 'spam' * 100
+        assert d.unused_data == b''
+        s3 = d.decompress(self.compressed[-3:] + b'spam' * 100)
+        assert d.unused_data == b'spam' * 100
         assert s1 + s2 + s3 == self.expanded
-        s4 = d.decompress('egg' * 50)
-        assert d.unused_data == 'egg' * 50
-        assert s4 == ''
+        s4 = d.decompress(b'egg' * 50)
+        assert d.unused_data == b'egg' * 50
+        assert s4 == b''
 
 
     def test_max_length(self):
@@ -215,8 +215,8 @@
         """
         We should be able to pass buffer objects instead of strings.
         """
-        assert self.zlib.crc32(buffer('hello, world.')) == -936931198
-        assert self.zlib.adler32(buffer('hello, world.')) == 571147447
+        assert self.zlib.crc32(buffer(b'hello, world.')) == -936931198
+        assert self.zlib.adler32(buffer(b'hello, world.')) == 571147447
 
         compressor = self.zlib.compressobj()
         bytes = compressor.compress(buffer(self.expanded))


More information about the pypy-commit mailing list