diff --git a/3rd/cjson/src/cJSONw.cpp b/3rd/cjson/src/cJSONw.cpp index 4f4479c6..ac192ecf 100644 --- a/3rd/cjson/src/cJSONw.cpp +++ b/3rd/cjson/src/cJSONw.cpp @@ -50,7 +50,7 @@ static cJSON* convert_python_object_to_cjson(PyObject* obj, VM* vm){ }else{ vm->TypeError(fmt("unrecognized type ", obj_type_name(vm, obj_t).escape())); } - UNREACHABLE(); + PK_UNREACHABLE(); } @@ -124,7 +124,7 @@ void add_module_cjson(VM* vm){ const char* end = start; while(*end != '\0' && *end != '\n') end++; vm->IOError(fmt("cjson: ", std::string_view(start, end-start))); - UNREACHABLE(); + PK_UNREACHABLE(); } PyObject* output = convert_cjson_to_python_object(json, vm); cJSON_Delete(json); diff --git a/examples/abort-the-vm/user_config.h b/examples/abort-the-vm/user_config.h index 22b1e71e..8f927c05 100644 --- a/examples/abort-the-vm/user_config.h +++ b/examples/abort-the-vm/user_config.h @@ -66,10 +66,10 @@ #ifdef _MSC_VER #define PK_ENABLE_COMPUTED_GOTO 0 -#define UNREACHABLE() __assume(0) +#define PK_UNREACHABLE() __assume(0) #else #define PK_ENABLE_COMPUTED_GOTO 1 -#define UNREACHABLE() __builtin_unreachable() +#define PK_UNREACHABLE() __builtin_unreachable() #endif diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index 820c0488..b6fee18f 100644 --- a/include/pocketpy/config.h +++ b/include/pocketpy/config.h @@ -78,10 +78,10 @@ #ifdef _MSC_VER #define PK_ENABLE_COMPUTED_GOTO 0 -#define UNREACHABLE() __assume(0) +#define PK_UNREACHABLE() __assume(0) #else #define PK_ENABLE_COMPUTED_GOTO 1 -#define UNREACHABLE() __builtin_unreachable() +#define PK_UNREACHABLE() __builtin_unreachable() #endif diff --git a/include/pocketpy/namedict.h b/include/pocketpy/namedict.h index 3f6026db..1ce3c4af 100644 --- a/include/pocketpy/namedict.h +++ b/include/pocketpy/namedict.h @@ -49,7 +49,7 @@ struct SmallNameDict{ return true; } ) - UNREACHABLE(); + PK_UNREACHABLE(); } V try_get(K key) const { diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 89a236b1..8319ed7e 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -223,7 +223,6 @@ public: PyObject* _find_type_object(const Str& type); Type _type(const Str& type); - PyTypeInfo* _type_info(const Str& type); PyTypeInfo* _type_info(Type type); const PyTypeInfo* _inst_type_info(PyObject* obj); diff --git a/src/ceval.cpp b/src/ceval.cpp index 534be4b7..3af69a10 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -799,7 +799,7 @@ __NEXT_STEP:; } if(!isinstance(TOP(), tp_exception)){ _builtin_error("TypeError", "exceptions must derive from Exception"); - UNREACHABLE(); + PK_UNREACHABLE(); } _error(POPX()); } DISPATCH(); @@ -855,7 +855,7 @@ __NEXT_STEP:; #undef DISPATCH_OP_CALL #undef CEVAL_STEP /**********************************************************************/ - UNREACHABLE(); + PK_UNREACHABLE(); }catch(HandledException){ continue; }catch(UnhandledException){ diff --git a/src/expr.cpp b/src/expr.cpp index e0e86521..63b26c59 100644 --- a/src/expr.cpp +++ b/src/expr.cpp @@ -611,7 +611,7 @@ namespace pkpy{ case TK("!="): ctx->emit_(OP_COMPARE_NE, BC_NOARG, line); break; case TK(">"): ctx->emit_(OP_COMPARE_GT, BC_NOARG, line); break; case TK(">="): ctx->emit_(OP_COMPARE_GE, BC_NOARG, line); break; - default: UNREACHABLE(); + default: PK_UNREACHABLE(); } // [b, RES] int index = ctx->emit_(OP_SHORTCUT_IF_FALSE_OR_POP, BC_NOARG, line); diff --git a/src/vm.cpp b/src/vm.cpp index 587482f7..7938549b 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -1,4 +1,5 @@ #include "pocketpy/vm.h" +#include "pocketpy/config.h" namespace pkpy{ @@ -27,7 +28,7 @@ namespace pkpy{ first = false; if(!is_non_tagged_type(k, vm->tp_str)){ vm->TypeError(fmt("json keys must be string, got ", obj_type_name(vm, vm->_tp(k)))); - UNREACHABLE(); + PK_UNREACHABLE(); } ss << _CAST(Str&, k).escape(false) << ": "; write_object(v); @@ -57,7 +58,7 @@ namespace pkpy{ write_dict(_CAST(Dict&, obj)); }else{ vm->TypeError(fmt("unrecognized type ", obj_type_name(vm, obj_t).escape())); - UNREACHABLE(); + PK_UNREACHABLE(); } } @@ -236,15 +237,6 @@ namespace pkpy{ return PK_OBJ_GET(Type, obj); } - PyTypeInfo* VM::_type_info(const Str& type){ - PyObject* obj = builtins->attr().try_get_likely_found(type); - if(obj == nullptr){ - for(auto& t: _all_types) if(t.name == type) return &t; - PK_FATAL_ERROR(); - } - return &_all_types[PK_OBJ_GET(Type, obj)]; - } - PyTypeInfo* VM::_type_info(Type type){ return &_all_types[type]; } @@ -476,7 +468,7 @@ i64 VM::py_hash(PyObject* obj){ } if(has_custom_eq){ TypeError(fmt("unhashable type: ", ti->name.escape())); - return 0; + PK_UNREACHABLE(); }else{ return PK_BITS(obj); } @@ -532,7 +524,7 @@ PyObject* VM::_format_string(Str spec, PyObject* obj){ } }catch(...){ ValueError("invalid format specifer"); - UNREACHABLE(); + PK_UNREACHABLE(); } if(type != 'f' && dot >= 0) ValueError("precision not allowed in the format specifier"); @@ -816,7 +808,7 @@ void VM::_prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, con vm->TypeError(fmt( co->name, "() takes ", decl_argc, " positional arguments but ", args.size(), " were given" )); - UNREACHABLE(); + PK_UNREACHABLE(); } int i = 0; @@ -925,11 +917,11 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){ TypeError(fmt( co->name, "() takes ", decl->args.size(), " positional arguments but ", args.size(), " were given" )); - UNREACHABLE(); + PK_UNREACHABLE(); } if(!kwargs.empty()){ TypeError(fmt(co->name, "() takes no keyword arguments")); - UNREACHABLE(); + PK_UNREACHABLE(); } s_data.reset(_base + co_nlocals); int i = 0; diff --git a/tests/04_str.py b/tests/04_str.py index b1800ff7..51ffacaa 100644 --- a/tests/04_str.py +++ b/tests/04_str.py @@ -1,12 +1,16 @@ assert 'testing' == 'test' + 'ing' assert 'testing' != 'test' + 'ing2' assert 'testing' < 'test' + 'ing2' +assert 'testing' <= 'test' + 'ing2' assert 'testing5' > 'test' + 'ing1' +assert 'testing5' >= 'test' + 'ing1' # test + *= assert 'abc' + 'def' == 'abcdef' assert 'abc' * 3 == 'abcabcabc' +assert repr('\\\n\t\'\r\b\x48') == r"'\\\n\t\'\r\bH'" + a = '' b = 'test' c ='test' diff --git a/tests/80_json.py b/tests/80_json.py index 5b63f449..865abf07 100644 --- a/tests/80_json.py +++ b/tests/80_json.py @@ -70,4 +70,16 @@ assert repr([1, 2, 3]) == '[1, 2, 3]' assert repr([1]) == '[1]' assert json.dumps([]) == '[]' assert json.dumps([1, 2, 3]) == '[1, 2, 3]' -assert json.dumps([1]) == '[1]' \ No newline at end of file +assert json.dumps([1]) == '[1]' + +try: + json.dumps({1: 2}) + assert False +except TypeError: + assert True + +try: + json.dumps(type) + assert False +except TypeError: + assert True