Skip to content

Commit eb46d2e

Browse files
Merge pull request #1375 from benoit-pierre/fix_1374
steno_dictionary: drop support for longest key callbacks
2 parents f3b894d + 9fb42bb commit eb46d2e

File tree

6 files changed

+31
-70
lines changed

6 files changed

+31
-70
lines changed

news.d/api/1375.break.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The support for `StenoDictionary` and `StenoDictionaryCollection` longest key callbacks is gone, use the `longest_key` properties instead.

news.d/bugfix/1375.core.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a memory leak on reloading externally modified dictionaries.

plover/steno_dictionary.py

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class StenoDictionary:
3030

3131
def __init__(self):
3232
self._dict = {}
33-
self._longest_key_length = 0
34-
self._longest_listener_callbacks = set()
33+
self._longest_key = 0
3534
self.reverse = collections.defaultdict(list)
3635
# Case-insensitive reverse dict
3736
self.casereverse = collections.defaultdict(list)
@@ -165,40 +164,25 @@ def casereverse_lookup(self, value):
165164
return set(self.casereverse.get(value, ()))
166165

167166
@property
168-
def _longest_key(self):
169-
return self._longest_key_length
170-
171-
@_longest_key.setter
172-
def _longest_key(self, longest_key):
173-
if longest_key == self._longest_key_length:
174-
return
175-
self._longest_key_length = longest_key
176-
for callback in self._longest_listener_callbacks:
177-
callback(longest_key)
178-
179-
def add_longest_key_listener(self, callback):
180-
self._longest_listener_callbacks.add(callback)
181-
182-
def remove_longest_key_listener(self, callback):
183-
self._longest_listener_callbacks.remove(callback)
167+
def longest_key(self):
168+
return self._longest_key
184169

185170

186171
class StenoDictionaryCollection:
187172

188173
def __init__(self, dicts=[]):
189174
self.dicts = []
190175
self.filters = []
191-
self.longest_key = 0
192-
self.longest_key_callbacks = set()
193176
self.set_dicts(dicts)
194177

178+
@property
179+
def longest_key(self):
180+
if not self.dicts:
181+
return 0
182+
return max(d.longest_key for d in self.dicts)
183+
195184
def set_dicts(self, dicts):
196-
for d in self.dicts:
197-
d.remove_longest_key_listener(self._longest_key_listener)
198185
self.dicts = dicts[:]
199-
for d in self.dicts:
200-
d.add_longest_key_listener(self._longest_key_listener)
201-
self._longest_key_listener()
202186

203187
def _lookup(self, key, dicts=None, filters=()):
204188
if dicts is None:
@@ -320,19 +304,3 @@ def add_filter(self, f):
320304

321305
def remove_filter(self, f):
322306
self.filters.remove(f)
323-
324-
def add_longest_key_listener(self, callback):
325-
self.longest_key_callbacks.add(callback)
326-
327-
def remove_longest_key_listener(self, callback):
328-
self.longest_key_callbacks.remove(callback)
329-
330-
def _longest_key_listener(self, ignored=None):
331-
if self.dicts:
332-
new_longest_key = max(d.longest_key for d in self.dicts)
333-
else:
334-
new_longest_key = 0
335-
if new_longest_key != self.longest_key:
336-
self.longest_key = new_longest_key
337-
for c in self.longest_key_callbacks:
338-
c(new_longest_key)

plover/translation.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,7 @@ def translate(self, stroke):
208208

209209
def set_dictionary(self, d):
210210
"""Set the dictionary."""
211-
callback = self._dict_callback
212-
if self._dictionary:
213-
self._dictionary.remove_longest_key_listener(callback)
214211
self._dictionary = d
215-
d.add_longest_key_listener(callback)
216212

217213
def get_dictionary(self):
218214
return self._dictionary
@@ -275,9 +271,6 @@ def _resize_translations(self):
275271
self._state.restrict_size(max(self._dictionary.longest_key,
276272
self._undo_length))
277273

278-
def _dict_callback(self, value):
279-
self._resize_translations()
280-
281274
def get_state(self):
282275
"""Get the state of the translator."""
283276
return self._state
@@ -342,9 +335,10 @@ def _find_translation_helper(self, stroke, suffixes=()):
342335
# stroke and build the stroke list for translation.
343336
num_strokes = 1
344337
translation_count = 0
338+
longest_key = self._dictionary.longest_key
345339
for t in reversed(self._state.translations):
346340
num_strokes += len(t)
347-
if num_strokes > self._dictionary.longest_key:
341+
if num_strokes > longest_key:
348342
break
349343
translation_count += 1
350344
translation_index = len(self._state.translations) - translation_count

