Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0908440

Browse files
authored
Merge pull request #13247 from anntzer/pdfpng
Simplify and optimize png writing in backend_pdf.
2 parents 5702999 + a08370e commit 0908440

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,22 +1345,16 @@ def _writePng(self, data):
13451345
Write the image *data* into the pdf file using png
13461346
predictors with Flate compression.
13471347
"""
1348-
13491348
buffer = BytesIO()
13501349
_png.write_png(data, buffer)
13511350
buffer.seek(8)
1352-
written = 0
1353-
header = bytearray(8)
13541351
while True:
1355-
n = buffer.readinto(header)
1356-
assert n == 8
1357-
length, type = struct.unpack(b'!L4s', bytes(header))
1352+
length, type = struct.unpack(b'!L4s', buffer.read(8))
13581353
if type == b'IDAT':
1359-
data = bytearray(length)
1360-
n = buffer.readinto(data)
1361-
assert n == length
1362-
self.currentstream.write(bytes(data))
1363-
written += n
1354+
data = buffer.read(length)
1355+
if len(data) != length:
1356+
raise RuntimeError("truncated data")
1357+
self.currentstream.write(data)
13641358
elif type == b'IEND':
13651359
break
13661360
else:

0 commit comments

Comments
 (0)