Author: Carl Friedrich Bolz-Tereick <cfb...@gmx.de> Branch: py3.6 Changeset: r94707:032703549e15 Date: 2018-05-29 13:14 +0200 http://bitbucket.org/pypy/pypy/changeset/032703549e15/
Log: use the actual object types for the error message, and use CPython's arguably clearer message 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 @@ -581,9 +581,9 @@ else: return rstring.StringBuilder() - def _unknown_presentation(self, tp): + def _unknown_presentation(self, w_val): raise oefmt(self.space.w_ValueError, - "unknown presentation for %s: '%s'", tp, self._type) + "unknown format code %s for object of type '%T'", self._type, w_val) def format_string(self, w_string): space = self.space @@ -593,7 +593,7 @@ if self._parse_spec("s", "<"): return self.wrap(string) if self._type != "s": - self._unknown_presentation("string") + self._unknown_presentation(w_string) if self._sign != "\0": raise oefmt(space.w_ValueError, "Sign not allowed in string format specifier") @@ -955,7 +955,7 @@ w_float = space.float(w_num) return self._format_float(w_float) else: - self._unknown_presentation("int" if kind == INT_KIND else "long") + self._unknown_presentation(w_num) def _parse_number(self, s, i): """Determine if s has a decimal point, and the index of the first # @@ -1038,7 +1038,7 @@ tp == "n" or tp == "%"): return self._format_float(w_float) - self._unknown_presentation("float") + self._unknown_presentation(w_float) def _format_complex(self, w_complex): flags = 0 @@ -1197,7 +1197,7 @@ tp == "G" or tp == "n"): return self._format_complex(w_complex) - self._unknown_presentation("complex") + self._unknown_presentation(w_complex) return Formatter unicode_formatter = make_formatting_class(for_unicode=True) diff --git a/pypy/objspace/std/test/test_newformat.py b/pypy/objspace/std/test/test_newformat.py --- a/pypy/objspace/std/test/test_newformat.py +++ b/pypy/objspace/std/test/test_newformat.py @@ -480,3 +480,10 @@ assert isinstance(first, str) for x, y in l: assert isinstance(y, str) + + def test_unknown_presentation_error_message(self): + class x(int): + pass + + excinfo = raises(ValueError, "{:j}".format, x(1)) + assert str(excinfo.value) == "unknown format code j for object of type 'x'" _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit