[Python-modules-commits] [python-exif] 05/19: str / bytes coexistence support

Wolfgang Borgert debacle at moszumanska.debian.org
Mon Oct 6 22:18:38 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to reference refs/remotes/upstream/develop
in repository python-exif.

commit 78a5ba8d3f8fea5573a853655a7f97bb43e1feed
Author: velis74 <jure.erznoznik at gmail.com>
Date:   Mon Oct 28 04:18:12 2013 +0100

    str / bytes coexistence support
---
 exifread/__init__.py | 82 +++++++++++++++++++++++++++-------------------------
 1 file changed, 42 insertions(+), 40 deletions(-)

diff --git a/exifread/__init__.py b/exifread/__init__.py
index 897619f..d392867 100644
--- a/exifread/__init__.py
+++ b/exifread/__init__.py
@@ -1,6 +1,7 @@
 import logging
 from .classes import *
 from .tags import *
+from .utils import ord_
 
 __version__ = '1.4.1'
 
@@ -8,7 +9,7 @@ logger = logging.getLogger('exifread')
 
 
 def increment_base(data, base):
-    return ord(data[base+2]) * 256 + ord(data[base+3]) + 2
+    return ord_(data[base+2]) * 256 + ord_(data[base+3]) + 2
 
 def process_file(f, stop_tag=DEFAULT_STOP_TAG, details=True, strict=False, debug=False):
     """
@@ -23,26 +24,26 @@ def process_file(f, stop_tag=DEFAULT_STOP_TAG, details=True, strict=False, debug
 
     # determine whether it's a JPEG or TIFF
     data = f.read(12)
-    if data[0:4] in ['II*\x00', 'MM\x00*']:
+    if data[0:4] in [b'II*\x00', b'MM\x00*']:
         # it's a TIFF file
         logger.debug("TIFF format recognized in data[0:4]")
         f.seek(0)
         endian = f.read(1)
         f.read(1)
         offset = 0
-    elif data[0:2] == '\xFF\xD8':
+    elif data[0:2] == b'\xFF\xD8':
         # it's a JPEG file
-        logger.debug("JPEG format recognized data[0:2]=0x%X%X", ord(data[0]), ord(data[1]))
+        logger.debug("JPEG format recognized data[0:2]=0x%X%X", ord_(data[0]), ord_(data[1]))
         base = 2
         logger.debug("data[2]=0x%X data[3]=0x%X data[6:10]=%s",
-                     ord(data[2]), ord(data[3]), data[6:10])
-        while data[2] == '\xFF' and data[6:10] in ('JFIF', 'JFXX', 'OLYM', 'Phot'):
-            length = ord(data[4]) * 256 + ord(data[5])
+                     ord_(data[2]), ord_(data[3]), data[6:10])
+        while ord_(data[2]) == 0xFF and data[6:10] in (b'JFIF', b'JFXX', b'OLYM', b'Phot'):
+            length = ord_(data[4]) * 256 + ord_(data[5])
             logger.debug(" Length offset is %s", length)
             f.read(length-8)
             # fake an EXIF beginning of file
             # I don't think this is used. --gd
-            data = '\xFF\x00' + f.read(10)
+            data = b'\xFF\x00' + f.read(10)
             fake_exif = 1
             if base > 2:
                 logger.debug(" Added to base")
@@ -59,73 +60,73 @@ def process_file(f, stop_tag=DEFAULT_STOP_TAG, details=True, strict=False, debug
         # base = 2
         while 1:
             logger.debug(" Segment base 0x%X", base)
-            if data[base:base+2] == '\xFF\xE1':
+            if data[base:base+2] == b'\xFF\xE1':
                 # APP1
                 logger.debug("  APP1 at base 0x%X", base)
-                logger.debug("  Length: 0x%X 0x%X", ord(data[base+2]),
-                             ord(data[base+3]))
+                logger.debug("  Length: 0x%X 0x%X", ord_(data[base+2]),
+                             ord_(data[base+3]))
                 logger.debug("  Code: %s", data[base+4:base+8])
-                if data[base+4:base+8] == "Exif":
+                if data[base+4:base+8] == b"Exif":
                     logger.debug("  Decrement base by 2 to get to pre-segment header (for compatibility with later code)")
                     base = base-2
                     break
                 increment = increment_base(data, base)
                 logger.debug(" Increment base by %s", increment)
                 base = base + increment
-            elif data[base:base+2] == '\xFF\xE0':
+            elif data[base:base+2] == b'\xFF\xE0':
                 # APP0
                 logger.debug("  APP0 at base 0x%X", base)
-                logger.debug("  Length: 0x%X 0x%X", ord(data[base+2]),
-                             ord(data[base+3]))
+                logger.debug("  Length: 0x%X 0x%X", ord_(data[base+2]),
+                             ord_(data[base+3]))
                 logger.debug("  Code: %s", data[base+4:base+8])
                 increment = increment_base(data, base)
                 logger.debug(" Increment base by %s", increment)
                 base = base + increment
-            elif data[base:base+2] == '\xFF\xE2':
+            elif data[base:base+2] == b'\xFF\xE2':
                 # APP2
                 logger.debug("  APP2 at base 0x%X", base)
-                logger.debug("  Length: 0x%X 0x%X", ord(data[base+2]),
-                             ord(data[base+3]))
+                logger.debug("  Length: 0x%X 0x%X", ord_(data[base+2]),
+                             ord_(data[base+3]))
                 logger.debug(" Code: %s", data[base+4:base+8])
                 increment = increment_base(data, base)
                 logger.debug(" Increment base by %s", increment)
                 base = base + increment
-            elif data[base:base+2] == '\xFF\xEE':
+            elif data[base:base+2] == b'\xFF\xEE':
                 # APP14
                 logger.debug("  APP14 Adobe segment at base 0x%X", base)
-                logger.debug("  Length: 0x%X 0x%X", ord(data[base+2]),
-                             ord(data[base+3]))
+                logger.debug("  Length: 0x%X 0x%X", ord_(data[base+2]),
+                             ord_(data[base+3]))
                 logger.debug("  Code: %s", data[base+4:base+8])
                 increment = increment_base(data, base)
                 logger.debug(" Increment base by %s", increment)
                 base = base + increment
                 logger.debug("  There is useful EXIF-like data here, but we have no parser for it.")
-            elif data[base:base+2] == '\xFF\xDB':
+            elif data[base:base+2] == b'\xFF\xDB':
                 logger.debug("  JPEG image data at base 0x%X No more segments are expected.",
                              base)
                 break
-            elif data[base:base+2] == '\xFF\xD8':
+            elif data[base:base+2] == b'\xFF\xD8':
                 # APP12
                 logger.debug("  FFD8 segment at base 0x%X", base)
                 logger.debug("  Got 0x%X 0x%X and %s instead",
-                             ord(data[base]),
-                             ord(data[base+1]),
+                             ord_(data[base]),
+                             ord_(data[base+1]),
                              data[4+base:10+base])
-                logger.debug("  Length: 0x%X 0x%X", ord(data[base+2]),
-                             ord(data[base+3]))
+                logger.debug("  Length: 0x%X 0x%X", ord_(data[base+2]),
+                             ord_(data[base+3]))
                 logger.debug("  Code: %s", data[base+4:base+8])
                 increment = increment_base(data, base)
                 logger.debug("  Increment base by %s", increment)
                 base = base + increment
-            elif data[base:base+2] == '\xFF\xEC':
+            elif data[base:base+2] == b'\xFF\xEC':
                 # APP12
                 logger.debug("  APP12 XMP (Ducky) or Pictureinfo segment at base 0x%X",
                              base)
-                logger.debug("  Got 0x%X and 0x%X instead", ord(data[base]),
-                             ord(data[base+1]))
+                logger.debug("  Got 0x%X and 0x%X instead", ord_(data[base]),
+                             ord_(data[base+1]))
                 logger.debug("  Length: 0x%X 0x%X",
-                             ord(data[base+2]),
-                             ord(data[base+3]))
+                             ord_(data[base+2]),
+                             ord_(data[base+3]))
                 logger.debug("Code: %s", data[base+4:base+8])
                 increment = increment_base(data, base)
                 logger.debug("  Increment base by %s", increment)
@@ -135,8 +136,8 @@ def process_file(f, stop_tag=DEFAULT_STOP_TAG, details=True, strict=False, debug
                 try:
                     increment = increment_base(data, base)
                     logger.debug("  Got 0x%X and 0x%X instead",
-                                 ord(data[base]),
-                                 ord(data[base+1]))
+                                 ord_(data[base]),
+                                 ord_(data[base+1]))
                 except:
                     logger.debug("  Unexpected/unhandled segment type or file content.")
                     return {}
@@ -144,34 +145,35 @@ def process_file(f, stop_tag=DEFAULT_STOP_TAG, details=True, strict=False, debug
                     logger.debug("  Increment base by %s", increment)
                     base = base + increment
         f.seek(base + 12)
-        if data[2+base] == '\xFF' and data[6+base:10+base] == 'Exif':
+        if ord_(data[2+base]) == 0xFF and data[6+base:10+base] == b'Exif':
             # detected EXIF header
             offset = f.tell()
             endian = f.read(1)
             #HACK TEST:  endian = 'M'
-        elif data[2+base] == '\xFF' and data[6+base:10+base+1] == 'Ducky':
+        elif ord_(data[2+base]) == 0xFF and data[6+base:10+base+1] == b'Ducky':
             # detected Ducky header.
             logger.debug("EXIF-like header (normally 0xFF and code): 0x%X and %s",
-                         ord(data[2+base]) , data[6+base:10+base+1])
+                         ord_(data[2+base]) , data[6+base:10+base+1])
             offset = f.tell()
             endian = f.read(1)
-        elif data[2+base] == '\xFF' and data[6+base:10+base+1] == 'Adobe':
+        elif ord_(data[2+base]) == 0xFF and data[6+base:10+base+1] == b'Adobe':
             # detected APP14 (Adobe)
             logger.debug("EXIF-like header (normally 0xFF and code): 0x%X and %s",
-                         ord(data[2+base]) , data[6+base:10+base+1])
+                         ord_(data[2+base]) , data[6+base:10+base+1])
             offset = f.tell()
             endian = f.read(1)
         else:
             # no EXIF information
             logger.debug("No EXIF header expected data[2+base]==0xFF and data[6+base:10+base]===Exif (or Duck)")
             logger.debug("Did get 0x%X and %s",
-                         ord(data[2+base]), data[6+base:10+base+1])
+                         ord_(data[2+base]), data[6+base:10+base+1])
             return {}
     else:
         # file format not recognized
         logger.debug("File format not recognized.")
         return {}
 
+    endian = chr(ord_(endian[0]))
     # deal with the EXIF info we found
     logger.debug("Endian format is %s (%s)", endian, {
         'I': 'Intel',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-exif.git



More information about the Python-modules-commits mailing list