1. Pypy
  2. Untitled project
  3. pypy

Commits

Simon Cross  committed 98e3ba2

Remove bytearray.__repr__ multimethod (fijal, hodgestar).

  • Participants
  • Parent commits dece801
  • Branches remove-string-smm

Comments (0)

Files changed (3)

File pypy/objspace/std/bytearrayobject.py

View file
 from pypy.objspace.std.bytearraytype import (
     makebytearraydata_w, new_bytearray)
 from pypy.objspace.std.bytearraytype import W_AbstractBytearrayObject
+from pypy.objspace.std.stringobject import string_escape_encode
 from pypy.objspace.std.unicodeobject import W_UnicodeObject  # XXX: kill this whem SMMs are dead
 from pypy.objspace.std.noneobject import W_NoneObject  # XXX: and this one.
 from pypy.objspace.std.stringobject import W_StringObject  # XXX: and this too.
             chars = space.bufferstr_new_w(w_chars)
         return _strip(space, self, chars, 0, 1)
 
+    def descr_repr(self, space):
+        s = "".join(self.data)
+        quote = "'"
+        if quote in s and '"' not in s:
+            quote = '"'
+
+        return space.wrap("bytearray(b" + string_escape_encode(s, quote) + ")")
+
     def __repr__(w_self):
         """ representation for debugging purposes """
         return "%s(%s)" % (w_self.__class__.__name__, ''.join(w_self.data))
                                                        w_table, w_deletechars)
     return String2Bytearray(space, w_res)
 
-# Mostly copied from repr__String, but without the "smart quote"
-# functionality.
-def repr__Bytearray(space, w_bytearray):
-    s = w_bytearray.data
-
-    # Good default if there are no replacements.
-    buf = StringBuilder(len("bytearray(b'')") + len(s))
-
-    buf.append("bytearray(b'")
-
-    for i in range(len(s)):
-        c = s[i]
-
-        if c == '\\' or c == "'":
-            buf.append('\\')
-            buf.append(c)
-        elif c == '\t':
-            buf.append('\\t')
-        elif c == '\r':
-            buf.append('\\r')
-        elif c == '\n':
-            buf.append('\\n')
-        elif not '\x20' <= c < '\x7f':
-            n = ord(c)
-            buf.append('\\x')
-            buf.append("0123456789abcdef"[n>>4])
-            buf.append("0123456789abcdef"[n&0xF])
-        else:
-            buf.append(c)
-
-    buf.append("')")
-
-    return space.wrap(buf.build())
-
 def str__Bytearray(space, w_bytearray):
     return space.wrap(''.join(w_bytearray.data))
 

File pypy/objspace/std/bytearraytype.py

View file
             space, w_encoding, w_errors)
         return decode_object(space, self, encoding, errors)
 
+    def descr_repr(self, space):
+        """x.__repr__() <==> repr(x)"""
+        raise NotImplementedError
+
 
 def getbytevalue(space, w_value):
     if space.isinstance_w(w_value, space.w_str):
     __new__ = interp2app(descr__new__),
     __hash__ = None,
     __reduce__ = interp2app(descr_bytearray__reduce__),
+    __repr__=interpindirect2app(W_AbstractBytearrayObject.descr_repr),
     fromhex = interp2app(descr_fromhex, as_classmethod=True),
     ljust=interpindirect2app(W_AbstractBytearrayObject.descr_ljust),
     rjust=interpindirect2app(W_AbstractBytearrayObject.descr_rjust),

File pypy/objspace/std/test/test_bytearrayobject.py

View file
     def test_repr(self):
         assert repr(bytearray()) == "bytearray(b'')"
         assert repr(bytearray('test')) == "bytearray(b'test')"
-        assert repr(bytearray("d'oh")) == r"bytearray(b'd\'oh')"
+        # CPython 2.7.3 produces a different repr for the test below,
+        # namely: 'bytearray(b"d\\\'oh")'
+        assert repr(bytearray("d'oh")) == 'bytearray(b"d\'oh")'
 
     def test_str(self):
         assert str(bytearray()) == ""