[Python-checkins] python/dist/src/Modules zlibmodule.c,2.65,2.66

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Mon, 03 Feb 2003 12:46:20 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv30447/Modules

Modified Files:
	zlibmodule.c 
Log Message:
- Thanks to Scott David Daniels, a subtle bug in how the zlib
  extension implemented flush() was fixed.  Scott also rewrite the
  zlib test suite using the unittest module.  (SF bug #640230 and
  patch #678531.)

Backport candidate I think.


Index: zlibmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/zlibmodule.c,v
retrieving revision 2.65
retrieving revision 2.66
diff -C2 -d -r2.65 -r2.66
*** zlibmodule.c	23 Jul 2002 06:31:14 -0000	2.65
--- zlibmodule.c	3 Feb 2003 20:45:47 -0000	2.66
***************
*** 657,680 ****
  static PyObject *
  PyZlib_unflush(compobject *self, PyObject *args)
- /*decompressor flush is a no-op because all pending data would have been
-   flushed by the decompress method. However, this routine previously called
-   inflateEnd, causing any further decompress or flush calls to raise
-   exceptions. This behaviour has been preserved.*/
  {
!     int err;
      PyObject * retval = NULL;
  
!     if (!PyArg_ParseTuple(args, ""))
  	return NULL;
  
      ENTER_ZLIB
  
!     err = inflateEnd(&(self->zst));
!     if (err != Z_OK)
! 	zlib_error(self->zst, err, "from inflateEnd()");
!     else {
! 	self->is_initialised = 0;
! 	retval = PyString_FromStringAndSize(NULL, 0);
      }
  
      LEAVE_ZLIB
--- 657,711 ----
  static PyObject *
  PyZlib_unflush(compobject *self, PyObject *args)
  {
!     int err, length = DEFAULTALLOC;
      PyObject * retval = NULL;
+     unsigned long start_total_out;
  
!     if (!PyArg_ParseTuple(args, "|i:flush", &length))
! 	return NULL;
!     if (!(retval = PyString_FromStringAndSize(NULL, length)))
  	return NULL;
  
+ 
      ENTER_ZLIB
  
!     start_total_out = self->zst.total_out;
!     self->zst.avail_out = length;
!     self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
! 
!     Py_BEGIN_ALLOW_THREADS
!     err = inflate(&(self->zst), Z_FINISH);
!     Py_END_ALLOW_THREADS
! 
!     /* while Z_OK and the output buffer is full, there might be more output,
!        so extend the output buffer and try again */
!     while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
! 	if (_PyString_Resize(&retval, length << 1) < 0)
! 	    goto error;
! 	self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
! 	self->zst.avail_out = length;
! 	length = length << 1;
! 
! 	Py_BEGIN_ALLOW_THREADS
! 	err = inflate(&(self->zst), Z_FINISH);
! 	Py_END_ALLOW_THREADS
!     }
! 
!     /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
!        various data structures. Note we should only get Z_STREAM_END when
!        flushmode is Z_FINISH */
!     if (err == Z_STREAM_END) {
! 	err = inflateEnd(&(self->zst));
!         self->is_initialised = 0;
! 	if (err != Z_OK) {
! 	    zlib_error(self->zst, err, "from inflateEnd()");
! 	    Py_DECREF(retval);
! 	    retval = NULL;
! 	    goto error;
! 	}
      }
+     _PyString_Resize(&retval, self->zst.total_out - start_total_out);
+ 
+ error:
  
      LEAVE_ZLIB
***************
*** 868,871 ****
--- 899,904 ----
      if (ver != NULL)
  	PyModule_AddObject(m, "ZLIB_VERSION", ver);
+ 
+     PyModule_AddStringConstant(m, "__version__", "1.0");
  
  #ifdef WITH_THREAD