Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doc/library/doctest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ Simple example::
>>> [1, 2, 3].remove(42)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: list.remove(x): x not in list
ValueError: "42" not in list

That doctest succeeds if :exc:`ValueError` is raised, with the ``list.remove(x):
x not in list`` detail as shown.
That doctest succeeds if :exc:`ValueError` is raised, with the
``"42" not in list`` detail as shown.

The expected output for an exception must start with a traceback header, which
may be either of the following two lines, indented the same as the first line of
Expand Down
2 changes: 1 addition & 1 deletion Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def indexOf(a, b):
if j == b:
return i
else:
raise ValueError('sequence.index(x): x not in sequence')
raise ValueError("'%.100r' not in sequence" % b)

def setitem(a, b, c):
"Same as a[b] = c."
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/list_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ def test_remove(self):
self.assertEqual(a, [0])
a.remove(0)
self.assertEqual(a, [])
# Verify that exception message contains value (issue13349)
with self.assertRaises(ValueError) as cm:
a.remove(0)
self.assertIn("0", str(cm.exception))

self.assertRaises(ValueError, a.remove, 0)

Expand Down
5 changes: 4 additions & 1 deletion Lib/test/seq_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@ def test_index(self):
self.assertEqual(u.index(0, 3), 3)
self.assertEqual(u.index(0, 3, 4), 3)
self.assertRaises(ValueError, u.index, 2, 0, -10)

# Verify that exception message contains value (issue13349)
with self.assertRaises(ValueError) as cm:
u.index(None)
self.assertIn(repr(None), str(cm.exception))
self.assertRaises(TypeError, u.index)

class BadExc(Exception):
Expand Down
9 changes: 8 additions & 1 deletion Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,10 @@ def test_index(self):
self.assertEqual(a.index(x), example.index(x))
self.assertRaises(ValueError, a.index, None)
self.assertRaises(ValueError, a.index, self.outside)
# Verify that exception message contains value (issue13349)
with self.assertRaises(ValueError) as cm:
a.index(self.outside)
self.assertIn(repr(self.outside), str(cm.exception))

def test_count(self):
example = 2*self.example
Expand All @@ -891,7 +895,10 @@ def test_remove(self):

a = array.array(self.typecode, self.example)
self.assertRaises(ValueError, a.remove, self.outside)

# Verify that exception message contains value (issue13349)
with self.assertRaises(ValueError) as cm:
a.remove(self.outside)
self.assertIn(repr(self.outside), str(cm.exception))
self.assertRaises(ValueError, a.remove, None)

def test_pop(self):
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ def test_remove(self):
self.assertEqual(d, deque('abdefghij'))
self.assertRaises(ValueError, d.remove, 'c')
self.assertEqual(d, deque('abdefghij'))
# Verify that exception message contains value (issue13349)
with self.assertRaises(ValueError) as cm:
d.remove(None)
self.assertIn(repr(None), str(cm.exception))

# Handle comparison errors
d = deque(['a', 'b', BadCmp(), 'c'])
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def test_indexOf(self):
self.assertRaises(TypeError, operator.indexOf, None, None)
self.assertEqual(operator.indexOf([4, 3, 2, 1], 3), 1)
self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
# Verify that exception message contains value (issue13349)
with self.assertRaises(ValueError) as cm:
operator.indexOf([0, 1, 2], None)
self.assertIn(repr(None), str(cm.exception))

def test_invert(self):
operator = self.module
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,10 @@ def test_simpleops(self):
self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 4
element.remove(subelement)
self.serialize_check(element, '<tag key="value" />') # 5
# Verify that exception message contains value (issue13349)
with self.assertRaises(ValueError) as cm:
element.remove(subelement)
self.assertEqual(str(cm.exception), 'list.remove(x): x not in list')
self.assertIn(repr(subelement), str(cm.exception))
self.serialize_check(element, '<tag key="value" />') # 6
element[0:0] = [subelement, subelement, subelement]
self.serialize_check(element[1], '<subtag />')
Expand Down
4 changes: 2 additions & 2 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ deque_index(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
index = 0;
}
}
PyErr_Format(PyExc_ValueError, "%R is not in deque", v);
PyErr_Format(PyExc_ValueError, "\"%.100R\" not in deque", v);
return NULL;
}

Expand Down Expand Up @@ -1201,7 +1201,7 @@ deque_remove(dequeobject *deque, PyObject *value)
}
_deque_rotate(deque, -1);
}
PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
PyErr_Format(PyExc_ValueError, "\"%.100R\" not in deque", value);
return NULL;
}

Expand Down
10 changes: 2 additions & 8 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1570,10 +1570,7 @@ _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)

if (!self->extra) {
/* element has no children, so raise exception */
PyErr_SetString(
PyExc_ValueError,
"list.remove(x): x not in list"
);
PyErr_Format(PyExc_ValueError, "\"%.100R\" not in Element", subelement);
return NULL;
}

Expand All @@ -1589,10 +1586,7 @@ _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)

if (i >= self->extra->length) {
/* subelement is not in children, so raise exception */
PyErr_SetString(
PyExc_ValueError,
"list.remove(x): x not in list"
);
PyErr_Format(PyExc_ValueError, "\"%.100R\" not in Element", subelement);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ array_array_index(arrayobject *self, PyObject *v)
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
PyErr_Format(PyExc_ValueError, "\"%.100R\" not in array", v);
return NULL;
}

Expand Down Expand Up @@ -1142,7 +1142,7 @@ array_array_remove(arrayobject *self, PyObject *v)
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
PyErr_Format(PyExc_ValueError, "\"%.100R\" not in array", v);
return NULL;
}

Expand Down
3 changes: 1 addition & 2 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1971,8 +1971,7 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
if (operation != PY_ITERSEARCH_INDEX)
goto Done;

PyErr_SetString(PyExc_ValueError,
"sequence.index(x): x not in sequence");
PyErr_Format(PyExc_ValueError, "'%.100R' not in sequence", obj);
/* fall into failure code */
Fail:
n = -1;
Expand Down
4 changes: 2 additions & 2 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2243,7 +2243,7 @@ list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
else if (cmp < 0)
return NULL;
}
PyErr_Format(PyExc_ValueError, "%R is not in list", value);
PyErr_Format(PyExc_ValueError, "\"%.100R\" not in list", value);
return NULL;
}

Expand Down Expand Up @@ -2301,7 +2301,7 @@ list_remove(PyListObject *self, PyObject *value)
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
PyErr_Format(PyExc_ValueError, "\"%.100R\" not in list", value);
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
PyErr_Format(PyExc_ValueError, "\"%.100R\" not in tuple", value);
return NULL;
}

Expand Down