Skip to content

Conversation

@radarhere
Copy link
Member

In ImageFile, the encoder cleanup() method is not called from Python if there is an error.

Pillow/src/PIL/ImageFile.py

Lines 505 to 520 in 2f2b48d

e = Image._getencoder(im.mode, e, a, im.encoderconfig)
if o > 0:
fp.seek(o)
e.setimage(im.im, b)
if e.pushes_fd:
e.setfd(fp)
l, s = e.encode_to_pyfd()
else:
while True:
l, s, d = e.encode(bufsize)
fp.write(d)
if s:
break
if s < 0:
raise OSError(f"encoder error {s} when writing image file") from exc
e.cleanup()

However, cleanup() would still be called from _dealloc in C.

Pillow/src/encode.c

Lines 94 to 98 in 2f2b48d

static void
_dealloc(ImagingEncoderObject *encoder) {
if (encoder->cleanup) {
encoder->cleanup(&encoder->state);
}

Except that doesn't help a PyEncoder. So this PR ensures cleanup() is called from ImageFile.

@hugovk hugovk merged commit c16737d into python-pillow:main Mar 6, 2022
@radarhere radarhere deleted the cleanup branch March 6, 2022 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants