[pypy-commit] pypy py3k: add the possibility to have unicode error messages with operrfmt

antocuni noreply at buildbot.pypy.org
Thu Aug 2 19:10:39 CEST 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r56536:81cd817ffe62
Date: 2012-08-02 18:10 +0200
http://bitbucket.org/pypy/pypy/changeset/81cd817ffe62/

Log:	add the possibility to have unicode error messages with operrfmt

diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py
--- a/pypy/interpreter/error.py
+++ b/pypy/interpreter/error.py
@@ -320,10 +320,12 @@
     return tuple(parts), tuple(formats)
 
 def get_operrcls2(valuefmt):
+    is_unicode = isinstance(valuefmt, unicode)
     strings, formats = decompose_valuefmt(valuefmt)
+    key = (is_unicode, formats)
     assert len(strings) == len(formats) + 1
     try:
-        OpErrFmt = _fmtcache2[formats]
+        OpErrFmt = _fmtcache2[key]
     except KeyError:
         from pypy.rlib.unroll import unrolling_iterable
         attrs = ['x%d' % i for i in range(len(formats))]
@@ -345,11 +347,17 @@
                     string = self.xstrings[i]
                     value = getattr(self, attr)
                     lst[i+i] = string
-                    lst[i+i+1] = str(value)
+                    if is_unicode:
+                        lst[i+i+1] = unicode(value)
+                    else:
+                        lst[i+i+1] = str(value)
                 lst[-1] = self.xstrings[-1]
-                return ''.join(lst)
+                if is_unicode:
+                    return u''.join(lst)
+                else:
+                    return ''.join(lst)
         #
-        _fmtcache2[formats] = OpErrFmt
+        _fmtcache2[key] = OpErrFmt
     return OpErrFmt, strings
 
 def get_operationerr_class(valuefmt):
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
@@ -1,3 +1,4 @@
+# -*- encoding: utf-8 -*-
 import py, os, errno
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.error import decompose_valuefmt, get_operrcls2
@@ -33,6 +34,13 @@
     operr3 = operationerrfmt("w_type2", "a %s b %s c", "bar", "4b")
     assert operr3.__class__ is not operr.__class__
 
+def test_operationerrfmt_unicode():
+    operr = operationerrfmt("w_type", u"abc %s def %d", u"&#224;&#232;&#236;", 42)
+    assert isinstance(operr, OperationError)
+    assert operr.w_type == "w_type"
+    assert operr._w_value is None
+    assert operr._compute_value() == u"abc &#224;&#232;&#236; def 42"
+
 def test_operationerrfmt_empty():
     py.test.raises(AssertionError, operationerrfmt, "w_type", "foobar")
 


More information about the pypy-commit mailing list