From 3e8bb098ff5482f4e1d5d22054bc8b97b210b3ca Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Tue, 5 Mar 2024 14:21:49 +0900 Subject: [PATCH] Raise exception if __repr__ returns non-str in pushStringBuilderFormat --- src/obj_str.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/obj_str.c b/src/obj_str.c index fabac0a..8b313ff 100644 --- a/src/obj_str.c +++ b/src/obj_str.c @@ -1131,9 +1131,7 @@ int krk_pushStringBuilderFormatV(struct StringBuilder * sb, const char * fmt, va } /* Bail on exception */ - if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) { - return 0; - } + if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) break; ++f; @@ -1225,6 +1223,8 @@ int krk_pushStringBuilderFormatV(struct StringBuilder * sb, const char * fmt, va krk_push(res); if (IS_STRING(res)) { pushStringBuilderStr(sb, AS_CSTRING(res), AS_STRING(res)->length); + } else if (!(krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION)) { + krk_runtimeError(vm.exceptions->typeError, "__repr__ returned non-string (type %T)", res); } krk_pop(); } @@ -1249,6 +1249,7 @@ int krk_pushStringBuilderFormatV(struct StringBuilder * sb, const char * fmt, va } } + if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) return 0; return 1; }