diff -r 63b45c959a2a Lib/test/test_array.py --- a/Lib/test/test_array.py Sun Nov 04 07:00:04 2012 +0200 +++ b/Lib/test/test_array.py Sat Dec 08 22:24:13 2012 -0800 @@ -838,6 +838,12 @@ self.assertRaises(ValueError, a.index, None) self.assertRaises(ValueError, a.index, self.outside) + # check that the item is included in the message (see #13349) + a = array.array(self.typecode, self.example) + with self.assertRaises(ValueError) as ve: + a.index(self.outside) + self.assertIn(repr(self.outside), str(ve.exception)) + def test_count(self): example = 2*self.example a = array.array(self.typecode, example) @@ -861,6 +867,12 @@ self.assertRaises(ValueError, a.remove, None) + # check that the item is included in the message (see #13349) + a = array.array(self.typecode, self.example) + with self.assertRaises(ValueError) as ve: + a.remove(self.outside) + self.assertIn(repr(self.outside), str(ve.exception)) + def test_pop(self): a = array.array(self.typecode) self.assertRaises(IndexError, a.pop) diff -r 63b45c959a2a Lib/test/test_deque.py --- a/Lib/test/test_deque.py Sun Nov 04 07:00:04 2012 +0200 +++ b/Lib/test/test_deque.py Sat Dec 08 22:24:13 2012 -0800 @@ -356,6 +356,12 @@ self.assertRaises(IndexError, d.remove, 'c') self.assertEqual(d, deque()) + # check that the item is included in the message (see #13349) + item = 't' + with self.assertRaises(ValueError) as ve: + d.remove(item) + self.assertIn(item, str(ve.exception)) + def test_repr(self): d = deque(range(200)) e = eval(repr(d)) diff -r 63b45c959a2a Lib/test/test_list.py --- a/Lib/test/test_list.py Sun Nov 04 07:00:04 2012 +0200 +++ b/Lib/test/test_list.py Sat Dec 08 22:24:13 2012 -0800 @@ -106,6 +106,32 @@ with self.assertRaises(TypeError): (3,) + L([1,2]) + def test_index(self): + lst = [1, 2, 3] + idx = lst.index(1) + self.assertEqual(idx,0) + + # check that the item is included in the message (see #13349) + item = 17 + with self.assertRaises(ValueError) as ve: + lst.index(item) + self.assertIn(str(item), str(ve.exception)) + + def test_remove(self): + lst = [1, 2, 3, 4] + lst.append(1) + lst.remove(1) + self.assertEqual(1, lst[len(lst)-1]) + + lst.remove(4) + self.assertIn(4,lst) + + # check that the item is included in the message (see #13349) + item = 99 + with self.assertRaises(ValueError) as ve: + lst.remove(item) + self.assertIn(str(item), str(ve.exception)) + def test_main(verbose=None): support.run_unittest(ListTest) diff -r 63b45c959a2a Lib/test/test_tuple.py --- a/Lib/test/test_tuple.py Sun Nov 04 07:00:04 2012 +0200 +++ b/Lib/test/test_tuple.py Sat Dec 08 22:24:13 2012 -0800 @@ -18,6 +18,26 @@ self.assertEqual(tuple(''), ()) self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) + def test_remove(self): + + tpl = ('a', 'b', 'c') + tpl.remove('a') + self.assertEqual(('b', 'c'), tpl) + + # check that the item is included in the message (see #13349) + item = 'SomeItem' + with self.assertRaises(ValueError) as ve: + tpl.remove(item) + self.assertIn(item, str(ve.exception)) + + # check that the item is included in the message (see #13349) + msg_fmt_len = len('tuple.index(..): element not in tuple') + item = " ".join([str(i) for i in range(1000)]) + with self.assertRaises(ValueError) as ve: + tpl.index(item) + self.assertIn(item[:99], str(ve.exception)) + self.assertGreaterEqual(100 + msg_fmt_len, len(str(ve.exception))) + def test_truth(self): super().test_truth() self.assertTrue(not ()) diff -r 63b45c959a2a Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Sun Nov 04 07:00:04 2012 +0200 +++ b/Lib/test/test_xml_etree.py Sat Dec 08 22:24:13 2012 -0800 @@ -261,9 +261,6 @@ >>> element.remove(subelement) >>> serialize(element) # 5 '' - >>> element.remove(subelement) - Traceback (most recent call last): - ValueError: list.remove(x): x not in list >>> serialize(element) # 6 '' >>> element[0:0] = [subelement, subelement, subelement] @@ -1888,6 +1885,22 @@ self.assertEqual(self._ilist(doc), all_tags) self.assertEqual(self._ilist(doc, '*'), all_tags) +class TreeBasicOperationTest(unittest.TestCase): + + def test_remove(self): + elem = ET.XML("") + + tag_name = "foo" + item = elem.find(tag_name) + elem.remove(item) + self.assertIsNone(elem.find(tag_name)) + + # test proper failure with error msg that includes the item + # that couldn't be removed (issue 13349). + item = "SomeTag" + with self.assertRaises(ValueError) as ve: + elem.remove(item) + self.assertIn(repr(item), str(ve.exception)) class TreeBuilderTest(unittest.TestCase): sample1 = ('extra) { /* element has no children, so raise exception */ - PyErr_SetString( - PyExc_ValueError, - "list.remove(x): x not in list" - ); + PyErr_Format(PyExc_ValueError, "element.remove(%0.100R): element not in list", element); return NULL; } @@ -1239,10 +1236,7 @@ if (i == self->extra->length) { /* element is not in children, so raise exception */ - PyErr_SetString( - PyExc_ValueError, - "list.remove(x): x not in list" - ); + PyErr_Format(PyExc_ValueError, "element.remove(%0.100R): element not in list", element); return NULL; } diff -r 63b45c959a2a Modules/arraymodule.c --- a/Modules/arraymodule.c Sun Nov 04 07:00:04 2012 +0200 +++ b/Modules/arraymodule.c Sat Dec 08 22:24:13 2012 -0800 @@ -999,7 +999,7 @@ else if (cmp < 0) return NULL; } - PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list"); + PyErr_Format(PyExc_ValueError, "array.index(%0.100R): element not in array", v); return NULL; } @@ -1041,7 +1041,7 @@ else if (cmp < 0) return NULL; } - PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list"); + PyErr_Format(PyExc_ValueError, "array.remove(%0.100R): element not in array", v); return NULL; } diff -r 63b45c959a2a Objects/listobject.c --- a/Objects/listobject.c Sun Nov 04 07:00:04 2012 +0200 +++ b/Objects/listobject.c Sat Dec 08 22:24:13 2012 -0800 @@ -2161,7 +2161,7 @@ else if (cmp < 0) return NULL; } - PyErr_Format(PyExc_ValueError, "%R is not in list", v); + PyErr_Format(PyExc_ValueError, "%0.100R not in list", v); return NULL; } @@ -2197,7 +2197,7 @@ else if (cmp < 0) return NULL; } - PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); + PyErr_Format(PyExc_ValueError, "list.remove(%0.100R): element not in list", v); return NULL; } diff -r 63b45c959a2a Objects/tupleobject.c --- a/Objects/tupleobject.c Sun Nov 04 07:00:04 2012 +0200 +++ b/Objects/tupleobject.c Sat Dec 08 22:24:13 2012 -0800 @@ -522,7 +522,7 @@ else if (cmp < 0) return NULL; } - PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple"); + PyErr_Format(PyExc_ValueError, "tuple.index(%.100R): element not in tuple", v); return NULL; }