From ab0d8a1f59aaa71b234c1c273dd61b951950678d Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Mon, 17 Mar 2008 20:24:09 +0000 Subject: zlib.crc32 and zlib.adler32 now return an unsigned value as any sane person would expect. Fixes issues1202. --- Doc/library/zlib.rst | 4 ++++ Lib/test/test_zlib.py | 8 ++++++++ Modules/zlibmodule.c | 4 ++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Doc/library/zlib.rst b/Doc/library/zlib.rst index dda0b9c..e55e52a 100644 --- a/Doc/library/zlib.rst +++ b/Doc/library/zlib.rst @@ -42,6 +42,8 @@ The available exception and functions in this module are: the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm. + Always returns an unsigned 32-bit integer. + .. function:: compress(string[, level]) @@ -74,6 +76,8 @@ The available exception and functions in this module are: the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm. + Always returns an unsigned 32-bit integer. + .. function:: decompress(string[, wbits[, bufsize]]) diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py index 13656d6..6b7d4a6 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -38,6 +38,14 @@ class ChecksumTestCase(unittest.TestCase): self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0)) self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1)) + def test_crc32_adler32_unsigned(self): + foo = 'abcdefghijklmnop' + # explicitly test signed behavior + self.assertEqual(zlib.crc32(foo), 2486878355) + self.assertEqual(zlib.crc32('spam'), 1138425661) + self.assertEqual(zlib.adler32(foo+foo), 3573550353) + self.assertEqual(zlib.adler32('spam'), 72286642) + class ExceptionTestCase(unittest.TestCase): diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index 441d61d..a978370 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -922,7 +922,7 @@ PyZlib_adler32(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "s#|k:adler32", &buf, &len, &adler32val)) return NULL; adler32val = adler32(adler32val, buf, len); - return PyLong_FromLong(adler32val); + return PyLong_FromUnsignedLong(adler32val & 0xffffffff); } PyDoc_STRVAR(crc32__doc__, @@ -940,7 +940,7 @@ PyZlib_crc32(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "s#|k:crc32", &buf, &len, &crc32val)) return NULL; crc32val = crc32(crc32val, buf, len); - return PyLong_FromLong(crc32val); + return PyLong_FromUnsignedLong(crc32val & 0xffffffff); } -- cgit v0.12