Fixed loading profile with non-ASCII path on Windows

This commit is contained in:
Andrew Murray 2020-09-13 13:53:58 +10:00
parent 2e029d9a79
commit c196af6ddb
3 changed files with 24 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import datetime import datetime
import os import os
import re import re
import shutil
from io import BytesIO from io import BytesIO
import pytest import pytest
@ -436,6 +437,19 @@ def test_extended_information():
assert p.xcolor_space == "RGB " 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(): def test_profile_typesafety():
"""Profile init type safety """Profile init type safety

View File

@ -111,12 +111,12 @@ class TestImageFont:
with open(FONT_PATH, "rb") as f: with open(FONT_PATH, "rb") as f:
self._render(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")) tempfile = str(tmp_path / ("temp_" + chr(128) + ".ttf"))
try: try:
shutil.copy(FONT_PATH, tempfile) shutil.copy(FONT_PATH, tempfile)
except UnicodeEncodeError: except UnicodeEncodeError:
pytest.skip("Unicode path could not be created") pytest.skip("Non-ASCII path could not be created")
ImageFont.truetype(tempfile, FONT_SIZE) ImageFont.truetype(tempfile, FONT_SIZE)

View File

@ -159,6 +159,14 @@ class ImageCmsProfile:
""" """
if isinstance(profile, str): 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) self._set(core.profile_open(profile), profile)
elif hasattr(profile, "read"): elif hasattr(profile, "read"):
self._set(core.profile_frombytes(profile.read())) self._set(core.profile_frombytes(profile.read()))