Raise exception if __repr__ returns non-str in pushStringBuilderFormat

This commit is contained in:
K. Lange 2024-03-05 14:21:49 +09:00
parent 6c41c32949
commit 3e8bb098ff

View File

@ -1131,9 +1131,7 @@ int krk_pushStringBuilderFormatV(struct StringBuilder * sb, const char * fmt, va
} }
/* Bail on exception */ /* Bail on exception */
if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) { if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) break;
return 0;
}
++f; ++f;
@ -1225,6 +1223,8 @@ int krk_pushStringBuilderFormatV(struct StringBuilder * sb, const char * fmt, va
krk_push(res); krk_push(res);
if (IS_STRING(res)) { if (IS_STRING(res)) {
pushStringBuilderStr(sb, AS_CSTRING(res), AS_STRING(res)->length); 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(); 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; return 1;
} }