Closed
Description
Bug report
Bug description:
lst = []
class C:
def __repr__(self):
raise RuntimeError("Oh no!")
try:
lst.index(C())
except ValueError as e:
print("This is good")
print(repr(e))
On Python 3.10 this prints
This is good
ValueError()
On Python 3.11 this prints
Traceback (most recent call last):
File "<path>", line 8, in <module>
lst.index(C())
File "<path>", line 5, in __repr__
raise RuntimeError("Oh no!")
RuntimeError: Oh no!
When PyErr_Format
fails in current versions of Python, it doesn't set the exception and instead raises the new one:
Lines 1162 to 1167 in 11d88a1
In earlier versions of Python it used to raise the requested exception unconditionally
Lines 1059 to 1064 in b6535ea
This looks to have been a deliberate change but it can affect the behaviour of real code (cython/cython#5894). Interestingly PyPy got there before you with this behaviour (https://foss.heptapod.net/pypy/pypy/-/issues/3978)
CPython versions tested on:
3.11
Operating systems tested on:
Linux