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

Do not discard error message if _imagingft fails to import #7047

Merged
merged 3 commits into from Apr 1, 2023

Conversation

nulano
Copy link
Contributor

@nulano nulano commented Mar 31, 2023

Helps #7040

Changes:

  • use _util.DeferredError if _imagingft fails to import instead of a custom helper from before the fork (similarly to ImageCms, added in Readthedocs #600),
  • if a module fails to import but is present (i.e. triggers ImportError but not ModuleNotFoundError), raise a warning in PIL.features,
  • remove no longer needed backwards compatibility code:

I've tested these changes by compiling Pillow in a Fedora 26 Docker container (with FreeType 2.7.1 and no other optional dependencies) with an intentional error in _imagingft:

# python3.7 -c "import PIL.ImageFont
> PIL.ImageFont.core.test"
Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "/usr/lib64/python3.7/site-packages/PIL/_util.py", line 19, in __getattr__
    raise self.ex
  File "/usr/lib64/python3.7/site-packages/PIL/ImageFont.py", line 58, in <module>
    from . import _imagingft as core
ImportError: /usr/lib64/python3.7/site-packages/PIL/_imagingft.cpython-37m-x86_64-linux-gnu.so: undefined symbol: FT_Done_MM_Var
# python3.7 -m PIL
--------------------------------------------------------------------
Pillow 9.5.0.dev0
Python 3.7.0b4 (default, May 16 2018, 15:51:23)
       [GCC 7.3.1 20180130 (Red Hat 7.3.1-2)]
--------------------------------------------------------------------
Python modules loaded from /usr/lib64/python3.7/site-packages/PIL
Binary modules loaded from /usr/lib64/python3.7/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.5.0.dev0
--- TKINTER support ok, loaded 8.6
/usr/lib64/python3.7/site-packages/PIL/features.py:39: UserWarning: /usr/lib64/python3.7/site-packages/PIL/_imagingft.cpython-37m-x86_64-linux-gnu.so: undefined symbol: FT_Done_MM_Var
  warnings.warn(str(ex))
*** FREETYPE2 support not installed
*** LITTLECMS2 support not installed
*** WEBP support not installed
*** WEBP Transparency support not installed
*** WEBPMUX support not installed
*** WEBP Animation support not installed
--- JPEG support ok, compiled for libjpeg-turbo 1.5.3
*** OPENJPEG (JPEG2000) support not installed
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.11
*** LIBTIFF support not installed
/usr/lib64/python3.7/site-packages/PIL/features.py:154: UserWarning: /usr/lib64/python3.7/site-packages/PIL/_imagingft.cpython-37m-x86_64-linux-gnu.so: undefined symbol: FT_Done_MM_Var
  warnings.warn(str(ex))
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
...

@radarhere
Copy link
Member

Would you mind sharing more specific details about how to replicate the intentional error you tested with?

@nulano
Copy link
Contributor Author

nulano commented Mar 31, 2023

I commented out the following lines:

Pillow/src/_imagingft.c

Lines 1068 to 1069 in 93afedf

#if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR > 9) || \
(FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 9 && FREETYPE_PATCH == 1)

#endif

Pillow/src/_imagingft.c

Lines 1266 to 1267 in 93afedf

#if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR > 9) || \
(FREETYPE_MAJOR == 2 && FREETYPE_MINOR == 9 && FREETYPE_PATCH == 1)

#endif

Then installed Pillow from source with pip as usual in a Fedora 26 Docker container which has FreeType 2.7.1.

@radarhere
Copy link
Member

I'm trying, but for reasons I don't understand, Pillow still thinks it has access to FreeType 2.12.1.

@nulano
Copy link
Contributor Author

nulano commented Mar 31, 2023

It looks like it downloaded the wheel: https://github.com/radarhere/docker-images/actions/runs/4575672322/jobs/8078793400#step:6:10950

For my tests I cloned my branch in ~/pillow, made the changes to src/_imagingft.c from my previous comment, then ran the following commands:

$ docker run -it --rm -v ~/pillow:/pillow fedora:26 bash
...
# cat /root/.bash_history
dnf install -y python37 gcc freetype-devel zlib-devel libjpeg-devel
cd /pillow/
python3.7 -m ensurepip
python3.7 -m pip install -U pip setuptools
python3.7 -m pip install . --no-cache-dir --ignore-installed
python3.7 -c "import PIL.ImageFont; PIL.ImageFont.core.test"
python3.7 -m PIL
history -a

@radarhere
Copy link
Member

You're right, I realised that shortly after posting. Thanks for the more in-depth instructions.

If anyone else is interested, here is the ImageFont import before and after, features.check_module before and after and features.check_feature before and after.

@hugovk hugovk merged commit 3cfdef3 into python-pillow:main Apr 1, 2023
64 of 65 checks passed
@nulano nulano deleted the freetype-import branch April 1, 2023 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants