Hi,

I just see that function what() of imghdr module requires str type for 
argument h which is totally wrong! An image file is composed of bytes and not 
characters.

Attached patch should fix it. Notes:
 - I used .startswith() instead of h[:len(s)] == s
 - I used h[0] == ord(b'P') instead of h[0] == b'P' because the second syntax 
doesn't work (see my other email "bytes: compare bytes to integer")
- str is allowed but doesn't work: what() always returns None

I dislike "h[0] == ord(b'P')", in Python 2.x it's simply "h[0] == 'P'". A 
shorter syntax would be "h[0] == 80" but I prefer explicit test. It's maybe 
stupid, we manipulate bytes and not character, so "h[0] == 80" is 
acceptable... maybe with a comment?


imghdr is included in unit tests?


Victor Stinner
http://hachoir.org/
Index: Lib/imghdr.py
===================================================================
--- Lib/imghdr.py	(révision 56910)
+++ Lib/imghdr.py	(copie de travail)
@@ -36,14 +36,14 @@
 
 def test_rgb(h, f):
     """SGI image library"""
-    if h[:2] == '\001\332':
+    if h[:2] == b'\001\332':
         return 'rgb'
 
 tests.append(test_rgb)
 
 def test_gif(h, f):
     """GIF ('87 and '89 variants)"""
-    if h[:6] in ('GIF87a', 'GIF89a'):
+    if h[:6] in (b'GIF87a', b'GIF89a'):
         return 'gif'
 
 tests.append(test_gif)
@@ -51,7 +51,7 @@
 def test_pbm(h, f):
     """PBM (portable bitmap)"""
     if len(h) >= 3 and \
-        h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
+        h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r':
         return 'pbm'
 
 tests.append(test_pbm)
@@ -59,7 +59,7 @@
 def test_pgm(h, f):
     """PGM (portable graymap)"""
     if len(h) >= 3 and \
-        h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r':
+        h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r':
         return 'pgm'
 
 tests.append(test_pgm)
@@ -67,55 +67,54 @@
 def test_ppm(h, f):
     """PPM (portable pixmap)"""
     if len(h) >= 3 and \
-        h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r':
+        h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r':
         return 'ppm'
 
 tests.append(test_ppm)
 
 def test_tiff(h, f):
     """TIFF (can be in Motorola or Intel byte order)"""
-    if h[:2] in ('MM', 'II'):
+    if h[:2] in (b'MM', b'II'):
         return 'tiff'
 
 tests.append(test_tiff)
 
 def test_rast(h, f):
     """Sun raster file"""
-    if h[:4] == '\x59\xA6\x6A\x95':
+    if h[:4] == b'\x59\xA6\x6A\x95':
         return 'rast'
 
 tests.append(test_rast)
 
 def test_xbm(h, f):
     """X bitmap (X10 or X11)"""
-    s = '#define '
-    if h[:len(s)] == s:
+    if h.startswith(b'#define '):
         return 'xbm'
 
 tests.append(test_xbm)
 
 def test_jpeg(h, f):
     """JPEG data in JFIF format"""
-    if h[6:10] == 'JFIF':
+    if h[6:10] == b'JFIF':
         return 'jpeg'
 
 tests.append(test_jpeg)
 
 def test_exif(h, f):
     """JPEG data in Exif format"""
-    if h[6:10] == 'Exif':
+    if h[6:10] == b'Exif':
         return 'jpeg'
 
 tests.append(test_exif)
 
 def test_bmp(h, f):
-    if h[:2] == 'BM':
+    if h[:2] == b'BM':
         return 'bmp'
 
 tests.append(test_bmp)
 
 def test_png(h, f):
-    if h[:8] == "\211PNG\r\n\032\n":
+    if h[:8] == b"\211PNG\r\n\032\n":
         return 'png'
 
 tests.append(test_png)
_______________________________________________
Python-3000 mailing list
Python-3000@python.org
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to