Skip to content

Commit 1bac1cf

Browse files
committed
Moved all comments logic into _ignore_comments()
1 parent 6eb6232 commit 1bac1cf

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

src/PIL/PpmImagePlugin.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,38 @@ def _find_comment_end(self, block, start=0):
156156
return min(a, b) if a * b > 0 else max(a, b) # lowest nonnegative index (or -1)
157157

158158
def _ignore_comments(self, block):
159-
"""
160-
Delete comments from block.
161-
If comment does not end in this block, raises a flag.
162-
"""
163-
comment_spans = False
159+
if self._comment_spans:
160+
# Finish current comment
161+
while block:
162+
comment_end = self._find_comment_end(block)
163+
if comment_end != -1:
164+
# Comment ends in this block
165+
# Delete tail of comment
166+
block = block[comment_end + 1 :]
167+
break
168+
else:
169+
# Comment spans whole block
170+
# So read the next block, looking for the end
171+
block = self._read_block()
172+
173+
# Search for any further comments
174+
self._comment_spans = False
164175
while True:
165-
comment_start = block.find(b"#") # look for next comment
166-
if comment_start == -1: # no comment found
176+
comment_start = block.find(b"#")
177+
if comment_start == -1:
178+
# No comment found
167179
break
168180
comment_end = self._find_comment_end(block, comment_start)
169-
if comment_end != -1: # comment ends in this block
170-
# delete comment
181+
if comment_end != -1:
182+
# Comment ends in this block
183+
# Delete comment
171184
block = block[:comment_start] + block[comment_end + 1 :]
172-
else: # last comment continues to next block(s)
185+
else:
186+
# Comment continues to next block(s)
173187
block = block[:comment_start]
174-
comment_spans = True
188+
self._comment_spans = True
175189
break
176-
return block, comment_spans
190+
return block
177191

178192
def _decode_bitonal(self):
179193
"""
@@ -183,22 +197,13 @@ def _decode_bitonal(self):
183197
data = bytearray()
184198
total_bytes = self.state.xsize * self.state.ysize
185199

186-
comment_spans = False
187200
while len(data) != total_bytes:
188201
block = self._read_block() # read next block
189202
if not block:
190203
# eof
191204
break
192205

193-
while block and comment_spans:
194-
comment_end = self._find_comment_end(block)
195-
if comment_end != -1: # comment ends in this block
196-
block = block[comment_end + 1 :] # delete tail of previous comment
197-
break
198-
else: # comment spans whole block
199-
block = self._read_block()
200-
201-
block, comment_spans = self._ignore_comments(block)
206+
block = self._ignore_comments(block)
202207

203208
tokens = b"".join(block.split())
204209
for token in tokens:
@@ -216,7 +221,6 @@ def _decode_blocks(self, maxval):
216221
bands = Image.getmodebands(self.mode)
217222
total_bytes = self.state.xsize * self.state.ysize * bands * out_byte_count
218223

219-
comment_spans = False
220224
half_token = False
221225
while len(data) != total_bytes:
222226
block = self._read_block() # read next block
@@ -227,15 +231,7 @@ def _decode_blocks(self, maxval):
227231
# eof
228232
break
229233

230-
while block and comment_spans:
231-
comment_end = self._find_comment_end(block)
232-
if comment_end != -1: # comment ends in this block
233-
block = block[comment_end + 1 :] # delete tail of previous comment
234-
break
235-
else: # comment spans whole block
236-
block = self._read_block()
237-
238-
block, comment_spans = self._ignore_comments(block)
234+
block = self._ignore_comments(block)
239235

240236
if half_token:
241237
block = half_token + block # stitch half_token to new block
@@ -264,6 +260,7 @@ def _decode_blocks(self, maxval):
264260
return data
265261

266262
def decode(self, buffer):
263+
self._comment_spans = False
267264
if self.mode == "1":
268265
data = self._decode_bitonal()
269266
rawmode = "1;8"

0 commit comments

Comments
 (0)