99_builtin_func.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. # 无法测试 -----------------------------------------------
  2. # #####: 41:static dylib_entry_t load_dylib(const char* path){
  3. # #####: 42: std::error_code ec;
  4. # #####: 43: auto p = std::filesystem::absolute(path, ec);
  5. # #####: 44: if(ec) return nullptr;
  6. # #####: 45: void* handle = dlopen(p.c_str(), RTLD_LAZY);
  7. # #####: 46: if(!handle) return nullptr;
  8. # #####: 47: return (dylib_entry_t)dlsym(handle, "pkpy_module__init__");
  9. # #####: 48:}
  10. # -----------------------------------------------
  11. # 128: 107: _vm->bind_builtin_func<2>("super", [](VM* vm, ArgsView args) {
  12. # 8: 108: vm->check_non_tagged_type(args[0], vm->tp_type);
  13. # 8: 109: Type type = PK_OBJ_GET(Type, args[0]);
  14. # 8: 110: if(!vm->isinstance(args[1], type)){
  15. # #####: 111: Str _0 = obj_type_name(vm, PK_OBJ_GET(Type, vm->_t(args[1])));
  16. # #####: 112: Str _1 = obj_type_name(vm, type);
  17. # #####: 113: vm->TypeError("super(): " + _0.escape() + " is not an instance of " + _1.escape());
  18. # #####: 114: }
  19. # 8: 115: Type base = vm->_all_types[type].base;
  20. # 16: 116: return vm->heap.gcnew(vm->tp_super, Super(args[1], base));
  21. # 8: 117: });
  22. # test super:
  23. class TestSuperBase():
  24. def __init__(self):
  25. self.base_attr = 1
  26. def base_method(self):
  27. return self.base_attr
  28. def error(self):
  29. raise Expection('未能拦截错误')
  30. class TestSuperChild1(TestSuperBase):
  31. def __init__(self):
  32. super(TestSuperChild1, self).__init__()
  33. def child_method(self):
  34. return super(TestSuperChild1, self).base_method()
  35. def error_handling(self):
  36. try:
  37. super(TestSuperChild1, self).error()
  38. except:
  39. pass
  40. class TestSuperChild2(TestSuperBase):
  41. pass
  42. test_base = TestSuperBase()
  43. # 测试属性
  44. assert test_base.base_attr == 1
  45. # 测试方法
  46. assert test_base.base_method() == 1
  47. test_child1 = TestSuperChild1()
  48. # 测试继承的属性
  49. assert test_child1.base_attr == 1
  50. # 测试继承的方法
  51. assert test_child1.base_method() == 1
  52. # 测试子类添加的方法
  53. assert test_child1.child_method() == 1
  54. # 测试子类的错误拦截
  55. test_child1.error_handling()
  56. test_child2 = TestSuperChild2()
  57. # 测试继承的属性
  58. assert test_child2.base_attr == 1
  59. # 测试继承的方法
  60. assert test_child2.base_method() == 1
  61. class TestSuperNoBaseMethod(TestSuperBase):
  62. def __init__(self):
  63. super(TestSuperNoBaseMethod, self).append(1)
  64. try:
  65. t = TestSuperNoParent()
  66. print('未能拦截错误')
  67. exit(1)
  68. except:
  69. pass
  70. try:
  71. t = TestSuperNoBaseMethod()
  72. print('未能拦截错误')
  73. exit(1)
  74. except:
  75. pass
  76. class B():
  77. pass
  78. class C():
  79. def method(self):
  80. super(C, self).method()
  81. class D():
  82. def method(self):
  83. super(B, self).method()
  84. try:
  85. c = C()
  86. c.method()
  87. print('未能拦截错误')
  88. exit(1)
  89. except:
  90. pass
  91. try:
  92. d = D()
  93. d.method()
  94. print('未能拦截错误')
  95. exit(1)
  96. except:
  97. pass
  98. # -----------------------------------------------
  99. # 114: 188: _vm->bind_builtin_func<1>("staticmethod", [](VM* vm, ArgsView args) {
  100. # #####: 189: return args[0];
  101. # -: 190: });
  102. # test staticmethod:
  103. class A():
  104. def __init__(self):
  105. self.a = 1
  106. @ staticmethod
  107. def static_method(txt):
  108. return txt
  109. assert A.static_method(123) == 123
  110. # 无法测试 -----------------------------------------------
  111. # 248: 192: _vm->bind_builtin_func<1>("__import__", [](VM* vm, ArgsView args) {
  112. # 67: 193: const Str& name = CAST(Str&, args[0]);
  113. # 67: 194: auto dot = name.sv().find_last_of(".");
  114. # 67: 195: if(dot != std::string_view::npos){
  115. # #####: 196: auto ext = name.sv().substr(dot);
  116. # #####: 197: if(ext == ".so" || ext == ".dll" || ext == ".dylib"){
  117. # #####: 198: dylib_entry_t entry = load_dylib(name.c_str());
  118. # #####: 199: if(!entry){
  119. # #####: 200: vm->_error("ImportError", "cannot load dynamic library: " + name.escape());
  120. # #####: 201: }
  121. # #####: 202: vm->_c.s_view.push(ArgsView(vm->s_data.end(), vm->s_data.end()));
  122. # #####: 203: const char* name = entry(vm, PK_VERSION);
  123. # #####: 204: vm->_c.s_view.pop();
  124. # #####: 205: if(name == nullptr){
  125. # #####: 206: vm->_error("ImportError", "module initialization failed: " + Str(name).escape());
  126. # #####: 207: }
  127. # #####: 208: return vm->_modules[name];
  128. # #####: 209: }
  129. # #####: 210: }
  130. # 67: 211: return vm->py_import(name);
  131. # 67: 212: });
  132. # 无法测试 -----------------------------------------------
  133. # 114: 238: _vm->bind_builtin_func<-1>("exit", [](VM* vm, ArgsView args) {
  134. # #####: 239: if(args.size() == 0) std::exit(0);
  135. # #####: 240: else if(args.size() == 1) std::exit(CAST(int, args[0]));
  136. # #####: 241: else vm->TypeError("exit() takes at most 1 argument");
  137. # #####: 242: return vm->None;
  138. # #####: 243: });
  139. # -----------------------------------------------
  140. # 114: 253: _vm->bind_builtin_func<1>("hash", [](VM* vm, ArgsView args){
  141. # #####: 254: i64 value = vm->py_hash(args[0]);
  142. # #####: 255: if(((value << 2) >> 2) != value) value >>= 2;
  143. # #####: 256: return VAR(value);
  144. # -: 257: });
  145. # test hash:
  146. # 测试整数类型的输入
  147. assert hash(0) == 0
  148. assert hash(123) == 123
  149. assert hash(-456) == -456
  150. # 测试字符串类型的输入
  151. assert type(hash("hello")) is int
  152. # 测试浮点数类型的输入
  153. assert type(hash(3.14)) is int
  154. assert type(hash(-2.71828)) is int
  155. # 测试边界情况
  156. assert type(hash(None)) is int
  157. assert hash(True) == 1
  158. assert hash(False) == 0
  159. # 测试元组
  160. assert type(hash((4, 5, 6, (1234,1122), 2.3983, 'abcd'))) is int
  161. # 测试自定义类和对象的输入
  162. class A():
  163. pass
  164. a = A()
  165. assert type(hash(A)) is int
  166. assert type(hash(a)) is int
  167. # 测试函数的输入
  168. def f():
  169. pass
  170. assert type(hash(a)) is int
  171. # 测试不可哈希对象
  172. try:
  173. hash({1:1})
  174. print('未能拦截错误')
  175. exit(1)
  176. except:
  177. pass
  178. try:
  179. hash([1])
  180. print('未能拦截错误')
  181. exit(1)
  182. except:
  183. pass
  184. # -----------------------------------------------
  185. # 114: 259: _vm->bind_builtin_func<1>("chr", [](VM* vm, ArgsView args) {
  186. # #####: 260: i64 i = CAST(i64, args[0]);
  187. # #####: 261: if (i < 0 || i > 128) vm->ValueError("chr() arg not in range(128)");
  188. # #####: 262: return VAR(std::string(1, (char)i));
  189. # #####: 263: });
  190. # test chr
  191. l = []
  192. for i in range(128):
  193. l.append(f'{i} {chr(i)}')
  194. assert l == ['0 \x00', '1 \x01', '2 \x02', '3 \x03', '4 \x04', '5 \x05', '6 \x06', '7 \x07', '8 \x08', '9 \t', '10 \n', '11 \x0b', '12 \x0c', '13 \r', '14 \x0e', '15 \x0f', '16 \x10', '17 \x11', '18 \x12', '19 \x13', '20 \x14', '21 \x15', '22 \x16', '23 \x17', '24 \x18', '25 \x19', '26 \x1a', '27 \x1b', '28 \x1c', '29 \x1d', '30 \x1e', '31 \x1f', '32 ', '33 !', '34 "', '35 #', '36 $', '37 %', '38 &', "39 '", '40 (', '41 )', '42 *', '43 +', '44 ,', '45 -', '46 .', '47 /', '48 0', '49 1', '50 2', '51 3', '52 4', '53 5', '54 6', '55 7', '56 8', '57 9', '58 :', '59 ;', '60 <', '61 =', '62 >', '63 ?', '64 @', '65 A', '66 B', '67 C', '68 D', '69 E', '70 F', '71 G', '72 H', '73 I', '74 J', '75 K', '76 L', '77 M', '78 N', '79 O', '80 P', '81 Q', '82 R', '83 S', '84 T', '85 U', '86 V', '87 W', '88 X', '89 Y', '90 Z', '91 [', '92 \\', '93 ]', '94 ^', '95 _', '96 `', '97 a', '98 b', '99 c', '100 d', '101 e', '102 f', '103 g', '104 h', '105 i', '106 j', '107 k', '108 l', '109 m', '110 n', '111 o', '112 p', '113 q', '114 r', '115 s', '116 t', '117 u', '118 v', '119 w', '120 x', '121 y', '122 z', '123 {', '124 |', '125 }', '126 ~', '127 \x7f']
  195. # 未完全测试准确性-----------------------------------------------
  196. # 116: 269: _vm->bind_builtin_func<1>("bin", [](VM* vm, ArgsView args) {
  197. # #####: 270: std::stringstream ss;
  198. # #####: 271: i64 x = CAST(i64, args[0]);
  199. # #####: 272: if(x < 0){ ss << "-"; x = -x; }
  200. # #####: 273: ss << "0b";
  201. # #####: 274: std::string bits;
  202. # #####: 275: while(x){
  203. # #####: 276: bits += (x & 1) ? '1' : '0';
  204. # #####: 277: x >>= 1;
  205. # -: 278: }
  206. # #####: 279: std::reverse(bits.begin(), bits.end());
  207. # #####: 280: if(bits.empty()) bits = "0";
  208. # #####: 281: ss << bits;
  209. # #####: 282: return VAR(ss.str());
  210. # #####: 283: });
  211. # test bin:
  212. assert type(bin(1234)) is str
  213. # 无法测试, 不能覆盖-----------------------------------------------
  214. # 136: 285: _vm->bind_builtin_func<1>("dir", [](VM* vm, ArgsView args) {
  215. # 10: 286: std::set<StrName> names;
  216. # 10: 287: if(!is_tagged(args[0]) && args[0]->is_attr_valid()){
  217. # #####: 288: std::vector<StrName> keys = args[0]->attr().keys();
  218. # #####: 289: names.insert(keys.begin(), keys.end());
  219. # #####: 290: }
  220. # 10: 291: const NameDict& t_attr = vm->_t(args[0])->attr();
  221. # 10: 292: std::vector<StrName> keys = t_attr.keys();
  222. # 10: 293: names.insert(keys.begin(), keys.end());
  223. # 10: 294: List ret;
  224. # 305: 295: for (StrName name : names) ret.push_back(VAR(name.sv()));
  225. # 10: 296: return VAR(std::move(ret));
  226. # 10: 297: });
  227. # test dir:
  228. # 未完全测试准确性-----------------------------------------------
  229. # 116: 299: _vm->bind__repr__(_vm->tp_object, [](VM* vm, PyObject* obj) {
  230. # #####: 300: if(is_tagged(obj)) FATAL_ERROR();
  231. # #####: 301: std::stringstream ss;
  232. # #####: 302: ss << "<" << OBJ_NAME(vm->_t(obj)) << " object at 0x";
  233. # #####: 303: ss << std::hex << reinterpret_cast<intptr_t>(obj) << ">";
  234. # #####: 304: return VAR(ss.str());
  235. # #####: 305: });
  236. # test __repr__:
  237. class A():
  238. def __init__(self):
  239. self.attr = 0
  240. repr(A())
  241. # 未完全测试准确性-----------------------------------------------
  242. # 33600: 318: _vm->bind_constructor<-1>("range", [](VM* vm, ArgsView args) {
  243. # 16742: 319: args._begin += 1; // skip cls
  244. # 16742: 320: Range r;
  245. # 16742: 321: switch (args.size()) {
  246. # 8735: 322: case 1: r.stop = CAST(i64, args[0]); break;
  247. # 3867: 323: case 2: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); break;
  248. # 4140: 324: case 3: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); r.step = CAST(i64, args[2]); break;
  249. # #####: 325: default: vm->TypeError("expected 1-3 arguments, got " + std::to_string(args.size()));
  250. # #####: 326: }
  251. # 33484: 327: return VAR(r);
  252. # 16742: 328: });
  253. # -: 329:
  254. # test range:
  255. try:
  256. range(1,2,3,4)
  257. print('未能拦截错误, 在测试 range')
  258. exit(1)
  259. except:
  260. pass
  261. # /************ int ************/
  262. try:
  263. int('asad')
  264. print('未能拦截错误, 在测试 int')
  265. exit(1)
  266. except:
  267. pass
  268. try:
  269. int(123, 16)
  270. print('未能拦截错误, 在测试 int')
  271. exit(1)
  272. except:
  273. pass
  274. # 未完全测试准确性-----------------------------------------------
  275. # 116: 392: _vm->bind_method<0>("int", "bit_length", [](VM* vm, ArgsView args) {
  276. # #####: 393: i64 x = _CAST(i64, args[0]);
  277. # #####: 394: if(x < 0) x = -x;
  278. # -: 395: int bits = 0;
  279. # #####: 396: while(x){ x >>= 1; bits++; }
  280. # #####: 397: return VAR(bits);
  281. # -: 398: });
  282. # test int.bit_length:
  283. assert type(int.bit_length(100)) is int
  284. # 未完全测试准确性-----------------------------------------------
  285. # 116: 400: _vm->bind__floordiv__(_vm->tp_int, [](VM* vm, PyObject* lhs_, PyObject* rhs_) {
  286. # #####: 401: i64 rhs = CAST(i64, rhs_);
  287. # #####: 402: return VAR(_CAST(i64, lhs_) / rhs);
  288. # -: 403: });
  289. # test int.__floordiv__:
  290. assert type(10//11) is int
  291. # 未完全测试准确性-----------------------------------------------
  292. # 116: 405: _vm->bind__mod__(_vm->tp_int, [](VM* vm, PyObject* lhs_, PyObject* rhs_) {
  293. # #####: 406: i64 rhs = CAST(i64, rhs_);
  294. # #####: 407: return VAR(_CAST(i64, lhs_) % rhs);
  295. # test int.__mod__:
  296. assert type(11%2) is int
  297. # /************ float ************/
  298. # 136: 433: _vm->bind_constructor<2>("float", [](VM* vm, ArgsView args) {
  299. # 10: 434: if (is_type(args[1], vm->tp_int)) return VAR((f64)CAST(i64, args[1]));
  300. # 9: 435: if (is_type(args[1], vm->tp_float)) return args[1];
  301. # 3: 436: if (is_type(args[1], vm->tp_bool)) return VAR(_CAST(bool, args[1]) ? 1.0 : 0.0);
  302. # 3: 437: if (is_type(args[1], vm->tp_str)) {
  303. # 3: 438: const Str& s = CAST(Str&, args[1]);
  304. # 3: 439: if(s == "inf") return VAR(INFINITY);
  305. # 2: 440: if(s == "-inf") return VAR(-INFINITY);
  306. # -: 441: try{
  307. # 2: 442: f64 val = Number::stof(s.str());
  308. # 2: 443: return VAR(val);
  309. # #####: 444: }catch(...){
  310. # #####: 445: vm->ValueError("invalid literal for float(): " + s.escape());
  311. # #####: 446: }
  312. # #####: 447: }
  313. # #####: 448: vm->TypeError("float() argument must be a int, float, bool or str");
  314. # #####: 449: return vm->None;
  315. # 10: 450: });
  316. # test float:
  317. try:
  318. float('asad')
  319. print('未能拦截错误, 在测试 float')
  320. exit(1)
  321. except:
  322. pass
  323. try:
  324. float([])
  325. print('未能拦截错误, 在测试 float')
  326. exit(1)
  327. except:
  328. pass
  329. # /************ str ************/
  330. # 未完全测试准确性-----------------------------------------------
  331. # 116: 495: _vm->bind_method<1>("str", "__rmul__", [](VM* vm, ArgsView args) {
  332. # #####: 496: const Str& self = _CAST(Str&, args[0]);
  333. # #####: 497: i64 n = CAST(i64, args[1]);
  334. # #####: 498: std::stringstream ss;
  335. # #####: 499: for(i64 i = 0; i < n; i++) ss << self.sv();
  336. # #####: 500: return VAR(ss.str());
  337. # #####: 501: });
  338. # test str.__rmul__:
  339. assert type(12 * '12') is str
  340. # 未完全测试准确性-----------------------------------------------
  341. # 116: 554: _vm->bind_method<1>("str", "index", [](VM* vm, ArgsView args) {
  342. # #####: 555: const Str& self = _CAST(Str&, args[0]);
  343. # #####: 556: const Str& sub = CAST(Str&, args[1]);
  344. # #####: 557: int index = self.index(sub);
  345. # #####: 558: if(index == -1) vm->ValueError("substring not found");
  346. # #####: 559: return VAR(index);
  347. # #####: 560: });
  348. # test str.index:
  349. assert type('25363546'.index('63')) is int
  350. try:
  351. '25363546'.index('err')
  352. print('未能拦截错误, 在测试 str.index')
  353. exit(1)
  354. except:
  355. pass
  356. # 未完全测试准确性-----------------------------------------------
  357. # 116: 562: _vm->bind_method<1>("str", "find", [](VM* vm, ArgsView args) {
  358. # #####: 563: const Str& self = _CAST(Str&, args[0]);
  359. # #####: 564: const Str& sub = CAST(Str&, args[1]);
  360. # #####: 565: return VAR(self.index(sub));
  361. # -: 566: });
  362. # test str.find:
  363. assert type('25363546'.find('63')) is int
  364. assert type('25363546'.find('err')) is int
  365. # /************ list ************/
  366. # 未完全测试准确性-----------------------------------------------
  367. # 174: 615: _vm->bind_constructor<-1>("list", [](VM* vm, ArgsView args) {
  368. # 29: 616: if(args.size() == 1+0) return VAR(List());
  369. # 29: 617: if(args.size() == 1+1){
  370. # 29: 618: return vm->py_list(args[1]);
  371. # -: 619: }
  372. # #####: 620: vm->TypeError("list() takes 0 or 1 arguments");
  373. # #####: 621: return vm->None;
  374. # 29: 622: });
  375. # test list:
  376. try:
  377. list(1,2)
  378. print('未能拦截错误, 在测试 list')
  379. exit(1)
  380. except:
  381. pass
  382. # 未完全测试准确性----------------------------------------------
  383. # 116: 648: _vm->bind_method<1>("list", "index", [](VM* vm, ArgsView args) {
  384. # #####: 649: List& self = _CAST(List&, args[0]);
  385. # #####: 650: PyObject* obj = args[1];
  386. # #####: 651: for(int i=0; i<self.size(); i++){
  387. # #####: 652: if(vm->py_equals(self[i], obj)) return VAR(i);
  388. # -: 653: }
  389. # #####: 654: vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
  390. # #####: 655: return vm->None;
  391. # #####: 656: });
  392. # test list.index:
  393. assert type([1,2,3,4,5].index(4)) is int
  394. try:
  395. [1,2,3,4,5].index(6)
  396. print('未能拦截错误, 在测试 list.index')
  397. exit(1)
  398. except:
  399. pass
  400. # 未完全测试准确性----------------------------------------------
  401. # 118: 658: _vm->bind_method<1>("list", "remove", [](VM* vm, ArgsView args) {
  402. # 1: 659: List& self = _CAST(List&, args[0]);
  403. # 1: 660: PyObject* obj = args[1];
  404. # 2: 661: for(int i=0; i<self.size(); i++){
  405. # 2: 662: if(vm->py_equals(self[i], obj)){
  406. # 1: 663: self.erase(i);
  407. # 1: 664: return vm->None;
  408. # -: 665: }
  409. # -: 666: }
  410. # #####: 667: vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
  411. # #####: 668: return vm->None;
  412. # 1: 669: });
  413. # test list.remove:
  414. try:
  415. [1,2,3,4,5].remove(6)
  416. print('未能拦截错误, 在测试 list.remove')
  417. exit(1)
  418. except:
  419. pass
  420. # 未完全测试准确性----------------------------------------------
  421. # 2536: 671: _vm->bind_method<-1>("list", "pop", [](VM* vm, ArgsView args) {
  422. # 1210: 672: List& self = _CAST(List&, args[0]);
  423. # 1210: 673: if(args.size() == 1+0){
  424. # 1208: 674: if(self.empty()) vm->IndexError("pop from empty list");
  425. # 1208: 675: return self.popx_back();
  426. # -: 676: }
  427. # 2: 677: if(args.size() == 1+1){
  428. # 2: 678: int index = CAST(int, args[1]);
  429. # 2: 679: index = vm->normalized_index(index, self.size());
  430. # 2: 680: PyObject* ret = self[index];
  431. # 2: 681: self.erase(index);
  432. # -: 682: return ret;
  433. # -: 683: }
  434. # #####: 684: vm->TypeError("pop() takes at most 1 argument");
  435. # #####: 685: return vm->None;
  436. # 1210: 686: });
  437. # test list.pop:
  438. try:
  439. [1,2,3,4,5].pop(1,2,3,4)
  440. print('未能拦截错误, 在测试 list.pop')
  441. exit(1)
  442. except:
  443. pass
  444. # 未完全测试准确性-----------------------------------------------
  445. # 116: 721: _vm->bind_method<1>("list", "__rmul__", [](VM* vm, ArgsView args) {
  446. # #####: 722: const List& self = _CAST(List&, args[0]);
  447. # #####: 723: if(!is_int(args[1])) return vm->NotImplemented;
  448. # #####: 724: int n = _CAST(int, args[1]);
  449. # #####: 725: List result;
  450. # #####: 726: result.reserve(self.size() * n);
  451. # #####: 727: for(int i = 0; i < n; i++) result.extend(self);
  452. # #####: 728: return VAR(std::move(result));
  453. # #####: 729: });
  454. # test list.__rmul__:
  455. assert type(12 * [12]) is list
  456. # /************ tuple ************/
  457. # 未完全测试准确性-----------------------------------------------
  458. # 180: 783: _vm->bind_constructor<-1>("tuple", [](VM* vm, ArgsView args) {
  459. # 32: 784: if(args.size() == 1+0) return VAR(Tuple(0));
  460. # 32: 785: if(args.size() == 1+1){
  461. # 32: 786: List list = CAST(List, vm->py_list(args[1]));
  462. # 32: 787: return VAR(Tuple(std::move(list)));
  463. # 32: 788: }
  464. # #####: 789: vm->TypeError("tuple() takes at most 1 argument");
  465. # #####: 790: return vm->None;
  466. # 32: 791: });
  467. # -: 792:
  468. # test tuple:
  469. try:
  470. tuple(1,2)
  471. print('未能拦截错误, 在测试 tuple')
  472. exit(1)
  473. except:
  474. pass
  475. # 未完全测试准确性-----------------------------------------------
  476. # 118: 793: _vm->bind__contains__(_vm->tp_tuple, [](VM* vm, PyObject* obj, PyObject* item) {
  477. # 1: 794: Tuple& self = _CAST(Tuple&, obj);
  478. # 3: 795: for(PyObject* i: self) if(vm->py_equals(i, item)) return vm->True;
  479. # #####: 796: return vm->False;
  480. # 1: 797: });
  481. # test tuple.__contains__:
  482. assert (1,2,3).__contains__(5) == False
  483. # 未完全测试准确性-----------------------------------------------
  484. # 116: 799: _vm->bind_method<1>("tuple", "count", [](VM* vm, ArgsView args) {
  485. # #####: 800: Tuple& self = _CAST(Tuple&, args[0]);
  486. # -: 801: int count = 0;
  487. # #####: 802: for(PyObject* i: self) if(vm->py_equals(i, args[1])) count++;
  488. # #####: 803: return VAR(count);
  489. # -: 804: });
  490. # test tuple.count:
  491. assert (1,2,2,3,3,3).count(3) == 3
  492. assert (1,2,2,3,3,3).count(0) == 0
  493. # /************ bool ************/
  494. # -----------------------------------------------
  495. # 116: 842: _vm->bind__repr__(_vm->tp_bool, [](VM* vm, PyObject* self) {
  496. # #####: 843: bool val = _CAST(bool, self);
  497. # #####: 844: return VAR(val ? "True" : "False");
  498. # -: 845: });
  499. # test bool.__repr__:
  500. assert repr(True) == 'True'
  501. assert repr(False) == 'False'
  502. # 未完全测试准确性-----------------------------------------------
  503. # 58: 851: const PK_LOCAL_STATIC auto f_bool_add = [](VM* vm, PyObject* lhs, PyObject* rhs) -> PyObject* {
  504. # #####: 852: int x = (int)_CAST(bool, lhs);
  505. # #####: 853: if(is_int(rhs)) return VAR(x + _CAST(int, rhs));
  506. # #####: 854: if(rhs == vm->True) return VAR(x + 1);
  507. # #####: 855: if(rhs == vm->False) return VAR(x);
  508. # #####: 856: return vm->NotImplemented;
  509. # #####: 857: };
  510. #
  511. # 58: 867: _vm->bind__add__(_vm->tp_bool, f_bool_add);
  512. # test bool.__add__:
  513. assert type(True + 1) is int
  514. assert type(True + False) is int
  515. assert type(True + True) is int
  516. # 未完全测试准确性-----------------------------------------------
  517. # 58: 859: const PK_LOCAL_STATIC auto f_bool_mul = [](VM* vm, PyObject* lhs, PyObject* rhs) -> PyObject* {
  518. # #####: 860: int x = (int)_CAST(bool, lhs);
  519. # #####: 861: if(is_int(rhs)) return VAR(x * _CAST(int, rhs));
  520. # #####: 862: if(rhs == vm->True) return VAR(x);
  521. # #####: 863: if(rhs == vm->False) return VAR(0);
  522. # #####: 864: return vm->NotImplemented;
  523. # #####: 865: };
  524. #
  525. # 58: 872: _vm->bind__mul__(_vm->tp_bool, f_bool_mul);
  526. # test bool.__mul__:
  527. assert type(True * 1) is int
  528. assert type(True * False) is int
  529. assert type(True * True) is int
  530. # 未完全测试准确性-----------------------------------------------
  531. # 116: 873: _vm->bind_method<1>("bool", "__radd__", [](VM* vm, ArgsView args){
  532. # #####: 874: return f_bool_add(vm, args[0], args[1]);
  533. # -: 875: });
  534. # test bool.__radd__:
  535. assert type(1 + True) is int
  536. # 未完全测试准确性-----------------------------------------------
  537. # 116: 878: _vm->bind_method<1>("bool", "__rmul__", [](VM* vm, ArgsView args){
  538. # #####: 879: return f_bool_mul(vm, args[0], args[1]);
  539. # -: 880: });
  540. # test bool.__rmul__:
  541. assert type(1 * True) is int
  542. # 未完全测试准确性-----------------------------------------------
  543. # 116: 882: _vm->bind__and__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  544. # #####: 883: return VAR(_CAST(bool, lhs) && CAST(bool, rhs));
  545. # -: 884: });
  546. # test bool.__and__:
  547. assert True & True == 1
  548. # 未完全测试准确性-----------------------------------------------
  549. # 116: 885: _vm->bind__or__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  550. # #####: 886: return VAR(_CAST(bool, lhs) || CAST(bool, rhs));
  551. # -: 887: });
  552. # test bool.__or__:
  553. assert True | True == 1
  554. # 未完全测试准确性-----------------------------------------------
  555. # 116: 888: _vm->bind__xor__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  556. # #####: 889: return VAR(_CAST(bool, lhs) != CAST(bool, rhs));
  557. # -: 890: });
  558. # test bool.__xor__:
  559. assert (True ^ True) == 0
  560. # 未完全测试准确性-----------------------------------------------
  561. # 120: 891: _vm->bind__eq__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  562. # 2: 892: if(is_non_tagged_type(rhs, vm->tp_bool)) return VAR(lhs == rhs);
  563. # #####: 893: if(is_int(rhs)) return VAR(_CAST(bool, lhs) == (bool)CAST(i64, rhs));
  564. # #####: 894: return vm->NotImplemented;
  565. # 2: 895: });
  566. # test bool.__eq__:
  567. assert (True == True) == 1
  568. # /************ bytes ************/
  569. # 未完全测试准确性-----------------------------------------------
  570. # 116: 922: _vm->bind__hash__(_vm->tp_bytes, [](VM* vm, PyObject* obj) {
  571. # #####: 923: const Bytes& self = _CAST(Bytes&, obj);
  572. # #####: 924: std::string_view view(self.data(), self.size());
  573. # #####: 925: return (i64)std::hash<std::string_view>()(view);
  574. # #####: 926: });
  575. # test bytes.__hash__:
  576. assert type(hash(bytes([0x41, 0x42, 0x43]))) is int
  577. # 未完全测试准确性-----------------------------------------------
  578. # 116: 928: _vm->bind__repr__(_vm->tp_bytes, [](VM* vm, PyObject* obj) {
  579. # #####: 929: const Bytes& self = _CAST(Bytes&, obj);
  580. # #####: 930: std::stringstream ss;
  581. # #####: 931: ss << "b'";
  582. # #####: 932: for(int i=0; i<self.size(); i++){
  583. # #####: 933: ss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << self[i];
  584. # -: 934: }
  585. # #####: 935: ss << "'";
  586. # #####: 936: return VAR(ss.str());
  587. # #####: 937: });
  588. # test bytes.__repr__:
  589. assert type(repr(bytes([0x41, 0x42, 0x43]))) is str
  590. # /************ slice ************/
  591. # 未完全测试准确性-----------------------------------------------
  592. # 116: 953: _vm->bind_constructor<4>("slice", [](VM* vm, ArgsView args) {
  593. # #####: 954: return VAR(Slice(args[1], args[2], args[3]));
  594. # -: 955: });
  595. # test slice:
  596. assert type(slice(0.1, 0.2, 0.3)) is slice
  597. # 未完全测试准确性-----------------------------------------------
  598. # 116: 1529: bind_property(_t(tp_slice), "start", [](VM* vm, ArgsView args){
  599. # #####: 1530: return CAST(Slice&, args[0]).start;
  600. # -: 1531: });
  601. # 116: 1532: bind_property(_t(tp_slice), "stop", [](VM* vm, ArgsView args){
  602. # #####: 1533: return CAST(Slice&, args[0]).stop;
  603. # -: 1534: });
  604. # 116: 1535: bind_property(_t(tp_slice), "step", [](VM* vm, ArgsView args){
  605. # #####: 1536: return CAST(Slice&, args[0]).step;
  606. # -: 1537: });
  607. s = slice(1, 2, 3)
  608. assert type(s) is slice
  609. assert s.start == 1
  610. assert s.stop == 2
  611. assert s.step == 3
  612. assert slice.__dict__['start'].__signature__ == 'start'
  613. # 未完全测试准确性-----------------------------------------------
  614. # 116: 957: _vm->bind__repr__(_vm->tp_slice, [](VM* vm, PyObject* obj) {
  615. # #####: 958: const Slice& self = _CAST(Slice&, obj);
  616. # #####: 959: std::stringstream ss;
  617. # #####: 960: ss << "slice(";
  618. # #####: 961: ss << CAST(Str, vm->py_repr(self.start)) << ", ";
  619. # #####: 962: ss << CAST(Str, vm->py_repr(self.stop)) << ", ";
  620. # #####: 963: ss << CAST(Str, vm->py_repr(self.step)) << ")";
  621. # #####: 964: return VAR(ss.str());
  622. # #####: 965: });
  623. # test slice.__repr__
  624. assert type(repr(slice(1,1,1))) is str
  625. # /************ mappingproxy ************/
  626. # 未完全测试准确性-----------------------------------------------
  627. # 116: 968: _vm->bind_method<0>("mappingproxy", "keys", [](VM* vm, ArgsView args) {
  628. # #####: 969: MappingProxy& self = _CAST(MappingProxy&, args[0]);
  629. # #####: 970: List keys;
  630. # #####: 971: for(StrName name : self.attr().keys()) keys.push_back(VAR(name.sv()));
  631. # #####: 972: return VAR(std::move(keys));
  632. # #####: 973: });
  633. # test mappingproxy.keys:
  634. class A():
  635. def __init__(self):
  636. self.a = 10
  637. def method(self):
  638. pass
  639. my_mappingproxy = A().__dict__
  640. assert type(my_mappingproxy.keys()) is list
  641. # 未完全测试准确性-----------------------------------------------
  642. # 116: 975: _vm->bind_method<0>("mappingproxy", "values", [](VM* vm, ArgsView args) {
  643. # #####: 976: MappingProxy& self = _CAST(MappingProxy&, args[0]);
  644. # #####: 977: List values;
  645. # #####: 978: for(auto& item : self.attr().items()) values.push_back(item.second);
  646. # #####: 979: return VAR(std::move(values));
  647. # #####: 980: });
  648. # test mappingproxy.values:
  649. class A():
  650. def __init__(self):
  651. self.a = 10
  652. def method(self):
  653. pass
  654. my_mappingproxy = A().__dict__
  655. assert type(my_mappingproxy.values()) is list
  656. # 未完全测试准确性-----------------------------------------------
  657. # 116: 992: _vm->bind__len__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) {
  658. # #####: 993: return (i64)_CAST(MappingProxy&, obj).attr().size();
  659. # -: 994: });
  660. # test mappingproxy.__len__:
  661. class A():
  662. def __init__(self):
  663. self.a = 10
  664. def method(self):
  665. pass
  666. my_mappingproxy = A().__dict__
  667. assert type(len(my_mappingproxy)) is int
  668. # 未完全测试准确性-----------------------------------------------
  669. # 116: 996: _vm->bind__hash__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) {
  670. # #####: 997: vm->TypeError("unhashable type: 'mappingproxy'");
  671. # #####: 998: return (i64)0;
  672. # #####: 999: });
  673. # test mappingproxy.__hash__:
  674. class A():
  675. def __init__(self):
  676. self.a = 10
  677. def method(self):
  678. pass
  679. my_mappingproxy = A().__dict__
  680. try:
  681. hash(my_mappingproxy)
  682. print('未能拦截错误, 在测试 mappingproxy.__hash__')
  683. exit(1)
  684. except TypeError:
  685. pass
  686. a = hash(object()) # object is hashable
  687. a = hash(A()) # A is hashable
  688. class B:
  689. def __eq__(self, o): return True
  690. try:
  691. hash(B())
  692. print('未能拦截错误, 在测试 B.__hash__')
  693. exit(1)
  694. except TypeError:
  695. pass
  696. # 未完全测试准确性-----------------------------------------------
  697. # 116: 1009: _vm->bind__repr__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) {
  698. # #####: 1010: MappingProxy& self = _CAST(MappingProxy&, obj);
  699. # #####: 1011: std::stringstream ss;
  700. # #####: 1012: ss << "mappingproxy({";
  701. # -: 1013: bool first = true;
  702. # #####: 1014: for(auto& item : self.attr().items()){
  703. # #####: 1015: if(!first) ss << ", ";
  704. # -: 1016: first = false;
  705. # #####: 1017: ss << item.first.escape() << ": " << CAST(Str, vm->py_repr(item.second));
  706. # -: 1018: }
  707. # #####: 1019: ss << "})";
  708. # #####: 1020: return VAR(ss.str());
  709. # #####: 1021: });
  710. # test mappingproxy.__repr__:
  711. class A():
  712. def __init__(self):
  713. self.a = 10
  714. def method(self):
  715. pass
  716. my_mappingproxy = A().__dict__
  717. assert type(repr(my_mappingproxy)) is str
  718. # /************ dict ************/
  719. # 未完全测试准确性-----------------------------------------------
  720. # 202: 1033: _vm->bind_method<-1>("dict", "__init__", [](VM* vm, ArgsView args){
  721. # 43: 1034: if(args.size() == 1+0) return vm->None;
  722. # 42: 1035: if(args.size() == 1+1){
  723. # 42: 1036: auto _lock = vm->heap.gc_scope_lock();
  724. # 42: 1037: Dict& self = _CAST(Dict&, args[0]);
  725. # 42: 1038: List& list = CAST(List&, args[1]);
  726. # 165: 1039: for(PyObject* item : list){
  727. # 123: 1040: Tuple& t = CAST(Tuple&, item);
  728. # 123: 1041: if(t.size() != 2){
  729. # #####: 1042: vm->ValueError("dict() takes an iterable of tuples (key, value)");
  730. # #####: 1043: return vm->None;
  731. # -: 1044: }
  732. # 123: 1045: self.set(t[0], t[1]);
  733. # 246: 1046: }
  734. # 42: 1047: return vm->None;
  735. # 42: 1048: }
  736. # #####: 1049: vm->TypeError("dict() takes at most 1 argument");
  737. # #####: 1050: return vm->None;
  738. # 43: 1051: });
  739. # test dict:
  740. assert type(dict([(1,2)])) is dict
  741. try:
  742. dict([(1, 2, 3)])
  743. print('未能拦截错误, 在测试 dict')
  744. exit(1)
  745. except:
  746. pass
  747. try:
  748. dict([(1, 2)], 1)
  749. print('未能拦截错误, 在测试 dict')
  750. exit(1)
  751. except:
  752. pass
  753. # 未完全测试准确性-----------------------------------------------
  754. # 116: 1057: _vm->bind__hash__(_vm->tp_dict, [](VM* vm, PyObject* obj) {
  755. # #####: 1058: vm->TypeError("unhashable type: 'dict'");
  756. # #####: 1059: return (i64)0;
  757. # #####: 1060: });
  758. # test dict.__hash__
  759. try:
  760. hash(dict([(1,2)]))
  761. print('未能拦截错误, 在测试 dict.__hash__')
  762. exit(1)
  763. except:
  764. pass
  765. # 未完全测试准确性-----------------------------------------------
  766. # 116: 1093: _vm->bind__iter__(_vm->tp_dict, [](VM* vm, PyObject* obj) {
  767. # #####: 1094: const Dict& self = _CAST(Dict&, obj);
  768. # #####: 1095: return vm->py_iter(VAR(self.keys()));
  769. # #####: 1096: });
  770. # test dict.__iter__
  771. for k in {1:2, 2:3, 3:4}:
  772. assert k in [1,2,3]
  773. # 未完全测试准确性-----------------------------------------------
  774. # 166: 1098: _vm->bind_method<-1>("dict", "get", [](VM* vm, ArgsView args) {
  775. # 25: 1099: Dict& self = _CAST(Dict&, args[0]);
  776. # 25: 1100: if(args.size() == 1+1){
  777. # #####: 1101: PyObject* ret = self.try_get(args[1]);
  778. # #####: 1102: if(ret != nullptr) return ret;
  779. # #####: 1103: return vm->None;
  780. # 25: 1104: }else if(args.size() == 1+2){
  781. # 25: 1105: PyObject* ret = self.try_get(args[1]);
  782. # 25: 1106: if(ret != nullptr) return ret;
  783. # 19: 1107: return args[2];
  784. # -: 1108: }
  785. # #####: 1109: vm->TypeError("get() takes at most 2 arguments");
  786. # #####: 1110: return vm->None;
  787. # 25: 1111: });
  788. # test dict.get
  789. assert {1:2, 3:4}.get(1) == 2
  790. assert {1:2, 3:4}.get(2) is None
  791. assert {1:2, 3:4}.get(20, 100) == 100
  792. try:
  793. {1:2, 3:4}.get(1,1, 1)
  794. print('未能拦截错误, 在测试 dict.get')
  795. exit(1)
  796. except:
  797. pass
  798. # 未完全测试准确性-----------------------------------------------
  799. # 116: 1151: _vm->bind__repr__(_vm->tp_dict, [](VM* vm, PyObject* obj) {
  800. # #####: 1152: Dict& self = _CAST(Dict&, obj);
  801. # #####: 1153: std::stringstream ss;
  802. # #####: 1154: ss << "{";
  803. # #####: 1155: bool first = true;
  804. # -: 1156:
  805. # #####: 1157: self.apply([&](PyObject* k, PyObject* v){
  806. # #####: 1158: if(!first) ss << ", ";
  807. # #####: 1159: first = false;
  808. # #####: 1160: Str key = CAST(Str&, vm->py_repr(k));
  809. # #####: 1161: Str value = CAST(Str&, vm->py_repr(v));
  810. # #####: 1162: ss << key << ": " << value;
  811. # #####: 1163: });
  812. # -: 1164:
  813. # #####: 1165: ss << "}";
  814. # #####: 1166: return VAR(ss.str());
  815. # #####: 1167: });
  816. # test dict.__repr__
  817. assert type(repr({1:2, 3:4})) is str
  818. # /************ property ************/
  819. class A():
  820. def __init__(self):
  821. self._name = '123'
  822. @property
  823. def value(self):
  824. return 2
  825. def get_name(self):
  826. '''
  827. doc string 1
  828. '''
  829. return self._name
  830. def set_name(self, val):
  831. '''
  832. doc string 2
  833. '''
  834. self._name = val
  835. assert A().value == 2
  836. assert A.__dict__['value'].__signature__ == ''
  837. A.name = property(A.get_name, A.set_name, "name: str")
  838. assert A.__dict__['name'].__signature__ == 'name: str'
  839. try:
  840. property(A.get_name, A.set_name, 1)
  841. print('未能拦截错误, 在测试 property')
  842. exit(1)
  843. except:
  844. pass
  845. # /************ module timeit ************/
  846. import timeit
  847. def aaa():
  848. for i in range(100):
  849. for j in range(100):
  850. pass
  851. assert type(timeit.timeit(aaa, 2)) is float
  852. # 未完全测试准确性-----------------------------------------------
  853. # 116: 1218: _vm->bind_property(_vm->_t(_vm->tp_function), "__doc__", [](VM* vm, ArgsView args) {
  854. # #####: 1219: Function& func = _CAST(Function&, args[0]);
  855. # #####: 1220: return VAR(func.decl->docstring);
  856. # -: 1221: });
  857. # function.__doc__
  858. def aaa():
  859. '12345'
  860. pass
  861. assert type(aaa.__doc__) is str
  862. # 未完全测试准确性-----------------------------------------------
  863. # 116: 1229: _vm->bind_property(_vm->_t(_vm->tp_function), "__signature__", [](VM* vm, ArgsView args) {
  864. # #####: 1230: Function& func = _CAST(Function&, args[0]);
  865. # #####: 1231: return VAR(func.decl->signature);
  866. # -: 1232: });
  867. # function.__signature__
  868. def aaa():
  869. pass
  870. assert type(aaa.__signature__) is str
  871. # /************ module time ************/
  872. import time
  873. # 未完全测试准确性-----------------------------------------------
  874. # 116: 1267: vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
  875. # #####: 1268: f64 seconds = CAST_F(args[0]);
  876. # #####: 1269: auto begin = std::chrono::system_clock::now();
  877. # #####: 1270: while(true){
  878. # #####: 1271: auto now = std::chrono::system_clock::now();
  879. # #####: 1272: f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() / 1000.0;
  880. # #####: 1273: if(elapsed >= seconds) break;
  881. # #####: 1274: }
  882. # #####: 1275: return vm->None;
  883. # #####: 1276: });
  884. # test time.time
  885. assert type(time.time()) is float
  886. local_t = time.localtime()
  887. assert type(local_t.tm_year) is int
  888. assert type(local_t.tm_mon) is int
  889. assert type(local_t.tm_mday) is int
  890. assert type(local_t.tm_hour) is int
  891. assert type(local_t.tm_min) is int
  892. assert type(local_t.tm_sec) is int
  893. assert type(local_t.tm_wday) is int
  894. assert type(local_t.tm_yday) is int
  895. assert type(local_t.tm_isdst) is int
  896. # 未完全测试准确性-----------------------------------------------
  897. # 116: 1267: vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
  898. # #####: 1268: f64 seconds = CAST_F(args[0]);
  899. # #####: 1269: auto begin = std::chrono::system_clock::now();
  900. # #####: 1270: while(true){
  901. # #####: 1271: auto now = std::chrono::system_clock::now();
  902. # #####: 1272: f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() / 1000.0;
  903. # #####: 1273: if(elapsed >= seconds) break;
  904. # #####: 1274: }
  905. # #####: 1275: return vm->None;
  906. # #####: 1276: });
  907. # test time.sleep
  908. time.sleep(0.1)
  909. # 未完全测试准确性-----------------------------------------------
  910. # 116: 1278: vm->bind_func<0>(mod, "localtime", [](VM* vm, ArgsView args) {
  911. # #####: 1279: auto now = std::chrono::system_clock::now();
  912. # #####: 1280: std::time_t t = std::chrono::system_clock::to_time_t(now);
  913. # #####: 1281: std::tm* tm = std::localtime(&t);
  914. # #####: 1282: Dict d(vm);
  915. # #####: 1283: d.set(VAR("tm_year"), VAR(tm->tm_year + 1900));
  916. # #####: 1284: d.set(VAR("tm_mon"), VAR(tm->tm_mon + 1));
  917. # #####: 1285: d.set(VAR("tm_mday"), VAR(tm->tm_mday));
  918. # #####: 1286: d.set(VAR("tm_hour"), VAR(tm->tm_hour));
  919. # #####: 1287: d.set(VAR("tm_min"), VAR(tm->tm_min));
  920. # #####: 1288: d.set(VAR("tm_sec"), VAR(tm->tm_sec + 1));
  921. # #####: 1289: d.set(VAR("tm_wday"), VAR((tm->tm_wday + 6) % 7));
  922. # #####: 1290: d.set(VAR("tm_yday"), VAR(tm->tm_yday + 1));
  923. # #####: 1291: d.set(VAR("tm_isdst"), VAR(tm->tm_isdst));
  924. # #####: 1292: return VAR(std::move(d));
  925. # #####: 1293: });
  926. # 58: 1294:}
  927. # test time.localtime
  928. assert type(time.localtime()) is time.struct_time
  929. # /************ module dis ************/
  930. import dis
  931. # 116: 1487: vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) {
  932. # #####: 1488: CodeObject_ code = get_code(vm, args[0]);
  933. # #####: 1489: vm->_stdout(vm, vm->disassemble(code));
  934. # #####: 1490: return vm->None;
  935. # #####: 1491: });
  936. # test dis.dis
  937. def aaa():
  938. pass
  939. assert dis.dis(aaa) is None
  940. # test min/max
  941. assert min(1, 2) == 1
  942. assert min(1, 2, 3) == 1
  943. assert min([1, 2]) == 1
  944. assert min([1, 2], key=lambda x: -x) == 2
  945. assert max(1, 2) == 2
  946. assert max(1, 2, 3) == 3
  947. assert max([1, 2]) == 2
  948. assert max([1, 2], key=lambda x: -x) == 1
  949. assert min([
  950. (1, 2),
  951. (1, 3),
  952. (1, 4),
  953. ]) == (1, 2)
  954. # test callable
  955. assert callable(lambda: 1) is True # function
  956. assert callable(1) is False # int
  957. assert callable(object) is True # type
  958. assert callable(object()) is False
  959. assert callable([].append) is True # bound method
  960. assert callable([].__getitem__) is True # bound method
  961. class A:
  962. def __init__(self):
  963. pass
  964. def __call__(self):
  965. pass
  966. assert callable(A) is True # type
  967. assert callable(A()) is True # instance with __call__
  968. assert callable(A.__call__) is True # bound method
  969. assert callable(A.__init__) is True # bound method
  970. assert callable(print) is True # builtin function
  971. assert callable(isinstance) is True # builtin function