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

init() if one of the formats is unrecognised #5037

Merged
merged 1 commit into from Nov 11, 2020

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented Nov 11, 2020

Resolves #5036

from PIL import Image
im = Image.open('Tests/images/hopper.webp', formats=('WEBP',))

Image.open calls preinit() first and then expects to loop through them. However, formats overrides this, and so WEBP is checked, which throws a KeyError because init() has not been called yet, as Image.open() only calls it if the preinit() formats do not match.

This PR resolves that.

@hugovk
Copy link
Member

@hugovk hugovk commented Nov 11, 2020

Can this be tested too?

@radarhere
Copy link
Member Author

@radarhere radarhere commented Nov 11, 2020

The key part of the change here is causing a module to be imported, so that Image.register_open is run.

Image.register_open(WebPImageFile.format, WebPImageFile, _accept)

The problem is that conftest

def pytest_report_header(config):
try:
from PIL import features
with io.StringIO() as out:
features.pilinfo(out=out, supported_formats=False)

calls Image.init() at the start of the tests

Pillow/src/PIL/features.py

Lines 216 to 230 in 2d6e51e

def pilinfo(out=None, supported_formats=True):
"""
Prints information about this installation of Pillow.
This function can be called with ``python -m PIL``.
:param out:
The output stream to print to. Defaults to ``sys.stdout`` if ``None``.
:param supported_formats:
If ``True``, a list of all supported image file formats will be printed.
"""
if out is None:
out = sys.stdout
Image.init()

So the plugins are already imported. I'm not seeing a way to re-import a Python module.

I think it would be possible to mock a plugin, but I feel like that would be overly complex.

@hugovk hugovk merged commit 0bb6a19 into python-pillow:master Nov 11, 2020
45 of 47 checks passed
@hugovk
Copy link
Member

@hugovk hugovk commented Nov 11, 2020

Okay, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

2 participants