From 327126b6496c94ab14d15e59095a5e8a754395a7 Mon Sep 17 00:00:00 2001 From: Rebecca Breu Date: Thu, 28 Dec 2023 14:46:54 +0100 Subject: [PATCH] Fix pasting images from Web when URL contanis non-ASCII chars (#87) --- beeref/fileio/image.py | 5 +++-- tests/fileio/test_image.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/beeref/fileio/image.py b/beeref/fileio/image.py index 91c4116..d68db80 100644 --- a/beeref/fileio/image.py +++ b/beeref/fileio/image.py @@ -84,9 +84,10 @@ def load_image(path): path = os.path.normpath(path.toLocalFile()) return (exif_rotated_image(path), path) + url = bytes(path.toEncoded()).decode() img = exif_rotated_image() try: - imgdata = request.urlopen(path.url()).read() + imgdata = request.urlopen(url).read() except URLError as e: logger.debug(f'Downloading image failed: {e.reason}') else: @@ -96,4 +97,4 @@ def load_image(path): f.write(imgdata) logger.debug(f'Temporarily saved in: {fname}') img = exif_rotated_image(fname) - return (img, path.url()) + return (img, url) diff --git a/tests/fileio/test_image.py b/tests/fileio/test_image.py index b39399f..f309217 100644 --- a/tests/fileio/test_image.py +++ b/tests/fileio/test_image.py @@ -91,6 +91,19 @@ def test_load_image_loads_from_existing_web_url(view, imgdata3x3): assert filename == url +@httpretty.activate +def test_load_image_loads_from_existing_web_url_non_ascii(view, imgdata3x3): + url = 'http://example.com/föö.png' + httpretty.register_uri( + httpretty.GET, + url, + body=imgdata3x3, + ) + img, filename = load_image(QtCore.QUrl(url)) + assert img.isNull() is False + assert filename == 'http://example.com/f%C3%B6%C3%B6.png' + + @httpretty.activate def test_load_image_loads_from_web_url_errors(view, imgfilename3x3): url = 'http://example.com/foo.png'