From bdfb9c60ccc21df278b4ee450e50d7627b367186 Mon Sep 17 00:00:00 2001 From: Christopher Pezley Date: Sat, 21 Oct 2017 21:01:07 +0200 Subject: [PATCH 1/4] Fix issue where opening a file whose name contains characters not present in locale would cause a crash. Fixes qutebrowser/qutebrowser/1450 --- qutebrowser/utils/urlutils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 709d7d732..dff1ef8f1 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -372,8 +372,14 @@ def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False): path = None if check_exists: - if path is not None and os.path.exists(path): - log.url.debug("URL is a local file") + if path is not None: + # If the path contains characters that the locale cannot handle, + # then we consider it as non-existent. + try: + if os.path.exists(path): + log.url.debug("URL is a local file") + except UnicodeEncodeError: + path = None else: path = None From 96eff656903787e7218e84ce931d9b75560d51c7 Mon Sep 17 00:00:00 2001 From: Christopher Pezley Date: Sun, 22 Oct 2017 18:41:29 +0200 Subject: [PATCH 2/4] Log when url contains characters not present in current locale. --- qutebrowser/utils/urlutils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index dff1ef8f1..e8dea98ae 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -373,15 +373,14 @@ def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False): if check_exists: if path is not None: - # If the path contains characters that the locale cannot handle, - # then we consider it as non-existent. try: if os.path.exists(path): log.url.debug("URL is a local file") except UnicodeEncodeError: + log.url.debug( + "URL contains characters which are not present in the " \ + "current locale") path = None - else: - path = None return path From f53d8135b0677f8e7987f8199c9fddebd59bee76 Mon Sep 17 00:00:00 2001 From: Christopher Pezley Date: Sun, 22 Oct 2017 19:39:46 +0200 Subject: [PATCH 3/4] Add test for opening non-ascii paths. --- tests/end2end/test_invocations.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/end2end/test_invocations.py b/tests/end2end/test_invocations.py index 1a91be2e0..d119e2da0 100644 --- a/tests/end2end/test_invocations.py +++ b/tests/end2end/test_invocations.py @@ -70,7 +70,7 @@ def temp_basedir_env(tmpdir, short_tmpdir): @pytest.mark.linux -def test_ascii_locale(request, server, tmpdir, quteproc_new): +def test_downloads_with_ascii_locale(request, server, tmpdir, quteproc_new): """Test downloads with LC_ALL=C set. https://github.com/qutebrowser/qutebrowser/issues/908 @@ -102,6 +102,23 @@ def test_ascii_locale(request, server, tmpdir, quteproc_new): assert (tmpdir / '?-issue908.bin').exists() +@pytest.mark.linux +def test_open_with_ascii_locale(request, server, tmpdir, quteproc_new): + """Test opening non-ascii URL with LC_ALL=C set. + + https://github.com/qutebrowser/qutebrowser/issues/1450 + """ + args = ['--temp-basedir'] + _base_args(request.config) + quteproc_new.start(args, env={'LC_ALL': 'C'}) + + # Test opening a file whose name contains non-ascii characters. + # No exception thrown means test success. + url = 'file:///föö.html' + quteproc_new.send_cmd(':open {}'.format(url)) + quteproc_new.wait_for(category='url', + message='URL contains characters *') + + @pytest.mark.linux def test_misconfigured_user_dirs(request, server, temp_basedir_env, tmpdir, quteproc_new): From f67c445f3dad94996c9c24f00d5846a64dd08468 Mon Sep 17 00:00:00 2001 From: Christopher Pezley Date: Mon, 23 Oct 2017 22:27:00 +0200 Subject: [PATCH 4/4] Add test for opening non-ascii paths from command line. --- tests/end2end/test_invocations.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/end2end/test_invocations.py b/tests/end2end/test_invocations.py index d119e2da0..21ec01ab6 100644 --- a/tests/end2end/test_invocations.py +++ b/tests/end2end/test_invocations.py @@ -119,6 +119,18 @@ def test_open_with_ascii_locale(request, server, tmpdir, quteproc_new): message='URL contains characters *') +@pytest.mark.linux +def test_open_command_line_with_ascii_locale(request, server, tmpdir, quteproc_new): + """Test opening file from the command line with a non-ascii name with LC_ALL=C set. + + https://github.com/qutebrowser/qutebrowser/issues/1450 + """ + # The file does not actually have to exist because the relevant checks will + # all be called. No exception thrown means test success. + args = ['--temp-basedir'] + _base_args(request.config) + ['/home/user/föö.html'] + quteproc_new.start(args, env={'LC_ALL': 'C'}) + + @pytest.mark.linux def test_misconfigured_user_dirs(request, server, temp_basedir_env, tmpdir, quteproc_new):