[pypy-commit] pypy default: Use the new context manager allocators, much nicer. Also update a comment.
alex_gaynor
noreply at buildbot.pypy.org
Thu Jul 28 07:37:22 CEST 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch:
Changeset: r46034:c5eae3c7710f
Date: 2011-07-27 22:37 -0700
http://bitbucket.org/pypy/pypy/changeset/c5eae3c7710f/
Log: Use the new context manager allocators, much nicer. Also update a
comment.
diff --git a/pypy/rlib/rzlib.py b/pypy/rlib/rzlib.py
--- a/pypy/rlib/rzlib.py
+++ b/pypy/rlib/rzlib.py
@@ -340,22 +340,17 @@
"""Common code for compress() and decompress().
"""
# Prepare the input buffer for the stream
- inbuf = lltype.malloc(rffi.CCHARP.TO, len(data), flavor='raw')
- try:
+ with lltype.scoped_alloc(rffi.CCHARP.TO, len(data)) as inbuf:
for i in xrange(len(data)):
inbuf[i] = data[i]
stream.c_next_in = rffi.cast(Bytefp, inbuf)
rffi.setintfield(stream, 'c_avail_in', len(data))
# Prepare the output buffer
- outbuf = lltype.malloc(rffi.CCHARP.TO, OUTPUT_BUFFER_SIZE,
- flavor='raw')
- try:
- # Strategy: we call deflate() to get as much output data as
- # fits in the buffer, then accumulate all output into a list
- # of characters 'result'. We don't need to gradually
- # increase the output buffer size because there is no
- # quadratic factor.
+ with lltype.scoped_alloc(rffi.CCHARP.TO, OUTPUT_BUFFER_SIZE) as outbuf:
+ # Strategy: we call deflate() to get as much output data as fits in
+ # the buffer, then accumulate all output into a StringBuffer
+ # 'result'.
result = StringBuilder()
while True:
@@ -395,11 +390,6 @@
# fallback case: report this error
raise RZlibError.fromstream(stream, err, while_doing)
- finally:
- lltype.free(outbuf, flavor='raw')
- finally:
- lltype.free(inbuf, flavor='raw')
-
# When decompressing, if the compressed stream of data was truncated,
# then the zlib simply returns Z_OK and waits for more. If it is
# complete it returns Z_STREAM_END.
More information about the pypy-commit
mailing list