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

Fixed loading profile with non-ASCII path on Windows #4914

Merged
merged 1 commit into from Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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