Commits

Philip Jenvey committed a7400b0

o fix tests under -A
o zlib adler/crc32 are now backed to unsigned results

  • Participants
  • Parent commits e762cce
  • Branches py3k

Comments (0)

Files changed (2)

File pypy/module/zlib/interp_zlib.py

 from pypy.rlib import rzlib
 
 
-if intmask(2**31) == -2**31:
-    # 32-bit platforms
-    unsigned_to_signed_32bit = intmask
-else:
-    # 64-bit platforms
-    def unsigned_to_signed_32bit(x):
-        # assumes that 'x' is in range(0, 2**32) to start with
-        SIGN_EXTEND2 = 1 << 31
-        return intmask((x ^ SIGN_EXTEND2) - SIGN_EXTEND2)
-
-
 @unwrap_spec(string='bufferstr', start='truncatedint_w')
 def crc32(space, string, start = rzlib.CRC32_DEFAULT_START):
     """
     """
     ustart = r_uint(start)
     checksum = rzlib.crc32(string, ustart)
-
-    # This is, perhaps, a little stupid.  zlib returns the checksum unsigned.
-    # CPython exposes it as a signed value, though. -exarkun
-    # Note that in CPython < 2.6 on 64-bit platforms the result is
-    # actually unsigned, but it was considered to be a bug so we stick to
-    # the 2.6 behavior and always return a number in range(-2**31, 2**31).
-    checksum = unsigned_to_signed_32bit(checksum)
-
     return space.wrap(checksum)
 
 
     """
     ustart = r_uint(start)
     checksum = rzlib.adler32(string, ustart)
-    # See comments in crc32() for the following line
-    checksum = unsigned_to_signed_32bit(checksum)
-
     return space.wrap(checksum)
 
 

File pypy/module/zlib/test/test_zlib.py

 except ImportError:
     import py; py.test.skip("no zlib module on this host Python")
 
-try:
-    from pypy.module.zlib import interp_zlib
-except ImportError:
-    import py; py.test.skip("no zlib C library on this machine")
- 
-def test_unsigned_to_signed_32bit():
-    assert interp_zlib.unsigned_to_signed_32bit(123) == 123
-    assert interp_zlib.unsigned_to_signed_32bit(2**31) == -2**31
-    assert interp_zlib.unsigned_to_signed_32bit(2**32-1) == -1
-
 
 class AppTestZlib(object):
     spaceconfig = dict(usemodules=['zlib'])
     def test_crc32(self):
         """
         When called with a string, zlib.crc32 should compute its CRC32 and
-        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).
+        return it as an unsigned 32 bit integer.
         """
         assert self.zlib.crc32(b'') == 0
-        assert self.zlib.crc32(b'\0') == -771559539
-        assert self.zlib.crc32(b'hello, world.') == -936931198
+        assert self.zlib.crc32(b'\0') == 3523407757
+        assert self.zlib.crc32(b'hello, world.') == 3358036098
 
 
     def test_crc32_start_value(self):
 
     def test_crc32_negative_start(self):
         v = self.zlib.crc32(b'', -1)
-        assert v == -1
+        assert v == 4294967295
 
     def test_crc32_negative_long_start(self):
         v = self.zlib.crc32(b'', -1)
-        assert v == -1
+        assert v == 4294967295
         assert self.zlib.crc32(b'foo', -99999999999999999999999) == 1611238463
 
     def test_crc32_long_start(self):
         import sys
         v = self.zlib.crc32(b'', sys.maxsize*2)
-        assert v == -2
+        assert v == 4294967294
         assert self.zlib.crc32(b'foo', 99999999999999999999999) == 1635107045
 
     def test_adler32(self):
         """
         When called with a string, zlib.adler32() should compute its adler 32
-        checksum and 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).
+        checksum and return it as an unsigned 32 bit integer.
         """
         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
+        assert self.zlib.adler32(b'x' * 23) == 2172062409
 
 
     def test_adler32_start_value(self):
         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
+        assert self.zlib.adler32(b'x' * 23, 42) == 2233862898
         hello = b'hello, '
         hellosum = self.zlib.adler32(hello)
         world = b'world.'
         assert helloworldsum == self.zlib.adler32(hello + world)
 
         assert self.zlib.adler32(b'foo', -1) == 45547858
-        assert self.zlib.adler32(b'foo', 99999999999999999999999) == -114818734
+        assert self.zlib.adler32(b'foo', 99999999999999999999999) == 4180148562
 
 
     def test_invalidLevel(self):
         assert d.unused_data == b'spam' * 100
         assert s1 + s2 + s3 == self.expanded
         s4 = d.decompress(b'egg' * 50)
-        assert d.unused_data == b'egg' * 50
+        assert d.unused_data == (b'spam' * 100) + (b'egg' * 50), d.unused_data
         assert s4 == b''
 
 
         """
         We should be able to pass buffer objects instead of strings.
         """
-        assert self.zlib.crc32(memoryview(b'hello, world.')) == -936931198
+        assert self.zlib.crc32(memoryview(b'hello, world.')) == 3358036098
         assert self.zlib.adler32(memoryview(b'hello, world.')) == 571147447
 
         compressor = self.zlib.compressobj()