[pypy-svn] r47149 - pypy/branch/bz2-module-rffi
fijal at codespeak.net
fijal at codespeak.net
Thu Oct 4 14:37:21 CEST 2007
Author: fijal
Date: Thu Oct 4 14:37:20 2007
New Revision: 47149
Modified:
pypy/branch/bz2-module-rffi/interp_bz2.py
Log:
Intermediate commit around moving bz2 module out of rctypes
Modified: pypy/branch/bz2-module-rffi/interp_bz2.py
==============================================================================
--- pypy/branch/bz2-module-rffi/interp_bz2.py (original)
+++ pypy/branch/bz2-module-rffi/interp_bz2.py Thu Oct 4 14:37:20 2007
@@ -1,34 +1,39 @@
-from pypy.rpython.rctypes.tool import ctypes_platform
-import pypy.rpython.rctypes.implementation # this defines rctypes magic
+from pypy.rpython.tool import rffi_platform as platform
+from pypy.rpython.lltypesystem import rffi
+from pypy.rpython.lltypesystem import lltype
from pypy.interpreter.error import OperationError
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.typedef import TypeDef, GetSetProperty
from pypy.interpreter.typedef import interp_attrproperty
from pypy.interpreter.gateway import ObjSpace, W_Root, NoneNotWrapped, interp2app
from pypy.rlib.streamio import Stream
-from ctypes import *
-import ctypes.util
import sys
-from bzlib import bz_stream, BZFILE, FILE
-
-#libbz2 = cdll.LoadLibrary(ctypes.util.find_library("bz2"))
-
-c_void = None
-
class CConfig:
- _header_ = """
- #include <stdio.h>
- #include <sys/types.h>
- #include <bzlib.h>
- """
-
- _includes_ = ["bzlib.h"]
- libbz2 = ctypes_platform.Library('bz2')
- off_t = ctypes_platform.SimpleType("off_t", c_longlong)
- size_t = ctypes_platform.SimpleType("size_t", c_ulong)
- BUFSIZ = ctypes_platform.ConstantInteger("BUFSIZ")
- SEEK_SET = ctypes_platform.ConstantInteger("SEEK_SET")
+ _includes_ = ['stdio.h', 'sys/types.h', 'bzlib.h']
+ _libraries_ = ['bz2']
+ calling_conv = 'c'
+
+ off_t = platform.SimpleType("off_t", rffi.LONGLONG)
+ size_t = platform.SimpleType("size_t", rffi.ULONG)
+ BUFSIZ = platform.ConstantInteger("BUFSIZ")
+ _alloc_type = lltype.FuncType([rffi.VOIDP, rffi.INT, rffi.INT], rffi.VOIDP)
+ _free_type = lltype.FuncType([rffi.VOIDP, rffi.VOIDP], lltype.Void)
+ SEEK_SET = platform.ConstantInteger("SEEK_SET")
+ bz_stream = platform.Struct('bz_stream',
+ [('next_in', rffi.CCHARP),
+ ('avail_in', rffi.UINT),
+ ('total_in_lo32', rffi.UINT),
+ ('total_in_hi32', rffi.UINT),
+ ('next_out', rffi.CCHARP),
+ ('avail_out', rffi.UINT),
+ ('total_out_lo32', rffi.UINT),
+ ('total_out_hi32', rffi.UINT),
+ ('state', rffi.VOIDP),
+ ('bzalloc', lltype.Ptr(_alloc_type)),
+ ('bzfree', lltype.Ptr(_free_type)),
+ ('opaque', rffi.VOIDP),
+ ])
constants = {}
constant_names = ['BZ_RUN', 'BZ_FLUSH', 'BZ_FINISH', 'BZ_OK',
@@ -37,11 +42,11 @@
'BZ_DATA_ERROR_MAGIC', 'BZ_IO_ERROR', 'BZ_UNEXPECTED_EOF',
'BZ_OUTBUFF_FULL', 'BZ_CONFIG_ERROR']
for name in constant_names:
- setattr(CConfig, name, ctypes_platform.DefinedConstantInteger(name))
+ setattr(CConfig, name, platform.DefinedConstantInteger(name))
class cConfig:
pass
-cConfig.__dict__.update(ctypes_platform.configure(CConfig))
+cConfig.__dict__.update(platform.configure(CConfig))
for name in constant_names:
value = getattr(cConfig, name)
@@ -49,8 +54,8 @@
constants[name] = value
locals().update(constants)
-libbz2 = cConfig.libbz2
off_t = cConfig.off_t
+bz_stream = cConfig.bz_stream
BUFSIZ = cConfig.BUFSIZ
SEEK_SET = cConfig.SEEK_SET
BZ_OK = cConfig.BZ_OK
@@ -69,15 +74,15 @@
else:
SMALLCHUNK = BUFSIZ
-if sizeof(c_int) > 4:
+if rffi.sizeof(rffi.INT) > 4:
BIGCHUNK = 512 * 32
else:
BIGCHUNK = 512 * 1024
-
+
MAXINT = sys.maxint
if BZ_CONFIG_ERROR:
- if sizeof(c_long) >= 8 or sizeof(c_longlong) >= 8:
+ if rffi.sizeof(rffi.LONG) >= 8 or rffi.sizeof(rffi.LONGLONG) >= 8:
def _bzs_total_out(bzs):
return (bzs.total_out_hi32 << 32) + bzs.total_out_lo32
else:
@@ -87,38 +92,35 @@
def _bzs_total_out(bzs):
return bzs.total_out
-# the least but one parameter should be c_void_p but it's not used
+def external(name, args, result):
+ return rffi.llexternal(name, args, result, includes=CConfig._includes_,
+ libraries=['bz2'])
+
+FILE = rffi.COpaquePtr('FILE')
+BZFILE = rffi.COpaquePtr('BZFILE')
+
+# the least but one parameter should be rffi.VOIDP but it's not used
# so I trick the compiler to not complain about constanst pointer passed
# to void* arg
-libbz2.BZ2_bzReadOpen.argtypes = [POINTER(c_int), POINTER(FILE), c_int,
- c_int, POINTER(c_int), c_int]
-libbz2.BZ2_bzReadOpen.restype = POINTER(BZFILE)
-libbz2.BZ2_bzWriteOpen.argtypes = [POINTER(c_int), POINTER(FILE), c_int,
- c_int, c_int]
-libbz2.BZ2_bzWriteOpen.restype = POINTER(BZFILE)
-libbz2.BZ2_bzReadClose.argtypes = [POINTER(c_int), POINTER(BZFILE)]
-libbz2.BZ2_bzReadClose.restype = c_void
-libbz2.BZ2_bzWriteClose.argtypes = [POINTER(c_int), POINTER(BZFILE),
- c_int, POINTER(c_uint), POINTER(c_uint)]
-libbz2.BZ2_bzWriteClose.restype = c_void
-libbz2.BZ2_bzRead.argtypes = [POINTER(c_int), POINTER(BZFILE), POINTER(c_char), c_int]
-libbz2.BZ2_bzRead.restype = c_int
-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
-libbz2.BZ2_bzCompressEnd.argtypes = [POINTER(bz_stream)]
-libbz2.BZ2_bzCompressEnd.restype = c_int
-libbz2.BZ2_bzCompress.argtypes = [POINTER(bz_stream), c_int]
-libbz2.BZ2_bzCompress.restype = c_int
-
-libbz2.BZ2_bzDecompressInit.argtypes = [POINTER(bz_stream), c_int, c_int]
-libbz2.BZ2_bzDecompressInit.restype = c_int
-libbz2.BZ2_bzDecompressEnd.argtypes = [POINTER(bz_stream)]
-libbz2.BZ2_bzDecompressEnd.restype = c_int
-libbz2.BZ2_bzDecompress.argtypes = [POINTER(bz_stream)]
-libbz2.BZ2_bzDecompress.restype = c_int
+BZ2_bzReadOpen = external('BZ2_bzReadOpen', [rffi.INTP, FILE, rffi.INT,
+ rffi.INT, rffi.INTP, rffi.INT], BZFILE)
+BZ2_bzWriteOpen = external('BZ2_bzWriteOpen', [rffi.INTP, FILE, rffi.INT,
+ rffi.INT, rffi.INT], BZFILE)
+BZ2_bzReadClose = external('BZ2_bzReadClose', [rffi.INTP, BZFILE], lltype.Void)
+BZ2_bzWriteClose = external('BZ2_bzWriteClose', [rffi.INTP, BZFILE,
+ rffi.INT, rffi.UINTP, rffi.UINTP], lltype.Void)
+BZ2_bzRead = external('BZ2_bzRead', [rffi.INTP, BZFILE, rffi.CCHARP, rffi.INT],
+ rffi.INT)
+BZ2_bzWrite = external('BZ2_bzWrite', [rffi.INTP, BZFILE, rffi.CCHARP,
+ rffi.INT], lltype.Void)
+BZ2_bzCompressInit = external('BZ2_bzCompressInit', [bz_stream, rffi.INT,
+ rffi.INT, rffi.INT], rffi.INT)
+BZ2_bzCompressEnd = external('BZ2_bzCompressEnd', [bz_stream], rffi.INT)
+BZ2_bzCompress = external('BZ2_bzCompress', [bz_stream, rffi.INT], rffi.INT)
+BZ2_bzDecompressInit = external('BZ2_bzDecompressInit', [bz_stream, rffi.INT,
+ rffi.INT], rffi.INT)
+BZ2_bzDecompressEnd = external('BZ2_bzDecompressEnd', [bz_stream], rffi.INT)
+BZ2_bzDecompress = external('BZ2_bzDecompress', [bz_stream], rffi.INT)
def _catch_bz2_error(space, bzerror):
if BZ_CONFIG_ERROR and bzerror == BZ_CONFIG_ERROR:
@@ -311,24 +313,27 @@
must be a number between 1 and 9."""
def __init__(self, space, compresslevel):
self.space = space
- self.bzs = bz_stream()
+ self.bzs = lltype.malloc(bz_stream, flavor='raw', zero=True)
self.running = False
+ self.initialized = False
self._init_bz2comp(compresslevel)
+ self.initialized = True
__init__.unwrap_spec = ['self', ObjSpace, int]
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)
+
+ bzerror = BZ2_bzCompressInit(self.bzs, compresslevel, 0, 0)
if bzerror != BZ_OK:
_catch_bz2_error(self.space, bzerror)
self.running = True
def __del__(self):
- libbz2.BZ2_bzCompressEnd(byref(self.bzs))
+ if self.initialized:
+ BZ2_bzCompressEnd(self.bzs)
def compress(self, data):
"""compress(data) -> string
@@ -354,14 +359,14 @@
in_buf = create_string_buffer(in_bufsize)
in_buf.value = data
- self.bzs.next_in = cast(in_buf, POINTER(c_char))
+ self.bzs.next_in = cast(in_buf, rffi.CCHARP)
self.bzs.avail_in = in_bufsize
- self.bzs.next_out = cast(out_buf, POINTER(c_char))
+ self.bzs.next_out = cast(out_buf, rffi.CCHARP)
self.bzs.avail_out = out_bufsize
temp = []
while True:
- bzerror = libbz2.BZ2_bzCompress(byref(self.bzs), BZ_RUN)
+ bzerror = BZ2_bzCompress(self.bzs, BZ_RUN)
if bzerror != BZ_RUN_OK:
_catch_bz2_error(self.space, bzerror)
@@ -374,7 +379,7 @@
out_bufsize = _new_buffer_size(out_bufsize)
out_buf = create_string_buffer(out_bufsize)
- self.bzs.next_out = cast(out_buf, POINTER(c_char))
+ self.bzs.next_out = cast(out_buf, rffi.CCHARP)
self.bzs.avail_out = out_bufsize
if temp:
@@ -397,14 +402,14 @@
out_bufsize = SMALLCHUNK
out_buf = create_string_buffer(out_bufsize)
- self.bzs.next_out = cast(out_buf, POINTER(c_char))
+ self.bzs.next_out = cast(out_buf, rffi.CCHARP)
self.bzs.avail_out = out_bufsize
total_out = _bzs_total_out(self.bzs)
temp = []
while True:
- bzerror = libbz2.BZ2_bzCompress(byref(self.bzs), BZ_FINISH)
+ bzerror = BZ2_bzCompress(self.bzs, BZ_FINISH)
if bzerror == BZ_STREAM_END:
break
elif bzerror != BZ_FINISH_OK:
@@ -416,7 +421,7 @@
out_bufsize = _new_buffer_size(out_bufsize)
out_buf = create_string_buffer(out_bufsize)
- self.bzs.next_out = cast(out_buf, POINTER(c_char))
+ self.bzs.next_out = cast(out_buf, rffi.CCHARP)
self.bzs.avail_out = out_bufsize
if temp:
@@ -464,17 +469,17 @@
self._init_bz2decomp()
def _init_bz2decomp(self):
- bzerror = libbz2.BZ2_bzDecompressInit(byref(self.bzs), 0, 0)
+ bzerror = BZ2_bzDecompressInit(self.bzs, 0, 0)
if bzerror != BZ_OK:
_catch_bz2_error(self.space, bzerror)
self.running = True
def __del__(self):
- libbz2.BZ2_bzDecompressEnd(byref(self.bzs))
+ BZ2_bzDecompressEnd(self.bzs)
def decompress(self, data):
- """"decompress(data) -> string
+ """decompress(data) -> string
Provide more data to the decompressor object. It will return chunks
of decompressed data whenever possible. If you try to decompress data
@@ -493,14 +498,14 @@
out_bufsize = SMALLCHUNK
out_buf = create_string_buffer(out_bufsize)
- self.bzs.next_in = cast(in_buf, POINTER(c_char))
+ self.bzs.next_in = cast(in_buf, rffi.CCHARP)
self.bzs.avail_in = in_bufsize
- self.bzs.next_out = cast(out_buf, POINTER(c_char))
+ self.bzs.next_out = cast(out_buf, rffi.CCHARP)
self.bzs.avail_out = out_bufsize
temp = []
while True:
- bzerror = libbz2.BZ2_bzDecompress(byref(self.bzs))
+ bzerror = BZ2_bzDecompress(self.bzs)
if bzerror == BZ_STREAM_END:
if self.bzs.avail_in != 0:
unused = [self.bzs.next_in[i] for i in range(self.bzs.avail_in)]
@@ -519,7 +524,7 @@
out_bufsize = _new_buffer_size(out_bufsize)
out_buf = create_string_buffer(out_bufsize)
- self.bzs.next_out = cast(out_buf, POINTER(c_char))
+ self.bzs.next_out = cast(out_buf, rffi.CCHARP)
self.bzs.avail_out = out_bufsize
if temp:
@@ -564,23 +569,23 @@
in_buf = create_string_buffer(in_bufsize)
in_buf.value = data
- bzs.next_in = cast(in_buf, POINTER(c_char))
+ bzs.next_in = cast(in_buf, rffi.CCHARP)
bzs.avail_in = in_bufsize
- bzs.next_out = cast(out_buf, POINTER(c_char))
+ bzs.next_out = cast(out_buf, rffi.CCHARP)
bzs.avail_out = out_bufsize
- bzerror = libbz2.BZ2_bzCompressInit(byref(bzs), compresslevel, 0, 0)
+ bzerror = BZ2_bzCompressInit(bzs, compresslevel, 0, 0)
if bzerror != BZ_OK:
_catch_bz2_error(space, bzerror)
total_out = _bzs_total_out(bzs)
temp = []
while True:
- bzerror = libbz2.BZ2_bzCompress(byref(bzs), BZ_FINISH)
+ bzerror = BZ2_bzCompress(bzs, BZ_FINISH)
if bzerror == BZ_STREAM_END:
break
elif bzerror != BZ_FINISH_OK:
- libbz2.BZ2_bzCompressEnd(byref(bzs))
+ BZ2_bzCompressEnd(bzs)
_catch_bz2_error(space, bzerror)
if bzs.avail_out == 0:
@@ -589,7 +594,7 @@
out_bufsize = _new_buffer_size(out_bufsize)
out_buf = create_string_buffer(out_bufsize)
- bzs.next_out = cast(out_buf, POINTER(c_char))
+ bzs.next_out = cast(out_buf, rffi.CCHARP)
bzs.avail_out = out_bufsize
if temp:
@@ -602,7 +607,7 @@
total_out = _bzs_total_out(bzs)
res = "".join([out_buf[i] for i in range(total_out)])
- libbz2.BZ2_bzCompressEnd(byref(bzs))
+ BZ2_bzCompressEnd(bzs)
return space.wrap(res)
compress.unwrap_spec = [ObjSpace, str, int]
@@ -624,26 +629,26 @@
out_bufsize = SMALLCHUNK
out_buf = create_string_buffer(out_bufsize)
- bzs.next_in = cast(in_buf, POINTER(c_char))
+ bzs.next_in = cast(in_buf, rffi.CCHARP)
bzs.avail_in = in_bufsize
- bzs.next_out = cast(out_buf, POINTER(c_char))
+ bzs.next_out = cast(out_buf, rffi.CCHARP)
bzs.avail_out = out_bufsize
- bzerror = libbz2.BZ2_bzDecompressInit(byref(bzs), 0, 0)
+ bzerror = BZ2_bzDecompressInit(bzs, 0, 0)
if bzerror != BZ_OK:
_catch_bz2_error(space, bzerror)
temp = []
while True:
- bzerror = libbz2.BZ2_bzDecompress(byref(bzs))
+ bzerror = BZ2_bzDecompress(bzs)
if bzerror == BZ_STREAM_END:
break
if bzerror != BZ_OK:
- libbz2.BZ2_bzDecompressEnd(byref(bzs))
+ BZ2_bzDecompressEnd(bzs)
_catch_bz2_error(space, bzerror)
if bzs.avail_in == 0:
- libbz2.BZ2_bzDecompressEnd(byref(bzs))
+ BZ2_bzDecompressEnd(bzs)
raise OperationError(space.w_ValueError,
space.wrap("couldn't find end of stream"))
elif bzs.avail_out == 0:
@@ -653,7 +658,7 @@
out_bufsize = _new_buffer_size(out_bufsize)
out_buf = create_string_buffer(out_bufsize)
- bzs.next_out = cast(out_buf, POINTER(c_char))
+ bzs.next_out = cast(out_buf, rffi.CCHARP)
bzs.avail_out = out_bufsize
total_out = _bzs_total_out(bzs)
@@ -664,6 +669,6 @@
else:
res = "".join([out_buf[i] for i in range(total_out) if out_buf[i] != '\x00'])
- libbz2.BZ2_bzDecompressEnd(byref(bzs))
+ BZ2_bzDecompressEnd(bzs)
return space.wrap(res)
decompress.unwrap_spec = [ObjSpace, str]
More information about the Pypy-commit
mailing list