[pypy-svn] r31060 - in pypy/dist/pypy/module/bz2: . test
rhymes at codespeak.net
rhymes at codespeak.net
Sun Aug 6 02:16:27 CEST 2006
Author: rhymes
Date: Sun Aug 6 02:16:24 2006
New Revision: 31060
Modified:
pypy/dist/pypy/module/bz2/__init__.py
pypy/dist/pypy/module/bz2/interp_bz2.py
pypy/dist/pypy/module/bz2/test/test_bz2.py
Log:
BZ2Compressor initial stuff
Modified: pypy/dist/pypy/module/bz2/__init__.py
==============================================================================
--- pypy/dist/pypy/module/bz2/__init__.py (original)
+++ pypy/dist/pypy/module/bz2/__init__.py Sun Aug 6 02:16:24 2006
@@ -3,6 +3,7 @@
class Module(MixedModule):
interpleveldefs = {
'BZ2File': 'interp_bz2.BZ2File',
+ 'BZ2Compressor': 'interp_bz2.BZ2Compressor',
}
appleveldefs = {
Modified: pypy/dist/pypy/module/bz2/interp_bz2.py
==============================================================================
--- pypy/dist/pypy/module/bz2/interp_bz2.py (original)
+++ pypy/dist/pypy/module/bz2/interp_bz2.py Sun Aug 6 02:16:24 2006
@@ -111,6 +111,9 @@
libbz2.BZ2_bzWrite.argtypes = [POINTER(c_int), POINTER(BZFILE), c_char_p, c_int]
libbz2.BZ2_bzWrite.restype = c_void
+libbz2.BZ2_bzCompressInit.argtypes = [POINTER(bz_stream), c_int, c_int, c_int]
+libbz2.BZ2_bzCompressInit.restype = c_int
+
libc.strerror.restype = c_char_p
libc.strerror.argtypes = [c_int]
libc.fclose.argtypes = [POINTER(FILE)]
@@ -747,6 +750,39 @@
softspace = interp_attrproperty("f_softspace", _BZ2File),
)
+class _BZ2Comp(Wrappable):
+ def __init__(self, space, compresslevel):
+ self.space = space
+ self.bzs = bz_stream()
+ self.running = False
+
+ self._init_bz2comp(compresslevel)
+
+ def _init_bz2comp(self, compresslevel):
+ if compresslevel < 1 or compresslevel > 9:
+ raise OperationError(self.space.w_ValueError,
+ self.space.wrap("compresslevel must be between 1 and 9"))
+
+ bzerror = libbz2.BZ2_bzCompressInit(byref(self.bzs), compresslevel, 0, 0)
+ if bzerror != BZ_OK:
+ _catch_bz2_error(self.space, bzerror)
+
+ self.running = True
+
+
+_BZ2Comp.typedef = TypeDef("_BZ2File")
+
+def BZ2Compressor(space, compresslevel=9):
+ """BZ2Compressor([compresslevel=9]) -> compressor object
+
+ Create a new compressor object. This object may be used to compress
+ data sequentially. If you want to compress data in one shot, use the
+ compress() function instead. The compresslevel parameter, if given,
+ must be a number between 1 and 9."""
+
+ return _BZ2Comp(space, compresslevel)
+BZ2Compressor.unwrap_spec = [ObjSpace, int]
+
def BZ2File(space, filename, mode='r', buffering=-1, compresslevel=9):
"""BZ2File(name [, mode='r', buffering=0, compresslevel=9]) -> file object
@@ -762,6 +798,7 @@
for this attribute is one of None (no newline read yet), '\\r', '\\n',
'\\r\\n' or a tuple containing all the newline types seen. Universal
newlines are available only when reading."""
+
return _BZ2File(space, filename, mode, buffering, compresslevel)
BZ2File.unwrap_spec = [ObjSpace, str, str, int, int]
Modified: pypy/dist/pypy/module/bz2/test/test_bz2.py
==============================================================================
--- pypy/dist/pypy/module/bz2/test/test_bz2.py (original)
+++ pypy/dist/pypy/module/bz2/test/test_bz2.py Sun Aug 6 02:16:24 2006
@@ -9,7 +9,7 @@
if os.path.exists("foo"):
os.unlink("foo")
-class AppTestBz2File:
+class AppTestBZ2File:
def setup_class(cls):
space = gettestobjspace(usemodules=('bz2',))
cls.space = space
@@ -568,6 +568,21 @@
assert decompress(f.read()) == TEXT
f.close()
+
+class AppTestBZ2Compressor:
+ def setup_class(cls):
+ space = gettestobjspace(usemodules=('bz2',))
+ cls.space = space
+
+ def test_creation(self):
+ from bz2 import BZ2Compressor
+
+ raises(TypeError, BZ2Compressor, "foo")
+ raises(ValueError, BZ2Compressor, 10)
+
+ BZ2Compressor(1)
+ BZ2Compressor(9)
+
# has_cmdline_bunzip2 = sys.platform not in ("win32", "os2emx", "riscos")
#
# if has_cmdline_bunzip2:
More information about the Pypy-commit
mailing list