Commits

Mikhail Korobov  committed 08be59d

segfaulting test & more error handling

  • Participants
  • Parent commits 6535785

Comments (0)

Files changed (2)

File src/dawg.pyx

     cdef Dictionary dct
     cdef _dawg.Dawg dawg
 
-    def __dealloc__(self):
-        self.dct.Clear()
-        self.dawg.Clear()
-
     def __init__(self, arg=None, input_is_sorted=False):
         if arg is None:
             arg = []
             arg = sorted(arg)
         self._build_from_iterable(arg)
 
+    def __dealloc__(self):
+        self.dct.Clear()
+        self.dawg.Clear()
 
     def _build_from_iterable(self, iterable):
         cdef DawgBuilder dawg_builder
             dawg_builder.Insert(b_key)
 
         dawg_builder.Finish(&self.dawg)
-        _dictionary_builder.Build(self.dawg, &(self.dct))
+        if not _dictionary_builder.Build(self.dawg, &(self.dct)):
+            raise Exception("Can't build dictionary")
 
     def __contains__(self, key):
         if isinstance(key, unicode):
         if not self.completer:
             self.completer = new Completer(self.dct, self.guide)
 
-
     def __dealloc__(self):
         self.guide.Clear()
-        del self.completer
+        if self.completer:
+            del self.completer
 
     cpdef list keys(self, unicode prefix=""):
         cdef bytes b_prefix = prefix.encode('utf8')
             return res
 
         self.completer.Start(index, b_prefix)
+
         while self.completer.Next():
             key = (<char*>self.completer.key()).decode('utf8')
             res.append(key)

File tests/test_dawg.py

             d.load(path)
             assert "can't load _dawg.Dictionary" in e.args[0]
 
+    def test_no_segfaults_on_empty_dawg(self):
+        pytest.skip("this test segfaults")
+        d = dawg.CompletionDAWG([])
+        assert d.keys() == []
+