[Python-3000] Fix imghdr module for bytes (original) (raw)

Adam Olsen rhamph at gmail.com
Sat Aug 11 02:45:33 CEST 2007


On 8/10/07, Victor Stinner <victor.stinner at haypocalc.com> wrote:

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?

Try h[0:1] == b'P'. Slicing will ensure it stays as a bytes object, rather than just giving the integer it contains.

-- Adam Olsen, aka Rhamphoryncus



More information about the Python-3000 mailing list