[pypy-svn] r70828 - in pypy/branch/lazy-operr-format/pypy/interpreter: . test
arigo at codespeak.net
arigo at codespeak.net
Mon Jan 25 14:45:15 CET 2010
Author: arigo
Date: Mon Jan 25 14:45:14 2010
New Revision: 70828
Modified:
pypy/branch/lazy-operr-format/pypy/interpreter/error.py
pypy/branch/lazy-operr-format/pypy/interpreter/test/test_error.py
Log:
Improve the implementation and the tests a bit.
Modified: pypy/branch/lazy-operr-format/pypy/interpreter/error.py
==============================================================================
--- pypy/branch/lazy-operr-format/pypy/interpreter/error.py (original)
+++ pypy/branch/lazy-operr-format/pypy/interpreter/error.py Mon Jan 25 14:45:14 2010
@@ -255,6 +255,8 @@
_fmtcache2 = {}
def decompose_valuefmt(valuefmt):
+ """Returns a tuple of string parts extracted from valuefmt,
+ and a tuple of format characters."""
formats = []
parts = valuefmt.split('%')
i = 1
@@ -263,10 +265,12 @@
formats.append(parts[i][0])
parts[i] = parts[i][1:]
i += 1
- elif parts[i].startswith('%'):
- parts[i-1] += parts.pop(i)
+ elif parts[i] == '': # support for '%%'
+ parts[i-1] += '%' + parts[i+1]
+ del parts[i:i+2]
else:
raise ValueError("invalid format string (only %s or %d supported)")
+ assert len(formats) > 0, "unsupported: no % command found"
return tuple(parts), tuple(formats)
def get_operrcls2(valuefmt):
@@ -313,6 +317,7 @@
needed."""
OpErrFmt, strings = get_operationerr_class(valuefmt)
return OpErrFmt(w_type, strings, *args)
+operationerrfmt._annspecialcase_ = 'specialize:arg(1)'
# ____________________________________________________________
Modified: pypy/branch/lazy-operr-format/pypy/interpreter/test/test_error.py
==============================================================================
--- pypy/branch/lazy-operr-format/pypy/interpreter/test/test_error.py (original)
+++ pypy/branch/lazy-operr-format/pypy/interpreter/test/test_error.py Mon Jan 25 14:45:14 2010
@@ -1,3 +1,4 @@
+import py
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.error import decompose_valuefmt, get_operrcls2
@@ -7,6 +8,8 @@
(("abc ", " def"), ('s',)))
assert (decompose_valuefmt("%s%d%s") ==
(("", "", "", ""), ('s', 'd', 's')))
+ assert (decompose_valuefmt("%s%d%%%s") ==
+ (("", "", "%", ""), ('s', 'd', 's')))
def test_get_operrcls2():
cls, strings = get_operrcls2('abc %s def %d')
@@ -14,6 +17,9 @@
assert issubclass(cls, OperationError)
inst = cls("w_type", strings, "hello", 42)
assert inst._compute_value() == "abc hello def 42"
+ cls2, strings2 = get_operrcls2('a %s b %d c')
+ assert cls2 is cls # caching
+ assert strings2 == ("a ", " b ", " c")
def test_operationerrfmt():
operr = operationerrfmt("w_type", "abc %s def %d", "foo", 42)
@@ -21,3 +27,10 @@
assert operr.w_type == "w_type"
assert operr._w_value is None
assert operr._compute_value() == "abc foo def 42"
+ operr2 = operationerrfmt("w_type2", "a %s b %d c", "bar", 43)
+ assert operr2.__class__ is operr.__class__
+ operr3 = operationerrfmt("w_type2", "a %s b %s c", "bar", "4b")
+ assert operr3.__class__ is not operr.__class__
+
+def test_operationerrfmt_empty():
+ py.test.raises(AssertionError, operationerrfmt, "w_type", "foobar")
More information about the Pypy-commit
mailing list