Author: Simon Cross <[email protected]>
Branch: remove-string-smm
Changeset: r63439:98e3ba2a00d2
Date: 2013-04-17 00:01 +0200
http://bitbucket.org/pypy/pypy/changeset/98e3ba2a00d2/
Log: Remove bytearray.__repr__ multimethod (fijal, hodgestar).
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
@@ -12,6 +12,7 @@
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.
@@ -143,6 +144,14 @@
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))
@@ -360,40 +369,6 @@
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))
diff --git a/pypy/objspace/std/bytearraytype.py
b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -131,6 +131,10 @@
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):
@@ -252,6 +256,7 @@
__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),
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
@@ -49,7 +49,9 @@
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()) == ""
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit