From f644adbb05d615a9902ef3643714d5fe8049cea3 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 2 Mar 2015 09:56:47 +1100 Subject: [PATCH] Fixed binary conversion bug, and added corresponding tests --- PIL/_binary.py | 8 ++++---- Tests/test_binary.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 Tests/test_binary.py diff --git a/PIL/_binary.py b/PIL/_binary.py index 89a05a46f..2f5e8ffd4 100644 --- a/PIL/_binary.py +++ b/PIL/_binary.py @@ -36,7 +36,7 @@ def i16le(c, o=0): c: string containing bytes to convert o: offset of bytes to convert in string """ - return unpack("H", c[o:o+2]) + return unpack(">H", c[o:o+2])[0] def i32be(c, o=0): - return unpack(">I", c[o:o+4]) + return unpack(">I", c[o:o+4])[0] # Output, le = little endian, be = big endian diff --git a/Tests/test_binary.py b/Tests/test_binary.py new file mode 100644 index 000000000..4d3fb5914 --- /dev/null +++ b/Tests/test_binary.py @@ -0,0 +1,28 @@ +from helper import unittest, PillowTestCase + +from PIL import _binary + +class TestBinary(PillowTestCase): + + def test_standard(self): + self.assertEqual(_binary.i8(b'*'), 42) + self.assertEqual(_binary.o8(42), b'*') + + def test_little_endian(self): + self.assertEqual(_binary.i16le(b'\xff\xff\x00\x00'), 65535) + self.assertEqual(_binary.i32le(b'\xff\xff\x00\x00'), 65535) + + self.assertEqual(_binary.o16le(65535), b'\xff\xff') + self.assertEqual(_binary.o32le(65535), b'\xff\xff\x00\x00') + + def test_big_endian(self): + self.assertEqual(_binary.i16be(b'\x00\x00\xff\xff'), 0) + self.assertEqual(_binary.i32be(b'\x00\x00\xff\xff'), 65535) + + self.assertEqual(_binary.o16be(65535), b'\xff\xff') + self.assertEqual(_binary.o32be(65535), b'\x00\x00\xff\xff') + +if __name__ == '__main__': + unittest.main() + +# End of file \ No newline at end of file