[pypy-svn] r69278 - in pypy/branch/faster-raise/pypy/module/exceptions: . test

fijal at codespeak.net fijal at codespeak.net
Sat Nov 14 13:53:07 CET 2009


Author: fijal
Date: Sat Nov 14 13:53:06 2009
New Revision: 69278

Modified:
   pypy/branch/faster-raise/pypy/module/exceptions/interp_exceptions.py
   pypy/branch/faster-raise/pypy/module/exceptions/test/test_exc.py
Log:
(arigo, fijal)
Split __init__ and __new__ in Exceptions


Modified: pypy/branch/faster-raise/pypy/module/exceptions/interp_exceptions.py
==============================================================================
--- pypy/branch/faster-raise/pypy/module/exceptions/interp_exceptions.py	(original)
+++ pypy/branch/faster-raise/pypy/module/exceptions/interp_exceptions.py	Sat Nov 14 13:53:06 2009
@@ -73,7 +73,7 @@
 from pypy.interpreter.baseobjspace import ObjSpace, Wrappable, W_Root
 from pypy.interpreter.typedef import TypeDef, interp_attrproperty_w,\
      GetSetProperty, interp_attrproperty, descr_get_dict, descr_set_dict
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, Arguments
 from pypy.interpreter.error import OperationError
 from pypy.rlib import rwin32
 
@@ -98,14 +98,19 @@
     and will be deprecated at some point. 
     """
     w_dict = None
+    args_w = []
 
-    def __init__(self, space, args_w):
-        self.args_w = args_w
+    def __init__(self, space):
         self.space = space
+        self.w_message = space.w_None
+
+    def descr_init(self, space, args_w):
+        self.args_w = args_w
         if len(args_w) == 1:
             self.w_message = args_w[0]
         else:
             self.w_message = space.wrap("")
+    descr_init.unwrap_spec = ['self', ObjSpace, 'args_w']
 
     def descr_str(self, space):
         lgt = len(self.args_w)
@@ -161,11 +166,11 @@
 def _new(cls, basecls=None):
     if basecls is None:
         basecls = cls
-    def descr_new_base_exception(space, w_subtype, args_w):
+    def descr_new_base_exception(space, w_subtype, __args__):
         exc = space.allocate_instance(cls, w_subtype)
-        basecls.__init__(exc, space, args_w)
+        basecls.__init__(exc, space)
         return space.wrap(exc)
-    descr_new_base_exception.unwrap_spec = [ObjSpace, W_Root, 'args_w']
+    descr_new_base_exception.unwrap_spec = [ObjSpace, W_Root, Arguments]
     descr_new_base_exception.func_name = 'descr_new_' + cls.__name__
     return interp2app(descr_new_base_exception)
 
@@ -174,6 +179,7 @@
     __doc__ = W_BaseException.__doc__,
     __module__ = 'exceptions',
     __new__ = _new(W_BaseException),
+    __init__ = interp2app(W_BaseException.descr_init),
     __str__ = interp2app(W_BaseException.descr_str),
     __repr__ = interp2app(W_BaseException.descr_repr),
     __dict__ = GetSetProperty(descr_get_dict, descr_set_dict,
@@ -237,13 +243,20 @@
 
 class W_UnicodeTranslateError(W_UnicodeError):
     """Unicode translation error."""
-    def __init__(self, space, w_object, w_start, w_end, w_reason):
+    object = u''
+    start = 0
+    end = 0
+    reason = ''
+    
+    def descr_init(self, space, w_object, w_start, w_end, w_reason):
         self.object = space.unicode_w(w_object)
         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_object, w_start,
-                                               w_end, w_reason])
+        W_BaseException.descr_init(self, space, [w_object, w_start,
+                                                 w_end, w_reason])
+    descr_init.unwrap_spec = ['self', ObjSpace, W_Root, W_Root, W_Root,
+                              W_Root]
 
     def descr_str(self, space):
         return space.appexec([space.wrap(self)], r"""(self):
