Author: Ronan Lamy <[email protected]>
Branch: PyBuffer
Changeset: r91035:01ef9fd9fb98
Date: 2017-04-10 18:36 +0100
http://bitbucket.org/pypy/pypy/changeset/01ef9fd9fb98/

Log:    Move getbytevalue() to space.byte_w() (to avoid local import hacks)

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1633,6 +1633,18 @@
     def fsencode_or_none_w(self, w_obj):
         return None if self.is_none(w_obj) else self.fsencode_w(w_obj)
 
+    def byte_w(self, w_obj):
+        """
+        Convert an index-like object to an interp-level char
+
+        Used for app-level code like "bytearray(b'abc')[0] = 42".
+        """
+        value = self.getindex_w(w_obj, None)
+        if not 0 <= value < 256:
+            # this includes the OverflowError in case the long is too large
+            raise oefmt(self.w_ValueError, "byte must be in range(0, 256)")
+        return chr(value)
+
     def int_w(self, w_obj, allow_conversion=True):
         """
         Unwrap an app-level int object into an interpret-level int.
diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -93,12 +93,10 @@
             return fmtiter.result_w[0]
 
     def setitem_w(self, space, idx, w_obj):
-        from pypy.objspace.std.bytesobject import getbytevalue
         from pypy.module.struct.formatiterator import PackFormatIterator
         itemsize = self.getitemsize()
         if itemsize == 1:
-            ch = getbytevalue(space, w_obj)
-            self.as_binary()[idx] = ch
+            self.as_binary()[idx] = space.byte_w(w_obj)
         else:
             # TODO: this probably isn't very fast
             fmtiter = PackFormatIterator(space, [w_obj], itemsize)
@@ -152,9 +150,7 @@
         return space.newint(ord(ch))
 
     def setitem_w(self, space, idx, w_obj):
-        from pypy.objspace.std.bytesobject import getbytevalue
-        ch = getbytevalue(space, w_obj)
-        self.data[idx] = ch
+        self.data[idx] = space.byte_w(w_obj)
 
 
 class BinaryBuffer(object):
diff --git a/pypy/module/_rawffi/interp_rawffi.py 
b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -5,7 +5,6 @@
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.typedef import interp_attrproperty
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
-from pypy.objspace.std.bytesobject import getbytevalue
 
 from rpython.rlib.clibffi import *
 from rpython.rlib.objectmodel import we_are_translated
@@ -410,7 +409,7 @@
                                               space.float_w(w_arg)))
     elif letter == "c":
         if space.isinstance_w(w_arg, space.w_int):
-            val = getbytevalue(space, w_arg)
+            val = space.byte_w(w_arg)
         else:
             s = space.bytes_w(w_arg)
             if len(s) != 1:
diff --git a/pypy/objspace/std/bytearrayobject.py 
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -13,8 +13,7 @@
 
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.error import OperationError, oefmt
-from pypy.objspace.std.bytesobject import (
-    getbytevalue, makebytesdata_w, newbytesdata_w)
+from pypy.objspace.std.bytesobject import makebytesdata_w, newbytesdata_w
 from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec
 from pypy.interpreter.typedef import TypeDef
 from pypy.interpreter.buffer import SimpleBuffer, BinaryBuffer
@@ -392,7 +391,7 @@
                                   slicelength, sequence2, empty_elem='\x00')
         else:
             idx = space.getindex_w(w_index, space.w_IndexError, "bytearray")
-            newvalue = getbytevalue(space, w_other)
+            newvalue = space.byte_w(w_other)
             self._data[self._fixindex(space, idx)] = newvalue
 
     def descr_delitem(self, space, w_idx):
@@ -418,7 +417,7 @@
                              _shrink_after_delete_from_start, self)
 
     def descr_append(self, space, w_item):
-        self._data.append(getbytevalue(space, w_item))
+        self._data.append(space.byte_w(w_item))
 
     def descr_extend(self, space, w_other):
         if isinstance(w_other, W_BytearrayObject):
@@ -428,10 +427,8 @@
 
     def descr_insert(self, space, w_idx, w_other):
         where = space.int_w(w_idx)
-        val = getbytevalue(space, w_other)
-        data = self.getdata()
-        length = len(data)
-        index = get_positive_index(where, length)
+        index = get_positive_index(where, len(self.getdata()))
+        val = space.byte_w(w_other)
         data.insert(index, val)
 
     @unwrap_spec(w_idx=WrappedDefault(-1))
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -542,7 +542,7 @@
                 length = space.len_w(w_source)
                 if jit.isconstant(length) and length == 1:
                     w_item = space.getitem(w_source, space.newint(0))
-                    value = getbytevalue(space, w_item)
+                    value = space.byte_w(w_item)
                     return W_BytesObject(value)
             else:
                 # special-case 'bytes(X)' if X has a __bytes__() method:
@@ -702,13 +702,6 @@
 W_BytesObject.EMPTY = W_BytesObject('')
 
 
-def getbytevalue(space, w_value):
-    value = space.getindex_w(w_value, None)
-    if not 0 <= value < 256:
-        # this includes the OverflowError in case the long is too large
-        raise oefmt(space.w_ValueError, "byte must be in range(0, 256)")
-    return chr(value)
-
 def invoke_bytes_method(space, w_source):
     w_bytes_method = space.lookup(w_source, "__bytes__")
     if w_bytes_method is not None:
@@ -809,7 +802,7 @@
             if not e.match(space, space.w_StopIteration):
                 raise
             break
-        value = getbytevalue(space, w_item)
+        value = space.byte_w(w_item)
         builder.append(value)
     return builder.build()
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to