From c057ad1535f5ac6d324ef6a2e53caecb3d5e3c49 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 17 Feb 2020 14:22:15 -0800 Subject: [PATCH] Warn on typos passed to features.check() If the feature isn't one of the recognized types, a UserWarning is emitted. --- Tests/test_features.py | 7 +++++++ src/PIL/features.py | 17 +++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Tests/test_features.py b/Tests/test_features.py index 88d10f652b1..ceffecf6495 100644 --- a/Tests/test_features.py +++ b/Tests/test_features.py @@ -44,6 +44,13 @@ def test_check_modules(): assert features.check_codec(feature) in [True, False] +def test_check_warns_on_nonexistent(): + with pytest.warns(UserWarning) as cm: + has_feature = features.check("typo") + assert has_feature is False + assert str(cm[-1].message) == "Unknown feature 'typo'." + + def test_supported_modules(): assert isinstance(features.get_supported_modules(), list) assert isinstance(features.get_supported_codecs(), list) diff --git a/src/PIL/features.py b/src/PIL/features.py index 5822febabe3..0a1d5d61190 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -1,6 +1,7 @@ import collections import os import sys +import warnings import PIL @@ -76,14 +77,14 @@ def get_supported_features(): def check(feature): - return ( - feature in modules - and check_module(feature) - or feature in codecs - and check_codec(feature) - or feature in features - and check_feature(feature) - ) + if feature in modules: + return check_module(feature) + if feature in codecs: + return check_codec(feature) + if feature in features: + return check_feature(feature) + warnings.warn("Unknown feature '%s'." % feature, stacklevel=2) + return False def get_supported():