@@ -258,19 +271,13 @@
         """)
     descr_str.unwrap_spec = ['self', ObjSpace]
 
-def descr_new_unicode_translate_error(space, w_subtype, w_object,
-                                      w_start, w_end, w_reason):
-    exc = space.allocate_instance(W_UnicodeTranslateError, w_subtype)
-    W_UnicodeTranslateError.__init__(exc, space, w_object, w_start,
-                                     w_end, w_reason)
-    return space.wrap(exc)
-
 W_UnicodeTranslateError.typedef = TypeDef(
     'UnicodeTranslateError',
     W_UnicodeError.typedef,
     __doc__ = W_UnicodeTranslateError.__doc__,
     __module__ = 'exceptions',
-    __new__ = interp2app(descr_new_unicode_translate_error),
+    __new__ = _new(W_UnicodeTranslateError),
+    __init__ = interp2app(W_UnicodeTranslateError.descr_init),
     __str__ = interp2app(W_UnicodeTranslateError.descr_str),
     object = readwrite_attrproperty('object', W_UnicodeTranslateError, 'unicode_w'),
     start  = readwrite_attrproperty('start', W_UnicodeTranslateError, 'int_w'),
@@ -307,17 +314,21 @@
 class W_EnvironmentError(W_StandardError):
     """Base class for I/O related errors."""
 
-    def __init__(self, space, args_w):
-        W_BaseException.__init__(self, space, args_w)
+    def __init__(self, space):
         self.w_errno = space.w_None
         self.w_strerror = space.w_None
-        self.w_filename = space.w_None
+        self.w_filename = space.w_None        
+        W_BaseException.__init__(self, space)
+
+    def descr_init(self, space, args_w):
+        W_BaseException.descr_init(self, space, args_w)
         if 2 <= len(args_w) <= 3:
             self.w_errno = args_w[0]
             self.w_strerror = args_w[1]
         if len(args_w) == 3:
             self.w_filename = args_w[2]
             self.args_w = [args_w[0], args_w[1]]
+    descr_init.unwrap_spec = ['self', ObjSpace, 'args_w']
 
     def descr_str(self, space):
         if (not space.is_w(self.w_errno, space.w_None) and
@@ -338,6 +349,7 @@
     __doc__ = W_EnvironmentError.__doc__,
     __module__ = 'exceptions',
     __new__ = _new(W_EnvironmentError),
+    __init__ = interp2app(W_EnvironmentError.descr_init),
     __str__ = interp2app(W_EnvironmentError.descr_str),
     errno    = readwrite_attrproperty_w('w_errno',    W_EnvironmentError),
     strerror = readwrite_attrproperty_w('w_strerror', W_EnvironmentError),
@@ -350,10 +362,14 @@
 class W_WindowsError(W_OSError):
     """MS-Windows OS system call failed."""
     
-    def __init__(self, space, args_w):
-        W_OSError.__init__(self, space, args_w)
+    def __init__(self, space):
+        self.w_winerror = space.w_None
+        W_OSError.__init__(self, space)
+
+    def descr_init(self, space, args_w):
         # Set errno to the POSIX errno, and winerror to the Win32
         # error code.
+        W_OSError.descr_init(self, space, args_w)
         try:
             errno = space.int_w(self.w_errno)
         except OperationError:
@@ -362,6 +378,7 @@
             errno = self._winerror_to_errno.get(errno, self._default_errno)
         self.w_winerror = self.w_errno
         self.w_errno = space.wrap(errno)
+    descr_init.unwrap_spec = ['self', ObjSpace, 'args_w']
 
     def descr_str(self, space):
         if (not space.is_w(self.w_winerror, space.w_None) and
@@ -387,6 +404,7 @@
     __doc__  = W_WindowsError.__doc__,
     __module__ = 'exceptions',
     __new__  = _new(W_WindowsError),
+    __init__ = interp2app(W_WindowsError.descr_init),
     __str__  = interp2app(W_WindowsError.descr_str),
     winerror = readwrite_attrproperty_w('w_winerror', W_WindowsError),
     )
@@ -413,8 +431,15 @@
 class W_SyntaxError(W_StandardError):
     """Invalid syntax."""
 
-    def __init__(self, space, args_w):
-        W_BaseException.__init__(self, space, args_w)
+    def __init__(self, space):
+        self.w_filename = space.w_None
+        self.w_lineno   = space.w_None
+        self.w_offset   = space.w_None
+        self.w_text     = space.w_None
+        self.w_msg      = space.w_None
+        W_BaseException.__init__(self, space)
+
+    def descr_init(self, space, args_w):
         # that's not a self.w_message!!!
         if len(args_w) > 0:
             self.w_msg = args_w[0]
@@ -426,11 +451,8 @@
             self.w_lineno   = values_w[1]
             self.w_offset   = values_w[2]
             self.w_text     = values_w[3]
-        else:
-            self.w_filename = space.w_None
-            self.w_lineno   = space.w_None
-            self.w_offset   = space.w_None
-            self.w_text     = space.w_None
+        W_BaseException.descr_init(self, space, args_w)
+    descr_init.unwrap_spec = ['self', ObjSpace, 'args_w']
 
     def descr_str(self, space):
         return space.appexec([self], """(self):
