[pypy-commit] pypy default: Replace more usages of rffi.alloc_buffer() with scoped_alloc_buffer().

arigo noreply at buildbot.pypy.org
Fri Jul 4 16:45:12 CEST 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r72359:8bc897775808
Date: 2014-07-04 16:44 +0200
http://bitbucket.org/pypy/pypy/changeset/8bc897775808/

Log:	Replace more usages of rffi.alloc_buffer() with
	scoped_alloc_buffer().

diff --git a/pypy/module/cpyext/test/test_unicodeobject.py b/pypy/module/cpyext/test/test_unicodeobject.py
--- a/pypy/module/cpyext/test/test_unicodeobject.py
+++ b/pypy/module/cpyext/test/test_unicodeobject.py
@@ -442,8 +442,8 @@
 
     def test_copy(self, space, api):
         w_x = space.wrap(u"abcd\u0660")
-        target_chunk, _ = rffi.alloc_unicodebuffer(space.int_w(space.len(w_x)))
-        #lltype.malloc(Py_UNICODE, space.int_w(space.len(w_x)), flavor='raw')
+        count1 = space.int_w(space.len(w_x))
+        target_chunk = lltype.malloc(rffi.CWCHARP.TO, count1, flavor='raw')
 
         x_chunk = api.PyUnicode_AS_UNICODE(w_x)
         api.Py_UNICODE_COPY(target_chunk, x_chunk, 4)
diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -188,16 +188,13 @@
             finally:
                 lltype.free(buf, flavor='raw')
         else:
-            raw_buf, gc_buf = rffi.alloc_buffer(size)
-            try:
-                returned_size = c_fread(raw_buf, 1, size, ll_file)
+            with rffi.scoped_alloc_buffer(size) as buf:
+                returned_size = c_fread(buf.raw, 1, size, ll_file)
                 returned_size = intmask(returned_size)  # is between 0 and size
                 if returned_size == 0:
                     if not c_feof(ll_file):
                         raise _error(ll_file)
-                s = rffi.str_from_buffer(raw_buf, gc_buf, size, returned_size)
-            finally:
-                rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
+                s = buf.str(returned_size)
             return s
 
     def seek(self, pos, whence=0):
@@ -270,25 +267,21 @@
 
     def readline(self):
         if self.ll_file:
-            raw_buf, gc_buf = rffi.alloc_buffer(BASE_LINE_SIZE)
-            try:
-                c = self._readline1(raw_buf)
+            with rffi.scoped_alloc_buffer(BASE_LINE_SIZE) as buf:
+                c = self._readline1(buf.raw)
                 if c >= 0:
-                    return rffi.str_from_buffer(raw_buf, gc_buf,
-                                                BASE_LINE_SIZE, c)
+                    return buf.str(c)
                 #
                 # this is the rare case: the line is longer than BASE_LINE_SIZE
                 s = StringBuilder()
                 while True:
-                    s.append_charpsize(raw_buf, BASE_LINE_SIZE - 1)
-                    c = self._readline1(raw_buf)
+                    s.append_charpsize(buf.raw, BASE_LINE_SIZE - 1)
+                    c = self._readline1(buf.raw)
                     if c >= 0:
                         break
                 #
-                s.append_charpsize(raw_buf, c)
+                s.append_charpsize(buf.raw, c)
                 return s.build()
-            finally:
-                rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
         raise ValueError("I/O operation on closed file")
 
 
diff --git a/rpython/rlib/rsocket.py b/rpython/rlib/rsocket.py
--- a/rpython/rlib/rsocket.py
+++ b/rpython/rlib/rsocket.py
@@ -827,15 +827,12 @@
         if timeout == 1:
             raise SocketTimeout
         elif timeout == 0:
-            raw_buf, gc_buf = rffi.alloc_buffer(buffersize)
-            try:
+            with rffi.scoped_alloc_buffer(buffersize) as buf:
                 read_bytes = _c.socketrecv(self.fd,
-                                           rffi.cast(rffi.VOIDP, raw_buf),
+                                           rffi.cast(rffi.VOIDP, buf.raw),
                                            buffersize, flags)
                 if read_bytes >= 0:
-                    return rffi.str_from_buffer(raw_buf, gc_buf, buffersize, read_bytes)
-            finally:
-                rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
+                    return buf.str(read_bytes)
         raise self.error_handler()
 
     def recvinto(self, rwbuffer, nbytes, flags=0):
@@ -852,11 +849,10 @@
         if timeout == 1:
             raise SocketTimeout
         elif timeout == 0:
-            raw_buf, gc_buf = rffi.alloc_buffer(buffersize)
-            try:
+            with rffi.scoped_alloc_buffer(buffersize) as buf:
                 address, addr_p, addrlen_p = self._addrbuf()
                 try:
-                    read_bytes = _c.recvfrom(self.fd, raw_buf, buffersize, flags,
+                    read_bytes = _c.recvfrom(self.fd, buf.raw, buffersize, flags,
                                              addr_p, addrlen_p)
                     addrlen = rffi.cast(lltype.Signed, addrlen_p[0])
                 finally:
@@ -867,10 +863,8 @@
                         address.addrlen = addrlen
                     else:
                         address = None
-                    data = rffi.str_from_buffer(raw_buf, gc_buf, buffersize, read_bytes)
+                    data = buf.str(read_bytes)
                     return (data, address)
-            finally:
-                rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
         raise self.error_handler()
 
     def recvfrom_into(self, rwbuffer, nbytes, flags=0):
diff --git a/rpython/rtyper/module/ll_os.py b/rpython/rtyper/module/ll_os.py
--- a/rpython/rtyper/module/ll_os.py
+++ b/rpython/rtyper/module/ll_os.py
@@ -1006,15 +1006,12 @@
             if count < 0:
                 raise OSError(errno.EINVAL, None)
             rposix.validate_fd(fd)
-            raw_buf, gc_buf = rffi.alloc_buffer(count)
-            try:
-                void_buf = rffi.cast(rffi.VOIDP, raw_buf)
+            with rffi.scoped_alloc_buffer(count) as buf:
+                void_buf = rffi.cast(rffi.VOIDP, buf.raw)
                 got = rffi.cast(lltype.Signed, os_read(fd, void_buf, count))
                 if got < 0:
                     raise OSError(rposix.get_errno(), "os_read failed")
-                return rffi.str_from_buffer(raw_buf, gc_buf, count, got)
-            finally:
-                rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
+                return buf.str(got)
 
         return extdef([int, int], SomeString(can_be_None=True),
                       "ll_os.ll_os_read", llimpl=os_read_llimpl)


More information about the pypy-commit mailing list