diff --git a/include/pocketpy/common/utils.hpp b/include/pocketpy/common/utils.hpp index 6b8ebfc0..abf53c0e 100644 --- a/include/pocketpy/common/utils.hpp +++ b/include/pocketpy/common/utils.hpp @@ -31,4 +31,6 @@ const inline char* kPlatformStrings[] = { #define PK_UNREACHABLE() __builtin_unreachable(); #endif +#define PK_FATAL_ERROR(...) { fprintf(stderr, __VA_ARGS__); std::abort(); } + } // namespace pkpy diff --git a/include/pocketpy/interpreter/vm.hpp b/include/pocketpy/interpreter/vm.hpp index e55efa28..07a3f32b 100644 --- a/include/pocketpy/interpreter/vm.hpp +++ b/include/pocketpy/interpreter/vm.hpp @@ -9,8 +9,6 @@ #include "pocketpy/interpreter/frame.hpp" #include "pocketpy/interpreter/profiler.hpp" -#include - namespace pkpy { /* Stack manipulation macros */ @@ -467,15 +465,7 @@ public: template Type _find_type_in_cxx_typeid_map() { auto it = _cxx_typeid_map.try_get(typeid(T)); - if(it == nullptr) { -#if __GNUC__ || __clang__ - throw std::runtime_error(__PRETTY_FUNCTION__ + std::string(" failed: T not found")); -#elif _MSC_VER - throw std::runtime_error(__FUNCSIG__ + std::string(" failed: T not found")); -#else - throw std::runtime_error("_find_type_in_cxx_typeid_map() failed: T not found"); -#endif - } + if(it == nullptr) PK_FATAL_ERROR("T not found in cxx_typeid_map") return *it; } diff --git a/src/common/any.cpp b/src/common/any.cpp index d3c76954..6d8558e7 100644 --- a/src/common/any.cpp +++ b/src/common/any.cpp @@ -1,14 +1,12 @@ #include "pocketpy/common/any.hpp" +#include "pocketpy/common/utils.hpp" -#include #include namespace pkpy { void any::__bad_any_cast(const std::type_index expected, const std::type_index actual) { - char error[256]; - snprintf(error, sizeof(error), "bad_any_cast: expected %s, got %s", expected.name(), actual.name()); - throw std::runtime_error(error); + PK_FATAL_ERROR("bad_any_cast: expected %s, got %s", expected.name(), actual.name()) } any::any(any&& other) noexcept : data(other.data), _vt(other._vt) { diff --git a/src/common/memorypool.cpp b/src/common/memorypool.cpp index bf3d91e2..55ec1f54 100644 --- a/src/common/memorypool.cpp +++ b/src/common/memorypool.cpp @@ -4,7 +4,6 @@ #include #include #include -#include namespace pkpy { diff --git a/src/common/str.cpp b/src/common/str.cpp index e7aa417a..90664be1 100644 --- a/src/common/str.cpp +++ b/src/common/str.cpp @@ -1,7 +1,6 @@ #include "pocketpy/common/str.hpp" #include "pocketpy/common/gil.hpp" -#include #include #include #include @@ -17,7 +16,7 @@ int utf8len(unsigned char c, bool suppress) { if((c & 0b11111000) == 0b11110000) return 4; if((c & 0b11111100) == 0b11111000) return 5; if((c & 0b11111110) == 0b11111100) return 6; - if(!suppress) throw std::runtime_error("invalid utf8 char: " + std::to_string(c)); + if(!suppress) PK_FATAL_ERROR("invalid utf8 char") return 0; } @@ -380,7 +379,7 @@ StrName StrName::get(std::string_view s) { // generate new index // https://github.com/python/cpython/blob/3.12/Objects/dictobject.c#L175 uint16_t index = ((_pesudo_random_index * 5) + 1) & 65535; - if(index == 0) throw std::runtime_error("StrName index overflow"); + if(index == 0) PK_FATAL_ERROR("StrName index overflow") auto res = _r_interned().emplace(index, s); assert(res.second); s = std::string_view(res.first->second); diff --git a/src/interpreter/frame.cpp b/src/interpreter/frame.cpp index c15e8e7a..d46e5391 100644 --- a/src/interpreter/frame.cpp +++ b/src/interpreter/frame.cpp @@ -1,8 +1,6 @@ #include "pocketpy/objects/stackmemory.hpp" #include "pocketpy/interpreter/frame.hpp" -#include - namespace pkpy { PyVar* FastLocals::try_get_name(StrName name) { int index = co->varnames_inv.get(name, -1); @@ -72,7 +70,7 @@ void Frame::prepare_jump_break(ValueStack* _s, int target) { int next_block = co->lines[target].iblock; while(i >= 0 && i != next_block) i = _exit_block(_s, i); - if(i != next_block) throw std::runtime_error("invalid jump"); + assert(i == next_block); } } diff --git a/src/objects/namedict.cpp b/src/objects/namedict.cpp index 28d004bc..0c8ea422 100644 --- a/src/objects/namedict.cpp +++ b/src/objects/namedict.cpp @@ -1,7 +1,5 @@ #include "pocketpy/objects/namedict.hpp" -#include - namespace pkpy { #define HASH_PROBE_1(key, ok, i) \ @@ -118,7 +116,7 @@ bool NameDict::contains(StrName key) const { PyVar NameDict::operator[] (StrName key) const { PyVar* val = try_get_2_likely_found(key); - if(val == nullptr) { throw std::runtime_error(_S("NameDict key not found: ", key.escape()).str()); } + if(val == nullptr) PK_FATAL_ERROR("NameDict key not found: %s", key.escape().c_str()) return *val; }