diff --git a/graalpython/com.oracle.graal.python.test/src/tests/test_repr.py b/graalpython/com.oracle.graal.python.test/src/tests/test_repr.py index 514b5a351e..cfe83c551d 100644 --- a/graalpython/com.oracle.graal.python.test/src/tests/test_repr.py +++ b/graalpython/com.oracle.graal.python.test/src/tests/test_repr.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # The Universal Permissive License (UPL), Version 1.0 @@ -66,3 +66,15 @@ def a_func(): assert_not_raises(lambda: repr(a_func)) assert_not_raises(lambda: repr(object())) assert_not_raises(lambda: repr(x)) + + +def test_repr_type_error_includes_returned_value(): + class ReprReturnsInt: + def __repr__(self): + return 42 + + try: + repr(ReprReturnsInt()) + assert False + except TypeError as e: + assert str(e) == "__repr__ returned non-string (type int)" diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectReprAsObjectNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectReprAsObjectNode.java index 961e07ca9c..6ccf2ca45d 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectReprAsObjectNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectReprAsObjectNode.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -108,7 +108,7 @@ static Object repr(VirtualFrame frame, Node inliningTarget, Object obj, if (checkNode.execute(inliningTarget, result)) { return result; } else { - throw raiseTypeError(inliningTarget, obj, raiseNode); + throw raiseTypeError(inliningTarget, result, raiseNode); } }