From b18efc775d5de4736e29b938a56325a9ab785b4e Mon Sep 17 00:00:00 2001 From: nulano Date: Fri, 31 Mar 2023 01:48:17 +0200 Subject: [PATCH 1/3] do not discard ImportError message in ImageFont --- src/PIL/ImageFont.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 30f6694e6cd..e3f72f52e7c 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -54,17 +54,12 @@ def __getattr__(name): raise AttributeError(msg) -class _ImagingFtNotInstalled: - # module placeholder - def __getattr__(self, id): - msg = "The _imagingft C module is not installed" - raise ImportError(msg) - - try: from . import _imagingft as core -except ImportError: - core = _ImagingFtNotInstalled() +except ImportError as ex: + from ._util import DeferredError + + core = DeferredError(ex) _UNSPECIFIED = object() From e97167401104c75e04e7966978d0388886314dd2 Mon Sep 17 00:00:00 2001 From: nulano Date: Fri, 31 Mar 2023 02:08:58 +0200 Subject: [PATCH 2/3] cleanup in _imagingft --- src/_imagingft.c | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/src/_imagingft.c b/src/_imagingft.c index 4f44d6a7158..c0082b4a76f 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -33,12 +33,6 @@ #include FT_COLOR_H #endif -#define KEEP_PY_UNICODE - -#if !defined(FT_LOAD_TARGET_MONO) -#define FT_LOAD_TARGET_MONO FT_LOAD_MONOCHROME -#endif - /* -------------------------------------------------------------------- */ /* error table */ @@ -420,11 +414,9 @@ text_layout_fallback( if (mask) { load_flags |= FT_LOAD_TARGET_MONO; } -#ifdef FT_LOAD_COLOR if (color) { load_flags |= FT_LOAD_COLOR; } -#endif for (i = 0; font_getchar(string, i, &ch); i++) { (*glyph_info)[i].index = FT_Get_Char_Index(self->face, ch); error = FT_Load_Glyph(self->face, (*glyph_info)[i].index, load_flags); @@ -581,11 +573,9 @@ font_getsize(FontObject *self, PyObject *args) { if (mask) { load_flags |= FT_LOAD_TARGET_MONO; } -#ifdef FT_LOAD_COLOR if (color) { load_flags |= FT_LOAD_COLOR; } -#endif /* * text bounds are given by: @@ -844,11 +834,9 @@ font_render(FontObject *self, PyObject *args) { if (mask) { load_flags |= FT_LOAD_TARGET_MONO; } -#ifdef FT_LOAD_COLOR if (color) { load_flags |= FT_LOAD_COLOR; } -#endif /* * calculate x_min and y_max @@ -958,13 +946,11 @@ font_render(FontObject *self, PyObject *args) { /* bitmap is now FT_PIXEL_MODE_GRAY, fall through */ case FT_PIXEL_MODE_GRAY: break; -#ifdef FT_LOAD_COLOR case FT_PIXEL_MODE_BGRA: if (color) { break; } /* we didn't ask for color, fall through to default */ -#endif default: PyErr_SetString(PyExc_OSError, "unsupported bitmap pixel mode"); goto glyph_error; @@ -995,7 +981,6 @@ font_render(FontObject *self, PyObject *args) { } else { target = im->image8[yy] + xx; } -#ifdef FT_LOAD_COLOR if (color && bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) { /* paste color glyph */ for (k = x0; k < x1; k++) { @@ -1010,9 +995,7 @@ font_render(FontObject *self, PyObject *args) { target[k * 4 + 3] = source[k * 4 + 3]; } } - } else -#endif - if (bitmap.pixel_mode == FT_PIXEL_MODE_GRAY) { + } else if (bitmap.pixel_mode == FT_PIXEL_MODE_GRAY) { if (color) { unsigned char *ink = (unsigned char *)&foreground_ink; for (k = x0; k < x1; k++) { From 3d4e9b107d22a3a3c4dee6fb20eb25d6f2639da8 Mon Sep 17 00:00:00 2001 From: nulano Date: Fri, 31 Mar 2023 02:57:58 +0200 Subject: [PATCH 3/3] warn if module is found but fails to import in PIL.features --- src/PIL/features.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/PIL/features.py b/src/PIL/features.py index 6f9d99e765a..80a16a75e0c 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -33,7 +33,10 @@ def check_module(feature): try: __import__(module) return True - except ImportError: + except ModuleNotFoundError: + return False + except ImportError as ex: + warnings.warn(str(ex)) return False @@ -145,7 +148,10 @@ def check_feature(feature): try: imported_module = __import__(module, fromlist=["PIL"]) return getattr(imported_module, flag) - except ImportError: + except ModuleNotFoundError: + return None + except ImportError as ex: + warnings.warn(str(ex)) return None