From 6077611646b1bb77bb86f60ac7bd6a3bf65d9ff0 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 20 Jan 2018 11:23:49 +1000 Subject: [PATCH] prevent exceptions when invalid utf-8 in db --- anki/db.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/anki/db.py b/anki/db.py index 0a2fb4112..afd0ae26e 100644 --- a/anki/db.py +++ b/anki/db.py @@ -12,6 +12,7 @@ DBError = sqlite.Error class DB: def __init__(self, path, timeout=0): self._db = sqlite.connect(path, timeout=timeout) + self._db.text_factory = self._textFactory self._path = path self.echo = os.environ.get("DBECHO") self.mod = False @@ -102,3 +103,7 @@ class DB: self._db.isolation_level = None else: self._db.isolation_level = '' + + # strip out invalid utf-8 when reading from db + def _textFactory(self, data): + return str(data, errors="ignore")