[pypy-commit] pypy default: support operationerrfmt sans fmt args (like plain OperationError) as a

pjenvey noreply at buildbot.pypy.org
Wed Jun 26 21:56:38 CEST 2013


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: 
Changeset: r65009:43ce1aa7ddcd
Date: 2013-06-26 12:35 -0700
http://bitbucket.org/pypy/pypy/changeset/43ce1aa7ddcd/

Log:	support operationerrfmt sans fmt args (like plain OperationError) as
	a convenience API

diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py
--- a/pypy/interpreter/error.py
+++ b/pypy/interpreter/error.py
@@ -29,12 +29,12 @@
     _application_traceback = None
 
     def __init__(self, w_type, w_value, tb=None):
-        assert w_type is not None
         self.setup(w_type)
         self._w_value = w_value
         self._application_traceback = tb
 
     def setup(self, w_type):
+        assert w_type is not None
         self.w_type = w_type
         if not we_are_translated():
             self.debug_excs = []
@@ -347,7 +347,6 @@
                 self.xstrings = strings
                 for i, _, attr in entries:
                     setattr(self, attr, args[i])
-                assert w_type is not None
 
             def _compute_value(self, space):
                 lst = [None] * (len(formats) + len(formats) + 1)
@@ -369,6 +368,18 @@
         _fmtcache2[formats] = OpErrFmt
     return OpErrFmt, strings
 
+class OpErrFmtNoArgs(OperationError):
+
+    def __init__(self, w_type, value):
+        self.setup(w_type)
+        self._value = value
+
+    def get_w_value(self, space):
+        w_value = self._w_value
+        if w_value is None:
+            self._w_value = w_value = space.wrap(self._value)
+        return w_value
+
 def get_operationerr_class(valuefmt):
     try:
         result = _fmtcache[valuefmt]
@@ -389,6 +400,8 @@
     %T - The result of space.type(w_arg).getname(space)
 
     """
+    if not len(args):
+        return OpErrFmtNoArgs(w_type, valuefmt)
     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
@@ -33,6 +33,14 @@
     operr3 = operationerrfmt("w_type2", "a %s b %s c", "bar", "4b")
     assert operr3.__class__ is not operr.__class__
 
+def test_operationerrfmt_noargs(space):
+    operr = operationerrfmt(space.w_AttributeError, "no attribute 'foo'")
+    operr.normalize_exception(space)
+    val = operr.get_w_value(space)
+    assert space.isinstance_w(val, space.w_AttributeError)
+    w_repr = space.repr(val)
+    assert space.str_w(w_repr) == "AttributeError(\"no attribute 'foo'\",)"
+
 def test_operationerrfmt_T(space):
     operr = operationerrfmt(space.w_AttributeError,
                             "'%T' object has no attribute '%s'",
diff --git a/pypy/objspace/std/dictmultiobject.py b/pypy/objspace/std/dictmultiobject.py
--- a/pypy/objspace/std/dictmultiobject.py
+++ b/pypy/objspace/std/dictmultiobject.py
@@ -225,8 +225,8 @@
             space.raise_key_error(w_key)
 
     def descr_reversed(self, space):
-        raise OperationError(space.w_TypeError, space.wrap(
-                'argument to reversed() must be a sequence'))
+        raise operationerrfmt(space.w_TypeError,
+                              'argument to reversed() must be a sequence')
 
     def descr_copy(self, space):
         """D.copy() -> a shallow copy of D"""


More information about the pypy-commit mailing list