Author: Philip Jenvey <[email protected]>
Branch: operrfmt-NT
Changeset: r64552:109b2c860963
Date: 2013-05-24 16:50 -0700
http://bitbucket.org/pypy/pypy/changeset/109b2c860963/
Log: add %N to return .getname(space)
diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py
--- a/pypy/interpreter/error.py
+++ b/pypy/interpreter/error.py
@@ -306,7 +306,7 @@
_fmtcache = {}
_fmtcache2 = {}
-_FMTS = tuple('sdT')
+_FMTS = tuple('sdNT')
def decompose_valuefmt(valuefmt):
"""Returns a tuple of string parts extracted from valuefmt,
@@ -363,9 +363,11 @@
string = self.xstrings[i]
value = getattr(self, attr)
lst[i+i] = string
- if fmt == 'T':
+ if fmt in 'NT':
space = self.w_type.space
- lst[i+i+1] = space.type(value).getname(space)
+ if fmt == 'T':
+ value = space.type(value)
+ lst[i+i+1] = value.getname(space)
else:
lst[i+i+1] = str(value)
lst[-1] = self.xstrings[-1]
@@ -385,7 +387,14 @@
def operationerrfmt(w_type, valuefmt, *args):
"""Equivalent to OperationError(w_type, space.wrap(valuefmt % args)).
More efficient in the (common) case where the value is not actually
- needed."""
+ needed.
+
+ Also supports the following extra format characters:
+
+ %N - The result of arg.getname(space)
+ %T - The result of space.type(arg).getname(space)
+
+ """
OpErrFmt, strings = get_operationerr_class(valuefmt)
return OpErrFmt(w_type, strings, *args)
operationerrfmt._annspecialcase_ = 'specialize:arg(1)'
diff --git a/pypy/interpreter/test/test_error.py
b/pypy/interpreter/test/test_error.py
--- a/pypy/interpreter/test/test_error.py
+++ b/pypy/interpreter/test/test_error.py
@@ -40,6 +40,16 @@
space.wrap('foo'), 'foo')
assert operr._compute_value() == "'str' object has no attribute 'foo'"
+def test_operationerrfmt_N(space):
+ operr = operationerrfmt(space.w_AttributeError,
+ "'%N' object has no attribute '%s'",
+ space.type(space.wrap('foo')), 'foo')
+ assert operr._compute_value() == "'str' object has no attribute 'foo'"
+ operr = operationerrfmt(space.w_AttributeError,
+ "'%N' object has no attribute '%s'",
+ space.wrap('foo'), 'foo')
+ assert operr._compute_value() == "'?' object has no attribute 'foo'"
+
def test_operationerrfmt_empty():
py.test.raises(AssertionError, operationerrfmt, "w_type", "foobar")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit