[pypy-commit] pypy default: test, fix __rmod__ and -A incompatibilities with cpython on str, unicode
mattip
pypy.commits at gmail.com
Tue Oct 18 11:29:57 EDT 2016
Author: Matti Picus <matti.picus at gmail.com>
Branch:
Changeset: r87860:20aad7aa5de1
Date: 2016-10-18 18:12 +0300
http://bitbucket.org/pypy/pypy/changeset/20aad7aa5de1/
Log: test, fix __rmod__ and -A incompatibilities with cpython on str,
unicode
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
@@ -605,6 +605,9 @@
def descr_mod(self, space, w_values):
return mod_format(space, self, w_values, do_unicode=False)
+ def descr_rmod(self, space, w_values):
+ return mod_format(space, w_values, self, do_unicode=False)
+
def descr_eq(self, space, w_other):
if space.config.objspace.std.withstrbuf:
from pypy.objspace.std.strbufobject import W_StringBufferObject
@@ -937,6 +940,7 @@
format = interpindirect2app(W_BytesObject.descr_format),
__format__ = interpindirect2app(W_BytesObject.descr__format__),
__mod__ = interpindirect2app(W_BytesObject.descr_mod),
+ __rmod__ = interpindirect2app(W_BytesObject.descr_rmod),
__getnewargs__ = interpindirect2app(
W_AbstractBytesObject.descr_getnewargs),
_formatter_parser = interp2app(W_BytesObject.descr_formatter_parser),
diff --git a/pypy/objspace/std/stringmethods.py b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -649,10 +649,10 @@
return self._starts_ends_overflow(prefix)
return endswith(value, prefix, start, end)
- def _strip(self, space, w_chars, left, right):
+ def _strip(self, space, w_chars, left, right, name='strip'):
"internal function called by str_xstrip methods"
value = self._val(space)
- chars = self._op_val(space, w_chars, strict='strip')
+ chars = self._op_val(space, w_chars, strict=name)
lpos = 0
rpos = len(value)
@@ -689,17 +689,17 @@
def descr_strip(self, space, w_chars=None):
if space.is_none(w_chars):
return self._strip_none(space, left=1, right=1)
- return self._strip(space, w_chars, left=1, right=1)
+ return self._strip(space, w_chars, left=1, right=1, name='strip')
def descr_lstrip(self, space, w_chars=None):
if space.is_none(w_chars):
return self._strip_none(space, left=1, right=0)
- return self._strip(space, w_chars, left=1, right=0)
+ return self._strip(space, w_chars, left=1, right=0, name='lstrip')
def descr_rstrip(self, space, w_chars=None):
if space.is_none(w_chars):
return self._strip_none(space, left=0, right=1)
- return self._strip(space, w_chars, left=0, right=1)
+ return self._strip(space, w_chars, left=0, right=1, name='rstrip')
def descr_swapcase(self, space):
selfvalue = self._val(space)
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
@@ -92,6 +92,7 @@
raises(ValueError, 'a%Zb'.__mod__, ((23,),))
def test_format(self):
+ import sys
raises(TypeError, "foo".__mod__, "bar")
raises(TypeError, u"foo".__mod__, "bar")
raises(TypeError, "foo".__mod__, u"bar")
@@ -105,6 +106,22 @@
assert result == "a foo b"
assert isinstance(result, cls)
+ for format, arg, cls in [("a %s b", "foo", str),
+ (u"a %s b", u"foo", unicode)]:
+ raises(TypeError, arg.__rmod__, format[:2])
+ result = arg.__rmod__(format)
+ assert result == "a foo b"
+ assert isinstance(result, cls)
+ for format, arg, cls in [(u"a %s b", "foo", str),
+ ("a %s b", u"foo", unicode)]:
+ result = arg.__rmod__(format)
+ if '__pypy__' in sys.builtin_module_names:
+ raises(TypeError, arg.__rmod__, format[:2])
+ assert result == "a foo b"
+ assert isinstance(result, cls)
+ else:
+ assert result is NotImplemented
+
def test_format_c_overflow(self):
raises(OverflowError, b'{0:c}'.format, -1)
raises(OverflowError, b'{0:c}'.format, 256)
@@ -114,6 +131,7 @@
exc_info = raises(TypeError, int_format.__mod__, '123')
expected = int_format + ' format: a number is required, not str'
assert str(exc_info.value) == expected
+ raises(TypeError, "None % 'abc'") # __rmod__
def test_split(self):
assert b"".split() == []
@@ -258,9 +276,9 @@
exc = raises(TypeError, s.strip, buffer(' '))
assert str(exc.value) == 'strip arg must be None, str or unicode'
exc = raises(TypeError, s.rstrip, buffer(' '))
- assert str(exc.value) == 'strip arg must be None, str or unicode'
+ assert str(exc.value) == 'rstrip arg must be None, str or unicode'
exc = raises(TypeError, s.lstrip, buffer(' '))
- assert str(exc.value) == 'strip arg must be None, str or unicode'
+ assert str(exc.value) == 'lstrip arg must be None, str or unicode'
def test_zfill(self):
assert b'123'.zfill(2) == b'123'
@@ -809,6 +827,10 @@
raises(TypeError, len, iter(iterable))
def test___radd__(self):
+ raises(TypeError, "None + ''")
+ raises(AttributeError, "'abc'.__radd__('def')")
+
+
class Foo(object):
def __radd__(self, other):
return 42
diff --git a/pypy/objspace/std/test/test_unicodeobject.py b/pypy/objspace/std/test/test_unicodeobject.py
--- a/pypy/objspace/std/test/test_unicodeobject.py
+++ b/pypy/objspace/std/test/test_unicodeobject.py
@@ -335,9 +335,9 @@
exc = raises(TypeError, s.strip, buffer(' '))
assert str(exc.value) == 'strip arg must be None, unicode or str'
exc = raises(TypeError, s.rstrip, buffer(' '))
- assert str(exc.value) == 'strip arg must be None, unicode or str'
+ assert str(exc.value) == 'rstrip arg must be None, unicode or str'
exc = raises(TypeError, s.lstrip, buffer(' '))
- assert str(exc.value) == 'strip arg must be None, unicode or str'
+ assert str(exc.value) == 'lstrip arg must be None, unicode or str'
def test_strip_str_unicode(self):
x = "--abc--".strip(u"-")
@@ -748,7 +748,9 @@
assert 'abc'.__add__(u'def') == u'abcdef'
assert u'abc'.__add__(u'def') == u'abcdef'
assert u'abc'.__add__('def') == u'abcdef'
- # xxx CPython has no str.__radd__ and no unicode.__radd__
+ assert u'abc'.__rmod__(u'%s') == u'abc'
+ ret = u'abc'.__rmod__('%s')
+ raises(AttributeError, "u'abc'.__radd__(u'def')")
def test_str_unicode_concat_overrides(self):
"Test from Jython about being bug-compatible with CPython."
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -319,6 +319,9 @@
def descr_mod(self, space, w_values):
return mod_format(space, self, w_values, do_unicode=True)
+ def descr_rmod(self, space, w_values):
+ return mod_format(space, w_values, self, do_unicode=True)
+
def descr_translate(self, space, w_table):
selfvalue = self._value
w_sys = space.getbuiltinmodule('sys')
@@ -633,6 +636,9 @@
def __mod__():
"""x.__mod__(y) <==> x%y"""
+ def __rmod__():
+ """x.__rmod__(y) <==> y%x"""
+
def __mul__():
"""x.__mul__(n) <==> x*n"""
@@ -1096,6 +1102,8 @@
doc=UnicodeDocstrings.__format__.__doc__),
__mod__ = interp2app(W_UnicodeObject.descr_mod,
doc=UnicodeDocstrings.__mod__.__doc__),
+ __rmod__ = interp2app(W_UnicodeObject.descr_rmod,
+ doc=UnicodeDocstrings.__rmod__.__doc__),
__getnewargs__ = interp2app(W_UnicodeObject.descr_getnewargs,
doc=UnicodeDocstrings.__getnewargs__.__doc__),
_formatter_parser = interp2app(W_UnicodeObject.descr_formatter_parser),
More information about the pypy-commit
mailing list