[pypy-svn] r10865 - pypy/dist/pypy/objspace/std

pedronis at codespeak.net pedronis at codespeak.net
Tue Apr 19 19:28:07 CEST 2005


Author: pedronis
Date: Tue Apr 19 19:28:07 2005
New Revision: 10865

Modified:
   pypy/dist/pypy/objspace/std/inttype.py
   pypy/dist/pypy/objspace/std/longtype.py
   pypy/dist/pypy/objspace/std/strutil.py
   pypy/dist/pypy/objspace/std/unicodeobject.py
Log:
use our own exception for _parse_string, avoids depending on .args and remove the last meaningful blocked blocks



Modified: pypy/dist/pypy/objspace/std/inttype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/inttype.py	(original)
+++ pypy/dist/pypy/objspace/std/inttype.py	Tue Apr 19 19:28:07 2005
@@ -1,5 +1,5 @@
 from pypy.objspace.std.stdtypedef import *
-from pypy.objspace.std.strutil import string_to_int
+from pypy.objspace.std.strutil import string_to_int, ParseStringError
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import NoneNotWrapped
 
@@ -13,9 +13,9 @@
             try:
                 # XXX can produce unwrapped long
                 value = string_to_int(space.str_w(w_value))
-            except ValueError, e:
+            except ParseStringError, e:
                 raise OperationError(space.w_ValueError,
-                                     space.wrap(e.args[0]))
+                                     space.wrap(e.msg))
         else:
             # otherwise, use the __int__() method
             w_obj = space.int(w_value)
@@ -49,9 +49,9 @@
             # XXX can produce unwrapped long, need real long impl to know
             # what to do
             value = string_to_int(s, base)
-        except ValueError, e:
+        except ParseStringError, e:
             raise OperationError(space.w_ValueError,
-                                 space.wrap(e.args[0]))
+                                 space.wrap(e.msg))
 
     if isinstance(value, long):
         if not space.is_true(space.is_(w_inttype, space.w_int)):

Modified: pypy/dist/pypy/objspace/std/longtype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/longtype.py	(original)
+++ pypy/dist/pypy/objspace/std/longtype.py	Tue Apr 19 19:28:07 2005
@@ -1,5 +1,5 @@
 from pypy.objspace.std.stdtypedef import *
-from pypy.objspace.std.strutil import string_to_long
+from pypy.objspace.std.strutil import string_to_long, ParseStringError
 from pypy.interpreter.error import OperationError
 from pypy.objspace.std.inttype import int_typedef
 from pypy.interpreter.gateway import NoneNotWrapped
@@ -14,9 +14,9 @@
             try:
                 # XXX value can be unwrapped long
                 value = string_to_long(space.str_w(w_value))
-            except ValueError, e:
+            except ParseStringError, e:
                 raise OperationError(space.w_ValueError,
-                                     space.wrap(e.args[0]))
+                                     space.wrap(e.msg))
         else:
             # otherwise, use the __long__() method
             w_obj = space.long(w_value)
@@ -45,9 +45,9 @@
         try:
             # XXX value can be unwrapped long
             value = string_to_long(s, base)
-        except ValueError, e:
+        except ParseStringError, e:
             raise OperationError(space.w_ValueError,
-                                 space.wrap(e.args[0]))
+                                 space.wrap(e.msg))
 #XXX
     w_obj = space.allocate_instance(W_LongObject, w_longtype)
     w_obj.__init__(space, *args_from_long(value))

Modified: pypy/dist/pypy/objspace/std/strutil.py
==============================================================================
--- pypy/dist/pypy/objspace/std/strutil.py	(original)
+++ pypy/dist/pypy/objspace/std/strutil.py	Tue Apr 19 19:28:07 2005
@@ -18,6 +18,11 @@
 class InvalidLiteral(Exception):
     pass
 
+class ParseStringError(ValueError):
+    def __init__(self, msg):
+        self.args = (msg,)
+        self.msg = msg
+
 def _parse_string(s, literal, base, fname):
     # internal utility for string_to_int() and string_to_long().
     sign = 1
@@ -34,7 +39,7 @@
         else:
             base = 10
     elif base < 2 or base > 36:
-        raise ValueError, "%s() base must be >= 2 and <= 36" % (fname,)
+        raise ParseStringError, "%s() base must be >= 2 and <= 36" % (fname,)
     try:
         if not s:
             raise InvalidLiteral
@@ -58,14 +63,14 @@
         return result * sign
     except InvalidLiteral:
         if literal:
-            raise ValueError, 'invalid literal for %s(): %s' % (fname, literal)
+            raise ParseStringError, 'invalid literal for %s(): %s' % (fname, literal)
         else:
-            raise ValueError, 'empty literal for %s()' % (fname,)
+            raise ParseStringError, 'empty literal for %s()' % (fname,)
 
 def string_to_int(s, base=10):
     """Utility to converts a string to an integer (or possibly a long).
     If base is 0, the proper base is guessed based on the leading
-    characters of 's'.  Raises ValueError in case of error.
+    characters of 's'.  Raises ParseStringError (a subclass of ValueError) in case of error.
     """
     s = literal = strip_spaces(s)
     return _parse_string(s, literal, base, 'int')

Modified: pypy/dist/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/unicodeobject.py	Tue Apr 19 19:28:07 2005
@@ -1,7 +1,7 @@
 from pypy.objspace.std.objspace import *
 from pypy.objspace.std.fake import fake_type, wrap_exception
 from pypy.objspace.std.stringobject import W_StringObject
-from pypy.objspace.std.strutil import string_to_int, string_to_long
+from pypy.objspace.std.strutil import string_to_int, string_to_long, ParseStringError
 
 W_UnicodeObject = fake_type(unicode)
 
@@ -84,12 +84,16 @@
 def int__Unicode(space, w_uni):
     try:
         return space.wrap(string_to_int(unicode_to_decimal_w(space, w_uni)))
+    except ParseStringError, e:
+        raise OperationError(space.w_ValueError, space.wrap(e.msg))
     except:
         wrap_exception(space)
 
 def long__Unicode(space, w_uni):
     try:
         return space.wrap(string_to_long(unicode_to_decimal_w(space, w_uni)))
+    except ParseStringError, e:
+        raise OperationError(space.w_ValueError, space.wrap(e.msg))    
     except:
         wrap_exception(space)
 



More information about the Pypy-commit mailing list