Commits

Philip Jenvey committed 8dabe46

provide a listview_int for bytes

  • Participants
  • Parent commits f44626e
  • Branches py3k-fix-strategies

Comments (0)

Files changed (4)

pypy/objspace/std/bytesobject.py

     def buffer_w(w_self, space):
         return StringBuffer(w_self._value)
 
-    # XXX: could provide listview_int?
-    #def listview_bytes(self):
-    #    return _create_list_from_bytes(self._value)
+    def listview_int(self):
+        return _create_list_from_bytes(self._value)
 
     def ord(self, space):
         if len(self._value) != 1:
 
 def _create_list_from_bytes(value):
     # need this helper function to allow the jit to look inside and inline
-    # listview_bytes
-    return [s for s in value]
+    # listview_int
+    return [ord(s) for s in value]
 
 W_BytesObject.EMPTY = W_BytesObject('')
 W_BytesObject.PREBUILT = [W_BytesObject(chr(i)) for i in range(256)]

pypy/objspace/std/objspace.py

             return w_obj.listview_int()
         if type(w_obj) is W_SetObject or type(w_obj) is W_FrozensetObject:
             return w_obj.listview_int()
+        if type(w_obj) is W_BytesObject:
+            # Python3 considers bytes strings as a list of numbers.
+            return w_obj.listview_int()
         if isinstance(w_obj, W_ListObject) and self._uses_list_iter(w_obj):
             return w_obj.getitems_int()
         return None

pypy/objspace/std/test/test_bytesobject.py

         w_slice = space.newslice(w(1), w_None, w(2))
         assert self.space.eq_w(space.getitem(w_str, w_slice), wb('el'))
 
-    def test_listview_bytes(self):
+    def test_listview_bytes_int(self):
         w_bytes = self.space.wrapbytes('abcd')
         # list(b'abcd') is a list of numbers
         assert self.space.listview_bytes(w_bytes) == None
+        assert self.space.listview_int(w_bytes) == [97, 98, 99, 100]
 
 class AppTestBytesObject:
 

pypy/objspace/std/test/test_setobject.py

 
         w_a = W_SetObject(self.space)
         _initialize_set(self.space, w_a, wb("abcdefg"))
-        assert sorted(self.space.listview_bytes(w_a)) == list("abcdefg")
-        assert self.space.listview_int(w_a) is None
+        assert sorted(self.space.listview_int(w_a)) == [97, 98, 99, 100, 101, 102, 103]
+        assert self.space.listview_bytes(w_a) is None
 
         w_b = W_SetObject(self.space)
         _initialize_set(self.space, w_b, self.space.newlist([w(1),w(2),w(3),w(4),w(5)]))