diff --git a/tests/99_builtin_func.py b/tests/99_builtin_func.py index 47744a6b..4800e9dc 100644 --- a/tests/99_builtin_func.py +++ b/tests/99_builtin_func.py @@ -207,8 +207,6 @@ def f(): assert type(hash(a)) is int # 测试不可哈希对象 - - # try: # hash({1:1}) # print('未能拦截错误') @@ -224,7 +222,6 @@ except: pass - # ----------------------------------------------- # 114: 259: _vm->bind_builtin_func<1>("chr", [](VM* vm, ArgsView args) { # #####: 260: i64 i = CAST(i64, args[0]); @@ -240,3 +237,785 @@ assert l == ['0 \x00', '1 \x01', '2 \x02', '3 \x03', '4 \x04', '5 \x05', '6 \x06 + + + + + + + +# 未完全测试准确性----------------------------------------------- +# 116: 269: _vm->bind_builtin_func<1>("bin", [](VM* vm, ArgsView args) { +# #####: 270: std::stringstream ss; +# #####: 271: i64 x = CAST(i64, args[0]); +# #####: 272: if(x < 0){ ss << "-"; x = -x; } +# #####: 273: ss << "0b"; +# #####: 274: std::string bits; +# #####: 275: while(x){ +# #####: 276: bits += (x & 1) ? '1' : '0'; +# #####: 277: x >>= 1; +# -: 278: } +# #####: 279: std::reverse(bits.begin(), bits.end()); +# #####: 280: if(bits.empty()) bits = "0"; +# #####: 281: ss << bits; +# #####: 282: return VAR(ss.str()); +# #####: 283: }); +# test bin: + +assert type(bin(1234)) is str + +# 无法测试, 不能覆盖----------------------------------------------- +# 136: 285: _vm->bind_builtin_func<1>("dir", [](VM* vm, ArgsView args) { +# 10: 286: std::set names; +# 10: 287: if(!is_tagged(args[0]) && args[0]->is_attr_valid()){ +# #####: 288: std::vector keys = args[0]->attr().keys(); +# #####: 289: names.insert(keys.begin(), keys.end()); +# #####: 290: } +# 10: 291: const NameDict& t_attr = vm->_t(args[0])->attr(); +# 10: 292: std::vector keys = t_attr.keys(); +# 10: 293: names.insert(keys.begin(), keys.end()); +# 10: 294: List ret; +# 305: 295: for (StrName name : names) ret.push_back(VAR(name.sv())); +# 10: 296: return VAR(std::move(ret)); +# 10: 297: }); +# test dir: + + +# 未完全测试准确性----------------------------------------------- +# 116: 299: _vm->bind__repr__(_vm->tp_object, [](VM* vm, PyObject* obj) { +# #####: 300: if(is_tagged(obj)) FATAL_ERROR(); +# #####: 301: std::stringstream ss; +# #####: 302: ss << "<" << OBJ_NAME(vm->_t(obj)) << " object at 0x"; +# #####: 303: ss << std::hex << reinterpret_cast(obj) << ">"; +# #####: 304: return VAR(ss.str()); +# #####: 305: }); +# test __repr__: +class A(): + def __init__(self): + self.attr = 0 + +repr(A()) + + +# 未完全测试准确性----------------------------------------------- +# 33600: 318: _vm->bind_constructor<-1>("range", [](VM* vm, ArgsView args) { +# 16742: 319: args._begin += 1; // skip cls +# 16742: 320: Range r; +# 16742: 321: switch (args.size()) { +# 8735: 322: case 1: r.stop = CAST(i64, args[0]); break; +# 3867: 323: case 2: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); break; +# 4140: 324: case 3: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); r.step = CAST(i64, args[2]); break; +# #####: 325: default: vm->TypeError("expected 1-3 arguments, got " + std::to_string(args.size())); +# #####: 326: } +# 33484: 327: return VAR(r); +# 16742: 328: }); +# -: 329: +# test range: + +try: + range(1,2,3,4) + print('未能拦截错误, 在测试 range') + exit(1) +except: + pass + +# /************ int ************/ +# 未完全测试准确性----------------------------------------------- +# 172: 367: _vm->bind_constructor<-1>("int", [](VM* vm, ArgsView args) { +# 28: 368: if(args.size() == 1+0) return VAR(0); +# 28: 369: if(args.size() == 1+1){ +# 26: 370: if (is_type(args[1], vm->tp_float)) return VAR((i64)CAST(f64, args[1])); +# 2: 371: if (is_type(args[1], vm->tp_int)) return args[1]; +# 1: 372: if (is_type(args[1], vm->tp_bool)) return VAR(_CAST(bool, args[1]) ? 1 : 0); +# -: 373: } +# 3: 374: if(args.size() > 1+2) vm->TypeError("int() takes at most 2 arguments"); +# 3: 375: if (is_type(args[1], vm->tp_str)) { +# 3: 376: int base = 10; +# 3: 377: if(args.size() == 1+2) base = CAST(i64, args[2]); +# 3: 378: const Str& s = CAST(Str&, args[1]); +# -: 379: try{ +# 3: 380: size_t parsed = 0; +# 3: 381: i64 val = Number::stoi(s.str(), &parsed, base); +# 3: 382: PK_ASSERT(parsed == s.length()); +# 3: 383: return VAR(val); +# 3: 384: }catch(...){ +# #####: 385: vm->ValueError("invalid literal for int(): " + s.escape()); +# #####: 386: } +# 3: 387: } +# #####: 388: vm->TypeError("invalid arguments for int()"); +# #####: 389: return vm->None; +# 28: 390: }); +# test int: + +try: + int('asad') + print('未能拦截错误, 在测试 int') + exit(1) +except: + pass + +try: + int(123, 16) + print('未能拦截错误, 在测试 int') + exit(1) +except: + pass + +# 未完全测试准确性----------------------------------------------- +# 116: 392: _vm->bind_method<0>("int", "bit_length", [](VM* vm, ArgsView args) { +# #####: 393: i64 x = _CAST(i64, args[0]); +# #####: 394: if(x < 0) x = -x; +# -: 395: int bits = 0; +# #####: 396: while(x){ x >>= 1; bits++; } +# #####: 397: return VAR(bits); +# -: 398: }); +# test int.bit_length: +assert type(int.bit_length(100)) is int + +# 未完全测试准确性----------------------------------------------- +# 116: 400: _vm->bind__floordiv__(_vm->tp_int, [](VM* vm, PyObject* lhs_, PyObject* rhs_) { +# #####: 401: i64 rhs = CAST(i64, rhs_); +# #####: 402: return VAR(_CAST(i64, lhs_) / rhs); +# -: 403: }); +# test int.__floordiv__: +assert type(10//11) is int + + +# 未完全测试准确性----------------------------------------------- +# 116: 405: _vm->bind__mod__(_vm->tp_int, [](VM* vm, PyObject* lhs_, PyObject* rhs_) { +# #####: 406: i64 rhs = CAST(i64, rhs_); +# #####: 407: return VAR(_CAST(i64, lhs_) % rhs); +# test int.__mod__: +assert type(11%2) is int + + +# /************ float ************/ +# 136: 433: _vm->bind_constructor<2>("float", [](VM* vm, ArgsView args) { +# 10: 434: if (is_type(args[1], vm->tp_int)) return VAR((f64)CAST(i64, args[1])); +# 9: 435: if (is_type(args[1], vm->tp_float)) return args[1]; +# 3: 436: if (is_type(args[1], vm->tp_bool)) return VAR(_CAST(bool, args[1]) ? 1.0 : 0.0); +# 3: 437: if (is_type(args[1], vm->tp_str)) { +# 3: 438: const Str& s = CAST(Str&, args[1]); +# 3: 439: if(s == "inf") return VAR(INFINITY); +# 2: 440: if(s == "-inf") return VAR(-INFINITY); +# -: 441: try{ +# 2: 442: f64 val = Number::stof(s.str()); +# 2: 443: return VAR(val); +# #####: 444: }catch(...){ +# #####: 445: vm->ValueError("invalid literal for float(): " + s.escape()); +# #####: 446: } +# #####: 447: } +# #####: 448: vm->TypeError("float() argument must be a int, float, bool or str"); +# #####: 449: return vm->None; +# 10: 450: }); +# test float: + +try: + float('asad') + print('未能拦截错误, 在测试 float') + exit(1) +except: + pass + +try: + float([]) + print('未能拦截错误, 在测试 float') + exit(1) +except: + pass + +# /************ str ************/ +# 未完全测试准确性----------------------------------------------- +# 116: 495: _vm->bind_method<1>("str", "__rmul__", [](VM* vm, ArgsView args) { +# #####: 496: const Str& self = _CAST(Str&, args[0]); +# #####: 497: i64 n = CAST(i64, args[1]); +# #####: 498: std::stringstream ss; +# #####: 499: for(i64 i = 0; i < n; i++) ss << self.sv(); +# #####: 500: return VAR(ss.str()); +# #####: 501: }); +# test str.__rmul__: +assert type(12 * '12') is str + +# 未完全测试准确性----------------------------------------------- +# 116: 554: _vm->bind_method<1>("str", "index", [](VM* vm, ArgsView args) { +# #####: 555: const Str& self = _CAST(Str&, args[0]); +# #####: 556: const Str& sub = CAST(Str&, args[1]); +# #####: 557: int index = self.index(sub); +# #####: 558: if(index == -1) vm->ValueError("substring not found"); +# #####: 559: return VAR(index); +# #####: 560: }); +# test str.index: +assert type('25363546'.index('63')) is int +try: + '25363546'.index('err') + print('未能拦截错误, 在测试 str.index') + exit(1) +except: + pass + + +# 未完全测试准确性----------------------------------------------- +# 116: 562: _vm->bind_method<1>("str", "find", [](VM* vm, ArgsView args) { +# #####: 563: const Str& self = _CAST(Str&, args[0]); +# #####: 564: const Str& sub = CAST(Str&, args[1]); +# #####: 565: return VAR(self.index(sub)); +# -: 566: }); +# test str.find: +assert type('25363546'.find('63')) is int +assert type('25363546'.find('err')) is int + + +# /************ list ************/ +# 未完全测试准确性----------------------------------------------- +# 174: 615: _vm->bind_constructor<-1>("list", [](VM* vm, ArgsView args) { +# 29: 616: if(args.size() == 1+0) return VAR(List()); +# 29: 617: if(args.size() == 1+1){ +# 29: 618: return vm->py_list(args[1]); +# -: 619: } +# #####: 620: vm->TypeError("list() takes 0 or 1 arguments"); +# #####: 621: return vm->None; +# 29: 622: }); +# test list: +try: + list(1,2) + print('未能拦截错误, 在测试 list') + exit(1) +except: + pass + +# 未完全测试准确性---------------------------------------------- +# 116: 648: _vm->bind_method<1>("list", "index", [](VM* vm, ArgsView args) { +# #####: 649: List& self = _CAST(List&, args[0]); +# #####: 650: PyObject* obj = args[1]; +# #####: 651: for(int i=0; ipy_equals(self[i], obj)) return VAR(i); +# -: 653: } +# #####: 654: vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list"); +# #####: 655: return vm->None; +# #####: 656: }); +# test list.index: +assert type([1,2,3,4,5].index(4)) is int +try: + [1,2,3,4,5].index(6) + print('未能拦截错误, 在测试 list.index') + exit(1) +except: + pass + + + +# 未完全测试准确性---------------------------------------------- +# 118: 658: _vm->bind_method<1>("list", "remove", [](VM* vm, ArgsView args) { +# 1: 659: List& self = _CAST(List&, args[0]); +# 1: 660: PyObject* obj = args[1]; +# 2: 661: for(int i=0; ipy_equals(self[i], obj)){ +# 1: 663: self.erase(i); +# 1: 664: return vm->None; +# -: 665: } +# -: 666: } +# #####: 667: vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list"); +# #####: 668: return vm->None; +# 1: 669: }); +# test list.remove: +try: + [1,2,3,4,5].remove(6) + print('未能拦截错误, 在测试 list.remove') + exit(1) +except: + pass + + +# 未完全测试准确性---------------------------------------------- +# 2536: 671: _vm->bind_method<-1>("list", "pop", [](VM* vm, ArgsView args) { +# 1210: 672: List& self = _CAST(List&, args[0]); +# 1210: 673: if(args.size() == 1+0){ +# 1208: 674: if(self.empty()) vm->IndexError("pop from empty list"); +# 1208: 675: return self.popx_back(); +# -: 676: } +# 2: 677: if(args.size() == 1+1){ +# 2: 678: int index = CAST(int, args[1]); +# 2: 679: index = vm->normalized_index(index, self.size()); +# 2: 680: PyObject* ret = self[index]; +# 2: 681: self.erase(index); +# -: 682: return ret; +# -: 683: } +# #####: 684: vm->TypeError("pop() takes at most 1 argument"); +# #####: 685: return vm->None; +# 1210: 686: }); +# test list.pop: +try: + [1,2,3,4,5].pop(1,2,3,4) + print('未能拦截错误, 在测试 list.pop') + exit(1) +except: + pass + + + +# 未完全测试准确性----------------------------------------------- +# 116: 721: _vm->bind_method<1>("list", "__rmul__", [](VM* vm, ArgsView args) { +# #####: 722: const List& self = _CAST(List&, args[0]); +# #####: 723: if(!is_int(args[1])) return vm->NotImplemented; +# #####: 724: int n = _CAST(int, args[1]); +# #####: 725: List result; +# #####: 726: result.reserve(self.size() * n); +# #####: 727: for(int i = 0; i < n; i++) result.extend(self); +# #####: 728: return VAR(std::move(result)); +# #####: 729: }); +# test list.__rmul__: +assert type(12 * [12]) is list + + +# /************ tuple ************/ +# 未完全测试准确性----------------------------------------------- +# 180: 783: _vm->bind_constructor<-1>("tuple", [](VM* vm, ArgsView args) { +# 32: 784: if(args.size() == 1+0) return VAR(Tuple(0)); +# 32: 785: if(args.size() == 1+1){ +# 32: 786: List list = CAST(List, vm->py_list(args[1])); +# 32: 787: return VAR(Tuple(std::move(list))); +# 32: 788: } +# #####: 789: vm->TypeError("tuple() takes at most 1 argument"); +# #####: 790: return vm->None; +# 32: 791: }); +# -: 792: +# test tuple: +try: + tuple(1,2) + print('未能拦截错误, 在测试 tuple') + exit(1) +except: + pass + + +# 未完全测试准确性----------------------------------------------- +# 118: 793: _vm->bind__contains__(_vm->tp_tuple, [](VM* vm, PyObject* obj, PyObject* item) { +# 1: 794: Tuple& self = _CAST(Tuple&, obj); +# 3: 795: for(PyObject* i: self) if(vm->py_equals(i, item)) return vm->True; +# #####: 796: return vm->False; +# 1: 797: }); +# test tuple.__contains__: +assert (1,2,3).__contains__(5) == False + + +# 未完全测试准确性----------------------------------------------- +# 116: 799: _vm->bind_method<1>("tuple", "count", [](VM* vm, ArgsView args) { +# #####: 800: Tuple& self = _CAST(Tuple&, args[0]); +# -: 801: int count = 0; +# #####: 802: for(PyObject* i: self) if(vm->py_equals(i, args[1])) count++; +# #####: 803: return VAR(count); +# -: 804: }); +# test tuple.count: +assert (1,2,2,3,3,3).count(3) == 3 +assert (1,2,2,3,3,3).count(0) == 0 + + +# /************ bool ************/ +# ----------------------------------------------- +# 116: 842: _vm->bind__repr__(_vm->tp_bool, [](VM* vm, PyObject* self) { +# #####: 843: bool val = _CAST(bool, self); +# #####: 844: return VAR(val ? "True" : "False"); +# -: 845: }); +# test bool.__repr__: +assert repr(True) == 'True' +assert repr(False) == 'False' + + + +# 未完全测试准确性----------------------------------------------- +# 58: 851: const PK_LOCAL_STATIC auto f_bool_add = [](VM* vm, PyObject* lhs, PyObject* rhs) -> PyObject* { +# #####: 852: int x = (int)_CAST(bool, lhs); +# #####: 853: if(is_int(rhs)) return VAR(x + _CAST(int, rhs)); +# #####: 854: if(rhs == vm->True) return VAR(x + 1); +# #####: 855: if(rhs == vm->False) return VAR(x); +# #####: 856: return vm->NotImplemented; +# #####: 857: }; +# +# 58: 867: _vm->bind__add__(_vm->tp_bool, f_bool_add); +# test bool.__add__: +assert type(True + 1) is int +assert type(True + False) is int +assert type(True + True) is int + + + +# 未完全测试准确性----------------------------------------------- +# 58: 859: const PK_LOCAL_STATIC auto f_bool_mul = [](VM* vm, PyObject* lhs, PyObject* rhs) -> PyObject* { +# #####: 860: int x = (int)_CAST(bool, lhs); +# #####: 861: if(is_int(rhs)) return VAR(x * _CAST(int, rhs)); +# #####: 862: if(rhs == vm->True) return VAR(x); +# #####: 863: if(rhs == vm->False) return VAR(0); +# #####: 864: return vm->NotImplemented; +# #####: 865: }; +# +# 58: 872: _vm->bind__mul__(_vm->tp_bool, f_bool_mul); +# test bool.__mul__: +assert type(True * 1) is int +assert type(True * False) is int +assert type(True * True) is int + + +# 未完全测试准确性----------------------------------------------- +# 116: 873: _vm->bind_method<1>("bool", "__radd__", [](VM* vm, ArgsView args){ +# #####: 874: return f_bool_add(vm, args[0], args[1]); +# -: 875: }); +# test bool.__radd__: +assert type(1 + True) is int + + +# 未完全测试准确性----------------------------------------------- +# 116: 878: _vm->bind_method<1>("bool", "__rmul__", [](VM* vm, ArgsView args){ +# #####: 879: return f_bool_mul(vm, args[0], args[1]); +# -: 880: }); +# test bool.__rmul__: +assert type(1 * True) is int + + +# 未完全测试准确性----------------------------------------------- +# 116: 882: _vm->bind__and__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) { +# #####: 883: return VAR(_CAST(bool, lhs) && CAST(bool, rhs)); +# -: 884: }); +# test bool.__and__: +assert True & True == 1 + +# 未完全测试准确性----------------------------------------------- +# 116: 885: _vm->bind__or__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) { +# #####: 886: return VAR(_CAST(bool, lhs) || CAST(bool, rhs)); +# -: 887: }); +# test bool.__or__: +assert True | True == 1 + +# 未完全测试准确性----------------------------------------------- +# 116: 888: _vm->bind__xor__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) { +# #####: 889: return VAR(_CAST(bool, lhs) != CAST(bool, rhs)); +# -: 890: }); +# test bool.__xor__: +assert (True ^ True) == 0 + +# 未完全测试准确性----------------------------------------------- +# 120: 891: _vm->bind__eq__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) { +# 2: 892: if(is_non_tagged_type(rhs, vm->tp_bool)) return VAR(lhs == rhs); +# #####: 893: if(is_int(rhs)) return VAR(_CAST(bool, lhs) == (bool)CAST(i64, rhs)); +# #####: 894: return vm->NotImplemented; +# 2: 895: }); +# test bool.__eq__: +assert (True == True) == 1 + + +# /************ bytes ************/ +# 未完全测试准确性----------------------------------------------- +# 116: 922: _vm->bind__hash__(_vm->tp_bytes, [](VM* vm, PyObject* obj) { +# #####: 923: const Bytes& self = _CAST(Bytes&, obj); +# #####: 924: std::string_view view(self.data(), self.size()); +# #####: 925: return (i64)std::hash()(view); +# #####: 926: }); +# test bytes.__hash__: +assert type(hash(bytes([0x41, 0x42, 0x43]))) is int + + +# 未完全测试准确性----------------------------------------------- +# 116: 928: _vm->bind__repr__(_vm->tp_bytes, [](VM* vm, PyObject* obj) { +# #####: 929: const Bytes& self = _CAST(Bytes&, obj); +# #####: 930: std::stringstream ss; +# #####: 931: ss << "b'"; +# #####: 932: for(int i=0; ibind_constructor<4>("slice", [](VM* vm, ArgsView args) { +# #####: 954: return VAR(Slice(args[1], args[2], args[3])); +# -: 955: }); +# test slice: +assert type(slice(0.1, 0.2, 0.3)) is slice + +# 未完全测试准确性----------------------------------------------- +# 116: 957: _vm->bind__repr__(_vm->tp_slice, [](VM* vm, PyObject* obj) { +# #####: 958: const Slice& self = _CAST(Slice&, obj); +# #####: 959: std::stringstream ss; +# #####: 960: ss << "slice("; +# #####: 961: ss << CAST(Str, vm->py_repr(self.start)) << ", "; +# #####: 962: ss << CAST(Str, vm->py_repr(self.stop)) << ", "; +# #####: 963: ss << CAST(Str, vm->py_repr(self.step)) << ")"; +# #####: 964: return VAR(ss.str()); +# #####: 965: }); +# test slice.__repr__ +assert type(repr(slice(1,1,1))) is str + +# /************ mappingproxy ************/ +# 未完全测试准确性----------------------------------------------- +# 116: 968: _vm->bind_method<0>("mappingproxy", "keys", [](VM* vm, ArgsView args) { +# #####: 969: MappingProxy& self = _CAST(MappingProxy&, args[0]); +# #####: 970: List keys; +# #####: 971: for(StrName name : self.attr().keys()) keys.push_back(VAR(name.sv())); +# #####: 972: return VAR(std::move(keys)); +# #####: 973: }); +# test mappingproxy.keys: +class A(): + def __init__(self): + self.a = 10 + def method(self): + pass + + +my_mappingproxy = A().__dict__ +assert type(my_mappingproxy.keys()) is list + + +# 未完全测试准确性----------------------------------------------- +# 116: 975: _vm->bind_method<0>("mappingproxy", "values", [](VM* vm, ArgsView args) { +# #####: 976: MappingProxy& self = _CAST(MappingProxy&, args[0]); +# #####: 977: List values; +# #####: 978: for(auto& item : self.attr().items()) values.push_back(item.second); +# #####: 979: return VAR(std::move(values)); +# #####: 980: }); +# test mappingproxy.values: +class A(): + def __init__(self): + self.a = 10 + def method(self): + pass + + +my_mappingproxy = A().__dict__ +assert type(my_mappingproxy.values()) is list + + + +# 未完全测试准确性----------------------------------------------- +# 116: 992: _vm->bind__len__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) { +# #####: 993: return (i64)_CAST(MappingProxy&, obj).attr().size(); +# -: 994: }); +# test mappingproxy.__len__: +class A(): + def __init__(self): + self.a = 10 + def method(self): + pass + + +my_mappingproxy = A().__dict__ +assert type(len(my_mappingproxy)) is int + + +# 未完全测试准确性----------------------------------------------- +# 116: 996: _vm->bind__hash__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) { +# #####: 997: vm->TypeError("unhashable type: 'mappingproxy'"); +# #####: 998: return (i64)0; +# #####: 999: }); +# test mappingproxy.__hash__: +class A(): + def __init__(self): + self.a = 10 + def method(self): + pass + + +my_mappingproxy = A().__dict__ + +try: + hash(my_mappingproxy) + print('未能拦截错误, 在测试 mappingproxy.__hash__') + exit(1) +except: + pass + +# 未完全测试准确性----------------------------------------------- +# 116: 1009: _vm->bind__repr__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) { +# #####: 1010: MappingProxy& self = _CAST(MappingProxy&, obj); +# #####: 1011: std::stringstream ss; +# #####: 1012: ss << "mappingproxy({"; +# -: 1013: bool first = true; +# #####: 1014: for(auto& item : self.attr().items()){ +# #####: 1015: if(!first) ss << ", "; +# -: 1016: first = false; +# #####: 1017: ss << item.first.escape() << ": " << CAST(Str, vm->py_repr(item.second)); +# -: 1018: } +# #####: 1019: ss << "})"; +# #####: 1020: return VAR(ss.str()); +# #####: 1021: }); +# test mappingproxy.__repr__: +class A(): + def __init__(self): + self.a = 10 + def method(self): + pass + + +my_mappingproxy = A().__dict__ +assert type(repr(my_mappingproxy)) is str + + +# /************ dict ************/ +# 未完全测试准确性----------------------------------------------- +# 202: 1033: _vm->bind_method<-1>("dict", "__init__", [](VM* vm, ArgsView args){ +# 43: 1034: if(args.size() == 1+0) return vm->None; +# 42: 1035: if(args.size() == 1+1){ +# 42: 1036: auto _lock = vm->heap.gc_scope_lock(); +# 42: 1037: Dict& self = _CAST(Dict&, args[0]); +# 42: 1038: List& list = CAST(List&, args[1]); +# 165: 1039: for(PyObject* item : list){ +# 123: 1040: Tuple& t = CAST(Tuple&, item); +# 123: 1041: if(t.size() != 2){ +# #####: 1042: vm->ValueError("dict() takes an iterable of tuples (key, value)"); +# #####: 1043: return vm->None; +# -: 1044: } +# 123: 1045: self.set(t[0], t[1]); +# 246: 1046: } +# 42: 1047: return vm->None; +# 42: 1048: } +# #####: 1049: vm->TypeError("dict() takes at most 1 argument"); +# #####: 1050: return vm->None; +# 43: 1051: }); +# test dict: +assert type(dict([(1,2)])) is dict + +try: + dict([(1, 2, 3)]) + print('未能拦截错误, 在测试 dict') + exit(1) +except: + pass + +try: + dict([(1, 2)], 1) + print('未能拦截错误, 在测试 dict') + exit(1) +except: + pass + +# 未完全测试准确性----------------------------------------------- +# 116: 1057: _vm->bind__hash__(_vm->tp_dict, [](VM* vm, PyObject* obj) { +# #####: 1058: vm->TypeError("unhashable type: 'dict'"); +# #####: 1059: return (i64)0; +# #####: 1060: }); +# test dict.__hash__ +try: + hash(dict([(1,2)])) + print('未能拦截错误, 在测试 dict.__hash__') + exit(1) +except: + pass + +# 未完全测试准确性----------------------------------------------- +# 116: 1093: _vm->bind__iter__(_vm->tp_dict, [](VM* vm, PyObject* obj) { +# #####: 1094: const Dict& self = _CAST(Dict&, obj); +# #####: 1095: return vm->py_iter(VAR(self.keys())); +# #####: 1096: }); +# test dict.__iter__ +for k in {1:2, 2:3, 3:4}: + assert k in [1,2,3] + + +# 未完全测试准确性----------------------------------------------- +# 166: 1098: _vm->bind_method<-1>("dict", "get", [](VM* vm, ArgsView args) { +# 25: 1099: Dict& self = _CAST(Dict&, args[0]); +# 25: 1100: if(args.size() == 1+1){ +# #####: 1101: PyObject* ret = self.try_get(args[1]); +# #####: 1102: if(ret != nullptr) return ret; +# #####: 1103: return vm->None; +# 25: 1104: }else if(args.size() == 1+2){ +# 25: 1105: PyObject* ret = self.try_get(args[1]); +# 25: 1106: if(ret != nullptr) return ret; +# 19: 1107: return args[2]; +# -: 1108: } +# #####: 1109: vm->TypeError("get() takes at most 2 arguments"); +# #####: 1110: return vm->None; +# 25: 1111: }); +# test dict.get + +assert {1:2, 3:4}.get(1) == 2 +assert {1:2, 3:4}.get(2) is None +assert {1:2, 3:4}.get(20, 100) == 100 + +try: + {1:2, 3:4}.get(1,1, 1) + print('未能拦截错误, 在测试 dict.get') + exit(1) +except: + pass + +# 未完全测试准确性----------------------------------------------- +# 116: 1151: _vm->bind__repr__(_vm->tp_dict, [](VM* vm, PyObject* obj) { +# #####: 1152: Dict& self = _CAST(Dict&, obj); +# #####: 1153: std::stringstream ss; +# #####: 1154: ss << "{"; +# #####: 1155: bool first = true; +# -: 1156: +# #####: 1157: self.apply([&](PyObject* k, PyObject* v){ +# #####: 1158: if(!first) ss << ", "; +# #####: 1159: first = false; +# #####: 1160: Str key = CAST(Str&, vm->py_repr(k)); +# #####: 1161: Str value = CAST(Str&, vm->py_repr(v)); +# #####: 1162: ss << key << ": " << value; +# #####: 1163: }); +# -: 1164: +# #####: 1165: ss << "}"; +# #####: 1166: return VAR(ss.str()); +# #####: 1167: }); +# test dict.__repr__ +assert type(repr({1:2, 3:4})) is str + +# /************ property ************/ + +# /************ module timeit ************/ + +# /************ module time ************/ +import time +# 未完全测试准确性----------------------------------------------- +# 116: 1267: vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) { +# #####: 1268: f64 seconds = CAST_F(args[0]); +# #####: 1269: auto begin = std::chrono::system_clock::now(); +# #####: 1270: while(true){ +# #####: 1271: auto now = std::chrono::system_clock::now(); +# #####: 1272: f64 elapsed = std::chrono::duration_cast(now - begin).count() / 1000.0; +# #####: 1273: if(elapsed >= seconds) break; +# #####: 1274: } +# #####: 1275: return vm->None; +# #####: 1276: }); +# test time.time +assert type(time.time()) is float + +# 未完全测试准确性----------------------------------------------- +# 116: 1267: vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) { +# #####: 1268: f64 seconds = CAST_F(args[0]); +# #####: 1269: auto begin = std::chrono::system_clock::now(); +# #####: 1270: while(true){ +# #####: 1271: auto now = std::chrono::system_clock::now(); +# #####: 1272: f64 elapsed = std::chrono::duration_cast(now - begin).count() / 1000.0; +# #####: 1273: if(elapsed >= seconds) break; +# #####: 1274: } +# #####: 1275: return vm->None; +# #####: 1276: }); +# test time.sleep +time.sleep(0.5) + +# 未完全测试准确性----------------------------------------------- +# 116: 1278: vm->bind_func<0>(mod, "localtime", [](VM* vm, ArgsView args) { +# #####: 1279: auto now = std::chrono::system_clock::now(); +# #####: 1280: std::time_t t = std::chrono::system_clock::to_time_t(now); +# #####: 1281: std::tm* tm = std::localtime(&t); +# #####: 1282: Dict d(vm); +# #####: 1283: d.set(VAR("tm_year"), VAR(tm->tm_year + 1900)); +# #####: 1284: d.set(VAR("tm_mon"), VAR(tm->tm_mon + 1)); +# #####: 1285: d.set(VAR("tm_mday"), VAR(tm->tm_mday)); +# #####: 1286: d.set(VAR("tm_hour"), VAR(tm->tm_hour)); +# #####: 1287: d.set(VAR("tm_min"), VAR(tm->tm_min)); +# #####: 1288: d.set(VAR("tm_sec"), VAR(tm->tm_sec + 1)); +# #####: 1289: d.set(VAR("tm_wday"), VAR((tm->tm_wday + 6) % 7)); +# #####: 1290: d.set(VAR("tm_yday"), VAR(tm->tm_yday + 1)); +# #####: 1291: d.set(VAR("tm_isdst"), VAR(tm->tm_isdst)); +# #####: 1292: return VAR(std::move(d)); +# #####: 1293: }); +# 58: 1294:} +# test time.localtime +assert type(time.localtime()) is dict + +