diff options
author | Gregory P. Smith <greg@mad-scientist.com> | 2008-03-17 18:48:05 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@mad-scientist.com> | 2008-03-17 18:48:05 (GMT) |
commit | f48f9d38c02ef914194eeeeb6cdae770e218af00 (patch) | |
tree | 66b91bf167ab2d565d9da953376e038b3380796b /Modules | |
parent | 33451d8ab143fb89374673d3aa751910c5b1031e (diff) | |
download | cpython-f48f9d38c02ef914194eeeeb6cdae770e218af00.zip cpython-f48f9d38c02ef914194eeeeb6cdae770e218af00.tar.gz cpython-f48f9d38c02ef914194eeeeb6cdae770e218af00.tar.bz2 |
Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
regardless of the native sizeof(long) used in the integer object.
This somewhat odd behavior of returning a signed is maintained in 2.x for
compatibility reasons of always returning an integer rather than a long object.
Fixes Issue1202 for Python 2.6
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/zlibmodule.c | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index ef00cca..384399d 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -884,37 +884,46 @@ PyDoc_STRVAR(adler32__doc__, "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n" "\n" "An optional starting value can be specified. The returned checksum is\n" -"an integer."); +"a signed integer."); static PyObject * PyZlib_adler32(PyObject *self, PyObject *args) { uLong adler32val = adler32(0L, Z_NULL, 0); Byte *buf; - int len; + int len, signed_val; if (!PyArg_ParseTuple(args, "s#|k:adler32", &buf, &len, &adler32val)) return NULL; - adler32val = adler32(adler32val, buf, len); - return PyInt_FromLong(adler32val); + /* In Python 2.x we return a signed integer regardless of native platform + * long size (the 32bit unsigned long is treated as 32-bit signed and sign + * extended into a 64-bit long inside the integer object). 3.0 does the + * right thing and returns unsigned. http://bugs.python.org/issue1202 */ + signed_val = adler32(adler32val, buf, len); + return PyInt_FromLong(signed_val); } PyDoc_STRVAR(crc32__doc__, "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n" "\n" "An optional starting value can be specified. The returned checksum is\n" -"an integer."); +"a signed integer."); static PyObject * PyZlib_crc32(PyObject *self, PyObject *args) { uLong crc32val = crc32(0L, Z_NULL, 0); Byte *buf; - int len; + int len, signed_val; + if (!PyArg_ParseTuple(args, "s#|k:crc32", &buf, &len, &crc32val)) return NULL; - crc32val = crc32(crc32val, buf, len); - return PyInt_FromLong(crc32val); + /* In Python 2.x we return a signed integer regardless of native platform + * long size (the 32bit unsigned long is treated as 32-bit signed and sign + * extended into a 64-bit long inside the integer object). 3.0 does the + * right thing and returns unsigned. http://bugs.python.org/issue1202 */ + signed_val = crc32(crc32val, buf, len); + return PyInt_FromLong(signed_val); } |