Skip to content

Commit

Permalink
Fixed loading profile with non-Unicode path on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Sep 13, 2020
1 parent 2e029d9 commit fc32293
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Tests/test_imagecms.py
@@ -1,6 +1,7 @@
import datetime
import os
import re
import shutil
from io import BytesIO

import pytest
Expand Down Expand Up @@ -436,6 +437,19 @@ def truncate_tuple(tuple_or_float):
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
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_imagefont.py
Expand Up @@ -111,12 +111,12 @@ def test_font_with_open_file(self):
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)

Expand Down
8 changes: 8 additions & 0 deletions src/PIL/ImageCms.py
Expand Up @@ -159,6 +159,14 @@ def __init__(self, profile):
"""

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()))
Expand Down

0 comments on commit fc32293

Please sign in to comment.