Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure cleanup() is called for PyEncoders #6096

Merged
merged 3 commits into from Mar 6, 2022

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.

None yet

2 participants