Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab0d8a1

Browse files
committed
zlib.crc32 and zlib.adler32 now return an unsigned value as any sane person
would expect. Fixes issues1202.
1 parent c55485b commit ab0d8a1

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

Doc/library/zlib.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ The available exception and functions in this module are:
4242
the algorithm is designed for use as a checksum algorithm, it is not suitable
4343
for use as a general hash algorithm.
4444

45+
Always returns an unsigned 32-bit integer.
46+
4547

4648
.. function:: compress(string[, level])
4749

@@ -74,6 +76,8 @@ The available exception and functions in this module are:
7476
the algorithm is designed for use as a checksum algorithm, it is not suitable
7577
for use as a general hash algorithm.
7678

79+
Always returns an unsigned 32-bit integer.
80+
7781

7882
.. function:: decompress(string[, wbits[, bufsize]])
7983

Lib/test/test_zlib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ def test_penguins(self):
3838
self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0))
3939
self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1))
4040

41+
def test_crc32_adler32_unsigned(self):
42+
foo = 'abcdefghijklmnop'
43+
# explicitly test signed behavior
44+
self.assertEqual(zlib.crc32(foo), 2486878355)
45+
self.assertEqual(zlib.crc32('spam'), 1138425661)
46+
self.assertEqual(zlib.adler32(foo+foo), 3573550353)
47+
self.assertEqual(zlib.adler32('spam'), 72286642)
48+
4149

4250

4351
class ExceptionTestCase(unittest.TestCase):

Modules/zlibmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ PyZlib_adler32(PyObject *self, PyObject *args)
922922
if (!PyArg_ParseTuple(args, "s#|k:adler32", &buf, &len, &adler32val))
923923
return NULL;
924924
adler32val = adler32(adler32val, buf, len);
925-
return PyLong_FromLong(adler32val);
925+
return PyLong_FromUnsignedLong(adler32val & 0xffffffff);
926926
}
927927

928928
PyDoc_STRVAR(crc32__doc__,
@@ -940,7 +940,7 @@ PyZlib_crc32(PyObject *self, PyObject *args)
940940
if (!PyArg_ParseTuple(args, "s#|k:crc32", &buf, &len, &crc32val))
941941
return NULL;
942942
crc32val = crc32(crc32val, buf, len);
943-
return PyLong_FromLong(crc32val);
943+
return PyLong_FromUnsignedLong(crc32val & 0xffffffff);
944944
}
945945

946946

0 commit comments

Comments
 (0)