99_builtin_func.py 41 KB

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