From f110cf4d53fd22ba191bfe54a97dfbcc385d2fa1 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Sat, 8 Apr 2017 17:18:20 -0400 Subject: [PATCH] Fix long hang after importing history. Turns out historyContains was getting called for the webkit backend multiple times when the browser starts. This was calling `url in history`, which was enumerating the entire history as `__contains__` was not defined. --- qutebrowser/browser/history.py | 3 +++ qutebrowser/misc/sql.py | 11 +++++++++++ tests/unit/browser/webkit/test_history.py | 8 ++++++++ tests/unit/misc/test_sql.py | 16 ++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/qutebrowser/browser/history.py b/qutebrowser/browser/history.py index 52d32c114..aef551e5c 100644 --- a/qutebrowser/browser/history.py +++ b/qutebrowser/browser/history.py @@ -83,6 +83,9 @@ class WebHistory(sql.SqlTable): def __repr__(self): return utils.get_repr(self, length=len(self)) + def __contains__(self, url): + return self.contains('url', url) + def _add_entry(self, entry): """Add an entry to the in-memory database.""" self.insert([entry.url_str(), entry.title, entry.atime, diff --git a/qutebrowser/misc/sql.py b/qutebrowser/misc/sql.py index 707e0c4ce..9cbf6024b 100644 --- a/qutebrowser/misc/sql.py +++ b/qutebrowser/misc/sql.py @@ -139,6 +139,17 @@ class SqlTable(QObject): rec = result.record() yield self.Entry(*[rec.value(i) for i in range(rec.count())]) + def contains(self, field, value): + """Return whether the table contains the matching item. + + Args: + field: Field to match. + value: Value to check for the given field. + """ + query = run_query("SELECT * FROM {} where {} = ? LIMIT 1" + .format(self._name, field), [value]) + return query.next() + def __len__(self): """Return the count of rows in the table.""" result = run_query("SELECT count(*) FROM {}".format(self._name)) diff --git a/tests/unit/browser/webkit/test_history.py b/tests/unit/browser/webkit/test_history.py index b2642f7c8..ec36f7a88 100644 --- a/tests/unit/browser/webkit/test_history.py +++ b/tests/unit/browser/webkit/test_history.py @@ -63,6 +63,14 @@ def test_len(hist): assert len(hist) == 1 +def test_contains(hist): + hist.add_url(QUrl('http://www.example.com/'), title='Title', atime=12345) + assert 'http://www.example.com/' in hist + assert not 'www.example.com' in hist + assert not 'Title' in hist + assert not 12345 in hist + + def test_get_recent(hist): hist.add_url(QUrl('http://www.qutebrowser.org/'), atime=67890) hist.add_url(QUrl('http://example.com/'), atime=12345) diff --git a/tests/unit/misc/test_sql.py b/tests/unit/misc/test_sql.py index 387b5ce46..edc156a55 100644 --- a/tests/unit/misc/test_sql.py +++ b/tests/unit/misc/test_sql.py @@ -88,6 +88,22 @@ def test_len(): assert len(table) == 3 +def test_contains(): + table = sql.SqlTable('Foo', ['name', 'val', 'lucky']) + table.insert(['one', 1, False]) + table.insert(['nine', 9, False]) + table.insert(['thirteen', 13, True]) + assert table.contains('name', 'one') + assert table.contains('name', 'thirteen') + assert table.contains('val', 9) + assert table.contains('lucky', False) + assert table.contains('lucky', True) + assert not table.contains('name', 'oone') + assert not table.contains('name', 1) + assert not table.contains('name', '*') + assert not table.contains('val', 10) + + def test_delete_all(qtbot): table = sql.SqlTable('Foo', ['name', 'val', 'lucky']) table.insert(['one', 1, False])