Fix regression in 2.20 that prevented some plugins from updating their preferences

This commit is contained in:
Kovid Goyal 2015-02-21 14:07:49 +05:30
parent b10feb7273
commit 3146735962
2 changed files with 22 additions and 8 deletions

View File

@ -107,12 +107,10 @@ class DBPrefs(dict): # {{{
def __setitem__(self, key, val): def __setitem__(self, key, val):
if not self.disable_setting: if not self.disable_setting:
try:
current_val = self[key]
except KeyError:
current_val = object()
if val != current_val:
raw = self.to_raw(val) raw = self.to_raw(val)
with self.db.conn:
dbraw = self.db.execute('SELECT val FROM preferences WHERE key=?', (key,)).fetchone()
if dbraw != raw:
self.db.execute('INSERT OR REPLACE INTO preferences (key,val) VALUES (?,?)', (key, raw)) self.db.execute('INSERT OR REPLACE INTO preferences (key,val) VALUES (?,?)', (key, raw))
dict.__setitem__(self, key, val) dict.__setitem__(self, key, val)

View File

@ -7,6 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os
from collections import namedtuple from collections import namedtuple
from functools import partial from functools import partial
from io import BytesIO from io import BytesIO
@ -692,3 +693,18 @@ class WritingTest(BaseTest):
ae(c.field_for('tags', 3), (t.id_map[lid], t.id_map[norm])) ae(c.field_for('tags', 3), (t.id_map[lid], t.id_map[norm]))
# }}} # }}}
def test_preferences(self): # {{{
' Test getting and setting of preferences, especially with mutable objects '
cache = self.init_cache()
prefs = cache.backend.prefs
prefs['test mutable'] = [1, 2, 3]
a = prefs['test mutable']
a.append(4)
self.assertIn(4, prefs['test mutable'])
prefs['test mutable'] = a
prefs.load_from_db()
self.assertIn(4, prefs['test mutable'])
before = os.stat(cache.backend.library_path)
prefs['test mutable'] = a
self.assertEqual(before, os.stat(cache.backend.library_path), 'The database was written to despite there being no change in value')
# }}}