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 1 commit into from Nov 11, 2020


Copy link

@radarhere radarhere commented Nov 11, 2020

Resolves #5036

from PIL import Image
im ='Tests/images/hopper.webp', formats=('WEBP',)) 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 only calls it if the preinit() formats do not match.

This PR resolves that.

Copy link

hugovk commented Nov 11, 2020

Can this be tested too?

Copy link
Member Author

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):
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


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

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
Copy link

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
None yet
None yet

Successfully merging this pull request may close these issues.

Unpopular formats argument of cause exceptions
2 participants