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

Fix issue where opening a file whose name contains characters not present in locale would cause a crash. #3177

Merged
merged 4 commits into from Nov 7, 2017

Conversation

lejar
Copy link
Contributor

@lejar lejar commented Oct 21, 2017

Fixes qutebrowser/qutebrowser/#1450


This change is Reviewable

present in locale would cause a crash.

Fixes qutebrowser/qutebrowser/1450
@lejar
Copy link
Contributor Author

lejar commented Oct 21, 2017

If there is a respective test I could write then if someone tells me where it goes I'd be more than happy to make it.

if os.path.exists(path):
log.url.debug("URL is a local file")
except UnicodeEncodeError:
path = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good idea to log this as debug, the same way the other branch gets logged.

@jgkamat
Copy link
Member

jgkamat commented Oct 21, 2017

I took a look at this, but

os.environ["LC_ALL"] = "C"

dosen't seem to actually set the locale, so I can't find a way to trigger this under tox. Even running tox with LC_ALL=C dosen't seem to work for me. The actual test should be simple, something like:

    def test_get_path_existing_unicode(self, os_mock):
        """Test unicode input on non-unicode systems don't crash."""
        os.environ["LC_ALL"] = "C"
        # os_mock.path.exists.return_value = False
        url = urlutils.get_path_if_valid("/föö", check_exists=True)
        assert url is None

But I don't know how to get this to reproduce reliably in tox.

if os.path.exists(path):
log.url.debug("URL is a local file")
except UnicodeEncodeError:
path = None
else:
path = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this else: anymore as we just do if path is not None: above now.

@The-Compiler
Copy link
Member

tests/end2end/test_invocations.py is the right place to add a test for this, as LC_ALL needs to be set before qutebrowser is launched. There's already test_ascii_locale there, might want to rename that to test_downloads_with_ascii_locale or so and write something similar for :open. Let me know if you need help!

@lejar
Copy link
Contributor Author

lejar commented Oct 22, 2017

I don't have the latest Qt installed on my machine so I'm using travis to check that the test I wrote succeeds while I install it.

