Author: Armin Rigo <[email protected]>
Branch: null_byte_after_str
Changeset: r85937:d689267af347
Date: 2016-07-30 21:34 +0200
http://bitbucket.org/pypy/pypy/changeset/d689267af347/

Log:    More usages of scoped_view_charp(), focusing on places that have a
        potentially large buffer (instead of just a file path, for example).

diff --git a/pypy/module/_locale/interp_locale.py 
b/pypy/module/_locale/interp_locale.py
--- a/pypy/module/_locale/interp_locale.py
+++ b/pypy/module/_locale/interp_locale.py
@@ -126,13 +126,9 @@
         space.isinstance_w(w_s2, space.w_str)):
 
         s1, s2 = space.str_w(w_s1), space.str_w(w_s2)
-        s1_c = rffi.str2charp(s1)
-        s2_c = rffi.str2charp(s2)
-        try:
-            return space.wrap(_strcoll(s1_c, s2_c))
-        finally:
-            rffi.free_charp(s1_c)
-            rffi.free_charp(s2_c)
+        with rffi.scoped_view_charp(s1) as s1_c:
+            with rffi.scoped_view_charp(s2) as s2_c:
+                return space.wrap(_strcoll(s1_c, s2_c))
 
     s1, s2 = space.unicode_w(w_s1), space.unicode_w(w_s2)
 
@@ -155,21 +151,15 @@
     n1 = len(s) + 1
 
     buf = lltype.malloc(rffi.CCHARP.TO, n1, flavor="raw", zero=True)
-    s_c = rffi.str2charp(s)
-    try:
+    with rffi.scoped_view_charp(s) as s_c:
         n2 = _strxfrm(buf, s_c, n1) + 1
-    finally:
-        rffi.free_charp(s_c)
     if n2 > n1:
         # more space needed
         lltype.free(buf, flavor="raw")
         buf = lltype.malloc(rffi.CCHARP.TO, intmask(n2),
                             flavor="raw", zero=True)
-        s_c = rffi.str2charp(s)
-        try:
+        with rffi.scoped_view_charp(s) as s_c:
             _strxfrm(buf, s_c, n2)
-        finally:
-            rffi.free_charp(s_c)
 
     val = rffi.charp2str(buf)
     lltype.free(buf, flavor="raw")
@@ -198,11 +188,8 @@
     def gettext(space, msg):
         """gettext(msg) -> string
         Return translation of msg."""
-        msg_c = rffi.str2charp(msg)
-        try:
+        with rffi.scoped_view_charp(msg) as msg_c:
             return space.wrap(rffi.charp2str(_gettext(msg_c)))
-        finally:
-            rffi.free_charp(msg_c)
 
     _dgettext = rlocale.external('dgettext', [rffi.CCHARP, rffi.CCHARP], 
rffi.CCHARP)
 
