Commits

Christian Tismer committed 49f4186

removed "if sys.version" stuff from TiffImagePlugin, added a derived int class.
Didn't patch built-ins to avoid problems in apps that rely on int for now.
Maybe this is version 0.6.1 ?
Stopping now for a while...

Comments (0)

Files changed (2)

tiffany/compat.py

 """
 compat.py should be imported first.
-It does certain patches and modifications for Python3 backward compatibility.
+It does certain patches and modifications for Python2 backward compatibility.
 """
 
 import sys
         class classmethod(builtins.classmethod):
             __func__ = property(lambda self: self.im_func)
         builtins.classmethod = classmethod
+
         class staticmethod(builtins.staticmethod):
             __func__ = property(lambda self: self.__get__(0))
         builtins.staticmethod = staticmethod
     _patch()
     del _patch
 
+#
 # circumventing the metaclass notation difference between py2 and py3
 # (borrowed from the "six" module)
+
+
 def with_metaclass(meta, base=object):
     """Create a base class with a metaclass."""
     return meta("NewBase", (base,), {})
+
+
+#
+# emulating int.from_bytes and int.to_bytes for python 2
+class _Int(int):
+    ''' Simple implementation of python 3.2 methods.
+        Can be made more general, but enough for tiffany
+    '''
+
+    @classmethod
+    def from_bytes(cls, bytes_, byteorder, signed=False):  # '*' ignored
+        c = bytes_
+        if signed:
+            raise NotImplementedError('signed is not implemented')
+        lng = len(bytes_)
+        if byteorder == 'little':
+            if lng == 2:
+                return ord(c[0]) + (ord(c[1]) << 8)
+            if lng == 4:
+                return (ord(c[0]) + (ord(c[1]) << 8) + (ord(c[2]) << 16) + 
+                       (ord(c[3]) << 24))
+        elif byteorder == 'big':
+            if lng == 2:
+                return ord(c[1]) + (ord(c[0]) << 8)
+            if lng == 4:
+                return (ord(c[3]) + (ord(c[2]) << 8) + (ord(c[1]) << 16) + 
+                       (ord(c[0]) << 24))
+        else:
+            raise ValueError("byteorder must be either 'little' or 'big'")
+        raise NotImplementedError("size other than 2 or 4 not implemented")
+
+    def to_bytes(self, lng, byteorder, signed=False):  # '*' ignored
+        i = self
+        if signed:
+            raise NotImplementedError('signed is not implemented')
+        if byteorder == 'little':
+            if lng == 2:
+                return chr(i & 255) + chr(i >> 8 & 255)
+            if lng == 4:
+                return (chr(i & 255) + chr(i >> 8 & 255) + 
+                       chr(i >> 16 & 255) + chr(i >> 24 & 255))
+        elif byteorder == 'big':
+            if lng == 2:
+                return chr(i >> 8 & 255) + chr(i & 255)
+            if lng == 4:
+                return (chr(i >> 24 & 255) + chr(i >> 16 & 255) + 
+                       chr(i >> 8 & 255) + chr(i & 255))
+        else:
+            raise ValueError("byteorder must be either 'little' or 'big'")
+        raise NotImplementedError("size other than 2 or 4 not implemented")
+
+
+if hasattr(int, 'from_bytes'):
+    int = int
+else:
+    int = _Int
+
+'''
+Remark:
+I could have been more rigorous and overwrite the builtins.int type
+with the new derived class. Then we could write
+
+    ol16 = lambda i: i.to_bytes(2, byteorder='little')
+
+instead of
+
+    ol16 = lambda i: int(i).to_bytes(2, byteorder='little')
+
+Maybe later. Right now I'm reluctant, in the fear to produce
+problems in applications just to make the code nicer.
+'''

tiffany/from-pil-py3/TiffImagePlugin.py

 __version__ = "1.3.6"
 
 import array, sys
-
+from tiffany.compat import int
 
 from . import Image
 from . import ImageFile
 # --------------------------------------------------------------------
 # Read TIFF files
 
-if sys.version_info[0] >= 3:
-    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')
-    
-    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)
-    def il32(c,o=0):
-        return ord(c[o]) + (ord(c[o+1])<<8) + (ord(c[o+2])<<16) + (ord(c[o+3])<<24)
-    def ol16(i):
-        return chr(i&255) + chr(i>>8&255)
-    def ol32(i):
-        return chr(i&255) + chr(i>>8&255) + chr(i>>16&255) + chr(i>>24&255)
-    
-    def ib16(c,o=0):
-        return ord(c[o+1]) + (ord(c[o])<<8)
-    def ib32(c,o=0):
-        return ord(c[o+3]) + (ord(c[o+2])<<8) + (ord(c[o+1])<<16) + (ord(c[o])<<24)
-    def ob16(i):
-        return chr(i>>8&255) + chr(i&255)
-    def ob32(i):
-        return chr(i>>24&255) + chr(i>>16&255) + chr(i>>8&255) + chr(i&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: int(i).to_bytes(2, byteorder='little')
+ol32 = lambda i: int(i).to_bytes(4, byteorder='little')
+
+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: int(i).to_bytes(2, byteorder='big')
+ob32 = lambda i: int(i).to_bytes(4, byteorder='big')
     
 # a few tag names, just to make the code below a bit more readable
 IMAGEWIDTH = 256