Skip to content

Commit 799e831

Browse files
committed
Separated out feature checking from selftest
1 parent a579a90 commit 799e831

File tree

2 files changed

+87
-32
lines changed

2 files changed

+87
-32
lines changed

PIL/features.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from PIL import Image
2+
3+
modules = {
4+
"PIL CORE": "PIL._imaging",
5+
"TKINTER": "PIL._imagingtk",
6+
"FREETYPE2": "PIL._imagingft",
7+
"LITTLECMS2": "PIL._imagingcms",
8+
"WEBP": "PIL._webp",
9+
"Transparent WEBP": ("WEBP", "WebPDecoderBuggyAlpha")
10+
}
11+
12+
13+
def check_module(feature):
14+
module = modules[feature]
15+
16+
method_to_call = None
17+
if type(module) is tuple:
18+
module, method_to_call = module
19+
20+
try:
21+
imported_module = __import__(module)
22+
except ImportError:
23+
# If a method is being checked, None means that
24+
# rather than the method failing, the module required for the method
25+
# failed to be imported first
26+
return None if method_to_call else False
27+
28+
if method_to_call:
29+
method = getattr(imported_module, method_to_call)
30+
return method() is True
31+
else:
32+
return True
33+
34+
35+
def get_supported_modules():
36+
supported_modules = []
37+
for feature in get_all_modules():
38+
if check_module(feature):
39+
supported_modules.append(feature)
40+
return supported_modules
41+
42+
43+
def get_all_modules():
44+
# While the dictionary keys could be used here,
45+
# a static list is used to maintain order
46+
return ["PIL CORE", "TKINTER", "FREETYPE2",
47+
"LITTLECMS2", "WEBP", "Transparent WEBP"]
48+
49+
codecs = {
50+
"JPEG": "jpeg",
51+
"JPEG 2000": "jpeg2k",
52+
"ZLIB (PNG/ZIP)": "zip",
53+
"LIBTIFF": "libtiff"
54+
}
55+
56+
57+
def check_codec(feature):
58+
codec = codecs[feature]
59+
return codec + "_encoder" in dir(Image.core)
60+
61+
62+
def get_supported_codecs():
63+
supported_codecs = []
64+
for feature in get_all_codecs():
65+
if check_codec(feature):
66+
supported_codecs.append(feature)
67+
return supported_codecs
68+
69+
70+
def get_all_codecs():
71+
return ["JPEG", "JPEG 2000", "ZLIB (PNG/ZIP)", "LIBTIFF"]

selftest.py

+16-32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
del sys.path[0]
1010

1111
from PIL import Image, ImageDraw, ImageFilter, ImageMath
12+
from PIL import features
1213

1314
if "--installed" in sys.argv:
1415
sys.path.insert(0, sys_path_0)
@@ -162,22 +163,6 @@ def testimage():
162163
"""
163164

164165

165-
def check_module(feature, module):
166-
try:
167-
__import__(module)
168-
except ImportError:
169-
print("***", feature, "support not installed")
170-
else:
171-
print("---", feature, "support ok")
172-
173-
174-
def check_codec(feature, codec):
175-
if codec + "_encoder" not in dir(Image.core):
176-
print("***", feature, "support not installed")
177-
else:
178-
print("---", feature, "support ok")
179-
180-
181166
if __name__ == "__main__":
182167
# check build sanity
183168

@@ -189,23 +174,22 @@ def check_codec(feature, codec):
189174
print("Python modules loaded from", os.path.dirname(Image.__file__))
190175
print("Binary modules loaded from", os.path.dirname(Image.core.__file__))
191176
print("-"*68)
192-
check_module("PIL CORE", "PIL._imaging")
193-
check_module("TKINTER", "PIL._imagingtk")
194-
check_codec("JPEG", "jpeg")
195-
check_codec("JPEG 2000", "jpeg2k")
196-
check_codec("ZLIB (PNG/ZIP)", "zip")
197-
check_codec("LIBTIFF", "libtiff")
198-
check_module("FREETYPE2", "PIL._imagingft")
199-
check_module("LITTLECMS2", "PIL._imagingcms")
200-
check_module("WEBP", "PIL._webp")
201-
try:
202-
from PIL import _webp
203-
if _webp.WebPDecoderBuggyAlpha():
204-
print("***", "Transparent WEBP", "support not installed")
177+
for feature in features.get_all_modules():
178+
supported = features.check_module(feature)
179+
180+
if supported is None:
181+
# A method was being tested, but the module required
182+
# for the method could not be correctly imported
183+
pass
184+
elif supported:
185+
print("---", feature, "support ok")
186+
else:
187+
print("***", feature, "support not installed")
188+
for feature in features.get_all_codecs():
189+
if features.check_codec(feature):
190+
print("---", feature, "support ok")
205191
else:
206-
print("---", "Transparent WEBP", "support ok")
207-
except Exception:
208-
pass
192+
print("***", feature, "support not installed")
209193
print("-"*68)
210194

211195
# use doctest to make sure the test program behaves as documented!

0 commit comments

Comments
 (0)