Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r58696:2d96b0512485
Date: 2012-11-02 18:18 -0700
http://bitbucket.org/pypy/pypy/changeset/2d96b0512485/

Log:    fix from/to_bytes default signed=False, add some more tests thanks
        Kenny Levinsen

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
@@ -120,7 +120,7 @@
                              space.wrap("too many digits in integer"))
 
 @unwrap_spec(s='bufferstr', byteorder=str, signed=bool)
-def descr_from_bytes(space, w_cls, s, byteorder, signed):
+def descr_from_bytes(space, w_cls, s, byteorder, signed=False):
     try:
         bigint = rbigint.frombytes(s, byteorder=byteorder, signed=signed)
     except InvalidEndiannessError:
@@ -130,7 +130,7 @@
     return newbigint(space, w_cls, bigint)
 
 @unwrap_spec(nbytes=int, byteorder=str, signed=bool)
-def descr_to_bytes(space, w_obj, nbytes, byteorder, signed):
+def descr_to_bytes(space, w_obj, nbytes, byteorder, signed=False):
     try:
         byte_string = space.bigint_w(w_obj).tobytes(nbytes, 
byteorder=byteorder, signed=signed)
     except InvalidEndiannessError:
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
@@ -310,6 +310,16 @@
     def test_from_bytes(self):
         assert int.from_bytes(b'c', 'little') == 99
         assert int.from_bytes(b'\x01\x01', 'little') == 257
+        assert int.from_bytes(b'\x01\x00', 'big') == 256
+        assert int.from_bytes(b'\x00\x80', 'little', signed=True) == -32768
+        raises(TypeError, int.from_bytes, '', 'big')
+        raises(ValueError, int.from_bytes, b'c', 'foo')
+
+    def test_to_bytes(self):
+        assert 65535 .to_bytes(2, 'big') == b'\xff\xff'
+        assert (-8388608).to_bytes(3, 'little', signed=True) == b'\x00\x00\x80'
+        raises(OverflowError, (-5).to_bytes, 1, 'big')
+        raises(ValueError, (-5).to_bytes, 1, 'foo')
 
     def test_negative_zero(self):
         x = eval("-0")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to