@@ -212,28 +199,21 @@
         Return translation of msg in domain."""
         if space.is_w(w_domain, space.w_None):
             domain = None
-            msg_c = rffi.str2charp(msg)
-            try:
+            with rffi.scoped_view_charp(msg) as msg_c:
                 result = _dgettext(domain, msg_c)
                 # note that 'result' may be the same pointer as 'msg_c',
                 # so it must be converted to an RPython string *before*
                 # we free msg_c.
                 result = rffi.charp2str(result)
-            finally:
-                rffi.free_charp(msg_c)
         else:
             domain = space.str_w(w_domain)
-            domain_c = rffi.str2charp(domain)
-            msg_c = rffi.str2charp(msg)
-            try:
-                result = _dgettext(domain_c, msg_c)
-                # note that 'result' may be the same pointer as 'msg_c',
-                # so it must be converted to an RPython string *before*
-                # we free msg_c.
-                result = rffi.charp2str(result)
-            finally:
-                rffi.free_charp(domain_c)
-                rffi.free_charp(msg_c)
+            with rffi.scoped_view_charp(domain) as domain_c:
+                with rffi.scoped_view_charp(msg) as msg_c:
+                    result = _dgettext(domain_c, msg_c)
+                    # note that 'result' may be the same pointer as 'msg_c',
+                    # so it must be converted to an RPython string *before*
+                    # we free msg_c.
+                    result = rffi.charp2str(result)
 
         return space.wrap(result)
 
@@ -247,29 +227,22 @@
 
         if space.is_w(w_domain, space.w_None):
             domain = None
-            msg_c = rffi.str2charp(msg)
-            try:
+            with rffi.scoped_view_charp(msg) as msg_c:
                 result = _dcgettext(domain, msg_c, rffi.cast(rffi.INT, 
category))
                 # note that 'result' may be the same pointer as 'msg_c',
                 # so it must be converted to an RPython string *before*
                 # we free msg_c.
                 result = rffi.charp2str(result)
-            finally:
-                rffi.free_charp(msg_c)
         else:
             domain = space.str_w(w_domain)
-            domain_c = rffi.str2charp(domain)
-            msg_c = rffi.str2charp(msg)
-            try:
-                result = _dcgettext(domain_c, msg_c,
-                                    rffi.cast(rffi.INT, category))
-                # note that 'result' may be the same pointer as 'msg_c',
-                # so it must be converted to an RPython string *before*
-                # we free msg_c.
-                result = rffi.charp2str(result)
-            finally:
-                rffi.free_charp(domain_c)
-                rffi.free_charp(msg_c)
+            with rffi.scoped_view_charp(domain) as domain_c:
+                with rffi.scoped_view_charp(msg) as msg_c:
+                    result = _dcgettext(domain_c, msg_c,
+                                        rffi.cast(rffi.INT, category))
+                    # note that 'result' may be the same pointer as 'msg_c',
+                    # so it must be converted to an RPython string *before*
+                    # we free msg_c.
+                    result = rffi.charp2str(result)
 
         return space.wrap(result)
 
@@ -286,15 +259,12 @@
             result = rffi.charp2str(result)
         else:
             domain = space.str_w(w_domain)
-            domain_c = rffi.str2charp(domain)
-            try:
+            with rffi.scoped_view_charp(domain) as domain_c:
                 result = _textdomain(domain_c)
                 # note that 'result' may be the same pointer as 'domain_c'
                 # (maybe?) so it must be converted to an RPython string
                 # *before* we free domain_c.
                 result = rffi.charp2str(result)
-            finally:
-                rffi.free_charp(domain_c)
 
         return space.wrap(result)
 
@@ -309,20 +279,13 @@
 
         if space.is_w(w_dir, space.w_None):
             dir = None
-            domain_c = rffi.str2charp(domain)
-            try:
+            with rffi.scoped_view_charp(domain) as domain_c:
                 dirname = _bindtextdomain(domain_c, dir)
-            finally:
-                rffi.free_charp(domain_c)
         else:
             dir = space.str_w(w_dir)
-            domain_c = rffi.str2charp(domain)
-            dir_c = rffi.str2charp(dir)
-            try:
-                dirname = _bindtextdomain(domain_c, dir_c)
-            finally:
-                rffi.free_charp(domain_c)
-                rffi.free_charp(dir_c)
+            with rffi.scoped_view_charp(domain) as domain_c:
+                with rffi.scoped_view_charp(dir) as dir_c:
+                    dirname = _bindtextdomain(domain_c, dir_c)
 
         if not dirname:
             errno = rposix.get_saved_errno()
@@ -340,20 +303,13 @@
 
             if space.is_w(w_codeset, space.w_None):
                 codeset = None
-                domain_c = rffi.str2charp(domain)
-                try:
+                with rffi.scoped_view_charp(domain) as domain_c:
                     result = _bind_textdomain_codeset(domain_c, codeset)
-                finally:
-                    rffi.free_charp(domain_c)
             else:
                 codeset = space.str_w(w_codeset)
-                domain_c = rffi.str2charp(domain)
-                codeset_c = rffi.str2charp(codeset)
-                try:
-                    result = _bind_textdomain_codeset(domain_c, codeset_c)
-                finally:
-                    rffi.free_charp(domain_c)
-                    rffi.free_charp(codeset_c)
+                with rffi.scoped_view_charp(domain) as domain_c:
+                    with rffi.scoped_view_charp(codeset) as codeset_c:
+                        result = _bind_textdomain_codeset(domain_c, codeset_c)
 
             if not result:
                 return space.w_None
diff --git a/pypy/module/_multiprocessing/interp_connection.py 
b/pypy/module/_multiprocessing/interp_connection.py
--- a/pypy/module/_multiprocessing/interp_connection.py
+++ b/pypy/module/_multiprocessing/interp_connection.py
@@ -401,21 +401,20 @@
             _WriteFile, ERROR_NO_SYSTEM_RESOURCES)
         from rpython.rlib import rwin32
 
-        charp = rffi.str2charp(buf)
-        written_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1,
-                                    flavor='raw')
-        try:
-            result = _WriteFile(
-                self.handle, rffi.ptradd(charp, offset),
-                size, written_ptr, rffi.NULL)
+        with rffi.scoped_view_charp(buf) as charp:
+            written_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1,
+                                        flavor='raw')
+            try:
+                result = _WriteFile(
+                    self.handle, rffi.ptradd(charp, offset),
+                    size, written_ptr, rffi.NULL)
 
-            if (result == 0 and
-                rwin32.GetLastError_saved() == ERROR_NO_SYSTEM_RESOURCES):
-                raise oefmt(space.w_ValueError,
-                            "Cannot send %d bytes over connection", size)
-        finally:
-            rffi.free_charp(charp)
-            lltype.free(written_ptr, flavor='raw')
+                if (result == 0 and
+                    rwin32.GetLastError_saved() == ERROR_NO_SYSTEM_RESOURCES):
+                    raise oefmt(space.w_ValueError,
+                                "Cannot send %d bytes over connection", size)
+            finally:
+                lltype.free(written_ptr, flavor='raw')
 
     def do_recv_string(self, space, buflength, maxlength):
         from pypy.module._multiprocessing.interp_win32 import (
diff --git a/pypy/module/_pypyjson/interp_decoder.py 
b/pypy/module/_pypyjson/interp_decoder.py
--- a/pypy/module/_pypyjson/interp_decoder.py
+++ b/pypy/module/_pypyjson/interp_decoder.py
@@ -52,13 +52,13 @@
         # 1) we automatically get the '\0' sentinel at the end of the string,
         #    which means that we never have to check for the "end of string"
         # 2) we can pass the buffer directly to strtod
-        self.ll_chars = rffi.str2charp(s)
+        self.ll_chars, self.buf_flag = rffi.get_nonmovingbuffer_final_null(s)
         self.end_ptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
         self.pos = 0
         self.last_type = TYPE_UNKNOWN
 
     def close(self):
-        rffi.free_charp(self.ll_chars)
+        rffi.free_nonmovingbuffer(self.s, self.ll_chars, self.buf_flag)
         lltype.free(self.end_ptr, flavor='raw')
 
     def getslice(self, start, end):
diff --git a/pypy/module/_ssl/interp_ssl.py b/pypy/module/_ssl/interp_ssl.py
--- a/pypy/module/_ssl/interp_ssl.py
+++ b/pypy/module/_ssl/interp_ssl.py
@@ -228,7 +228,7 @@
 
         Mix string into the OpenSSL PRNG state.  entropy (a float) is a lower
         bound on the entropy contained in string."""
