1997-04-29 12:38:09 -03:00
/* zlibmodule.c -- gzip-compatible data compression */
2001-01-31 06:28:03 -04:00
/* See http://www.info-zip.org/pub/infozip/zlib/ */
2001-01-31 15:39:44 -04:00
/* Windows users: read Python's PCbuild\readme.txt */
2001-01-31 06:28:03 -04:00
1997-04-29 12:38:09 -03:00
1997-06-03 19:21:47 -03:00
# include "Python.h"
# include "zlib.h"
1997-04-29 12:38:09 -03:00
2001-09-07 13:27:31 -03:00
# ifdef WITH_THREAD
# include "pythread.h"
/* #defs ripped off from _tkinter.c, even though the situation here is much
simpler , because we don ' t have to worry about waiting for Tcl
events ! And , since zlib itself is threadsafe , we don ' t need to worry
about re - entering zlib functions .
What we _do_ have to worry about is releasing the global lock _in
general_ in the zlibmodule functions , because of all the calls to
Python functions , which assume that the global lock is held . So
only two types of calls are wrapped in Py_BEGIN / END_ALLOW_THREADS :
those that grab the zlib lock , and those that involve other
time - consuming functions where we need to worry about holding up
other Python threads .
We don ' t need to worry about the string inputs being modified out
from underneath us , because string objects are immutable . However ,
we do need to make sure we take on ownership , so that the strings
are not deleted out from under us during a thread swap .
N . B .
Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
that modify the components of preexisting de / compress objects , it
could prove to be a performance gain on multiprocessor machines if
there was an de / compress object - specific lock . However , for the
moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
de / compress objects .
*/
static PyThread_type_lock zlib_lock = NULL ; /* initialized on module load */
# define ENTER_ZLIB \
{ Py_BEGIN_ALLOW_THREADS PyThread_acquire_lock ( zlib_lock , 1 ) ; \
Py_END_ALLOW_THREADS
# define LEAVE_ZLIB \
PyThread_release_lock ( zlib_lock ) ; }
# else
# define ENTER_ZLIB
# define LEAVE_ZLIB
# endif
1997-04-29 12:38:09 -03:00
/* The following parameters are copied from zutil.h, version 0.95 */
# define DEFLATED 8
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
# else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
# endif
# define DEF_WBITS MAX_WBITS
1999-04-07 17:23:17 -03:00
/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
# define DEFAULTALLOC (16*1024)
1997-04-29 12:38:09 -03:00
# define PyInit_zlib initzlib
staticforward PyTypeObject Comptype ;
staticforward PyTypeObject Decomptype ;
static PyObject * ZlibError ;
typedef struct
{
PyObject_HEAD
z_stream zst ;
1999-03-25 17:21:08 -04:00
PyObject * unused_data ;
1999-01-29 17:49:34 -04:00
int is_initialised ;
1997-04-29 12:38:09 -03:00
} compobject ;
1997-06-03 19:21:03 -03:00
static char compressobj__doc__ [ ] =
" compressobj() -- Return a compressor object. \n "
" compressobj(level) -- Return a compressor object, using the given compression level. \n "
;
static char decompressobj__doc__ [ ] =
" decompressobj() -- Return a decompressor object. \n "
" decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits. \n "
;
1997-04-29 12:38:09 -03:00
static compobject *
2000-07-10 06:57:19 -03:00
newcompobject ( PyTypeObject * type )
1997-04-29 12:38:09 -03:00
{
2001-02-20 22:15:56 -04:00
compobject * self ;
2000-05-03 20:44:39 -03:00
self = PyObject_New ( compobject , type ) ;
1997-04-29 12:38:09 -03:00
if ( self = = NULL )
return NULL ;
1999-01-29 17:49:34 -04:00
self - > is_initialised = 0 ;
1999-03-25 17:21:08 -04:00
self - > unused_data = PyString_FromString ( " " ) ;
1997-04-29 12:38:09 -03:00
return self ;
}
1997-06-03 19:21:03 -03:00
static char compress__doc__ [ ] =
" compress(string) -- Compress string using the default compression level, "
" returning a string containing compressed data. \n "
" compress(string, level) -- Compress string, using the chosen compression "
" level (from 1 to 9). Return a string containing the compressed data. \n "
;
1997-04-29 12:38:09 -03:00
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_compress ( PyObject * self , PyObject * args )
1997-04-29 12:38:09 -03:00
{
2001-09-07 13:27:31 -03:00
PyObject * ReturnVal = NULL ;
1997-04-29 12:38:09 -03:00
Byte * input , * output ;
int length , level = Z_DEFAULT_COMPRESSION , err ;
z_stream zst ;
2001-09-07 13:27:31 -03:00
int return_error ;
PyObject * inputString ;
1997-04-29 12:38:09 -03:00
2001-09-07 13:27:31 -03:00
/* require Python string object, optional 'level' arg */
if ( ! PyArg_ParseTuple ( args , " S|i:compress " , & inputString , & level ) )
1997-04-29 12:38:09 -03:00
return NULL ;
2001-09-07 13:27:31 -03:00
/* now get a pointer to the internal string */
2001-09-08 13:23:34 -03:00
if ( PyString_AsStringAndSize ( inputString , ( char * * ) & input , & length ) = = - 1 )
2001-09-07 13:27:31 -03:00
return NULL ;
1997-09-04 20:39:23 -03:00
zst . avail_out = length + length / 1000 + 12 + 1 ;
2001-09-07 13:27:31 -03:00
1997-04-29 12:38:09 -03:00
output = ( Byte * ) malloc ( zst . avail_out ) ;
if ( output = = NULL )
{
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory to compress data " ) ;
2001-09-07 13:27:31 -03:00
free ( output ) ;
1997-04-29 12:38:09 -03:00
return NULL ;
}
1998-12-18 18:13:11 -04:00
2001-09-07 13:27:31 -03:00
/* Past the point of no return. From here on out, we need to make sure
we clean up mallocs & INCREFs . */
Py_INCREF ( inputString ) ; /* increment so that we hold ref */
1998-12-21 13:15:00 -04:00
zst . zalloc = ( alloc_func ) NULL ;
1997-06-03 19:21:47 -03:00
zst . zfree = ( free_func ) Z_NULL ;
1997-04-29 12:38:09 -03:00
zst . next_out = ( Byte * ) output ;
zst . next_in = ( Byte * ) input ;
zst . avail_in = length ;
err = deflateInit ( & zst , level ) ;
2001-09-07 13:27:31 -03:00
return_error = 0 ;
1997-04-29 12:38:09 -03:00
switch ( err )
{
case ( Z_OK ) :
break ;
case ( Z_MEM_ERROR ) :
PyErr_SetString ( PyExc_MemoryError ,
" Out of memory while compressing data " ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
break ;
1997-04-29 12:38:09 -03:00
case ( Z_STREAM_ERROR ) :
PyErr_SetString ( ZlibError ,
" Bad compression level " ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
break ;
1997-04-29 12:38:09 -03:00
default :
{
1997-09-04 20:39:23 -03:00
if ( zst . msg = = Z_NULL )
PyErr_Format ( ZlibError , " Error %i while compressing data " ,
err ) ;
else
PyErr_Format ( ZlibError , " Error %i while compressing data: %.200s " ,
err , zst . msg ) ;
1997-04-29 12:38:09 -03:00
deflateEnd ( & zst ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
1997-04-29 12:38:09 -03:00
}
}
2001-09-07 13:27:31 -03:00
if ( ! return_error ) {
Py_BEGIN_ALLOW_THREADS
err = deflate ( & zst , Z_FINISH ) ;
Py_END_ALLOW_THREADS
switch ( err )
{
case ( Z_STREAM_END ) :
break ;
/* Are there other errors to be trapped here? */
default :
{
if ( zst . msg = = Z_NULL )
1997-09-04 20:39:23 -03:00
PyErr_Format ( ZlibError , " Error %i while compressing data " ,
err ) ;
2001-09-07 13:27:31 -03:00
else
1997-09-04 20:39:23 -03:00
PyErr_Format ( ZlibError , " Error %i while compressing data: %.200s " ,
err , zst . msg ) ;
2001-09-07 13:27:31 -03:00
deflateEnd ( & zst ) ;
return_error = 1 ;
}
}
if ( ! return_error ) {
err = deflateEnd ( & zst ) ;
if ( err = = Z_OK )
ReturnVal = PyString_FromStringAndSize ( ( char * ) output , zst . total_out ) ;
else {
{
if ( zst . msg = = Z_NULL )
PyErr_Format ( ZlibError , " Error %i while finishing compression " ,
err ) ;
else
PyErr_Format ( ZlibError ,
" Error %i while finishing compression: %.200s " ,
err , zst . msg ) ;
}
}
1997-09-04 20:39:23 -03:00
}
}
2001-09-07 13:27:31 -03:00
1997-04-29 12:38:09 -03:00
free ( output ) ;
2001-09-07 13:27:31 -03:00
Py_DECREF ( inputString ) ;
1997-04-29 12:38:09 -03:00
return ReturnVal ;
}
1997-06-03 19:21:03 -03:00
static char decompress__doc__ [ ] =
1997-08-13 20:19:55 -03:00
" decompress(string) -- Decompress the data in string, returning a string containing the decompressed data. \n "
" decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits. \n "
" decompress(string, wbits, bufsize) -- Decompress the data in string with a window buffer size of wbits and an initial output buffer size of bufsize. \n "
1997-06-03 19:21:03 -03:00
;
1997-04-29 12:38:09 -03:00
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_decompress ( PyObject * self , PyObject * args )
1997-04-29 12:38:09 -03:00
{
1997-08-13 20:19:55 -03:00
PyObject * result_str ;
Byte * input ;
1997-04-29 12:38:09 -03:00
int length , err ;
1997-08-13 20:19:55 -03:00
int wsize = DEF_WBITS , r_strlen = DEFAULTALLOC ;
1997-04-29 12:38:09 -03:00
z_stream zst ;
2001-09-07 13:27:31 -03:00
int return_error ;
PyObject * inputString ;
if ( ! PyArg_ParseTuple ( args , " S|ii:decompress " , & inputString , & wsize , & r_strlen ) )
return NULL ;
2001-09-08 13:23:34 -03:00
if ( PyString_AsStringAndSize ( inputString , ( char * * ) & input , & length ) = = - 1 )
1997-04-29 12:38:09 -03:00
return NULL ;
1997-09-04 20:39:23 -03:00
if ( r_strlen < = 0 )
r_strlen = 1 ;
1998-12-18 18:13:11 -04:00
1997-04-29 12:38:09 -03:00
zst . avail_in = length ;
1997-08-13 20:19:55 -03:00
zst . avail_out = r_strlen ;
2001-09-07 13:27:31 -03:00
1997-08-13 20:19:55 -03:00
if ( ! ( result_str = PyString_FromStringAndSize ( NULL , r_strlen ) ) )
{
1997-04-29 12:38:09 -03:00
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory to decompress data " ) ;
return NULL ;
1997-08-13 20:19:55 -03:00
}
2001-09-07 13:27:31 -03:00
/* Past the point of no return. From here on out, we need to make sure
we clean up mallocs & INCREFs . */
Py_INCREF ( inputString ) ; /* increment so that we hold ref */
1997-06-03 19:21:47 -03:00
zst . zalloc = ( alloc_func ) NULL ;
zst . zfree = ( free_func ) Z_NULL ;
1997-08-13 20:19:55 -03:00
zst . next_out = ( Byte * ) PyString_AsString ( result_str ) ;
1997-04-29 12:38:09 -03:00
zst . next_in = ( Byte * ) input ;
1997-08-13 20:19:55 -03:00
err = inflateInit2 ( & zst , wsize ) ;
2001-09-07 13:27:31 -03:00
return_error = 0 ;
1997-04-29 12:38:09 -03:00
switch ( err )
{
case ( Z_OK ) :
break ;
case ( Z_MEM_ERROR ) :
PyErr_SetString ( PyExc_MemoryError ,
" Out of memory while decompressing data " ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
1997-04-29 12:38:09 -03:00
default :
{
1997-09-04 20:39:23 -03:00
if ( zst . msg = = Z_NULL )
PyErr_Format ( ZlibError , " Error %i preparing to decompress data " ,
err ) ;
else
PyErr_Format ( ZlibError ,
" Error %i while preparing to decompress data: %.200s " ,
err , zst . msg ) ;
1997-04-29 12:38:09 -03:00
inflateEnd ( & zst ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
1997-04-29 12:38:09 -03:00
}
}
2001-09-07 13:27:31 -03:00
1997-04-29 12:38:09 -03:00
do
{
2001-09-07 13:27:31 -03:00
if ( return_error )
break ;
Py_BEGIN_ALLOW_THREADS
1997-04-29 12:38:09 -03:00
err = inflate ( & zst , Z_FINISH ) ;
2001-09-07 13:27:31 -03:00
Py_END_ALLOW_THREADS
1997-04-29 12:38:09 -03:00
switch ( err )
{
case ( Z_STREAM_END ) :
1997-08-13 20:19:55 -03:00
break ;
1998-04-23 17:22:11 -03:00
case ( Z_BUF_ERROR ) :
2000-10-09 11:18:10 -03:00
/*
* If there is at least 1 byte of room according to zst . avail_out
* and we get this error , assume that it means zlib cannot
* process the inflate call ( ) due to an error in the data .
*/
if ( zst . avail_out > 0 )
{
PyErr_Format ( ZlibError , " Error %i while decompressing data " ,
err ) ;
inflateEnd ( & zst ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
break ;
2000-10-09 11:18:10 -03:00
}
/* fall through */
case ( Z_OK ) :
1997-08-13 20:19:55 -03:00
/* need more memory */
if ( _PyString_Resize ( & result_str , r_strlen < < 1 ) = = - 1 )
1997-04-29 12:38:09 -03:00
{
PyErr_SetString ( PyExc_MemoryError ,
" Out of memory while decompressing data " ) ;
inflateEnd ( & zst ) ;
2001-09-07 13:27:31 -03:00
result_str = NULL ;
return_error = 1 ;
1997-04-29 12:38:09 -03:00
}
1997-08-18 12:31:24 -03:00
zst . next_out = ( unsigned char * ) PyString_AsString ( result_str ) + r_strlen ;
1997-08-13 20:19:55 -03:00
zst . avail_out = r_strlen ;
r_strlen = r_strlen < < 1 ;
break ;
1997-04-29 12:38:09 -03:00
default :
{
1997-09-04 20:39:23 -03:00
if ( zst . msg = = Z_NULL )
PyErr_Format ( ZlibError , " Error %i while decompressing data " ,
err ) ;
else
PyErr_Format ( ZlibError ,
" Error %i while decompressing data: %.200s " ,
err , zst . msg ) ;
1997-04-29 12:38:09 -03:00
inflateEnd ( & zst ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
1997-04-29 12:38:09 -03:00
}
}
} while ( err ! = Z_STREAM_END ) ;
2001-09-07 13:27:31 -03:00
if ( ! return_error ) {
err = inflateEnd ( & zst ) ;
if ( err ! = Z_OK )
{
if ( zst . msg = = Z_NULL )
1997-09-04 20:39:23 -03:00
PyErr_Format ( ZlibError ,
" Error %i while finishing data decompression " ,
err ) ;
2001-09-07 13:27:31 -03:00
else
1997-09-04 20:39:23 -03:00
PyErr_Format ( ZlibError ,
" Error %i while finishing data decompression: %.200s " ,
err , zst . msg ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
1997-04-29 12:38:09 -03:00
return NULL ;
}
2001-09-07 13:27:31 -03:00
}
if ( ! return_error )
_PyString_Resize ( & result_str , zst . total_out ) ;
else {
Py_XDECREF ( result_str ) ; /* sets result_str == NULL, if not already */
}
Py_DECREF ( inputString ) ;
1997-08-13 20:19:55 -03:00
return result_str ;
1997-04-29 12:38:09 -03:00
}
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_compressobj ( PyObject * selfptr , PyObject * args )
1997-04-29 12:38:09 -03:00
{
compobject * self ;
int level = Z_DEFAULT_COMPRESSION , method = DEFLATED ;
int wbits = MAX_WBITS , memLevel = DEF_MEM_LEVEL , strategy = 0 , err ;
1997-08-13 20:19:55 -03:00
2000-02-29 09:59:29 -04:00
if ( ! PyArg_ParseTuple ( args , " |iiiii:compressobj " , & level , & method , & wbits ,
1997-08-13 20:19:55 -03:00
& memLevel , & strategy ) )
return NULL ;
1997-09-04 20:39:23 -03:00
self = newcompobject ( & Comptype ) ;
1997-04-29 12:38:09 -03:00
if ( self = = NULL ) return ( NULL ) ;
1997-09-04 20:39:23 -03:00
self - > zst . zalloc = ( alloc_func ) NULL ;
self - > zst . zfree = ( free_func ) Z_NULL ;
err = deflateInit2 ( & self - > zst , level , method , wbits , memLevel , strategy ) ;
1997-04-29 12:38:09 -03:00
switch ( err )
{
case ( Z_OK ) :
1999-01-29 17:49:34 -04:00
self - > is_initialised = 1 ;
1997-04-29 12:38:09 -03:00
return ( PyObject * ) self ;
case ( Z_MEM_ERROR ) :
1999-01-29 17:49:34 -04:00
Py_DECREF ( self ) ;
1997-04-29 12:38:09 -03:00
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory for compression object " ) ;
return NULL ;
case ( Z_STREAM_ERROR ) :
1999-01-29 17:49:34 -04:00
Py_DECREF ( self ) ;
1997-04-29 12:38:09 -03:00
PyErr_SetString ( PyExc_ValueError ,
1997-09-04 20:39:23 -03:00
" Invalid initialization option " ) ;
1997-04-29 12:38:09 -03:00
return NULL ;
default :
{
1997-09-04 20:39:23 -03:00
if ( self - > zst . msg = = Z_NULL )
PyErr_Format ( ZlibError ,
" Error %i while creating compression object " ,
err ) ;
else
PyErr_Format ( ZlibError ,
" Error %i while creating compression object: %.200s " ,
err , self - > zst . msg ) ;
1999-01-29 17:49:34 -04:00
Py_DECREF ( self ) ;
1997-04-29 12:38:09 -03:00
return NULL ;
}
}
}
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_decompressobj ( PyObject * selfptr , PyObject * args )
1997-04-29 12:38:09 -03:00
{
int wbits = DEF_WBITS , err ;
compobject * self ;
2000-02-29 09:59:29 -04:00
if ( ! PyArg_ParseTuple ( args , " |i:decompressobj " , & wbits ) )
1997-04-29 12:38:09 -03:00
{
return NULL ;
}
self = newcompobject ( & Decomptype ) ;
if ( self = = NULL ) return ( NULL ) ;
1997-06-03 19:21:47 -03:00
self - > zst . zalloc = ( alloc_func ) NULL ;
self - > zst . zfree = ( free_func ) Z_NULL ;
1997-04-29 12:38:09 -03:00
err = inflateInit2 ( & self - > zst , wbits ) ;
switch ( err )
1997-09-04 20:39:23 -03:00
{
1997-04-29 12:38:09 -03:00
case ( Z_OK ) :
1999-01-29 17:49:34 -04:00
self - > is_initialised = 1 ;
1997-04-29 12:38:09 -03:00
return ( PyObject * ) self ;
1997-09-04 20:39:23 -03:00
case ( Z_STREAM_ERROR ) :
1999-01-29 17:49:34 -04:00
Py_DECREF ( self ) ;
1997-09-04 20:39:23 -03:00
PyErr_SetString ( PyExc_ValueError ,
" Invalid initialization option " ) ;
return NULL ;
1997-04-29 12:38:09 -03:00
case ( Z_MEM_ERROR ) :
1999-01-29 17:49:34 -04:00
Py_DECREF ( self ) ;
1997-04-29 12:38:09 -03:00
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory for decompression object " ) ;
return NULL ;
default :
1997-09-04 20:39:23 -03:00
{
if ( self - > zst . msg = = Z_NULL )
PyErr_Format ( ZlibError ,
" Error %i while creating decompression object " ,
err ) ;
else
PyErr_Format ( ZlibError ,
" Error %i while creating decompression object: %.200s " ,
err , self - > zst . msg ) ;
1999-01-29 17:49:34 -04:00
Py_DECREF ( self ) ;
1997-04-29 12:38:09 -03:00
return NULL ;
}
1997-09-04 20:39:23 -03:00
}
1997-04-29 12:38:09 -03:00
}
static void
2000-07-10 06:57:19 -03:00
Comp_dealloc ( compobject * self )
1997-04-29 12:38:09 -03:00
{
2001-09-07 13:27:31 -03:00
ENTER_ZLIB
1999-01-29 17:49:34 -04:00
if ( self - > is_initialised )
deflateEnd ( & self - > zst ) ;
1999-03-25 17:21:08 -04:00
Py_XDECREF ( self - > unused_data ) ;
2000-05-03 20:44:39 -03:00
PyObject_Del ( self ) ;
2001-09-07 13:27:31 -03:00
LEAVE_ZLIB
1997-04-29 12:38:09 -03:00
}
static void
2000-07-10 06:57:19 -03:00
Decomp_dealloc ( compobject * self )
1997-04-29 12:38:09 -03:00
{
2001-09-07 13:27:31 -03:00
ENTER_ZLIB
2001-02-20 22:15:56 -04:00
if ( self - > is_initialised )
inflateEnd ( & self - > zst ) ;
1999-03-25 17:21:08 -04:00
Py_XDECREF ( self - > unused_data ) ;
2000-05-03 20:44:39 -03:00
PyObject_Del ( self ) ;
2001-09-07 13:27:31 -03:00
LEAVE_ZLIB
1997-04-29 12:38:09 -03:00
}
1997-06-03 19:21:03 -03:00
static char comp_compress__doc__ [ ] =
" compress(data) -- Return a string containing a compressed version of the data. \n \n "
" After calling this function, some of the input data may still \n "
" be stored in internal buffers for later processing. \n "
" Call the flush() method to clear these buffers. "
;
1997-04-29 12:38:09 -03:00
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_objcompress ( compobject * self , PyObject * args )
1997-04-29 12:38:09 -03:00
{
2001-02-20 22:15:56 -04:00
int err , inplen , length = DEFAULTALLOC ;
1997-04-29 12:38:09 -03:00
PyObject * RetVal ;
Byte * input ;
1997-09-04 20:39:23 -03:00
unsigned long start_total_out ;
2001-09-07 13:27:31 -03:00
int return_error ;
PyObject * inputString ;
1997-04-29 12:38:09 -03:00
2001-09-07 13:27:31 -03:00
if ( ! PyArg_ParseTuple ( args , " S:compress " , & inputString ) )
return NULL ;
2001-09-08 13:23:34 -03:00
if ( PyString_AsStringAndSize ( inputString , ( char * * ) & input , & inplen ) = = - 1 )
2001-02-20 22:15:56 -04:00
return NULL ;
2001-09-07 13:27:31 -03:00
2001-02-20 22:15:56 -04:00
if ( ! ( RetVal = PyString_FromStringAndSize ( NULL , length ) ) ) {
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory to compress data " ) ;
return NULL ;
}
2001-09-07 13:27:31 -03:00
ENTER_ZLIB
Py_INCREF ( inputString ) ;
2001-02-20 22:15:56 -04:00
start_total_out = self - > zst . total_out ;
1997-09-04 20:39:23 -03:00
self - > zst . avail_in = inplen ;
self - > zst . next_in = input ;
2001-02-20 22:15:56 -04:00
self - > zst . avail_out = length ;
self - > zst . next_out = ( unsigned char * ) PyString_AsString ( RetVal ) ;
2001-09-07 13:27:31 -03:00
Py_BEGIN_ALLOW_THREADS
2001-02-20 22:15:56 -04:00
err = deflate ( & ( self - > zst ) , Z_NO_FLUSH ) ;
2001-09-07 13:27:31 -03:00
Py_END_ALLOW_THREADS
return_error = 0 ;
2001-02-20 22:15:56 -04:00
/* 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 & & self - > zst . avail_out = = 0 ) {
if ( _PyString_Resize ( & RetVal , length < < 1 ) = = - 1 ) {
1997-08-14 18:06:42 -03:00
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory to compress data " ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
break ;
2001-02-20 22:15:56 -04:00
}
self - > zst . next_out = ( unsigned char * ) PyString_AsString ( RetVal ) + length ;
self - > zst . avail_out = length ;
length = length < < 1 ;
2001-09-07 13:27:31 -03:00
Py_BEGIN_ALLOW_THREADS
2001-02-20 22:15:56 -04:00
err = deflate ( & ( self - > zst ) , Z_NO_FLUSH ) ;
2001-09-07 13:27:31 -03:00
Py_END_ALLOW_THREADS
1997-08-14 18:06:42 -03:00
}
2001-02-20 22:15:56 -04:00
/* We will only get Z_BUF_ERROR if the output buffer was full but there
wasn ' t more output when we tried again , so it is not an error condition */
2001-09-07 13:27:31 -03:00
if ( ! return_error ) {
if ( err ! = Z_OK & & err ! = Z_BUF_ERROR ) {
if ( self - > zst . msg = = Z_NULL )
PyErr_Format ( ZlibError , " Error %i while compressing " ,
err ) ;
else
PyErr_Format ( ZlibError , " Error %i while compressing: %.200s " ,
err , self - > zst . msg ) ;
return_error = 1 ;
Py_DECREF ( RetVal ) ;
}
1997-08-14 18:06:42 -03:00
}
2001-09-07 13:27:31 -03:00
if ( return_error )
RetVal = NULL ; /* should have been handled by DECREF */
else
_PyString_Resize ( & RetVal , self - > zst . total_out - start_total_out ) ;
Py_DECREF ( inputString ) ;
LEAVE_ZLIB
1997-04-29 12:38:09 -03:00
return RetVal ;
}
1997-06-03 19:21:03 -03:00
static char decomp_decompress__doc__ [ ] =
" decompress(data) -- Return a string containing the decompressed version of the data. \n \n "
" After calling this function, some of the input data may still \n "
" be stored in internal buffers for later processing. \n "
" Call the flush() method to clear these buffers. "
;
1997-04-29 12:38:09 -03:00
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_objdecompress ( compobject * self , PyObject * args )
1997-04-29 12:38:09 -03:00
{
2001-02-20 22:15:56 -04:00
int err , inplen , length = DEFAULTALLOC ;
1997-04-29 12:38:09 -03:00
PyObject * RetVal ;
Byte * input ;
1997-09-04 20:39:23 -03:00
unsigned long start_total_out ;
2001-09-07 13:27:31 -03:00
int return_error ;
PyObject * inputString ;
1997-09-04 20:39:23 -03:00
2001-09-07 13:27:31 -03:00
if ( ! PyArg_ParseTuple ( args , " S:decompress " , & inputString ) )
1997-04-29 12:38:09 -03:00
return NULL ;
2001-09-08 13:23:34 -03:00
if ( PyString_AsStringAndSize ( inputString , ( char * * ) & input , & inplen ) = = - 1 )
2001-09-07 13:27:31 -03:00
return NULL ;
2001-02-20 22:15:56 -04:00
if ( ! ( RetVal = PyString_FromStringAndSize ( NULL , length ) ) ) {
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory to compress data " ) ;
return NULL ;
}
2001-09-07 13:27:31 -03:00
ENTER_ZLIB
return_error = 0 ;
Py_INCREF ( inputString ) ;
1997-09-04 20:39:23 -03:00
start_total_out = self - > zst . total_out ;
self - > zst . avail_in = inplen ;
self - > zst . next_in = input ;
2001-02-20 22:15:56 -04:00
self - > zst . avail_out = length ;
1997-08-18 12:31:24 -03:00
self - > zst . next_out = ( unsigned char * ) PyString_AsString ( RetVal ) ;
2001-09-07 13:27:31 -03:00
Py_BEGIN_ALLOW_THREADS
2001-02-20 22:15:56 -04:00
err = inflate ( & ( self - > zst ) , Z_SYNC_FLUSH ) ;
2001-09-07 13:27:31 -03:00
Py_END_ALLOW_THREADS
2001-02-20 22:15:56 -04:00
/* 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 & & self - > zst . avail_out = = 0 ) {
if ( _PyString_Resize ( & RetVal , length < < 1 ) = = - 1 ) {
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory to compress data " ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
break ;
2001-02-20 22:15:56 -04:00
}
self - > zst . next_out = ( unsigned char * ) PyString_AsString ( RetVal ) + length ;
self - > zst . avail_out = length ;
length = length < < 1 ;
2001-09-07 13:27:31 -03:00
Py_BEGIN_ALLOW_THREADS
2001-02-20 22:15:56 -04:00
err = inflate ( & ( self - > zst ) , Z_SYNC_FLUSH ) ;
2001-09-07 13:27:31 -03:00
Py_END_ALLOW_THREADS
1997-08-13 20:19:55 -03:00
}
2001-09-07 13:27:31 -03:00
2001-02-20 22:15:56 -04:00
/* The end of the compressed data has been reached, so set the unused_data
attribute to a string containing the remainder of the data in the string .
Note that this is also a logical place to call inflateEnd , but the old
behaviour of only calling it on flush ( ) is preserved . */
2001-09-07 13:27:31 -03:00
if ( ! return_error ) {
if ( err = = Z_STREAM_END ) {
Py_XDECREF ( self - > unused_data ) ; /* Free the original, empty string */
self - > unused_data = PyString_FromStringAndSize ( ( char * ) self - > zst . next_in ,
2001-02-20 22:15:56 -04:00
self - > zst . avail_in ) ;
2001-09-07 13:27:31 -03:00
if ( self - > unused_data = = NULL ) {
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory to unused_data " ) ;
Py_DECREF ( RetVal ) ;
return_error = 1 ;
}
/* We will only get Z_BUF_ERROR if the output buffer was full but there
wasn ' t more output when we tried again , so it is not an error
condition */
} else if ( err ! = Z_OK & & err ! = Z_BUF_ERROR ) {
if ( self - > zst . msg = = Z_NULL )
PyErr_Format ( ZlibError , " Error %i while decompressing " ,
err ) ;
else
PyErr_Format ( ZlibError , " Error %i while decompressing: %.200s " ,
err , self - > zst . msg ) ;
1997-09-04 20:39:23 -03:00
Py_DECREF ( RetVal ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
1997-04-29 12:38:09 -03:00
}
1999-03-25 17:21:08 -04:00
}
2001-09-07 13:27:31 -03:00
if ( ! return_error ) {
_PyString_Resize ( & RetVal , self - > zst . total_out - start_total_out ) ;
}
else
RetVal = NULL ; /* should be handled by DECREF */
Py_DECREF ( inputString ) ;
LEAVE_ZLIB
1997-04-29 12:38:09 -03:00
return RetVal ;
}
1997-06-03 19:21:03 -03:00
static char comp_flush__doc__ [ ] =
1998-12-18 18:13:11 -04:00
" flush( [mode] ) -- Return a string containing any remaining compressed data. \n "
" mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n "
" default value used when mode is not specified is Z_FINISH. \n "
" If mode == Z_FINISH, the compressor object can no longer be used after \n "
" calling the flush() method. Otherwise, more data can still be compressed. \n "
1997-06-03 19:21:03 -03:00
;
1997-04-29 12:38:09 -03:00
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_flush ( compobject * self , PyObject * args )
1997-04-29 12:38:09 -03:00
{
2001-02-20 22:15:56 -04:00
int err , length = DEFAULTALLOC ;
1997-04-29 12:38:09 -03:00
PyObject * RetVal ;
1998-12-18 18:13:11 -04:00
int flushmode = Z_FINISH ;
unsigned long start_total_out ;
2001-09-07 13:27:31 -03:00
int return_error ;
1998-12-18 18:13:11 -04:00
2000-02-29 09:59:29 -04:00
if ( ! PyArg_ParseTuple ( args , " |i:flush " , & flushmode ) )
2001-02-20 22:15:56 -04:00
return NULL ;
1998-12-18 18:13:11 -04:00
/* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
doing any work at all ; just return an empty string . */
2001-02-20 22:15:56 -04:00
if ( flushmode = = Z_NO_FLUSH ) {
return PyString_FromStringAndSize ( NULL , 0 ) ;
}
2001-09-07 13:27:31 -03:00
1997-08-14 18:06:42 -03:00
if ( ! ( RetVal = PyString_FromStringAndSize ( NULL , length ) ) ) {
2001-02-20 22:15:56 -04:00
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory to compress data " ) ;
return NULL ;
1997-08-14 18:06:42 -03:00
}
2001-09-07 13:27:31 -03:00
ENTER_ZLIB
1998-12-18 18:13:11 -04:00
start_total_out = self - > zst . total_out ;
2001-02-20 22:15:56 -04:00
self - > zst . avail_in = 0 ;
1997-08-14 18:06:42 -03:00
self - > zst . avail_out = length ;
2001-02-20 22:15:56 -04:00
self - > zst . next_out = ( unsigned char * ) PyString_AsString ( RetVal ) ;
2001-09-07 13:27:31 -03:00
Py_BEGIN_ALLOW_THREADS
2001-02-20 22:15:56 -04:00
err = deflate ( & ( self - > zst ) , flushmode ) ;
2001-09-07 13:27:31 -03:00
Py_END_ALLOW_THREADS
return_error = 0 ;
2001-02-20 22:15:56 -04:00
/* 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 & & self - > zst . avail_out = = 0 ) {
if ( _PyString_Resize ( & RetVal , length < < 1 ) = = - 1 ) {
PyErr_SetString ( PyExc_MemoryError ,
" Can't allocate memory to compress data " ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
break ;
2001-02-20 22:15:56 -04:00
}
self - > zst . next_out = ( unsigned char * ) PyString_AsString ( RetVal ) + length ;
self - > zst . avail_out = length ;
length = length < < 1 ;
2001-09-07 13:27:31 -03:00
Py_BEGIN_ALLOW_THREADS
2001-02-20 22:15:56 -04:00
err = deflate ( & ( self - > zst ) , flushmode ) ;
2001-09-07 13:27:31 -03:00
Py_END_ALLOW_THREADS
1999-01-06 18:56:24 -04:00
}
2001-09-07 13:27:31 -03:00
2001-02-20 22:15:56 -04:00
/* 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 , but checking both for safety */
2001-09-07 13:27:31 -03:00
if ( ! return_error ) {
if ( err = = Z_STREAM_END & & flushmode = = Z_FINISH ) {
err = deflateEnd ( & ( self - > zst ) ) ;
if ( err ! = Z_OK ) {
if ( self - > zst . msg = = Z_NULL )
PyErr_Format ( ZlibError , " Error %i from deflateEnd() " ,
err ) ;
else
PyErr_Format ( ZlibError , " Error %i from deflateEnd(): %.200s " ,
err , self - > zst . msg ) ;
Py_DECREF ( RetVal ) ;
return_error = 1 ;
}
else
self - > is_initialised = 0 ;
/* We will only get Z_BUF_ERROR if the output buffer was full but there
wasn ' t more output when we tried again , so it is not an error
condition */
} else if ( err ! = Z_OK & & err ! = Z_BUF_ERROR ) {
1997-09-04 20:39:23 -03:00
if ( self - > zst . msg = = Z_NULL )
2001-09-07 13:27:31 -03:00
PyErr_Format ( ZlibError , " Error %i while flushing " ,
1998-12-18 18:13:11 -04:00
err ) ;
1997-09-04 20:39:23 -03:00
else
2001-09-07 13:27:31 -03:00
PyErr_Format ( ZlibError , " Error %i while flushing: %.200s " ,
1998-12-18 18:13:11 -04:00
err , self - > zst . msg ) ;
1997-09-04 20:39:23 -03:00
Py_DECREF ( RetVal ) ;
2001-09-07 13:27:31 -03:00
return_error = 1 ;
1998-12-18 18:13:11 -04:00
}
1997-08-14 18:06:42 -03:00
}
2001-09-07 13:27:31 -03:00
if ( ! return_error )
_PyString_Resize ( & RetVal , self - > zst . total_out - start_total_out ) ;
else
RetVal = NULL ; /* should have been handled by DECREF */
LEAVE_ZLIB
1997-04-29 12:38:09 -03:00
return RetVal ;
}
1997-06-03 19:21:03 -03:00
static char decomp_flush__doc__ [ ] =
" flush() -- Return a string containing any remaining decompressed data. "
" The decompressor object can no longer be used after this call. "
;
1997-04-29 12:38:09 -03:00
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_unflush ( compobject * self , PyObject * args )
2001-02-20 22:15:56 -04:00
/*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 . */
1997-04-29 12:38:09 -03:00
{
2001-02-20 22:15:56 -04:00
int err ;
2001-09-07 13:27:31 -03:00
PyObject * retval ;
1997-04-29 12:38:09 -03:00
2000-08-02 23:04:05 -03:00
if ( ! PyArg_ParseTuple ( args , " " ) )
2001-02-20 22:15:56 -04:00
return NULL ;
2001-09-07 13:27:31 -03:00
ENTER_ZLIB
1997-04-29 12:38:09 -03:00
err = inflateEnd ( & ( self - > zst ) ) ;
2001-02-20 22:15:56 -04:00
if ( err ! = Z_OK ) {
if ( self - > zst . msg = = Z_NULL )
PyErr_Format ( ZlibError , " Error %i from inflateEnd() " ,
err ) ;
else
PyErr_Format ( ZlibError , " Error %i from inflateEnd(): %.200s " ,
err , self - > zst . msg ) ;
2001-09-07 13:27:31 -03:00
retval = NULL ;
} else {
self - > is_initialised = 0 ;
retval = PyString_FromStringAndSize ( NULL , 0 ) ;
1997-09-04 20:39:23 -03:00
}
2001-09-07 13:27:31 -03:00
LEAVE_ZLIB
return retval ;
1997-04-29 12:38:09 -03:00
}
static PyMethodDef comp_methods [ ] =
{
2000-08-02 23:04:05 -03:00
{ " compress " , ( binaryfunc ) PyZlib_objcompress ,
METH_VARARGS , comp_compress__doc__ } ,
{ " flush " , ( binaryfunc ) PyZlib_flush ,
METH_VARARGS , comp_flush__doc__ } ,
1997-04-29 12:38:09 -03:00
{ NULL , NULL }
} ;
static PyMethodDef Decomp_methods [ ] =
{
2000-08-02 23:04:05 -03:00
{ " decompress " , ( binaryfunc ) PyZlib_objdecompress ,
METH_VARARGS , decomp_decompress__doc__ } ,
{ " flush " , ( binaryfunc ) PyZlib_unflush ,
METH_VARARGS , decomp_flush__doc__ } ,
1997-04-29 12:38:09 -03:00
{ NULL , NULL }
} ;
static PyObject *
2000-07-10 06:57:19 -03:00
Comp_getattr ( compobject * self , char * name )
1997-04-29 12:38:09 -03:00
{
2001-09-07 13:27:31 -03:00
/* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
internal data . */
return Py_FindMethod ( comp_methods , ( PyObject * ) self , name ) ;
1997-04-29 12:38:09 -03:00
}
static PyObject *
2000-07-10 06:57:19 -03:00
Decomp_getattr ( compobject * self , char * name )
1997-04-29 12:38:09 -03:00
{
2001-09-07 13:27:31 -03:00
PyObject * retval ;
ENTER_ZLIB
1999-03-25 17:21:08 -04:00
if ( strcmp ( name , " unused_data " ) = = 0 )
{
Py_INCREF ( self - > unused_data ) ;
2001-09-07 13:27:31 -03:00
retval = self - > unused_data ;
1999-03-25 17:21:08 -04:00
}
2001-09-07 13:27:31 -03:00
else
retval = Py_FindMethod ( Decomp_methods , ( PyObject * ) self , name ) ;
LEAVE_ZLIB
return retval ;
1997-04-29 12:38:09 -03:00
}
1997-06-03 19:21:03 -03:00
static char adler32__doc__ [ ] =
" adler32(string) -- Compute an Adler-32 checksum of string, using "
" a default starting value, and returning an integer value. \n "
" adler32(string, value) -- Compute an Adler-32 checksum of string, using "
" the starting value provided, and returning an integer value \n "
;
1997-04-29 12:38:09 -03:00
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_adler32 ( PyObject * self , PyObject * args )
1997-04-29 12:38:09 -03:00
{
1997-09-04 20:39:23 -03:00
uLong adler32val = adler32 ( 0L , Z_NULL , 0 ) ;
Byte * buf ;
int len ;
1997-04-29 12:38:09 -03:00
2000-02-29 09:59:29 -04:00
if ( ! PyArg_ParseTuple ( args , " s#|l:adler32 " , & buf , & len , & adler32val ) )
1997-09-04 20:39:23 -03:00
{
return NULL ;
}
adler32val = adler32 ( adler32val , buf , len ) ;
return PyInt_FromLong ( adler32val ) ;
1997-04-29 12:38:09 -03:00
}
1997-06-03 19:21:03 -03:00
static char crc32__doc__ [ ] =
" crc32(string) -- Compute a CRC-32 checksum of string, using "
" a default starting value, and returning an integer value. \n "
" crc32(string, value) -- Compute a CRC-32 checksum of string, using "
" the starting value provided, and returning an integer value. \n "
;
1997-04-29 12:38:09 -03:00
static PyObject *
2000-07-10 06:57:19 -03:00
PyZlib_crc32 ( PyObject * self , PyObject * args )
1997-04-29 12:38:09 -03:00
{
1997-09-04 20:39:23 -03:00
uLong crc32val = crc32 ( 0L , Z_NULL , 0 ) ;
Byte * buf ;
int len ;
2000-02-29 09:59:29 -04:00
if ( ! PyArg_ParseTuple ( args , " s#|l:crc32 " , & buf , & len , & crc32val ) )
1997-09-04 20:39:23 -03:00
{
return NULL ;
}
crc32val = crc32 ( crc32val , buf , len ) ;
return PyInt_FromLong ( crc32val ) ;
1997-04-29 12:38:09 -03:00
}
static PyMethodDef zlib_methods [ ] =
{
2000-08-02 23:04:05 -03:00
{ " adler32 " , ( PyCFunction ) PyZlib_adler32 ,
METH_VARARGS , adler32__doc__ } ,
{ " compress " , ( PyCFunction ) PyZlib_compress ,
METH_VARARGS , compress__doc__ } ,
{ " compressobj " , ( PyCFunction ) PyZlib_compressobj ,
METH_VARARGS , compressobj__doc__ } ,
{ " crc32 " , ( PyCFunction ) PyZlib_crc32 ,
METH_VARARGS , crc32__doc__ } ,
{ " decompress " , ( PyCFunction ) PyZlib_decompress ,
METH_VARARGS , decompress__doc__ } ,
{ " decompressobj " , ( PyCFunction ) PyZlib_decompressobj ,
METH_VARARGS , decompressobj__doc__ } ,
{ NULL , NULL }
1997-04-29 12:38:09 -03:00
} ;
statichere PyTypeObject Comptype = {
1997-12-18 01:21:29 -04:00
PyObject_HEAD_INIT ( 0 )
1997-04-29 12:38:09 -03:00
0 ,
" Compress " ,
sizeof ( compobject ) ,
0 ,
( destructor ) Comp_dealloc , /*tp_dealloc*/
0 , /*tp_print*/
( getattrfunc ) Comp_getattr , /*tp_getattr*/
0 , /*tp_setattr*/
0 , /*tp_compare*/
0 , /*tp_repr*/
0 , /*tp_as_number*/
0 , /*tp_as_sequence*/
0 , /*tp_as_mapping*/
} ;
statichere PyTypeObject Decomptype = {
1997-12-18 01:21:29 -04:00
PyObject_HEAD_INIT ( 0 )
1997-04-29 12:38:09 -03:00
0 ,
" Decompress " ,
sizeof ( compobject ) ,
0 ,
( destructor ) Decomp_dealloc , /*tp_dealloc*/
0 , /*tp_print*/
( getattrfunc ) Decomp_getattr , /*tp_getattr*/
0 , /*tp_setattr*/
0 , /*tp_compare*/
0 , /*tp_repr*/
0 , /*tp_as_number*/
0 , /*tp_as_sequence*/
0 , /*tp_as_mapping*/
} ;
/* The following insint() routine was blatantly ripped off from
socketmodule . c */
/* Convenience routine to export an integer value.
For simplicity , errors ( which are unlikely anyway ) are ignored . */
static void
2000-07-10 06:57:19 -03:00
insint ( PyObject * d , char * name , int value )
1997-04-29 12:38:09 -03:00
{
PyObject * v = PyInt_FromLong ( ( long ) value ) ;
if ( v = = NULL ) {
/* Don't bother reporting this error */
PyErr_Clear ( ) ;
}
else {
PyDict_SetItemString ( d , name , v ) ;
Py_DECREF ( v ) ;
}
}
1997-06-03 19:21:03 -03:00
static char zlib_module_documentation [ ] =
" The functions in this module allow compression and decompression "
" using the zlib library, which is based on GNU zip. \n \n "
" adler32(string) -- Compute an Adler-32 checksum. \n "
" adler32(string, start) -- Compute an Adler-32 checksum using a given starting value. \n "
" compress(string) -- Compress a string. \n "
" compress(string, level) -- Compress a string with the given level of compression (1--9). \n "
" compressobj([level]) -- Return a compressor object. \n "
" crc32(string) -- Compute a CRC-32 checksum. \n "
" crc32(string, start) -- Compute a CRC-32 checksum using a given starting value. \n "
1999-12-20 18:13:38 -04:00
" decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string. \n "
1997-06-03 19:21:03 -03:00
" decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size). \n \n "
" Compressor objects support compress() and flush() methods; decompressor \n "
" objects support decompress() and flush(). "
;
1998-12-04 14:50:17 -04:00
DL_EXPORT ( void )
2000-07-21 03:00:07 -03:00
PyInit_zlib ( void )
1997-04-29 12:38:09 -03:00
{
1997-09-04 20:39:23 -03:00
PyObject * m , * d , * ver ;
1997-12-18 01:21:29 -04:00
Comptype . ob_type = & PyType_Type ;
Decomptype . ob_type = & PyType_Type ;
1997-06-03 19:21:03 -03:00
m = Py_InitModule4 ( " zlib " , zlib_methods ,
zlib_module_documentation ,
( PyObject * ) NULL , PYTHON_API_VERSION ) ;
1997-04-29 12:38:09 -03:00
d = PyModule_GetDict ( m ) ;
1997-10-01 01:29:29 -03:00
ZlibError = PyErr_NewException ( " zlib.error " , NULL , NULL ) ;
1999-12-22 12:13:54 -04:00
if ( ZlibError ! = NULL )
PyDict_SetItemString ( d , " error " , ZlibError ) ;
1997-09-04 20:39:23 -03:00
1997-04-29 12:38:09 -03:00
insint ( d , " MAX_WBITS " , MAX_WBITS ) ;
insint ( d , " DEFLATED " , DEFLATED ) ;
insint ( d , " DEF_MEM_LEVEL " , DEF_MEM_LEVEL ) ;
1997-09-04 20:39:23 -03:00
insint ( d , " Z_BEST_SPEED " , Z_BEST_SPEED ) ;
insint ( d , " Z_BEST_COMPRESSION " , Z_BEST_COMPRESSION ) ;
insint ( d , " Z_DEFAULT_COMPRESSION " , Z_DEFAULT_COMPRESSION ) ;
insint ( d , " Z_FILTERED " , Z_FILTERED ) ;
insint ( d , " Z_HUFFMAN_ONLY " , Z_HUFFMAN_ONLY ) ;
insint ( d , " Z_DEFAULT_STRATEGY " , Z_DEFAULT_STRATEGY ) ;
1998-12-18 18:13:11 -04:00
insint ( d , " Z_FINISH " , Z_FINISH ) ;
insint ( d , " Z_NO_FLUSH " , Z_NO_FLUSH ) ;
insint ( d , " Z_SYNC_FLUSH " , Z_SYNC_FLUSH ) ;
insint ( d , " Z_FULL_FLUSH " , Z_FULL_FLUSH ) ;
1997-09-04 20:39:23 -03:00
ver = PyString_FromString ( ZLIB_VERSION ) ;
1999-12-22 12:13:54 -04:00
if ( ver ! = NULL ) {
PyDict_SetItemString ( d , " ZLIB_VERSION " , ver ) ;
Py_DECREF ( ver ) ;
}
2001-09-07 13:27:31 -03:00
# ifdef WITH_THREAD
zlib_lock = PyThread_allocate_lock ( ) ;
# endif // WITH_THREAD
1997-04-29 12:38:09 -03:00
}