[pypy-svn] r4892 - in pypy/branch/src-newobjectmodel/pypy: interpreter interpreter/test objspace
hpk at codespeak.net
hpk at codespeak.net
Fri Jun 4 09:30:52 CEST 2004
Author: hpk
Date: Fri Jun 4 09:30:50 2004
New Revision: 4892
Added:
pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_typedef.py
- copied, changed from r4883, pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_objspace.py
Modified:
pypy/branch/src-newobjectmodel/pypy/interpreter/baseobjspace.py
pypy/branch/src-newobjectmodel/pypy/interpreter/generator.py
pypy/branch/src-newobjectmodel/pypy/interpreter/pyopcode.py
pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py
pypy/branch/src-newobjectmodel/pypy/objspace/descroperation.py
pypy/branch/src-newobjectmodel/pypy/objspace/trivial.py
Log:
- get rid of NoValue specialty and raise/catch
the good old boring OperationError
- added a test for traceback attributes
Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/baseobjspace.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/baseobjspace.py Fri Jun 4 09:30:50 2004
@@ -3,7 +3,7 @@
from pypy.interpreter.miscutils import Stack, getthreadlocals
import pypy.module
-__all__ = ['ObjSpace', 'OperationError', 'NoValue', 'Wrappable']
+__all__ = ['ObjSpace', 'OperationError', 'Wrappable']
class Wrappable(object):
@@ -12,11 +12,6 @@
def __spacebind__(self, space):
return self
-class NoValue(Exception):
- """Raised to signal absence of value, e.g. in the iterator accessing
- method 'op.next()' of object spaces."""
-
-
class ObjSpace:
"""Base class for the interpreter-level implementations of object spaces.
http://codespeak.net/moin/pypy/moin.cgi/ObjectSpace"""
@@ -121,7 +116,9 @@
while True:
try:
w_item = self.next(w_iterator)
- except NoValue:
+ except OperationError, e:
+ if not e.match(self, self.w_StopIteration):
+ raise
break # done
if expected_length is not None and len(items) == expected_length:
raise ValueError, "too many values to unpack"
@@ -308,5 +305,5 @@
# newstring([w_1, w_2,...]) -> w_string from ascii numbers (bytes)
# newdict([(w_key,w_value),...]) -> w_dict
#newslice(w_start,w_stop,w_step) -> w_slice (any argument may be a real None)
-# next(w_iter) -> w_value or raise NoValue
+# next(w_iter) -> w_value or raise StopIteration
#
Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/generator.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/generator.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/generator.py Fri Jun 4 09:30:50 2004
@@ -1,5 +1,5 @@
from pypy.interpreter.error import OperationError
-from pypy.interpreter.baseobjspace import NoValue
+from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.eval import Frame
from pypy.interpreter.pyframe import ControlFlowException, ExitFrame
from pypy.interpreter import function, gateway
@@ -36,7 +36,7 @@
YIELD_STMT = YIELD_VALUE # misnamed in old versions of dis.opname
-class GeneratorIterator(object):
+class GeneratorIterator(Wrappable):
"An iterator created by a generator."
def __init__(self, frame):
@@ -48,45 +48,24 @@
return self.space.wrap(self)
def descr_next(self):
- # raise NoValue when exhausted
+ space = self.frame.space
if self.running:
- space = self.frame.space
raise OperationError(space.w_ValueError,
space.wrap('generator already executing'))
if self.frame.exhausted:
- raise NoValue
+ raise OperationError(space.w_StopIteration, space.w_None)
self.running = True
try:
- try: return Frame.run(self.frame)
+ try:
+ return Frame.run(self.frame)
except OperationError, e:
if e.match(self.space, self.space.w_StopIteration):
- raise NoValue
+ raise OperationError(space.w_StopIteration, space.w_None)
else:
raise
finally:
self.running = False
- # XXX the next two methods we don't really want here,
- # it appears to be there for trivial object space
- def next(self):
- try:
- return self.descr_next() #
- except NoValue:
- raise OperationError(self.space.w_StopIteration,
- self.space.w_None)
- app_next = gateway.interp2app(next)
-
- # XXX the following is for TrivialObjSpace only, when iteration is
- # done by C code (e.g. when calling 'list(g())').
- def __iter__(self):
- class hack(object):
- def next(h):
- try:
- return self.descr_next()
- except NoValue:
- raise StopIteration
- return hack()
-
#
# the specific ControlFlowExceptions used by generators
#
@@ -102,4 +81,4 @@
"""Signals a 'return' statement inside a generator."""
def emptystack(self, frame):
frame.exhausted = True
- raise NoValue
+ raise OperationError(frame.space.w_StopIteration, frame.space.w_None)
Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/pyopcode.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/pyopcode.py Fri Jun 4 09:30:50 2004
@@ -4,7 +4,7 @@
pyfastscope.py and pynestedscope.py.
"""
-from pypy.interpreter.baseobjspace import OperationError, NoValue
+from pypy.interpreter.baseobjspace import OperationError
from pypy.interpreter.eval import UNDEFINED
from pypy.interpreter import baseobjspace, gateway, function
from pypy.interpreter import pyframe, pytraceback
@@ -649,7 +649,9 @@
w_iterator = f.valuestack.top()
try:
w_nextitem = f.space.next(w_iterator)
- except NoValue:
+ except OperationError, e:
+ if not e.match(f.space, f.space.w_StopIteration):
+ raise
# iterator exhausted
f.valuestack.pop()
f.next_instr += jumpby
Copied: pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_typedef.py (from r4883, pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_objspace.py)
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_objspace.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_typedef.py Fri Jun 4 09:30:50 2004
@@ -4,115 +4,26 @@
# this test isn't so much to test that the objspace interface *works*
# -- it's more to test that it's *there*
-class TestObjSpace(testit.TestCase):
-
- def setUp(self):
- self.space = testit.objspace()
-
- def tearDown(self):
- pass
+class TestTraceBackAttributes(testit.AppTestCase):
def test_newstring(self):
- w = self.space.wrap
- s = 'abc'
- chars_w = [w(ord(c)) for c in s]
- self.assertEqual_w(w(s), self.space.newstring(chars_w))
-
- def test_newstring_fail(self):
- w = self.space.wrap
- s = 'abc'
- not_chars_w = [w(c) for c in s]
- self.assertRaises_w(self.space.w_TypeError,
- self.space.newstring,
- not_chars_w)
- self.assertRaises_w(self.space.w_ValueError,
- self.space.newstring,
- [w(-1)])
-
- def test_newlist(self):
- w = self.space.wrap
- l = range(10)
- w_l = self.space.newlist([w(i) for i in l])
- self.assertEqual_w(w_l, w(l))
-
- def test_newdict(self):
- w = self.space.wrap
- items = [(0, 1), (3, 4)]
- items_w = [(w(k), w(v)) for (k, v) in items]
- d = dict(items)
- w_d = self.space.newdict(items_w)
- self.assertEqual_w(w_d, w(d))
-
- def test_newtuple(self):
- w = self.space.wrap
- t = tuple(range(10))
- w_t = self.space.newtuple([w(i) for i in t])
- self.assertEqual_w(w_t, w(t))
-
- def test_is_true(self):
- w = self.space.wrap
- true = (1==1)
- false = (1==0)
- w_true = w(true)
- w_false = w(false)
- self.failUnless(self.space.is_true(w_true))
- self.failIf(self.space.is_true(w_false))
-
- def test_is_(self):
- w_l = self.space.newlist([])
- w_m = self.space.newlist([])
- self.assertEqual(self.space.is_(w_l, w_l), self.space.w_True)
- self.assertEqual(self.space.is_(w_l, w_m), self.space.w_False)
-
- def test_newbool(self):
- self.assertEqual(self.space.newbool(0), self.space.w_False)
- self.assertEqual(self.space.newbool(1), self.space.w_True)
-
- def test_unpackiterable(self):
- w = self.space.wrap
- l = [w(1), w(2), w(3), w(4)]
- w_l = self.space.newlist(l)
- self.assertEqual(self.space.unpackiterable(w_l), l)
- self.assertEqual(self.space.unpackiterable(w_l, 4), l)
- self.assertRaises(ValueError, self.space.unpackiterable, w_l, 3)
- self.assertRaises(ValueError, self.space.unpackiterable, w_l, 5)
-
- def test_unpacktuple(self):
- w = self.space.wrap
- l = [w(1), w(2), w(3), w(4)]
- w_l = self.space.newtuple(l)
- self.assertEqual(self.space.unpacktuple(w_l), l)
- self.assertEqual(self.space.unpacktuple(w_l, 4), l)
- self.assertRaises(ValueError, self.space.unpacktuple, w_l, 3)
- self.assertRaises(ValueError, self.space.unpacktuple, w_l, 5)
-
- def test_exception_match(self):
- self.failUnless(self.space.exception_match(self.space.w_ValueError,
- self.space.w_ValueError))
- self.failUnless(self.space.exception_match(self.space.w_IndexError,
- self.space.w_LookupError))
- self.failIf(self.space.exception_match(self.space.w_ValueError,
- self.space.w_LookupError))
-
-class ModuleMinimalTest(testit.IntTestCase):
- def setUp(self):
- self.space = testit.objspace()
-
- def test_sys_exists(self):
- w_sys = self.space.get_builtin_module('sys')
- self.assert_(self.space.is_true(w_sys))
-
- def test_import_exists(self):
- space = self.space
- w_builtin = space.get_builtin_module('__builtin__')
- self.assert_(space.is_true(w_builtin))
- w_name = space.wrap('__import__')
- w_import = self.space.getattr(w_builtin, w_name)
- self.assert_(space.is_true(w_import))
-
- def test_sys_import(self):
- from pypy.interpreter.main import run_string
- run_string('import sys', space=self.space)
+ import sys
+ def f():
+ raise TypeError, "hello"
+
+ def g():
+ f()
+
+ try:
+ g()
+ except:
+ typ,val,tb = sys.exc_info()
+ else:
+ raise AssertionError, "should have raised"
+ self.assert_(hasattr(tb, 'tb_frame'))
+ self.assert_(hasattr(tb, 'tb_lasti'))
+ self.assert_(hasattr(tb, 'tb_lineno'))
+ self.assert_(hasattr(tb, 'tb_next'))
if __name__ == '__main__':
testit.main()
Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py Fri Jun 4 09:30:50 2004
@@ -123,10 +123,10 @@
)
PyTraceback.typedef = TypeDef("traceback",
- tb_frame = attrproperty('tb_frame'),
- tb_lasti = attrproperty('tb_lasti'),
- tb_lineno = attrproperty('tb_line'),
- tb_next = attrproperty('tb_next'),
+ tb_frame = attrproperty('frame'),
+ tb_lasti = attrproperty('lasti'),
+ tb_lineno = attrproperty('lineno'),
+ tb_next = attrproperty('next'),
)
GeneratorIterator.typedef = TypeDef("generator",
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/descroperation.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/descroperation.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/descroperation.py Fri Jun 4 09:30:50 2004
@@ -1,6 +1,6 @@
import operator
from pypy.interpreter.error import OperationError
-from pypy.interpreter.baseobjspace import ObjSpace, NoValue
+from pypy.interpreter.baseobjspace import ObjSpace
from pypy.interpreter.function import Function
class Object:
@@ -167,12 +167,7 @@
if w_descr is None:
raise OperationError(space.w_TypeError,
space.wrap("iterator has no next() method"))
- try:
- return space.get_and_call_function(w_descr,w_obj)
- except OperationError, e:
- if not e.match(space, space.w_StopIteration):
- raise
- raise NoValue
+ return space.get_and_call_function(w_descr,w_obj)
def getitem(space,w_obj,w_key):
w_descr = space.lookup(w_obj,'__getitem__')
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/trivial.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/trivial.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/trivial.py Fri Jun 4 09:30:50 2004
@@ -33,7 +33,8 @@
# of the exceptions...
self.w_Exception = type('Exception', (),
- {'__init__':__init__, '__str__': __str__})
+ {'__init__':__init__, '__str__': __str__,
+ 'originalex': Exception})
done = {'Exception': self.w_Exception}
@@ -61,6 +62,7 @@
base = done[b.__name__]
newtype = type(next, (base,), {})
setattr(self, 'w_' + next, newtype)
+ newtype.originalex = v
done[next] = newtype
stack.pop()
else:
@@ -163,7 +165,14 @@
def make_stuff(descr=descr, descrname=descrname, space=self):
def stuff(w_obj, *args, **kwds):
fn = descr.get_function(space)
- return fn.descr_function_call(w_obj, *args, **kwds)
+ try:
+ return fn.descr_function_call(w_obj, *args, **kwds)
+ except OperationError, e:
+ if not hasattr(e.w_type, 'originalex'):
+ raise # XXX
+ # XXX normalize ...
+ #if isinstance(e.w_value, e.w_type):
+ raise e.w_type.originalex(repr(e.w_value)) # e.w_value)
return stuff
descrdict[descrname] = make_stuff()
else:
@@ -203,7 +212,6 @@
#traceback.print_exc()
#ec = self.getexecutioncontext() # .framestack.items[-1]
#ec.print_detailed_traceback(self)
-
etype, evalue, etb = sys.exc_info()
if etype is OperationError:
raise etype, evalue, etb # just re-raise it
More information about the Pypy-commit
mailing list