-        with rffi.scoped_str2charp(string) as buf:
+        with rffi.get_nonmovingbuffer(string) as buf:
             libssl_RAND_add(buf, len(string), entropy)
 
     def RAND_status(space):
diff --git a/pypy/module/cppyy/capi/builtin_capi.py 
b/pypy/module/cppyy/capi/builtin_capi.py
--- a/pypy/module/cppyy/capi/builtin_capi.py
+++ b/pypy/module/cppyy/capi/builtin_capi.py
@@ -537,9 +537,8 @@
     releasegil=ts_helper,
     compilation_info=backend.eci)
 def c_charp2stdstring(space, svalue):
-    charp = rffi.str2charp(svalue)
-    result = _c_charp2stdstring(charp)
-    rffi.free_charp(charp)
+    with rffi.scoped_view_charp(svalue) as charp:
+        result = _c_charp2stdstring(charp)
     return result
 _c_stdstring2stdstring = rffi.llexternal(
     "cppyy_stdstring2stdstring",
diff --git a/pypy/module/cppyy/capi/cint_capi.py 
b/pypy/module/cppyy/capi/cint_capi.py
--- a/pypy/module/cppyy/capi/cint_capi.py
+++ b/pypy/module/cppyy/capi/cint_capi.py
@@ -82,9 +82,8 @@
     releasegil=ts_helper,
     compilation_info=eci)
 def c_charp2TString(space, svalue):
-    charp = rffi.str2charp(svalue)
-    result = _c_charp2TString(charp)
-    rffi.free_charp(charp)
+    with rffi.scoped_view_charp(svalue) as charp:
+        result = _c_charp2TString(charp)
     return result
 _c_TString2TString = rffi.llexternal(
     "cppyy_TString2TString",
diff --git a/pypy/module/cppyy/capi/loadable_capi.py 
b/pypy/module/cppyy/capi/loadable_capi.py
--- a/pypy/module/cppyy/capi/loadable_capi.py
+++ b/pypy/module/cppyy/capi/loadable_capi.py
@@ -65,6 +65,7 @@
                 else:    # only other use is sring
                     n = len(obj._string)
                     assert raw_string == rffi.cast(rffi.CCHARP, 0)
+                    # XXX could use rffi.get_nonmovingbuffer_final_null()
                     raw_string = rffi.str2charp(obj._string)
                     data = rffi.cast(rffi.CCHARPP, data)
                     data[0] = raw_string
diff --git a/pypy/module/cpyext/bytesobject.py 
b/pypy/module/cpyext/bytesobject.py
--- a/pypy/module/cpyext/bytesobject.py
+++ b/pypy/module/cpyext/bytesobject.py
@@ -96,7 +96,8 @@
         raise oefmt(space.w_ValueError,
             "bytes_attach called on object with ob_size %d but trying to store 
%d",
             py_str.c_ob_size, len(s))
-    rffi.c_memcpy(py_str.c_ob_sval, rffi.str2charp(s), len(s))
+    with rffi.scoped_nonmovingbuffer(s) as s_ptr:
+        rffi.c_memcpy(py_str.c_ob_sval, s_ptr, len(s))
     py_str.c_ob_sval[len(s)] = '\0'
     py_str.c_ob_shash = space.hash_w(w_obj)
     py_str.c_ob_sstate = rffi.cast(rffi.INT, 1) # SSTATE_INTERNED_MORTAL
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to