Commits

Christian Tismer  committed 79e2ad1

implementation with int.from_bytes done. Maybe emulate that for 2.x in compat.py?

  • Participants
  • Parent commits a94a36b

Comments (0)

Files changed (2)

File tiffany/from-pil-py3/TiffImagePlugin.py

 # Read TIFF files
 
 if sys.version_info[0] >= 3:
-    def il16(c,o=0):
-        return c[o] + (c[o+1] << 8)
-    def il32(c,o=0):
-        return c[o] + (c[o+1] << 8) + (c[o+2] << 16) + (c[o+3] << 24)
-    def ol16(i):
-        return bytes((i&255, i >> 8&255))
-    def ol32(i):
-        return bytes((i&255, i >> 8&255, i >> 16&255, i >> 24&255))
+    il16 = lambda c, o=0: int.from_bytes(c[o:o+2], byteorder='little') 
+    il32 = lambda c, o=0: int.from_bytes(c[o:o+4], byteorder='little')
+    ol16 = lambda i: i.to_bytes(2, byteorder='little')
+    ol32 = lambda i: i.to_bytes(4, byteorder='little')
     
-    def ib16(c,o=0):
-        return c[o+1] + (c[o] << 8)
-    def ib32(c,o=0):
-        return c[o+3] + (c[o+2] << 8) + (c[o+1] << 16) + (c[o] << 24)
-    def ob16(i):
-        return bytes((i >> 8&255, i&255))
-    def ob32(i):
-        return bytes((i >> 24&255, i >> 16&255, i >> 8&255, i&255))
+    ib16 = lambda c, o=0: int.from_bytes(c[o:o+2], byteorder='big') 
+    ib32 = lambda c, o=0: int.from_bytes(c[o:o+4], byteorder='big') 
+    ob16 = lambda i: i.to_bytes(2, byteorder='big')
+    ob32 = lambda i: i.to_bytes(4, byteorder='big')
 else:
     def il16(c,o=0):
         return ord(c[o]) + (ord(c[o+1])<<8)

File tiffany/test/test_tiffany.py

             if not name.startswith('__'):
                 locals()[name] = staticmethod(meth)
 
-tiffanydir = join(dirname(__file__), '..', '..')
-sys.path.insert(0, tiffanydir)
+try:
+    import tiffany
+except ImportError:
+    # when run from the source
+    tiffanydir = join(dirname(__file__), '..', '..')
+    sys.path.insert(0, tiffanydir)
 
 from tiffany.TiffImagePlugin import *
 from random import randint
 TEST_ARR = bytes(randint(0, 255) for i in range(8))
 
-def test_il16():
-    for ofs in range(len(TEST_ARR) - 2 + 1):
-        assert il16(TEST_ARR, ofs) == v0_6.il16(TEST_ARR, ofs)
+def _check(f1, f2, lng, g1, g2):
+    for ofs in range(len(TEST_ARR) - lng + 1):
+        v, w = f1(TEST_ARR, ofs), f2(TEST_ARR, ofs)
+        assert v == w
+        assert g1(v) == g2(w)
 
-def test_il32():
-    for ofs in range(len(TEST_ARR) - 4 + 1):
-        assert il32(TEST_ARR, ofs) == v0_6.il32(TEST_ARR, ofs)
+def test_l16():
+    _check(il16, v0_6.il16, 2, ol16, v0_6.ol16)
 
-def test_ib16():
-    for ofs in range(len(TEST_ARR) - 2 + 1):
-        assert ib16(TEST_ARR, ofs) == v0_6.ib16(TEST_ARR, ofs)
+def test_l32():
+    _check(il32, v0_6.il32, 4, ol32, v0_6.ol32)
 
-def test_ib32():
-    for ofs in range(len(TEST_ARR) - 4 + 1):
-        assert ib32(TEST_ARR, ofs) == v0_6.ib32(TEST_ARR, ofs)
+def test_b16():
+    _check(ib16, v0_6.ib16, 2, ob16, v0_6.ob16)
+
+def test_b32():
+    _check(ib32, v0_6.ib32, 4, ob32, v0_6.ob32)
+