Skip to content

Commit 3cfdef3

Browse files
authored
Merge pull request #7047 from nulano/freetype-import
Do not discard error message if _imagingft fails to import
2 parents ac78780 + 3d4e9b1 commit 3cfdef3

File tree

3 files changed

+13
-29
lines changed

3 files changed

+13
-29
lines changed

Diff for: src/PIL/ImageFont.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,12 @@ def __getattr__(name):
5454
raise AttributeError(msg)
5555

5656

57-
class _ImagingFtNotInstalled:
58-
# module placeholder
59-
def __getattr__(self, id):
60-
msg = "The _imagingft C module is not installed"
61-
raise ImportError(msg)
62-
63-
6457
try:
6558
from . import _imagingft as core
66-
except ImportError:
67-
core = _ImagingFtNotInstalled()
59+
except ImportError as ex:
60+
from ._util import DeferredError
61+
62+
core = DeferredError(ex)
6863

6964

7065
_UNSPECIFIED = object()

Diff for: src/PIL/features.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ def check_module(feature):
3333
try:
3434
__import__(module)
3535
return True
36-
except ImportError:
36+
except ModuleNotFoundError:
37+
return False
38+
except ImportError as ex:
39+
warnings.warn(str(ex))
3740
return False
3841

3942

@@ -145,7 +148,10 @@ def check_feature(feature):
145148
try:
146149
imported_module = __import__(module, fromlist=["PIL"])
147150
return getattr(imported_module, flag)
148-
except ImportError:
151+
except ModuleNotFoundError:
152+
return None
153+
except ImportError as ex:
154+
warnings.warn(str(ex))
149155
return None
150156

151157

Diff for: src/_imagingft.c

+1-18
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@
3333
#include FT_COLOR_H
3434
#endif
3535

36-
#define KEEP_PY_UNICODE
37-
38-
#if !defined(FT_LOAD_TARGET_MONO)
39-
#define FT_LOAD_TARGET_MONO FT_LOAD_MONOCHROME
40-
#endif
41-
4236
/* -------------------------------------------------------------------- */
4337
/* error table */
4438

@@ -420,11 +414,9 @@ text_layout_fallback(
420414
if (mask) {
421415
load_flags |= FT_LOAD_TARGET_MONO;
422416
}
423-
#ifdef FT_LOAD_COLOR
424417
if (color) {
425418
load_flags |= FT_LOAD_COLOR;
426419
}
427-
#endif
428420
for (i = 0; font_getchar(string, i, &ch); i++) {
429421
(*glyph_info)[i].index = FT_Get_Char_Index(self->face, ch);
430422
error = FT_Load_Glyph(self->face, (*glyph_info)[i].index, load_flags);
@@ -581,11 +573,9 @@ font_getsize(FontObject *self, PyObject *args) {
581573
if (mask) {
582574
load_flags |= FT_LOAD_TARGET_MONO;
583575
}
584-
#ifdef FT_LOAD_COLOR
585576
if (color) {
586577
load_flags |= FT_LOAD_COLOR;
587578
}
588-
#endif
589579

590580
/*
591581
* text bounds are given by:
@@ -844,11 +834,9 @@ font_render(FontObject *self, PyObject *args) {
844834
if (mask) {
845835
load_flags |= FT_LOAD_TARGET_MONO;
846836
}
847-
#ifdef FT_LOAD_COLOR
848837
if (color) {
849838
load_flags |= FT_LOAD_COLOR;
850839
}
851-
#endif
852840

853841
/*
854842
* calculate x_min and y_max
@@ -958,13 +946,11 @@ font_render(FontObject *self, PyObject *args) {
958946
/* bitmap is now FT_PIXEL_MODE_GRAY, fall through */
959947
case FT_PIXEL_MODE_GRAY:
960948
break;
961-
#ifdef FT_LOAD_COLOR
962949
case FT_PIXEL_MODE_BGRA:
963950
if (color) {
964951
break;
965952
}
966953
/* we didn't ask for color, fall through to default */
967-
#endif
968954
default:
969955
PyErr_SetString(PyExc_OSError, "unsupported bitmap pixel mode");
970956
goto glyph_error;
@@ -995,7 +981,6 @@ font_render(FontObject *self, PyObject *args) {
995981
} else {
996982
target = im->image8[yy] + xx;
997983
}
998-
#ifdef FT_LOAD_COLOR
999984
if (color && bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
1000985
/* paste color glyph */
1001986
for (k = x0; k < x1; k++) {
@@ -1010,9 +995,7 @@ font_render(FontObject *self, PyObject *args) {
1010995
target[k * 4 + 3] = source[k * 4 + 3];
1011996
}
1012997
}
1013-
} else
1014-
#endif
1015-
if (bitmap.pixel_mode == FT_PIXEL_MODE_GRAY) {
998+
} else if (bitmap.pixel_mode == FT_PIXEL_MODE_GRAY) {
1016999
if (color) {
10171000
unsigned char *ink = (unsigned char *)&foreground_ink;
10181001
for (k = x0; k < x1; k++) {

0 commit comments

Comments
 (0)