From b66b8f028b256447d0694a5d8cb04b6554e2e2d0 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Tue, 24 Oct 2017 12:10:39 +0200 Subject: [PATCH] Fix #1375 -- out-of-bounds on token.nbor() --- spacy/tests/regression/test_issue1375.py | 2 +- spacy/tokens/token.pyx | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/spacy/tests/regression/test_issue1375.py b/spacy/tests/regression/test_issue1375.py index 72070758d..6f74d9a6d 100644 --- a/spacy/tests/regression/test_issue1375.py +++ b/spacy/tests/regression/test_issue1375.py @@ -3,7 +3,7 @@ import pytest from ...vocab import Vocab from ...tokens.doc import Doc -@pytest.mark.xfail + def test_issue1375(): '''Test that token.nbor() raises IndexError for out-of-bounds access.''' doc = Doc(Vocab(), words=['0', '1', '2']) diff --git a/spacy/tokens/token.pyx b/spacy/tokens/token.pyx index 9ff59eabe..514934ca7 100644 --- a/spacy/tokens/token.pyx +++ b/spacy/tokens/token.pyx @@ -127,6 +127,9 @@ cdef class Token: i (int): The relative position of the token to get. Defaults to 1. RETURNS (Token): The token at position `self.doc[self.i+i]`. """ + if self.i+i < 0 or (self.i+i >= len(self.doc)): + msg = "Error accessing doc[%d].nbor(%d), for doc of length %d" + raise IndexError(msg % (self.i, i, len(self.doc))) return self.doc[self.i+i] def similarity(self, other):