[Python-3000-checkins] r61666 - in python/branches/py3k: Lib/test/test_zlib.py Modules/binascii.c Modules/zlibmodule.c

gregory.p.smith python-3000-checkins at python.org
Thu Mar 20 07:20:10 CET 2008


Author: gregory.p.smith
Date: Thu Mar 20 07:20:09 2008
New Revision: 61666

Modified:
   python/branches/py3k/Lib/test/test_zlib.py
   python/branches/py3k/Modules/binascii.c
   python/branches/py3k/Modules/zlibmodule.c
Log:
crc32 always returns unsigned.  cleanup the code a bit and revert r61648 with
the proper fix.


Modified: python/branches/py3k/Lib/test/test_zlib.py
==============================================================================
--- python/branches/py3k/Lib/test/test_zlib.py	(original)
+++ python/branches/py3k/Lib/test/test_zlib.py	Thu Mar 20 07:20:09 2008
@@ -42,14 +42,14 @@
     def test_crc32_adler32_unsigned(self):
         foo = 'abcdefghijklmnop'
         # explicitly test signed behavior
-        self.assertEqual(zlib.crc32(foo), -1808088941)
+        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)
 
     def test_same_as_binascii_crc32(self):
         foo = 'abcdefghijklmnop'
-        crc = -1808088941
+        crc = 2486878355
         self.assertEqual(binascii.crc32(foo), crc)
         self.assertEqual(zlib.crc32(foo), crc)
         self.assertEqual(binascii.crc32('spam'), zlib.crc32('spam'))

Modified: python/branches/py3k/Modules/binascii.c
==============================================================================
--- python/branches/py3k/Modules/binascii.c	(original)
+++ python/branches/py3k/Modules/binascii.c	Thu Mar 20 07:20:09 2008
@@ -898,33 +898,21 @@
 binascii_crc32(PyObject *self, PyObject *args)
 { /* By Jim Ahlstrom; All rights transferred to CNRI */
 	unsigned char *bin_data;
-	unsigned long crc = 0UL;	/* initial value of CRC */
+	unsigned int crc = 0;	/* initial value of CRC */
 	Py_ssize_t len;
-	long result;
+	unsigned int result;
 
-	if ( !PyArg_ParseTuple(args, "s#|k:crc32", &bin_data, &len, &crc) )
+	if ( !PyArg_ParseTuple(args, "s#|I:crc32", &bin_data, &len, &crc) )
 		return NULL;
 
 	crc = ~ crc;
-#if SIZEOF_LONG > 4
-	/* only want the trailing 32 bits */
-	crc &= 0xFFFFFFFFUL;
-#endif
-	while (len--)
-		crc = crc_32_tab[(crc ^ *bin_data++) & 0xffUL] ^ (crc >> 8);
+	while (len--) {
+		crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
 		/* Note:  (crc >> 8) MUST zero fill on left */
+	}
 
-	result = (long)(crc ^ 0xFFFFFFFFUL);
-#if SIZEOF_LONG > 4
-	/* Extend the sign bit.  This is one way to ensure the result is the
-	 * same across platforms.  The other way would be to return an
-	 * unbounded unsigned long, but the evidence suggests that lots of
-	 * code outside this treats the result as if it were a signed 4-byte
-	 * integer.
-	 */
-	result |= -(result & (1L << 31));
-#endif
-	return PyLong_FromLong(result);
+	result = (crc ^ 0xFFFFFFFF);
+	return PyLong_FromUnsignedLong(result & 0xffffffff);
 }
 
 

Modified: python/branches/py3k/Modules/zlibmodule.c
==============================================================================
--- python/branches/py3k/Modules/zlibmodule.c	(original)
+++ python/branches/py3k/Modules/zlibmodule.c	Thu Mar 20 07:20:09 2008
@@ -915,14 +915,14 @@
 static PyObject *
 PyZlib_adler32(PyObject *self, PyObject *args)
 {
-    uLong adler32val = adler32(0L, Z_NULL, 0);
+    unsigned int adler32val = adler32(0L, Z_NULL, 0);
     Byte *buf;
     int len;
 
-    if (!PyArg_ParseTuple(args, "s#|k:adler32", &buf, &len, &adler32val))
+    if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
 	return NULL;
     adler32val = adler32(adler32val, buf, len);
-    return PyLong_FromUnsignedLong(adler32val & 0xffffffff);
+    return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
 }
 
 PyDoc_STRVAR(crc32__doc__,
@@ -934,13 +934,13 @@
 static PyObject *
 PyZlib_crc32(PyObject *self, PyObject *args)
 {
-    uLong crc32val = crc32(0L, Z_NULL, 0);
+    unsigned int crc32val = crc32(0L, Z_NULL, 0);
     Byte *buf;
     int len;
-    if (!PyArg_ParseTuple(args, "s#|k:crc32", &buf, &len, &crc32val))
+    if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
 	return NULL;
     crc32val = crc32(crc32val, buf, len);
-    return PyLong_FromLong(crc32val & 0xffffffff);
+    return PyLong_FromUnsignedLong(crc32val & 0xffffffffU);
 }
 
 


More information about the Python-3000-checkins mailing list