Author: Richard Plangger <[email protected]>
Branch: py3.5-byteformat
Changeset: r86773:0f2109d50a32
Date: 2016-08-31 11:44 +0200
http://bitbucket.org/pypy/pypy/changeset/0f2109d50a32/

Log:    added descr_mod to W_BytearrayObject + tests, already passing

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
@@ -21,6 +21,7 @@
 from pypy.objspace.std.stringmethods import StringMethods, _get_buffer
 from pypy.objspace.std.bytesobject import W_BytesObject
 from pypy.objspace.std.util import get_positive_index
+from pypy.objspace.std.formatting import mod_format, FORMAT_BYTEARRAY
 
 NON_HEX_MSG = "non-hexadecimal number found in fromhex() arg at position %d"
 
@@ -446,6 +447,9 @@
     def descr_hex(self, space):
         return _array_to_hexstring(space, self.data, len(self.data), True)
 
+    def descr_mod(self, space, w_values):
+        return mod_format(space, self, w_values, fmt_type=FORMAT_BYTEARRAY)
+
     @staticmethod
     def _iter_getitem_result(self, space, index):
         assert isinstance(self, W_BytearrayObject)
@@ -599,6 +603,9 @@
     def __mul__():
         """x.__mul__(n) <==> x*n"""
 
+    def __mod__():
+        """x.__mod__(y) <==> x % y"""
+
     def __ne__():
         """x.__ne__(y) <==> x!=y"""
 
@@ -1099,6 +1106,8 @@
                              doc=BytearrayDocstrings.__setitem__.__doc__),
     __delitem__ = interp2app(W_BytearrayObject.descr_delitem,
                              doc=BytearrayDocstrings.__delitem__.__doc__),
+    __mod__ = interp2app(W_BytearrayObject.descr_mod,
+                           doc=BytearrayDocstrings.__mod__.__doc__),
 
     append = interp2app(W_BytearrayObject.descr_append,
                         doc=BytearrayDocstrings.append.__doc__),
diff --git a/pypy/objspace/std/formatting.py b/pypy/objspace/std/formatting.py
--- a/pypy/objspace/std/formatting.py
+++ b/pypy/objspace/std/formatting.py
@@ -527,7 +527,10 @@
 def format(space, w_fmt, values_w, w_valuedict, fmt_type):
     "Entry point"
     if fmt_type != FORMAT_UNICODE:
-        fmt = space.str_w(w_fmt)
+        if fmt_type == FORMAT_BYTEARRAY:
+            fmt = w_fmt.buffer_w(space, 0).as_str()
+        else:
+            fmt = space.str_w(w_fmt)
         formatter = StringFormatter(space, fmt, values_w, w_valuedict)
         try:
             result = formatter.format()
@@ -538,7 +541,7 @@
             if fmt_type == FORMAT_BYTES:
                 return space.newbytes(result)
             elif fmt_type == FORMAT_BYTEARRAY:
-                return space.newbytearray(result)
+                return space.newbytearray([c for c in result])
             return space.wrap(result)
     fmt = space.unicode_w(w_fmt)
     formatter = UnicodeFormatter(space, fmt, values_w, w_valuedict)
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py 
b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -1,6 +1,7 @@
+# coding: utf-8
+
 from pypy import conftest
 
-
 class AppTestBytesArray:
     def setup_class(cls):
         cls.w_runappdirect = cls.space.wrap(conftest.option.runappdirect)
@@ -527,3 +528,20 @@
     def test_hex(self):
         assert bytearray(b'santa claus').hex() == "73616e746120636c617573"
 
+    def test_format(self):
+        """
+        assert bytearray(b'a%db') % 2 == b'a2b'
+        assert bytearray(b'00%.2f').__mod__((0.01234,)) == b'000.01'
+        assert bytearray(b'%04X') % 10 == b'000A'
+        assert bytearray(b'%c') % 48 == b'0'
+        assert bytearray(b'%c') % b'a' == b'a'
+        """
+
+    def test_format_b(self):
+        """
+        assert bytearray(b'%b') % b'abc' == b'abc'
+        assert bytearray(b'%b') % u'&#12399;&#12356;'.encode('utf-8') == 
u'&#12399;&#12356;'.encode('utf-8')
+        raises(TypeError, 'bytearray(b"%b") % 3.14')
+        raises(TypeError, 'bytearray(b"%b") % "hello world"')
+        assert bytearray(b'%b %b') % (b'a', bytearray(b'f f e')) == b'a f f e'
+        """
diff --git a/pypy/objspace/std/test/test_bytesobject.py 
b/pypy/objspace/std/test/test_bytesobject.py
--- a/pypy/objspace/std/test/test_bytesobject.py
+++ b/pypy/objspace/std/test/test_bytesobject.py
@@ -891,4 +891,3 @@
         raises(TypeError, 'b"%b" % "hello world"')
         assert b'%b %b' % (b'a', bytearray(b'f f e')) == b'a f f e'
         """
-
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to