[meld] meldbuffer: Add an option for logging cache inconsistencies
- From: Kai Willadsen <kaiw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [meld] meldbuffer: Add an option for logging cache inconsistencies
- Date: Sat, 7 Aug 2021 23:17:16 +0000 (UTC)
commit aa01abf92a97b3192e741a50ebf90c51f0a0fa3e
Author: Kai Willadsen <kai willadsen gmail com>
Date: Sun Aug 8 08:28:58 2021 +1000
meldbuffer: Add an option for logging cache inconsistencies
This is disabled by default because it's not really that much of a
check, and also because what is the user going to do here anyway?
meld/meldbuffer.py | 14 ++++++++++++--
test/test_buffer_lines.py | 25 +++++++++++++++++++++++++
2 files changed, 37 insertions(+), 2 deletions(-)
---
diff --git a/meld/meldbuffer.py b/meld/meldbuffer.py
index fba5675d..55342f17 100644
--- a/meld/meldbuffer.py
+++ b/meld/meldbuffer.py
@@ -15,7 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
-from typing import List, Optional
+from typing import Any, List, Optional
from gi.repository import Gio, GLib, GObject, GtkSource
@@ -222,7 +222,7 @@ class BufferLines:
#: available.
lines: List[Optional[str]]
- def __init__(self, buf, textfilter=None):
+ def __init__(self, buf, textfilter=None, *, cache_debug: bool = False):
self.buf = buf
if textfilter is not None:
self.textfilter = textfilter
@@ -237,6 +237,16 @@ class BufferLines:
buf.connect("insert-text", self.on_insert_text),
buf.connect("delete-range", self.on_delete_range),
buf.connect_after("insert-text", self.after_insert_text),
+ if cache_debug:
+ buf.connect_after("insert-text", self._check_cache_invariant),
+ buf.connect_after("delete-range", self._check_cache_invariant),
+
+ def _check_cache_invariant(self, *args: Any) -> None:
+ if len(self.lines) != len(self):
+ log.error(
+ "Cache line count does not match buffer line count: "
+ f"{len(self.lines)} != {len(self)}",
+ )
def on_insert_text(self, buf, it, text, textlen):
buf.move_mark(self.mark, it)
diff --git a/test/test_buffer_lines.py b/test/test_buffer_lines.py
index f5e9b032..0830725d 100644
--- a/test/test_buffer_lines.py
+++ b/test/test_buffer_lines.py
@@ -119,3 +119,28 @@ def test_meld_buffer_delete_range(buffer_setup):
assert buffer_lines[5] == "7"
assert buffer_lines.lines[4:7] == ["4", "7", "8"]
+
+
+def test_meld_buffer_cache_debug(caplog, buffer_setup):
+
+ buffer, buffer_lines = buffer_setup
+ buffer_lines = BufferLines(buffer, cache_debug=True)
+
+ # Invalidate our line cache...
+ buffer_lines.lines.append("invalid")
+
+ # ...and check that insertion/deletion logs an error
+ buffer.insert(
+ buffer.get_iter_at_line(5),
+ "hey",
+ )
+ assert len(caplog.records) == 1
+ assert caplog.records[0].msg.startswith("Cache line count does not match")
+ caplog.clear()
+
+ buffer.delete(
+ buffer.get_iter_at_line(5),
+ buffer.get_iter_at_line(7),
+ )
+ assert len(caplog.records) == 1
+ assert caplog.records[0].msg.startswith("Cache line count does not match")
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]