Author: Richard Plangger <[email protected]>
Branch: strbuf-as-buffer
Changeset: r88974:371ebe3ffc26
Date: 2016-12-08 15:54 +0100
http://bitbucket.org/pypy/pypy/changeset/371ebe3ffc26/

Log:    expose a raw_ptr for a resizable list for StringBuffer

diff --git a/rpython/rlib/buffer.py b/rpython/rlib/buffer.py
--- a/rpython/rlib/buffer.py
+++ b/rpython/rlib/buffer.py
@@ -2,6 +2,8 @@
 Buffer protocol support.
 """
 from rpython.rlib import jit
+from rpython.rlib.rgc import (resizable_list_supporting_raw_ptr,
+        nonmoving_raw_ptr_for_resizable_list)
 
 
 class Buffer(object):
@@ -76,13 +78,14 @@
         return [1]
 
 class StringBuffer(Buffer):
-    __slots__ = ['value', '_charp']
+    __slots__ = ['value', 'charlist']
     _immutable_ = True
 
     def __init__(self, value):
         self.value = value
         self.readonly = True
-        self._charp = 0
+        # currently the
+        self.charlist = None
 
     def getlength(self):
         return len(self.value)
@@ -107,10 +110,10 @@
         return Buffer.getslice(self, start, stop, step, size)
 
     def get_raw_address(self):
-        from rpython.rtyper.lltypesystem import rffi
-        if self._charp == 0:
-            self._charp = rffi.str2charp_gc(self.value)
-        return self._charp
+        if not self.charlist:
+            data = [c for c in self.value]
+            self.charlist = resizable_list_supporting_raw_ptr(data)
+        return nonmoving_raw_ptr_for_resizable_list(self.charlist)
 
 
 
diff --git a/rpython/rlib/test/test_buffer.py b/rpython/rlib/test/test_buffer.py
--- a/rpython/rlib/test/test_buffer.py
+++ b/rpython/rlib/test/test_buffer.py
@@ -71,4 +71,3 @@
     assert addr[0] == b'h'
     assert addr[4] == b'o'
     assert addr[6] == b'w'
-    assert addr[len(b'hello world')] == b'\x00'
diff --git a/rpython/rtyper/lltypesystem/rffi.py 
b/rpython/rtyper/lltypesystem/rffi.py
--- a/rpython/rtyper/lltypesystem/rffi.py
+++ b/rpython/rtyper/lltypesystem/rffi.py
@@ -805,19 +805,6 @@
         else:
             lltype.free(cp, flavor='raw', track_allocation=False)
 
-    # str -> char*
-    def str2charp_gc(s):
-        """ str -> char* but collected by the gc
-        """
-        array = lltype.malloc(TYPEP.TO, len(s) + 1, flavor='gc', immortal=True)
-        i = len(s)
-        ll_s = llstrtype(s)
-        copy_string_to_raw(ll_s, array, 0, i)
-        array[i] = lastchar
-        return array
-    str2charp_gc._annenforceargs_ = [strtype]
-
-
     # str -> already-existing char[maxsize]
     def str2chararray(s, array, maxsize):
         length = min(len(s), maxsize)
@@ -997,20 +984,20 @@
         return result
     charpsize2str._annenforceargs_ = [None, int]
 
-    return (str2charp, free_charp, str2charp_gc, charp2str,
+    return (str2charp, free_charp, charp2str,
             get_nonmovingbuffer, free_nonmovingbuffer,
             get_nonmovingbuffer_final_null,
             alloc_buffer, str_from_buffer, keep_buffer_alive_until_here,
             charp2strn, charpsize2str, str2chararray, str2rawmem,
             )
 
-(str2charp, free_charp, str2charp_gc, charp2str,
+(str2charp, free_charp, charp2str,
  get_nonmovingbuffer, free_nonmovingbuffer, get_nonmovingbuffer_final_null,
  alloc_buffer, str_from_buffer, keep_buffer_alive_until_here,
  charp2strn, charpsize2str, str2chararray, str2rawmem,
  ) = make_string_mappings(str)
 
-(unicode2wcharp, free_wcharp, _, wcharp2unicode,
+(unicode2wcharp, free_wcharp, wcharp2unicode,
  get_nonmoving_unicodebuffer, free_nonmoving_unicodebuffer, __not_usable,
  alloc_unicodebuffer, unicode_from_buffer, keep_unicodebuffer_alive_until_here,
  wcharp2unicoden, wcharpsize2unicode, unicode2wchararray, unicode2rawmem,
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to