@@ -458,6 +480,7 @@
     'SyntaxError',
     W_StandardError.typedef,
     __new__ = _new(W_SyntaxError),
+    __init__ = interp2app(W_SyntaxError.descr_init),
     __str__ = interp2app(W_SyntaxError.descr_str),
     __doc__ = W_SyntaxError.__doc__,
     __module__ = 'exceptions',
@@ -474,19 +497,23 @@
 class W_SystemExit(W_BaseException):
     """Request to exit from the interpreter."""
     
-    def __init__(self, space, args_w):
-        W_BaseException.__init__(self, space, args_w)
-        if len(args_w) == 0:
-            self.w_code = space.w_None
-        elif len(args_w) == 1:
+    def __init__(self, space):
+        self.w_code = space.w_None
+        W_BaseException.__init__(self, space)
+
+    def descr_init(self, space, args_w):
+        if len(args_w) == 1:
             self.w_code = args_w[0]
-        else:
+        elif len(args_w) > 1:
             self.w_code = space.newtuple(args_w)
+        W_BaseException.descr_init(self, space, args_w)
+    descr_init.unwrap_spec = ['self', ObjSpace, 'args_w']
 
 W_SystemExit.typedef = TypeDef(
     'SystemExit',
     W_BaseException.typedef,
     __new__ = _new(W_SystemExit),
+    __init__ = interp2app(W_SystemExit.descr_init),
     __doc__ = W_SystemExit.__doc__,
     __module__ = 'exceptions',
     code    = readwrite_attrproperty_w('w_code', W_SystemExit)
@@ -515,15 +542,22 @@
 
 class W_UnicodeDecodeError(W_UnicodeError):
     """Unicode decoding error."""
+    encoding = ''
+    object = ''
+    start = 0
+    end = 0
+    reason = ''
 
-    def __init__(self, space, w_encoding, w_object, w_start, w_end, w_reason):
+    def descr_init(self, space, w_encoding, w_object, w_start, w_end, w_reason):
         self.encoding = space.str_w(w_encoding)
         self.object = space.str_w(w_object)
         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_encoding, w_object,
-                                               w_start, w_end, w_reason])
+        W_BaseException.descr_init(self, space, [w_encoding, w_object,
+                                                 w_start, w_end, w_reason])
+    descr_init.unwrap_spec = ['self', ObjSpace, W_Root, W_Root, W_Root, W_Root,
+                              W_Root]
 
     def descr_str(self, space):
         return space.appexec([self], """(self):
@@ -536,19 +570,13 @@
         """)
     descr_str.unwrap_spec = ['self', ObjSpace]
 