plover_build_utils/testing/steno_dictionary.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,31 +204,22 @@ class _WritableDictionaryTests:
204204

205205
def test_longest_key(self):
206206
'''
207-
Check `longest_key` support (including callbacks handling).
207+
Check `longest_key` support.
208208
'''
209209
assert self.DICT_SUPPORT_SEQUENCE_METHODS
210-
notifications = []
211-
def listener(longest_key):
212-
notifications.append(longest_key)
213210
d = self.DICT_CLASS()
214211
assert d.longest_key == 0
215-
d.add_longest_key_listener(listener)
216212
d[('S',)] = 'a'
217213
assert d.longest_key == 1
218-
assert notifications == [1]
219214
d[('S', 'S', 'S', 'S')] = 'b'
220215
assert d.longest_key == 4
221-
assert notifications == [1, 4]
222216
d[('S', 'S')] = 'c'
223217
assert d.longest_key == 4
224218
assert d[('S', 'S')] == 'c'
225-
assert notifications == [1, 4]
226219
del d[('S', 'S', 'S', 'S')]
227220
assert d.longest_key == 2
228-
assert notifications == [1, 4, 2]
229221
del d[('S',)]
230222
assert d.longest_key == 2
231-
assert notifications == [1, 4, 2]
232223
if self.DICT_SUPPORT_REVERSE_LOOKUP:
233224
assert d.reverse_lookup('c') == {('S', 'S')}
234225
else:
@@ -239,13 +230,10 @@ def listener(longest_key):
239230
assert d.casereverse_lookup('c') == set()
240231
d.clear()
241232
assert d.longest_key == 0
242-
assert notifications == [1, 4, 2, 0]
243233
assert d.reverse_lookup('c') == set()
244234
assert d.casereverse_lookup('c') == set()
245-
d.remove_longest_key_listener(listener)
246235
d[('S', 'S')] = 'c'
247236
assert d.longest_key == 2
248-
assert notifications == [1, 4, 2, 0]
249237

250238
def test_casereverse_del(self):
251239
'''

test/test_translation.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
from plover.oslayer.config import PLATFORM
1111
from plover.steno import Stroke, normalize_steno
12+
13+
import pytest
14+
1215
from plover.steno_dictionary import StenoDictionary, StenoDictionaryCollection
1316
from plover.translation import Translation, Translator, _State
1417
from plover.translation import escape_translation, unescape_translation
@@ -63,28 +66,34 @@ def setup_method(self):
6366
self.dc = StenoDictionaryCollection([self.d])
6467
self.t.set_dictionary(self.dc)
6568

66-
def test_dictionary_update_grows_size1(self):
67-
self.d[('S',)] = '1'
68-
self._check_size_call(1)
69-
70-
def test_dictionary_update_grows_size4(self):
71-
self.d[('S', 'PT', '-Z', 'TOP')] = 'hi'
72-
self._check_size_call(4)
69+
@pytest.mark.parametrize('key', (
70+
('S',),
71+
('S', 'PT', '-Z', 'TOP'),
72+
))
73+
def test_dictionary_update_grows_size(self, key):
74+
self.d[key] = 'key'
75+
self.t.translate(stroke('T-'))
76+
self._check_size_call(len(key))
7377

7478
def test_dictionary_update_no_grow(self):
7579
self.t.set_min_undo_length(4)
7680
self._check_size_call(4)
7781
self.clear()
7882
self.d[('S', 'T')] = 'nothing'
83+
self.t.translate(stroke('T-'))
7984
self._check_size_call(4)
8085

8186
def test_dictionary_update_shrink(self):
8287
self.d[('S', 'T', 'P', '-Z', '-D')] = '1'
88+
self.t.translate(stroke('T-'))
8389
self._check_size_call(5)
8490
self.clear()
8591
self.d[('A', 'P')] = '2'
86-
self._check_no_size_call()
92+
self.t.translate(stroke('T-'))
93+
self._check_size_call(5)
94+
self.clear()
8795
del self.d[('S', 'T', 'P', '-Z', '-D')]
96+
self.t.translate(stroke('T-'))
8897
self._check_size_call(2)
8998

9099
def test_dictionary_update_no_shrink(self):

0 commit comments

Comments
 (0)