[pypy-svn] r69215 - in pypy/branch/faster-raise/pypy/module/tempexceptions: . test
fijal at codespeak.net
fijal at codespeak.net
Thu Nov 12 11:45:43 CET 2009
Author: fijal
Date: Thu Nov 12 11:45:42 2009
New Revision: 69215
Modified:
pypy/branch/faster-raise/pypy/module/tempexceptions/__init__.py
pypy/branch/faster-raise/pypy/module/tempexceptions/interp_exceptions.py
pypy/branch/faster-raise/pypy/module/tempexceptions/test/test_exc.py
Log:
(arigo, fijal) Slight progress
Modified: pypy/branch/faster-raise/pypy/module/tempexceptions/__init__.py
==============================================================================
--- pypy/branch/faster-raise/pypy/module/tempexceptions/__init__.py (original)
+++ pypy/branch/faster-raise/pypy/module/tempexceptions/__init__.py Thu Nov 12 11:45:42 2009
@@ -7,6 +7,51 @@
appleveldefs = {}
interpleveldefs = {
+ 'ArithmeticError' : 'interp_exceptions.W_ArithmeticError',
+ 'AssertionError' : 'interp_exceptions.W_AssertionError',
+ 'AttributeError' : 'interp_exceptions.W_AttributeError',
'BaseException' : 'interp_exceptions.W_BaseException',
+ 'DeprecationWarning' : 'interp_exceptions.W_DeprecationWarning',
+ 'EOFError' : 'interp_exceptions.W_EOFError',
+ 'EnvironmentError' : 'interp_exceptions.W_EnvironmentError',
'Exception' : 'interp_exceptions.W_Exception',
+ 'FloatingPointError' : 'interp_exceptions.W_FloatingPointError',
+ 'FutureWarning' : 'interp_exceptions.W_FutureWarning',
+ 'GeneratorExit' : 'interp_exceptions.W_GeneratorExit',
+ 'IOError' : 'interp_exceptions.W_IOError',
+ 'ImportError' : 'interp_exceptions.W_ImportError',
+ 'ImportWarning' : 'interp_exceptions.W_ImportWarning',
+ 'IndentationError' : 'interp_exceptions.W_IndentationError',
+ 'IndexError' : 'interp_exceptions.W_IndexError',
+ 'KeyError' : 'interp_exceptions.W_KeyError',
+ 'KeyboardInterrupt' : 'interp_exceptions.W_KeyboardInterrupt',
+ 'LookupError' : 'interp_exceptions.W_LookupError',
+ 'MemoryError' : 'interp_exceptions.W_MemoryError',
+ 'NameError' : 'interp_exceptions.W_NameError',
+ 'NotImplementedError' : 'interp_exceptions.W_NotImplementedError',
+ 'OSError' : 'interp_exceptions.W_OSError',
+ 'OverflowError' : 'interp_exceptions.W_OverflowError',
+ 'PendingDeprecationWarning' : 'interp_exceptions.W_PendingDeprecationWarning',
+ 'ReferenceError' : 'interp_exceptions.W_ReferenceError',
+ 'RuntimeError' : 'interp_exceptions.W_RuntimeError',
+ 'RuntimeWarning' : 'interp_exceptions.W_RuntimeWarning',
+ 'StandardError' : 'interp_exceptions.W_StandardError',
+ 'StopIteration' : 'interp_exceptions.W_StopIteration',
+ 'SyntaxError' : 'interp_exceptions.W_SyntaxError',
+ 'SyntaxWarning' : 'interp_exceptions.W_SyntaxWarning',
+ 'SystemError' : 'interp_exceptions.W_SystemError',
+ 'SystemExit' : 'interp_exceptions.W_SystemExit',
+ 'TabError' : 'interp_exceptions.W_TabError',
+ 'TypeError' : 'interp_exceptions.W_TypeError',
+ 'UnboundLocalError' : 'interp_exceptions.W_UnboundLocalError',
+ 'UnicodeDecodeError' : 'interp_exceptions.W_UnicodeDecodeError',
+ 'UnicodeEncodeError' : 'interp_exceptions.W_UnicodeEncodeError',
+ 'UnicodeError' : 'interp_exceptions.W_UnicodeError',
+ 'UnicodeTranslateError' : 'interp_exceptions.W_UnicodeTranslateError',
+ 'UnicodeWarning' : 'interp_exceptions.W_UnicodeWarning',
+ 'UserWarning' : 'interp_exceptions.W_UserWarning',
+ 'ValueError' : 'interp_exceptions.W_ValueError',
+ 'Warning' : 'interp_exceptions.W_Warning',
+ 'ZeroDivisionError' : 'interp_exceptions.W_ZeroDivisionError',
+
}
Modified: pypy/branch/faster-raise/pypy/module/tempexceptions/interp_exceptions.py
==============================================================================
--- pypy/branch/faster-raise/pypy/module/tempexceptions/interp_exceptions.py (original)
+++ pypy/branch/faster-raise/pypy/module/tempexceptions/interp_exceptions.py Thu Nov 12 11:45:42 2009
@@ -72,7 +72,7 @@
from pypy.interpreter.baseobjspace import ObjSpace, Wrappable, W_Root
from pypy.interpreter.typedef import TypeDef, interp_attrproperty_w,\
- GetSetProperty
+ GetSetProperty, interp_attrproperty, descr_get_dict, descr_set_dict
from pypy.interpreter.gateway import interp2app
class W_BaseException(Wrappable):
@@ -81,9 +81,11 @@
The __getitem__ method is provided for backwards-compatibility
and will be deprecated at some point.
"""
+ w_dict = None
def __init__(self, space, args_w):
self.args_w = args_w
+ self.space = space
if len(args_w) == 1:
self.w_message = args_w[0]
else:
@@ -111,6 +113,17 @@
def descr_getargs(space, self):
return space.newtuple(self.args_w)
+ def getdict(self):
+ if self.w_dict is None:
+ self.w_dict = self.space.newdict()
+ return self.w_dict
+
+ def setdict(self, space, w_dict):
+ if not space.is_true(space.isinstance( w_dict, space.w_dict )):
+ raise OperationError( space.w_TypeError, space.wrap("setting exceptions's dictionary to a non-dict") )
+ self.w_dict = w_dict
+
+
def _new(cls):
def descr_new_base_exception(space, w_subtype, args_w):
exc = space.allocate_instance(cls, w_subtype)
@@ -126,16 +139,111 @@
__new__ = _new(W_BaseException),
__str__ = interp2app(W_BaseException.descr_str),
__repr__ = interp2app(W_BaseException.descr_repr),
+ __dict__ = GetSetProperty(descr_get_dict, descr_set_dict,
+ cls=W_BaseException),
message = interp_attrproperty_w('w_message', W_BaseException),
args = GetSetProperty(W_BaseException.descr_getargs),
)
-class W_Exception(W_BaseException):
- """Common base class for all non-exit exceptions."""
+def _new_exception(name, base, docstring, **kwargs):
+ class W_Exception(base):
+ __doc__ = docstring
+
+ W_Exception.__name__ = 'W_' + name
+
+ for k, v in kwargs.items():
+ kwargs[k] = interp2app(v.__get__(None, W_Exception))
+ W_Exception.typedef = TypeDef(
+ name,
+ base.typedef,
+ __doc__ = W_Exception.__doc__,
+ __new__ = _new(W_Exception),
+ **kwargs
+ )
+ return W_Exception
+
+W_Exception = _new_exception('Exception', W_BaseException,
+ """Common base class for all non-exit exceptions.""")
+
+W_GeneratorExit = _new_exception('GeneratorExit', W_Exception,
+ """Request that a generator exit.""")
+
+W_StandardError = _new_exception('StandardError', W_Exception,
+ """Base class for all standard Python exceptions.""")
+
+W_ValueError = _new_exception('ValueError', W_StandardError,
+ """Inappropriate argument value (of correct type).""")
+
+W_ImportError = _new_exception('ImportError', W_StandardError,
+ """Import can't find module, or can't find name in module.""")
+
+W_RuntimeError = _new_exception('RuntimeError', W_StandardError,
+ """Unspecified run-time error.""")
+
+W_UnicodeError = _new_exception('UnicodeError', W_ValueError,
+ """Unicode related error.""")
+
+
+class W_UnicodeTranslateError(W_UnicodeError):
+ """Unicode translation error."""
+ def __init__(self, space, w_obj, w_start, w_end, w_reason):
+ self.object = space.unicode_w(w_obj)
+ self.start = space.int_w(w_start)
+ self.end = space.int_w(w_end)
+ self.reason = space.str_w(w_reason)
+ W_BaseException.__init__(self, space, [w_obj, w_start, w_end, w_reason])
-W_Exception.typedef = TypeDef(
- 'Exception',
- W_BaseException.typedef,
- __doc__ = W_Exception.__doc__,
- __new__ = _new(W_Exception),
+ def descr_str(self, space):
+ return space.appexec([space.wrap(self)], """(self):
+ if self.end == self.start + 1:
+ badchar = ord(self.object[self.start])
+ if badchar <= 0xff:
+ return "can't translate character u'\\\\x%02x' in position %d: %s" % (badchar, self.start, self.reason)
+ if badchar <= 0xffff:
+ return "can't translate character u'\\\\u%04x' in position %d: %s"%(badchar, self.start, self.reason)
+ return "can't translate character u'\\\\U%08x' in position %d: %s"%(badchar, self.start, self.reason)
+ return "can't translate characters in position %d-%d: %s" % (self.start, self.end - 1, self.reason)
+ """)
+ descr_str.unwrap_spec = ['self', ObjSpace]
+
+def descr_new_unicode_translate_error(space, w_subtype, w_obj, w_start, w_end,
+ w_reason):
+ exc = space.allocate_instance(W_UnicodeTranslateError, w_subtype)
+ W_UnicodeTranslateError.__init__(exc, space, w_obj, w_start,
+ w_end, w_reason)
+ return space.wrap(exc)
+
+def readwrite_attrproperty(name, cls, unwrapname):
+ def fget(space, obj):
+ return space.wrap(getattr(obj, name))
+ def fset(space, obj, w_val):
+ setattr(obj, name, getattr(space, unwrapname)(w_val))
+ return GetSetProperty(fget, fset, cls=cls)
+
+W_UnicodeTranslateError.typedef = TypeDef(
+ 'UnicodeTranslateError',
+ W_UnicodeError.typedef,
+ __doc__ = W_UnicodeTranslateError.__doc__,
+ __new__ = interp2app(descr_new_unicode_translate_error),
+ __str__ = interp2app(W_UnicodeTranslateError.descr_str),
+ object = readwrite_attrproperty('object', W_UnicodeTranslateError, 'unicode_w'),
+ start = readwrite_attrproperty('start', W_UnicodeTranslateError, 'int_w'),
+ end = readwrite_attrproperty('end', W_UnicodeTranslateError, 'int_w'),
+ reason = readwrite_attrproperty('reason', W_UnicodeTranslateError, 'str_w'),
)
+
+W_LookupError = _new_exception('LookupError', W_StandardError,
+ """Base class for lookup errors.""")
+
+def key_error_str(self, space):
+ if len(self.args_w) == 0:
+ return space.wrap('')
+ elif len(self.args_w) == 1:
+ return space.repr(self.args_w[0])
+ else:
+ return space.str(space.newtuple(self.args_w))
+key_error_str.unwrap_spec = ['self', ObjSpace]
+
+W_KeyError = _new_exception('KeyError', W_LookupError,
+ """Mapping key not found.""",
+ __str__ = key_error_str)
Modified: pypy/branch/faster-raise/pypy/module/tempexceptions/test/test_exc.py
==============================================================================
--- pypy/branch/faster-raise/pypy/module/tempexceptions/test/test_exc.py (original)
+++ pypy/branch/faster-raise/pypy/module/tempexceptions/test/test_exc.py Thu Nov 12 11:45:42 2009
@@ -20,6 +20,9 @@
assert repr(BaseException(3, "x")) == "BaseException(3, 'x')"
assert str(BaseException(3, "x")) == "(3, 'x')"
assert BaseException(3, "x").message == ''
+ x = BaseException()
+ x.xyz = 3
+ assert x.xyz == 3
def test_exc(self):
from tempexceptions import Exception, BaseException
@@ -28,3 +31,34 @@
assert isinstance(Exception(), Exception)
assert isinstance(Exception(), BaseException)
assert repr(Exception(3, "x")) == "Exception(3, 'x')"
+
+ def test_custom_class(self):
+ from tempexceptions import Exception
+
+ class MyException(Exception):
+ def __init__(self, x):
+ self.x = x
+
+ def __str__(self):
+ return self.x
+
+ assert issubclass(MyException, Exception)
+ assert str(MyException("x")) == "x"
+
+ def test_unicode_translate_error(self):
+ from tempexceptions import UnicodeTranslateError
+ ut = UnicodeTranslateError(u"x", 1, 5, "bah")
+ assert ut.object == u'x'
+ assert ut.start == 1
+ assert ut.end == 5
+ assert ut.reason == 'bah'
+ assert ut.args == (u'x', 1, 5, 'bah')
+ assert ut.message == ''
+ ut.object = u'y'
+ assert ut.object == u'y'
+ assert str(ut) == "can't translate characters in position 1-4: bah"
+
+ def test_key_error(self):
+ from tempexceptions import KeyError
+
+ assert str(KeyError('s')) == "'s'"
More information about the Pypy-commit
mailing list