From: <pj...@us...> - 2009-09-09 04:59:27
|
Revision: 6772 http://jython.svn.sourceforge.net/jython/?rev=6772&view=rev Author: pjenvey Date: 2009-09-09 04:59:15 +0000 (Wed, 09 Sep 2009) Log Message: ----------- allow zlib input from arrays fixes #1444 Modified Paths: -------------- trunk/jython/Lib/zlib.py trunk/jython/NEWS Added Paths: ----------- trunk/jython/Lib/test/test_zlib_jy.py Added: trunk/jython/Lib/test/test_zlib_jy.py =================================================================== --- trunk/jython/Lib/test/test_zlib_jy.py (rev 0) +++ trunk/jython/Lib/test/test_zlib_jy.py 2009-09-09 04:59:15 UTC (rev 6772) @@ -0,0 +1,39 @@ +"""Misc zlib tests + +Made for Jython. +""" +import unittest +import zlib +from array import array +from test import test_support + +class ArrayTestCase(unittest.TestCase): + + def test_array(self): + self._test_array(zlib.compress, zlib.decompress) + + def test_array_compressobj(self): + def compress(value): + co = zlib.compressobj() + return co.compress(value) + co.flush() + def decompress(value): + dco = zlib.decompressobj() + return dco.decompress(value) + dco.flush() + self._test_array(compress, decompress) + + def _test_array(self, compress, decompress): + self.assertEqual(compress(array('c', 'jython')), + compress('jython')) + intarray = array('i', range(5)) + self.assertEqual(compress(intarray), + compress(intarray.tostring())) + compressed = array('c', compress('jython')) + self.assertEqual('jython', decompress(compressed)) + + +def test_main(): + test_support.run_unittest(ArrayTestCase) + + +if __name__ == '__main__': + test_main() Modified: trunk/jython/Lib/zlib.py =================================================================== --- trunk/jython/Lib/zlib.py 2009-09-09 04:16:49 UTC (rev 6771) +++ trunk/jython/Lib/zlib.py 2009-09-09 04:59:15 UTC (rev 6772) @@ -13,7 +13,9 @@ Compressor objects support compress() and flush() methods; decompressor objects support decompress() and flush(). """ -import jarray, binascii +import array +import binascii +import jarray from java.util.zip import Adler32, Deflater, Inflater, DataFormatException from java.lang import Long, String @@ -59,13 +61,14 @@ if level < Z_BEST_SPEED or level > Z_BEST_COMPRESSION: raise error, "Bad compression level" deflater = Deflater(level, 0) + string = _to_input(string) deflater.setInput(string, 0, len(string)) deflater.finish() return _get_deflate_data(deflater) def decompress(string, wbits=0, bufsize=16384): inflater = Inflater(wbits < 0) - inflater.setInput(string) + inflater.setInput(_to_input(string)) return _get_inflate_data(inflater) @@ -84,6 +87,7 @@ def compress(self, string): if self._ended: raise error("compressobj may not be used after flush(Z_FINISH)") + string = _to_input(string) self.deflater.setInput(string, 0, len(string)) return _get_deflate_data(self.deflater) @@ -123,6 +127,7 @@ if max_length < 0: raise ValueError("max_length must be a positive integer") + string = _to_input(string) self.inflater.setInput(string) inflated = _get_inflate_data(self.inflater, max_length) @@ -146,6 +151,8 @@ self.inflater.end() return last +def _to_input(string): + return string.tostring() if isinstance(string, array.array) else string def _get_deflate_data(deflater): buf = jarray.zeros(1024, 'b') Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2009-09-09 04:16:49 UTC (rev 6771) +++ trunk/jython/NEWS 2009-09-09 04:59:15 UTC (rev 6772) @@ -8,6 +8,7 @@ - [ 1457 ] Cannot write an array in a file opened in r+b mode. - [ 1382 ] __cmp__ on certain types raises ArrayStoreException - [ 1443 ] Can't update() hashlib.sha1() with array.array('c') + - [ 1444 ] Can't zlib.compress() with array.array('c') Jython 2.5.1rc1 New Features This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |