diff --git a/pysam/libctabixproxies.pyx b/pysam/libctabixproxies.pyx index 9b81af16..152c3c3e 100644 --- a/pysam/libctabixproxies.pyx +++ b/pysam/libctabixproxies.pyx @@ -365,14 +365,14 @@ cdef class NamedTupleProxy(TupleProxy): '''set attribute.''' cdef int idx idx, f = self.map_key2field[key] - if self.nfields < idx: + if idx >= self.nfields: raise KeyError("field %s not set" % key) TupleProxy.__setitem__(self, idx, str(value)) def __getattr__(self, key): cdef int idx idx, f = self.map_key2field[key] - if self.nfields < idx: + if idx >= self.nfields: raise KeyError("field %s not set" % key) if f == str: return force_str(self.fields[idx], diff --git a/tests/tabix_test.py b/tests/tabix_test.py index 33c3e708..d82379d1 100644 --- a/tests/tabix_test.py +++ b/tests/tabix_test.py @@ -501,6 +501,9 @@ def testRead(self): self.assertEqual(c[0], r.contig) self.assertEqual(int(c[1]), r.start) self.assertEqual(int(c[2]), r.end) + # Needs lambda so that the property getter isn't called too soon + self.assertRaises(KeyError, lambda: r.name) + self.assertRaises(KeyError, lambda: r.score) self.assertEqual(list(c), list(r)) self.assertEqual("\t".join(map(str, c)), str(r)) @@ -524,6 +527,12 @@ def testWrite(self): self.assertEqual(int(c[2]) + 1, r.end) self.assertEqual(str(int(c[2]) + 1), r[2]) + with self.assertRaises(IndexError): + r.name = "test" + + with self.assertRaises(IndexError): + r.score = 1 + class TestVCF(unittest.TestCase):