[pypy-svn] r79262 - in pypy/branch/fast-forward/pypy/objspace/std: . test

afa at codespeak.net afa at codespeak.net
Thu Nov 18 18:16:45 CET 2010


Author: afa
Date: Thu Nov 18 18:16:44 2010
New Revision: 79262

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/inttype.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
Log:
Implement bytearray->int conversion


Modified: pypy/branch/fast-forward/pypy/objspace/std/inttype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/inttype.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/inttype.py	Thu Nov 18 18:16:44 2010
@@ -1,5 +1,6 @@
 from pypy.interpreter import gateway, typedef
 from pypy.interpreter.error import OperationError
+from pypy.interpreter.buffer import Buffer
 from pypy.objspace.std.register_all import register_all
 from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
 from pypy.objspace.std.strutil import (string_to_int, string_to_bigint,
@@ -62,6 +63,18 @@
 
 # ____________________________________________________________
 
+def string_to_int_or_long(space, string, base=10):
+    w_longval = None
+    value = 0
+    try:
+        value = string_to_int(string, base)
+    except ParseStringError, e:
+        raise OperationError(space.w_ValueError,
+                             space.wrap(e.msg))
+    except ParseStringOverflowError, e:
+        w_longval = retry_to_w_long(space, e.parser)
+    return value, w_longval
+
 def retry_to_w_long(space, parser, base=0):
     parser.rewind()
     try:
@@ -78,31 +91,35 @@
     w_value = w_x     # 'x' is the keyword argument name in CPython
     value = 0
     if w_base is None:
+        ok = False
         # check for easy cases
         if type(w_value) is W_IntObject:
             value = w_value.intval
+            ok = True
         elif space.is_true(space.isinstance(w_value, space.w_str)):
-            try:
-                value = string_to_int(space.str_w(w_value))
-            except ParseStringError, e:
-                raise OperationError(space.w_ValueError,
-                                     space.wrap(e.msg))
-            except ParseStringOverflowError, e:
-                 w_longval = retry_to_w_long(space, e.parser)
+            value, w_longval = string_to_int_or_long(space, space.str_w(w_value))
+            ok = True
         elif space.is_true(space.isinstance(w_value, space.w_unicode)):
             if space.config.objspace.std.withropeunicode:
                 from pypy.objspace.std.ropeunicodeobject import unicode_to_decimal_w
             else:
                 from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
             string = unicode_to_decimal_w(space, w_value)
-            try:
-                value = string_to_int(string)
-            except ParseStringError, e:
-                raise OperationError(space.w_ValueError,
-                                     space.wrap(e.msg))
-            except ParseStringOverflowError, e:
-                 w_longval = retry_to_w_long(space, e.parser)
+            value, w_longval = string_to_int_or_long(space, string)
+            ok = True
         else:
+            # If object supports the buffer interface
+            try:
+                w_buffer = space.buffer(w_value)
+            except OperationError, e:
+                if not e.match(space, space.w_TypeError):
+                    raise
+            else:
+                buf = space.interp_w(Buffer, w_buffer)
+                value, w_longval = string_to_int_or_long(space, buf.as_str())
+                ok = True
+
+        if not ok:
             # otherwise, use the __int__() then __trunc__() methods
             try:
                 w_obj = space.int(w_value)
@@ -139,13 +156,8 @@
                 raise OperationError(space.w_TypeError,
                                      space.wrap("int() can't convert non-string "
                                                 "with explicit base"))
-        try:
-            value = string_to_int(s, base)
-        except ParseStringError, e:
-            raise OperationError(space.w_ValueError,
-                                 space.wrap(e.msg))
-        except ParseStringOverflowError, e:
-            w_longval = retry_to_w_long(space, e.parser, base)
+
+        value, w_longval = string_to_int_or_long(space, s, base)
 
     if w_longval is not None:
         if not space.is_w(w_inttype, space.w_int):

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py	Thu Nov 18 18:16:44 2010
@@ -192,3 +192,6 @@
         u = b.decode('utf-8')
         assert isinstance(u, unicode)
         assert u == u'abcdefghi'
+
+    def test_int(self):
+        assert int(bytearray('-1234')) == -1234



More information about the Pypy-commit mailing list