[pypy-svn] r54040 - in pypy/branch/io-improvements/pypy/rlib: . test
fijal at codespeak.net
fijal at codespeak.net
Wed Apr 23 12:39:05 CEST 2008
Author: fijal
Date: Wed Apr 23 12:39:05 2008
New Revision: 54040
Modified:
pypy/branch/io-improvements/pypy/rlib/rgc.py
pypy/branch/io-improvements/pypy/rlib/test/test_rgc.py
Log:
Primitive operations for implementing string builder, not working.
Modified: pypy/branch/io-improvements/pypy/rlib/rgc.py
==============================================================================
--- pypy/branch/io-improvements/pypy/rlib/rgc.py (original)
+++ pypy/branch/io-improvements/pypy/rlib/rgc.py Wed Apr 23 12:39:05 2008
@@ -232,3 +232,39 @@
hop.exception_cannot_occur()
return hop.genop(opname, vlist, resulttype = hop.r_result.lowleveltype)
+
+def raw_array_of_shape(T, init_size):
+ """ Allocates a raw array of given shape. This array is suitable
+ for resizing by resize_raw_array or finalizing calling
+ cast_raw_array_to_shape
+ """
+ from pypy.rpython.lltypesystem import lltype
+ return lltype.malloc(lltype.Array(lltype.Char, hints={'nolength':True}),
+ init_size, flavor='raw')
+
+class RawArrayOfShapeEntry(ExtRegistryEntry):
+ _about_ = raw_array_of_shape
+
+def resize_raw_array(arr, old_size, new_size):
+ """ Resize raw array returned by raw_array_of_shape from old_size
+ to new_size. Returns pointer to new array (in case resizing copied
+ contents of old array to new place
+ """
+ from pypy.rpython.lltypesystem import lltype
+ # we don't have any realloc on top of cpython
+ new_ar = lltype.malloc(lltype.Array(lltype.Char, hints={'nolength':True}),
+ new_size, flavor='raw')
+ for i in range(old_size):
+ new_ar[i] = arr[i]
+ lltype.free(arr, flavor='raw')
+ return new_ar
+
+class ResizeRawArrayEntry(ExtRegistryEntry):
+ _about_ = resize_raw_array
+
+def cast_raw_array_to_shape(T, arr):
+ """ Cast raw array returned by raw_array_of_shape to type T.
+ """
+ from pypy.rpython.lltypesystem import rffi, lltype
+ return rffi.cast(lltype.Ptr(T), arr)
+
Modified: pypy/branch/io-improvements/pypy/rlib/test/test_rgc.py
==============================================================================
--- pypy/branch/io-improvements/pypy/rlib/test/test_rgc.py (original)
+++ pypy/branch/io-improvements/pypy/rlib/test/test_rgc.py Wed Apr 23 12:39:05 2008
@@ -2,6 +2,7 @@
from pypy.rpython.lltypesystem import lltype
from pypy.rlib import rgc # Force registration of gc.collect
import gc
+import py
def test_collect():
def f():
@@ -36,3 +37,19 @@
assert res == True
+def test_raw_array():
+ py.test.skip("Not working")
+ from pypy.rpython.lltypesystem.rstr import STR
+ from pypy.rpython.annlowlevel import hlstr
+
+ def f():
+ arr = rgc.raw_array_of_shape(STR, 1)
+ arr[0] = 'a'
+ arr = rgc.resize_raw_array(arr, 1, 2)
+ arr[1] = 'b'
+ return hlstr(rgc.cast_raw_array_to_shape(STR, arr))
+
+ assert f() == 'ab'
+# from pypy.translator.c.test.test_genc import compile
+# fn = compile(f, [])
+# assert fn() == 'ab'
More information about the Pypy-commit
mailing list