-def descr_new_unicode_decode_error(space, w_subtype, w_encoding, w_object,
-                                   w_start, w_end, w_reason):
-    exc = space.allocate_instance(W_UnicodeDecodeError, w_subtype)
-    W_UnicodeDecodeError.__init__(exc, space, w_encoding, w_object, w_start,
-                                  w_end, w_reason)
-    return space.wrap(exc)
-
 W_UnicodeDecodeError.typedef = TypeDef(
     'UnicodeDecodeError',
     W_UnicodeError.typedef,
     __doc__ = W_UnicodeDecodeError.__doc__,
     __module__ = 'exceptions',
-    __new__ = interp2app(descr_new_unicode_decode_error),
+    __new__ = _new(W_UnicodeDecodeError),
+    __init__ = interp2app(W_UnicodeDecodeError.descr_init),
     __str__ = interp2app(W_UnicodeDecodeError.descr_str),
     encoding = readwrite_attrproperty('encoding', W_UnicodeDecodeError, 'str_w'),
     object = readwrite_attrproperty('object', W_UnicodeDecodeError, 'str_w'),
@@ -600,14 +628,22 @@
 class W_UnicodeEncodeError(W_UnicodeError):
     """Unicode encoding error."""
 
-    def __init__(self, space, w_encoding, w_object, w_start, w_end, w_reason):
+    encoding = ''
+    object = u''
+    start = 0
+    end = 0
+    reason = ''
+
+    def descr_init(self, space, w_encoding, w_object, w_start, w_end, w_reason):
         self.encoding = space.str_w(w_encoding)
         self.object = space.unicode_w(w_object)
         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_encoding, w_object,
-                                               w_start, w_end, w_reason])
+        W_BaseException.descr_init(self, space, [w_encoding, w_object,
+                                                 w_start, w_end, w_reason])
+    descr_init.unwrap_spec = ['self', ObjSpace, W_Root, W_Root, W_Root, W_Root,
+                              W_Root]
 
     def descr_str(self, space):
         return space.appexec([self], r"""(self):
@@ -626,19 +662,13 @@
         """)
     descr_str.unwrap_spec = ['self', ObjSpace]
 
-def descr_new_unicode_encode_error(space, w_subtype, w_encoding, w_object,
-                                   w_start, w_end, w_reason):
-    exc = space.allocate_instance(W_UnicodeEncodeError, w_subtype)
-    W_UnicodeEncodeError.__init__(exc, space, w_encoding, w_object, w_start,
-                                  w_end, w_reason)
-    return space.wrap(exc)
-
 W_UnicodeEncodeError.typedef = TypeDef(
     'UnicodeEncodeError',
     W_UnicodeError.typedef,
     __doc__ = W_UnicodeEncodeError.__doc__,
     __module__ = 'exceptions',
-    __new__ = interp2app(descr_new_unicode_encode_error),
+    __new__ = _new(W_UnicodeEncodeError),
+    __init__ = interp2app(W_UnicodeEncodeError.descr_init),
     __str__ = interp2app(W_UnicodeEncodeError.descr_str),
     encoding = readwrite_attrproperty('encoding', W_UnicodeEncodeError, 'str_w'),
     object = readwrite_attrproperty('object', W_UnicodeEncodeError, 'unicode_w'),

Modified: pypy/branch/faster-raise/pypy/module/exceptions/test/test_exc.py
==============================================================================
--- pypy/branch/faster-raise/pypy/module/exceptions/test/test_exc.py	(original)
+++ pypy/branch/faster-raise/pypy/module/exceptions/test/test_exc.py	Sat Nov 14 13:53:06 2009
@@ -31,6 +31,15 @@
         x.message = "xyz"
         assert x.message == "xyz"
 
+    def test_kwargs(self):
+        from exceptions import Exception
+        class X(Exception):
+            def __init__(self, x=3):
+                self.x = x
+
+        x = X(x=8)
+        assert x.x == 8
+
     def test_exc(self):
         from exceptions import Exception, BaseException
 



More information about the Pypy-commit mailing list