Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r81531:6a1567a45cc7
Date: 2016-01-03 02:26 +0100
http://bitbucket.org/pypy/pypy/changeset/6a1567a45cc7/

Log:    More tests, more fixes

diff --git a/pypy/module/cpyext/longobject.py b/pypy/module/cpyext/longobject.py
--- a/pypy/module/cpyext/longobject.py
+++ b/pypy/module/cpyext/longobject.py
@@ -231,18 +231,20 @@
 
     # xxx not the most efficient implementation possible, but should work
     result = NULLRBIGINT
-    c = 0
+    most_significant = 0
 
     for i in range(0, n):
         if little_endian:
             c = intmask(bytes[n - i - 1])
         else:
             c = intmask(bytes[i])
+        if i == 0:
+            most_significant = c
 
         result = result.lshift(8)
         result = result.int_add(c)
 
-    if signed and c >= 0x80:
+    if signed and most_significant >= 0x80:
         result = result.sub(ONERBIGINT.lshift(8 * n))
 
     return space.newlong_from_rbigint(result)
diff --git a/pypy/module/cpyext/test/test_longobject.py 
b/pypy/module/cpyext/test/test_longobject.py
--- a/pypy/module/cpyext/test/test_longobject.py
+++ b/pypy/module/cpyext/test/test_longobject.py
@@ -180,6 +180,22 @@
         assert module.from_bytearray(False, False) == 0x9ABC
         assert module.from_bytearray(False, True) == -0x6544
 
+    def test_frombytearray_2(self):
+        module = self.import_extension('foo', [
+            ("from_bytearray", "METH_VARARGS",
+             """
+                 int little_endian, is_signed;
+                 if (!PyArg_ParseTuple(args, "ii", &little_endian, &is_signed))
+                     return NULL;
+                 return _PyLong_FromByteArray("\x9A\xBC\x41", 3,
+                                              little_endian, is_signed);
+             """),
+            ])
+        assert module.from_bytearray(True, False) == 0x41BC9A
+        assert module.from_bytearray(True, True) == 0x41BC9A
+        assert module.from_bytearray(False, False) == 0x9ABC41
+        assert module.from_bytearray(False, True) == -0x6543BF
+
     def test_fromunicode(self):
         module = self.import_extension('foo', [
             ("from_unicode", "METH_O",
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to