Author: Ronan Lamy <[email protected]>
Branch: PyBuffer
Changeset: r91105:2a6cbf8c6e27
Date: 2017-04-21 03:10 +0100
http://bitbucket.org/pypy/pypy/changeset/2a6cbf8c6e27/

Log:    Merge space.getarg_w('y#') and space.charbuf_w()

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1488,13 +1488,10 @@
 
     def charbuf_w(self, w_obj):
         # Old buffer interface, returns a character buffer 
(PyObject_AsCharBuffer)
-        try:
-            buf = w_obj.buffer_w(self, self.BUF_SIMPLE)
-        except BufferInterfaceNotFound:
-            raise oefmt(self.w_TypeError,
-                        "expected an object with a buffer interface")
+        if self.isinstance_w(w_obj, self.w_bytes):  # XXX: is this shortcut 
useful?
+            return w_obj.bytes_w(self)
         else:
-            return buf.as_str()
+            return self.readbuf_w(w_obj).as_str()
 
     def _getarg_error(self, expected, w_obj):
         if self.is_none(w_obj):
@@ -1539,12 +1536,7 @@
         elif code == 'y*':
             return self.readbuf_w(w_obj)
         elif code == 'y#':
-            if self.isinstance_w(w_obj, self.w_bytes):
-                return w_obj.bytes_w(self)
-            try:
-                return w_obj.buffer_w(self, self.BUF_SIMPLE).as_str()
-            except BufferInterfaceNotFound:
-                self._getarg_error("bytes-like object", w_obj)
+            return self.charbuf_w(w_obj)
         else:
             assert False
 
diff --git a/pypy/module/_codecs/interp_codecs.py 
b/pypy/module/_codecs/interp_codecs.py
--- a/pypy/module/_codecs/interp_codecs.py
+++ b/pypy/module/_codecs/interp_codecs.py
@@ -955,7 +955,7 @@
     if space.isinstance_w(w_string, space.w_unicode):
         return space.newtuple([w_string, space.len(w_string)])
 
-    string = space.readbuf_w(w_string).as_str()
+    string = space.charbuf_w(w_string)
     space.warn(space.newtext("unicode_internal codec has been deprecated"),
                space.w_DeprecationWarning)
 
@@ -983,7 +983,7 @@
         return space.newtuple([space.newbytes(result), space.newint(len(uni))])
     else:
         # special case for this codec: bytes are returned as is
-        string = space.readbuf_w(w_uni).as_str()
+        string = space.charbuf_w(w_uni)
         return space.newtuple([space.newbytes(string), 
space.newint(len(string))])
 
 # ____________________________________________________________
diff --git a/pypy/module/_io/interp_bufferedio.py 
b/pypy/module/_io/interp_bufferedio.py
--- a/pypy/module/_io/interp_bufferedio.py
+++ b/pypy/module/_io/interp_bufferedio.py
@@ -769,7 +769,7 @@
 
     def write_w(self, space, w_data):
         self._check_closed(space, "write to closed file")
-        data = space.readbuf_w(w_data).as_str()
+        data = space.charbuf_w(w_data)
         size = len(data)
 
         with self.lock:
diff --git a/pypy/module/_io/interp_fileio.py b/pypy/module/_io/interp_fileio.py
--- a/pypy/module/_io/interp_fileio.py
+++ b/pypy/module/_io/interp_fileio.py
@@ -410,7 +410,7 @@
     def write_w(self, space, w_data):
         self._check_closed(space)
         self._check_writable(space)
-        data = space.readbuf_w(w_data).as_str()
+        data = space.charbuf_w(w_data)
 
         while True:
             try:
diff --git a/pypy/module/array/interp_array.py 
b/pypy/module/array/interp_array.py
--- a/pypy/module/array/interp_array.py
+++ b/pypy/module/array/interp_array.py
@@ -402,8 +402,7 @@
         machine values, as if it had been read from a file using the
         fromfile() method).
         """
-        buf = space.readbuf_w(w_s)
-        s = buf.as_str()
+        s = space.charbuf_w(w_s)
         self._frombytes(space, s)
 
     def _frombytes(self, space, s):
diff --git a/pypy/module/mmap/interp_mmap.py b/pypy/module/mmap/interp_mmap.py
--- a/pypy/module/mmap/interp_mmap.py
+++ b/pypy/module/mmap/interp_mmap.py
@@ -52,7 +52,7 @@
     def find(self, w_tofind, w_start=None, w_end=None):
         self.check_valid()
         space = self.space
-        tofind = space.getarg_w('y#', w_tofind)
+        tofind = space.charbuf_w(w_tofind)
         if w_start is None:
             start = self.mmap.pos
         else:
@@ -66,7 +66,7 @@
     def rfind(self, w_tofind, w_start=None, w_end=None):
         self.check_valid()
         space = self.space
-        tofind = space.getarg_w('y#', w_tofind)
+        tofind = space.charbuf_w(w_tofind)
         if w_start is None:
             start = self.mmap.pos
         else:
@@ -98,7 +98,7 @@
 
     def write(self, w_data):
         self.check_valid()
-        data = self.space.getarg_w('y#', w_data)
+        data = self.space.charbuf_w(w_data)
         self.check_writeable()
         try:
             self.mmap.write(data)
diff --git a/pypy/module/posix/interp_posix.py 
b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -285,10 +285,10 @@
 def write(space, fd, w_data):
     """Write a string to a file descriptor.  Return the number of bytes
 actually written, which may be smaller than len(data)."""
-    data = space.readbuf_w(w_data)
+    data = space.charbuf_w(w_data)
     while True:
         try:
-            res = os.write(fd, data.as_str())
+            res = os.write(fd, data)
         except OSError as e:
             wrap_oserror(space, e, eintr_retry=True)
         else:
@@ -392,10 +392,10 @@
 def pwrite(space, fd, w_data, offset):
     """Write a string to a file descriptor at a given offset.
     """
-    data = space.readbuf_w(w_data)
+    data = space.charbuf_w(w_data)
     while True:
         try:
-            res = rposix.pwrite(fd, data.as_str(), offset)
+            res = rposix.pwrite(fd, data, offset)
         except OSError as e:
             wrap_oserror(space, e, eintr_retry=True)
         else:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to