[pypy-commit] pypy operrfmt-NT: simplify: rearrange so _compute_value always gets a space, just avoid calling
pjenvey
noreply at buildbot.pypy.org
Mon May 27 21:36:36 CEST 2013
Author: Philip Jenvey <pjenvey at underboss.org>
Branch: operrfmt-NT
Changeset: r64592:38b554a7b5de
Date: 2013-05-27 12:32 -0700
http://bitbucket.org/pypy/pypy/changeset/38b554a7b5de/
Log: simplify: rearrange so _compute_value always gets a space, just
avoid calling it in __str__ if we lack one
diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py
--- a/pypy/interpreter/error.py
+++ b/pypy/interpreter/error.py
@@ -60,7 +60,9 @@
"NOT_RPYTHON: Convenience for tracebacks."
s = self._w_value
if self.__class__ is not OperationError and s is None:
- s = self._compute_value()
+ space = getattr(self.w_type, 'space')
+ if space is not None:
+ s = self._compute_value(space)
return '[%s: %s]' % (self.w_type, s)
def errorstr(self, space, use_repr=False):
@@ -267,11 +269,11 @@
def get_w_value(self, space):
w_value = self._w_value
if w_value is None:
- value = self._compute_value()
+ value = self._compute_value(space)
self._w_value = w_value = space.wrap(value)
return w_value
- def _compute_value(self):
+ def _compute_value(self, space):
raise NotImplementedError
def get_traceback(self):
@@ -298,17 +300,6 @@
"""
self._application_traceback = traceback
-def _space_from_type(w_type):
- """Grab a space if a W_TypeObject, or None"""
- from pypy.objspace.std.typeobject import W_TypeObject
- # HACK: isinstance(w_type, W_TypeObject) won't translate under the
- # fake objspace, but w_type.__class__ is W_TypeObject does and short
- # circuits to a False constant there, causing the isinstance to be
- # ignored =[
- if (w_type is not None and w_type.__class__ is W_TypeObject and
- isinstance(w_type, W_TypeObject)):
- return w_type.space
-
# ____________________________________________________________
# optimization only: avoid the slowest operation -- the string
# formatting with '%' -- in the common case were we don't
@@ -339,25 +330,6 @@
assert len(formats) > 0, "unsupported: no % command found"
return tuple(parts), tuple(formats)
-def _format_NT(space, fmt, w_value):
- """Process operationerrfmt's %N/T formats"""
- if space is not None:
- if fmt == 'T':
- w_value = space.type(w_value)
- return w_value.getname(space)
- elif not we_are_translated():
- # may not be able to grab space due to testing environments,
- # fallback
- if fmt == 'T':
- tp = type(w_value)
- typedef = getattr(tp, 'typedef', None)
- return tp.__name__ if typedef is None else typedef.name
- for attr in 'name', '__name__':
- name = getattr(w_value, attr, None)
- if name is not None:
- return name
- return '?'
-
def get_operrcls2(valuefmt):
strings, formats = decompose_valuefmt(valuefmt)
assert len(strings) == len(formats) + 1
@@ -377,17 +349,18 @@
setattr(self, attr, args[i])
assert w_type is not None
- def _compute_value(self):
+ def _compute_value(self, space):
lst = [None] * (len(formats) + len(formats) + 1)
for i, fmt, attr in entries:
- string = self.xstrings[i]
+ lst[i + i] = self.xstrings[i]
value = getattr(self, attr)
- lst[i+i] = string
if fmt in 'NT':
- lst[i+i+1] = _format_NT(_space_from_type(self.w_type),
- fmt, value)
+ if fmt == 'T':
+ value = space.type(value)
+ result = value.getname(space)
else:
- lst[i+i+1] = str(value)
+ result = str(value)
+ lst[i + i + 1] = result
lst[-1] = self.xstrings[-1]
return ''.join(lst)
#
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
@@ -12,22 +12,22 @@
assert (decompose_valuefmt("%s%d%%%s") ==
(("", "", "%", ""), ('s', 'd', 's')))
-def test_get_operrcls2():
+def test_get_operrcls2(space):
cls, strings = get_operrcls2('abc %s def %d')
assert strings == ("abc ", " def ", "")
assert issubclass(cls, OperationError)
inst = cls("w_type", strings, "hello", 42)
- assert inst._compute_value() == "abc hello def 42"
+ assert inst._compute_value(space) == "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():
+def test_operationerrfmt(space):
operr = operationerrfmt("w_type", "abc %s def %d", "foo", 42)
assert isinstance(operr, OperationError)
assert operr.w_type == "w_type"
assert operr._w_value is None
- assert operr._compute_value() == "abc foo def 42"
+ assert operr._compute_value(space) == "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")
@@ -37,29 +37,29 @@
operr = operationerrfmt(space.w_AttributeError,
"'%T' object has no attribute '%s'",
space.wrap('foo'), 'foo')
- assert operr._compute_value() == "'str' object has no attribute 'foo'"
+ assert operr._compute_value(space) == "'str' object has no attribute 'foo'"
operr = operationerrfmt("w_type",
"'%T' object has no attribute '%s'",
space.wrap('foo'), 'foo')
- assert operr._compute_value() == "'str' object has no attribute 'foo'"
+ assert operr._compute_value(space) == "'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'"
+ assert operr._compute_value(space) == "'str' object has no attribute 'foo'"
operr = operationerrfmt("w_type",
"'%N' object has no attribute '%s'",
space.type(space.wrap('foo')), 'foo')
- assert operr._compute_value() == "'str' object has no attribute 'foo'"
+ assert operr._compute_value(space) == "'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'"
+ assert operr._compute_value(space) == "'?' object has no attribute 'foo'"
operr = operationerrfmt("w_type",
"'%N' object has no attribute '%s'",
space.wrap('foo'), 'foo')
- assert operr._compute_value() == "'?' object has no attribute 'foo'"
+ assert operr._compute_value(space) == "'?' object has no attribute 'foo'"
def test_operationerrfmt_empty():
py.test.raises(AssertionError, operationerrfmt, "w_type", "foobar")
More information about the pypy-commit
mailing list