[Python-3000-checkins] r61829 - in python/branches/py3k: Lib/gzip.py Modules/binascii.c Modules/zlibmodule.c setup.py

christian.heimes python-3000-checkins at python.org
Mon Mar 24 03:19:29 CET 2008


Author: christian.heimes
Date: Mon Mar 24 03:19:29 2008
New Revision: 61829

Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Lib/gzip.py
   python/branches/py3k/Modules/binascii.c
   python/branches/py3k/Modules/zlibmodule.c
   python/branches/py3k/setup.py
Log:
Merged revisions 61820-61823 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r61820 | gregory.p.smith | 2008-03-23 23:14:38 +0100 (Sun, 23 Mar 2008) | 2 lines
  
  replace calls to get the initial values with the raw constants.
........
  r61821 | gregory.p.smith | 2008-03-24 00:43:02 +0100 (Mon, 24 Mar 2008) | 2 lines
  
  A bugfix for r61813, it would fail if the data size was >=2**32.
........
  r61822 | gregory.p.smith | 2008-03-24 00:45:12 +0100 (Mon, 24 Mar 2008) | 2 lines
  
  prevent a warning from the struct module when data size >= 2**32.
........
  r61823 | gregory.p.smith | 2008-03-24 01:08:01 +0100 (Mon, 24 Mar 2008) | 4 lines
  
  Have the binascii module use zlib's optimized crc32() function when available
  to reduce our code size (1k data table and tiny bit of code).  It falls back
  to its own without zlib.
........


Modified: python/branches/py3k/Lib/gzip.py
==============================================================================
--- python/branches/py3k/Lib/gzip.py	(original)
+++ python/branches/py3k/Lib/gzip.py	Mon Mar 24 03:19:29 2008
@@ -17,7 +17,6 @@
 
 def U32(i):
     """Return i as an unsigned integer, assuming it fits in 32 bits.
-
     If it's >= 2GB when viewed as a 32-bit unsigned int, return a long.
     """
     if i < 0:
@@ -320,7 +319,7 @@
         if crc32 != self.crc:
             raise IOError("CRC check failed %s != %s" % (hex(crc32),
                                                          hex(self.crc)))
-        elif isize != self.size:
+        elif isize != (self.size & 0xffffffff):
             raise IOError("Incorrect length of data produced")
 
     def close(self):
@@ -328,7 +327,7 @@
             self.fileobj.write(self.compress.flush())
             write32u(self.fileobj, self.crc)
             # self.size may exceed 2GB, or even 4GB
-            write32u(self.fileobj, self.size)
+            write32u(self.fileobj, self.size & 0xffffffff)
             self.fileobj = None
         elif self.mode == READ:
             self.fileobj = None

Modified: python/branches/py3k/Modules/binascii.c
==============================================================================
--- python/branches/py3k/Modules/binascii.c	(original)
+++ python/branches/py3k/Modules/binascii.c	Mon Mar 24 03:19:29 2008
@@ -56,6 +56,9 @@
 #define PY_SSIZE_T_CLEAN
 
 #include "Python.h"
+#ifdef USE_ZLIB_CRC32
+#include "zlib.h"
+#endif
 
 static PyObject *Error;
 static PyObject *Incomplete;
@@ -776,6 +779,20 @@
 PyDoc_STRVAR(doc_crc32,
 "(data, oldcrc = 0) -> newcrc. Compute CRC-32 incrementally");
 
+#ifdef USE_ZLIB_CRC32
+/* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */
+static PyObject *
+binascii_crc32(PyObject *self, PyObject *args)
+{
+    uLong crc32val = 0;  /* crc32(0L, Z_NULL, 0) */
+    Byte *buf;
+    int len;
+    if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
+        return NULL;
+    crc32val = crc32(crc32val, buf, len);
+    return PyLong_FromUnsignedLong(crc32val & 0xffffffffU);
+}
+#else  /* USE_ZLIB_CRC32 */
 /*  Crc - 32 BIT ANSI X3.66 CRC checksum files
     Also known as: ISO 3307
 **********************************************************************|
@@ -914,6 +931,7 @@
 	result = (crc ^ 0xFFFFFFFF);
 	return PyLong_FromUnsignedLong(result & 0xffffffff);
 }
+#endif  /* USE_ZLIB_CRC32 */
 
 
 static PyObject *

Modified: python/branches/py3k/Modules/zlibmodule.c
==============================================================================
--- python/branches/py3k/Modules/zlibmodule.c	(original)
+++ python/branches/py3k/Modules/zlibmodule.c	Mon Mar 24 03:19:29 2008
@@ -915,7 +915,7 @@
 static PyObject *
 PyZlib_adler32(PyObject *self, PyObject *args)
 {
-    unsigned int adler32val = adler32(0L, Z_NULL, 0);
+    uLong adler32val = 1;  /* adler32(0L, Z_NULL, 0) */
     Byte *buf;
     int len;
 
@@ -934,7 +934,7 @@
 static PyObject *
 PyZlib_crc32(PyObject *self, PyObject *args)
 {
-    unsigned int crc32val = crc32(0L, Z_NULL, 0);
+    uLong crc32val = 0;  /* crc32(0L, Z_NULL, 0) */
     Byte *buf;
     int len;
     if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))

Modified: python/branches/py3k/setup.py
==============================================================================
--- python/branches/py3k/setup.py	(original)
+++ python/branches/py3k/setup.py	Mon Mar 24 03:19:29 2008
@@ -472,9 +472,6 @@
         # select(2); not on ancient System V
         exts.append( Extension('select', ['selectmodule.c']) )
 
-        # Helper module for various ascii-encoders
-        exts.append( Extension('binascii', ['binascii.c']) )
-
         # Fred Drake's interface to the Python parser
         exts.append( Extension('parser', ['parsermodule.c']) )
 
@@ -1005,6 +1002,7 @@
         # You can upgrade zlib to version 1.1.4 yourself by going to
         # http://www.gzip.org/zlib/
         zlib_inc = find_file('zlib.h', [], inc_dirs)
+        have_zlib = False
         if zlib_inc is not None:
             zlib_h = zlib_inc[0] + '/zlib.h'
             version = '"0.0.0"'
@@ -1026,6 +1024,7 @@
                     exts.append( Extension('zlib', ['zlibmodule.c'],
                                            libraries = ['z'],
                                            extra_link_args = zlib_extra_link_args))
+                    have_zlib = True
                 else:
                     missing.append('zlib')
             else:
@@ -1033,6 +1032,21 @@
         else:
             missing.append('zlib')
 
+        # Helper module for various ascii-encoders.  Uses zlib for an optimized
+        # crc32 if we have it.  Otherwise binascii uses its own.
+        if have_zlib:
+            extra_compile_args = ['-DUSE_ZLIB_CRC32']
+            libraries = ['z']
+            extra_link_args = zlib_extra_link_args
+        else:
+            extra_compile_args = []
+            libraries = []
+            extra_link_args = []
+        exts.append( Extension('binascii', ['binascii.c'],
+                               extra_compile_args = extra_compile_args,
+                               libraries = libraries,
+                               extra_link_args = extra_link_args) )
+
         # Gustavo Niemeyer's bz2 module.
         if (self.compiler.find_library_file(lib_dirs, 'bz2')):
             if sys.platform == "darwin":


More information about the Python-3000-checkins mailing list