diff --git a/spacy/lexeme.pyx b/spacy/lexeme.pyx index 10c934ba4..8c1b5f34c 100644 --- a/spacy/lexeme.pyx +++ b/spacy/lexeme.pyx @@ -40,6 +40,11 @@ cdef class Lexeme: assert self.c.orth == orth def __richcmp__(self, other, int op): + if other is None: + if op == 0 or op == 1 or op == 2: + return False + else: + return True if isinstance(other, Lexeme): a = self.orth b = other.orth diff --git a/spacy/tests/regression/test_issue1757.py b/spacy/tests/regression/test_issue1757.py new file mode 100644 index 000000000..384d3d067 --- /dev/null +++ b/spacy/tests/regression/test_issue1757.py @@ -0,0 +1,18 @@ +'''Test comparison against None doesn't cause segfault''' +from __future__ import unicode_literals + +from ...tokens import Doc +from ...vocab import Vocab + +def test_issue1757(): + doc = Doc(Vocab(), words=['a', 'b', 'c']) + assert not doc[0] < None + assert not doc[0] == None + assert doc[0] >= None + span = doc[:2] + assert not span < None + assert not span == None + assert span >= None + lex = vocab['a'] + assert not lex == None + assert not lex < None