Author: Philip Jenvey <pjen...@underboss.org> Branch: stdlib-2.7.11 Changeset: r83205:7f47c732437b Date: 2016-03-20 15:09 -0700 http://bitbucket.org/pypy/pypy/changeset/7f47c732437b/
Log: cpython issue7267: range check format(int, 'c') diff --git a/pypy/objspace/std/newformat.py b/pypy/objspace/std/newformat.py --- a/pypy/objspace/std/newformat.py +++ b/pypy/objspace/std/newformat.py @@ -771,6 +771,11 @@ msg = "sign not allowed with 'c' presentation type" raise OperationError(space.w_ValueError, space.wrap(msg)) value = space.int_w(w_num) + max_char = runicode.MAXUNICODE if self.is_unicode else 0xFF + if not (0 <= value <= max_char): + raise oefmt(space.w_OverflowError, + "%%c arg not in range(%s)", + hex(max_char)) if self.is_unicode: result = runicode.UNICHR(value) else: 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 @@ -103,6 +103,10 @@ assert result == "a foo b" assert isinstance(result, cls) + def test_format_c_overflow(self): + raises(OverflowError, b'{0:c}'.format, -1) + raises(OverflowError, b'{0:c}'.format, 256) + def test_split(self): assert "".split() == [] assert "".split('x') == [''] 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 @@ -947,6 +947,11 @@ assert repr("%s" % u) == "u'__unicode__ overridden'" assert repr("{}".format(u)) == "'__unicode__ overridden'" + def test_format_c_overflow(self): + import sys + raises(OverflowError, u'{0:c}'.format, -1) + raises(OverflowError, u'{0:c}'.format, sys.maxunicode + 1) + def test_replace_with_buffer(self): assert u'abc'.replace(buffer('b'), buffer('e')) == u'aec' assert u'abc'.replace(buffer('b'), u'e') == u'aec' _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit