Author: Brian Kearns <[email protected]>
Branch: 
Changeset: r68444:21bfa9ad6eaf
Date: 2013-12-16 19:46 -0500
http://bitbucket.org/pypy/pypy/changeset/21bfa9ad6eaf/

Log:    support creating long from buffer

diff --git a/pypy/objspace/std/longtype.py b/pypy/objspace/std/longtype.py
--- a/pypy/objspace/std/longtype.py
+++ b/pypy/objspace/std/longtype.py
@@ -2,6 +2,7 @@
 from pypy.interpreter import typedef
 from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault,\
      interpindirect2app
+from pypy.interpreter.buffer import Buffer
 from pypy.objspace.std.model import W_Object
 from pypy.objspace.std.stdtypedef import StdTypeDef
 from rpython.rlib.rstring import ParseStringError
@@ -46,9 +47,17 @@
             return string_to_w_long(space, w_longtype,
                                     unicode_to_decimal_w(space, w_value))
         else:
-            raise operationerrfmt(space.w_TypeError,
-                "long() argument must be a string or a number, not '%T'",
-                w_value)
+            try:
+                w_buffer = space.buffer(w_value)
+            except OperationError, e:
+                if not e.match(space, space.w_TypeError):
+                    raise
+                raise operationerrfmt(space.w_TypeError,
+                    "long() argument must be a string or a number, not '%T'",
+                    w_value)
+            else:
+                buf = space.interp_w(Buffer, w_buffer)
+                return string_to_w_long(space, w_longtype, buf.as_str())
     else:
         base = space.int_w(w_base)
 
diff --git a/pypy/objspace/std/test/test_longobject.py 
b/pypy/objspace/std/test/test_longobject.py
--- a/pypy/objspace/std/test/test_longobject.py
+++ b/pypy/objspace/std/test/test_longobject.py
@@ -249,6 +249,8 @@
         n = -sys.maxint-1
         assert long(n) == n
         assert str(long(n)) == str(n)
+        a = buffer('123')
+        assert long(a) == 123L
 
     def test_huge_longs(self):
         import operator
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to