log.url.debug("URL is a local file")
except UnicodeEncodeError:
log.url.debug(
"URL contains characters which are not present in the " \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the \ here, inside parentheses whitespace is ignored by Python.


https://github.com/qutebrowser/qutebrowser/issues/1450
"""
args = ['--temp-basedir'] + _base_args(request.config)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to also pass a file to open here on the commandline, as that seemed to trigger a slightly different bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't crash, but just shows URL_NOT_FOUND, which I think would be correct. Regardless, I added a test for it in case that breaks in the future.

Copy link
Member

@The-Compiler The-Compiler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't had time to take a closer look yet, but this seems to break the entire testsuite on Travis. Can you take a look at that please?

@lejar
Copy link
Contributor Author

lejar commented Oct 25, 2017

@The-Compiler I'm having a lot of trouble getting the tests to actually run on my machine. According to the tox output, the tests are forcing the webengine but they just fail. Do I need a special build of qt/pyqt?

@The-Compiler
Copy link
Member

Does it help if you run tox with -- --qute-bdd-webengine?

@The-Compiler
Copy link
Member

I've also tried to make the entire testsuite run with LC_ALL=C, with this patch:

diff --git a/pytest.ini b/pytest.ini
index 1dc9e3ba5..0cc5e49aa 100644
--- a/pytest.ini
+++ b/pytest.ini
@@ -26,6 +26,7 @@ markers =
     no_invalid_lines: Don't fail on unparseable lines in end2end tests
     issue2478: Tests which are broken on Windows with QtWebEngine, https://github.com/qutebrowser/qutebrowser/issues/2478
     fake_os: Fake utils.is_* to a fake operating system
+    unicode_locale: Tests which need an unicode locale to work
 qt_log_level_fail = WARNING
 qt_log_ignore =
     ^SpellCheck: .*
diff --git a/tests/conftest.py b/tests/conftest.py
index 301658d56..0c28ed17b 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -63,6 +63,8 @@ def _apply_platform_markers(config, item):
         ('no_ci', 'CI' in os.environ, "Skipped on CI."),
         ('issue2478', utils.is_windows and config.webengine,
          "Broken with QtWebEngine on Windows"),
+        ('unicode_locale', sys.getfilesystemencoding() == 'ascii',
+         "Skipped because of ASCII locale"),
     ]
 
     for searched_marker, condition, default_reason in markers:
diff --git a/tests/end2end/features/history.feature b/tests/end2end/features/history.feature
index 86e5d6f00..aa126c4f7 100644
--- a/tests/end2end/features/history.feature
+++ b/tests/end2end/features/history.feature
@@ -32,6 +32,7 @@ Feature: Page history
         Then the history should contain:
             http://localhost:(port)/data/title%20with%20spaces.html Test title
 
+    @unicode_locale
     Scenario: History item with umlauts
         When I open data/äöü.html
         Then the history should contain:
diff --git a/tests/end2end/test_invocations.py b/tests/end2end/test_invocations.py
index 1a91be2e0..921eb085a 100644
--- a/tests/end2end/test_invocations.py
+++ b/tests/end2end/test_invocations.py
@@ -70,6 +70,7 @@ def temp_basedir_env(tmpdir, short_tmpdir):
 
 
 @pytest.mark.linux
+@pytest.mark.unicode_locale
 def test_ascii_locale(request, server, tmpdir, quteproc_new):
     """Test downloads with LC_ALL=C set.
 
diff --git a/tests/unit/commands/test_userscripts.py b/tests/unit/commands/test_userscripts.py
index 9a6490aca..32f9a1bb9 100644
--- a/tests/unit/commands/test_userscripts.py
+++ b/tests/unit/commands/test_userscripts.py
@@ -241,9 +241,8 @@ def test_unicode_error(caplog, qtbot, py_proc, runner):
             runner.store_html('')
 
     assert len(caplog.records) == 1
-    expected = ("Invalid unicode in userscript output: 'utf-8' codec can't "
-                "decode byte 0x80 in position 0: invalid start byte")
-    assert caplog.records[0].message == expected
+    expected = "Invalid unicode in userscript output: "
+    assert caplog.records[0].message.startswith(expected)
 
 
 @pytest.mark.fake_os('unknown')

Continuing that effort once this PR is in.

@lejar
Copy link
Contributor Author

lejar commented Oct 29, 2017

@The-Compiler No I still get lots of errors. Here's a run-down of what I'm doing:
System information:

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.3 LTS
Release:	16.04
Codename:	xenial

$ python --version
Python 3.6.3

$ python -mpip freeze
attrs==17.2.0
colorama==0.3.9
cssutils==1.0.2
Jinja2==2.9.6
MarkupSafe==1.0
pkg-resources==0.0.0
Pygments==2.2.0
pyPEG2==2.15.2
PyYAML==3.12

PyQt is 5.9 (self compiled)
sip is 4.19.3 (self compiled)
Qt is 5.9.2 (self compiled)

Running the tests on commit 4c2aeb0

$ ls
config.py  doc  icons  LICENSE  MANIFEST.in  misc  pytest.ini  qutebrowser  qutebrowser.py  README.asciidoc  relative  requirements.txt  scripts  setup.py  tests  tox.ini  www
$ tox -e py36 -- --qute-bdd-webengine

The most common error I'm getting is

E       Failed: Got logging message on logger misc with level WARNING: YAML load took unusually long, please report this at https://github.com/qutebrowser/qutebrowser/issues/2777
E       duration: 14.380468s
E       PyYAML version: 3.12
E       C extension: False
E       Stack:

@The-Compiler
Copy link
Member

I've commented in #2777 about that issue. Can you please reply there?

@The-Compiler The-Compiler merged commit f67c445 into qutebrowser:master Nov 7, 2017
@The-Compiler
Copy link
Member

I haven't had time to look at why the testsuite is failing for you in that weird way yet, so I took over here to merge things - I hope you don't mind! 😉

The reason the (existing) tests were failing is because path wasn't set to None correctly if the file didn't exist - I fixed that in 9f89861.

The new tests also needed some fixes to work properly - I added them in fdc4343.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants