From c196af6ddb520e11de06788d4dab11220b1f476f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 13 Sep 2020 13:53:58 +1000 Subject: [PATCH] Fixed loading profile with non-ASCII path on Windows --- Tests/test_imagecms.py | 14 ++++++++++++++ Tests/test_imagefont.py | 4 ++-- src/PIL/ImageCms.py | 8 ++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index e9149b843..72f732d78 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -1,6 +1,7 @@ import datetime import os import re +import shutil from io import BytesIO import pytest @@ -436,6 +437,19 @@ def test_extended_information(): assert p.xcolor_space == "RGB " +def test_non_ascii_path(tmp_path): + skip_missing() + tempfile = str(tmp_path / ("temp_" + chr(128) + ".icc")) + try: + shutil.copy(SRGB, tempfile) + except UnicodeEncodeError: + pytest.skip("Non-ASCII path could not be created") + + o = ImageCms.getOpenProfile(tempfile) + p = o.profile + assert p.model == "IEC 61966-2-1 Default RGB Colour Space - sRGB" + + def test_profile_typesafety(): """Profile init type safety diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index aa7ec6fa6..62b6230ae 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -111,12 +111,12 @@ class TestImageFont: with open(FONT_PATH, "rb") as f: self._render(f) - def test_non_unicode_path(self, tmp_path): + def test_non_ascii_path(self, tmp_path): tempfile = str(tmp_path / ("temp_" + chr(128) + ".ttf")) try: shutil.copy(FONT_PATH, tempfile) except UnicodeEncodeError: - pytest.skip("Unicode path could not be created") + pytest.skip("Non-ASCII path could not be created") ImageFont.truetype(tempfile, FONT_SIZE) diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index 3856cb843..8c4740ddc 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -159,6 +159,14 @@ class ImageCmsProfile: """ if isinstance(profile, str): + if sys.platform == "win32": + profile_bytes_path = profile.encode() + try: + profile_bytes_path.decode("ascii") + except UnicodeDecodeError: + with open(profile, "rb") as f: + self._set(core.profile_frombytes(f.read())) + return self._set(core.profile_open(profile), profile) elif hasattr(profile, "read"): self._set(core.profile_frombytes(profile.read()))