@@ -49,44 +49,38 @@ class PpmImageFile(ImageFile.ImageFile):
4949 format = "PPM"
5050 format_description = "Pbmplus image"
5151
52- def _read_magic (self , magic = b"" ):
53- while True : # read until next whitespace
52+ def _read_magic (self ):
53+ magic = b""
54+ # read until whitespace or longest available magic number
55+ for _ in range (6 ):
5456 c = self .fp .read (1 )
5557 if not c or c in b_whitespace :
5658 break
5759 magic += c
58- if len (magic ) > 6 : # exceeded max magic number length
59- break
6060 return magic
6161
62- def _read_token (self , token = b"" ):
63- def _ignore_comment (): # ignores rest of the line; stops at CR, LF or EOF
64- while self .fp .read (1 ) not in b"\r \n " :
65- pass
66-
67- while True : # read until non-whitespace is found
62+ def _read_token (self ):
63+ token = b""
64+ while len (token ) <= 10 : # read until next whitespace or limit of 10 characters
6865 c = self .fp .read (1 )
69- if c == b"#" : # found comment, ignore it
70- _ignore_comment ()
71- continue
7266 if not c :
73- raise ValueError ("Reached EOF while reading header" )
74- if c in b_whitespace : # found whitespace, ignore it
75- continue
76- break
77-
78- token += c
79-
80- while True : # read until next whitespace
81- c = self .fp .read (1 )
82- if c == b"#" :
83- _ignore_comment ()
84- continue
85- if c in b_whitespace : # token ended
8667 break
68+ elif c in b_whitespace : # token ended
69+ if not token :
70+ # skip whitespace at start
71+ continue
72+ break
73+ elif c == b"#" :
74+ # ignores rest of the line; stops at CR, LF or EOF
75+ while self .fp .read (1 ) not in b"\r \n " :
76+ pass
77+ continue
8778 token += c
88- if len (token ) > 10 :
89- raise ValueError (f"Token too long in file header: { token } " )
79+ if not token :
80+ # Token was not even 1 byte
81+ raise ValueError ("Reached EOF while reading header" )
82+ elif len (token ) > 10 :
83+ raise ValueError (f"Token too long in file header: { token } " )
9084 return token
9185
9286 def _open (self ):
0 commit comments