builtins.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. #include "pocketpy/common/str.h"
  2. #include "pocketpy/objects/base.h"
  3. #include "pocketpy/objects/codeobject.h"
  4. #include "pocketpy/pocketpy.h"
  5. #include "pocketpy/common/utils.h"
  6. #include "pocketpy/objects/object.h"
  7. #include "pocketpy/common/sstream.h"
  8. #include "pocketpy/interpreter/vm.h"
  9. #include "pocketpy/common/_generated.h"
  10. #include <math.h>
  11. static bool builtins_exit(int argc, py_Ref argv) {
  12. int code = 0;
  13. if(argc > 1) return TypeError("exit() takes at most 1 argument");
  14. if(argc == 1) {
  15. PY_CHECK_ARG_TYPE(0, tp_int);
  16. code = py_toint(argv);
  17. }
  18. exit(code);
  19. return false;
  20. }
  21. static bool builtins_input(int argc, py_Ref argv) {
  22. if(argc > 1) return TypeError("input() takes at most 1 argument");
  23. const char* prompt = "";
  24. if(argc == 1) {
  25. if(!py_checkstr(argv)) return false;
  26. prompt = py_tostr(argv);
  27. }
  28. py_callbacks()->print(prompt);
  29. c11_sbuf buf;
  30. c11_sbuf__ctor(&buf);
  31. while(true) {
  32. int c = py_callbacks()->getchr();
  33. if(c == '\n' || c == '\r') break;
  34. if(c == EOF) break;
  35. c11_sbuf__write_char(&buf, c);
  36. }
  37. c11_sbuf__py_submit(&buf, py_retval());
  38. return true;
  39. }
  40. static bool builtins_repr(int argc, py_Ref argv) {
  41. PY_CHECK_ARGC(1);
  42. return py_repr(argv);
  43. }
  44. static bool builtins_len(int argc, py_Ref argv) {
  45. PY_CHECK_ARGC(1);
  46. return py_len(argv);
  47. }
  48. static bool builtins_hex(int argc, py_Ref argv) {
  49. PY_CHECK_ARGC(1);
  50. PY_CHECK_ARG_TYPE(0, tp_int);
  51. py_i64 val = py_toint(argv);
  52. if(val == 0) {
  53. py_newstr(py_retval(), "0x0");
  54. return true;
  55. }
  56. c11_sbuf ss;
  57. c11_sbuf__ctor(&ss);
  58. if(val < 0) {
  59. c11_sbuf__write_char(&ss, '-');
  60. val = -val;
  61. }
  62. c11_sbuf__write_cstr(&ss, "0x");
  63. bool non_zero = true;
  64. for(int i = 56; i >= 0; i -= 8) {
  65. unsigned char cpnt = (val >> i) & 0xff;
  66. c11_sbuf__write_hex(&ss, cpnt, non_zero);
  67. if(cpnt != 0) non_zero = false;
  68. }
  69. c11_sbuf__py_submit(&ss, py_retval());
  70. return true;
  71. }
  72. static bool builtins_iter(int argc, py_Ref argv) {
  73. PY_CHECK_ARGC(1);
  74. return py_iter(argv);
  75. }
  76. static bool builtins_next(int argc, py_Ref argv) {
  77. if(argc == 0 || argc > 2) return TypeError("next() takes 1 or 2 arguments");
  78. int res = py_next(argv);
  79. if(res == -1) return false;
  80. if(res) return true;
  81. if(argc == 1) {
  82. // StopIteration stored in py_retval()
  83. return py_raise(py_retval());
  84. } else {
  85. py_assign(py_retval(), py_arg(1));
  86. return true;
  87. }
  88. }
  89. static bool builtins_hash(int argc, py_Ref argv) {
  90. PY_CHECK_ARGC(1);
  91. py_i64 val;
  92. if(!py_hash(argv, &val)) return false;
  93. py_newint(py_retval(), val);
  94. return true;
  95. }
  96. static bool builtins_abs(int argc, py_Ref argv) {
  97. PY_CHECK_ARGC(1);
  98. return pk_callmagic(__abs__, 1, argv);
  99. }
  100. static bool builtins_divmod(int argc, py_Ref argv) {
  101. PY_CHECK_ARGC(2);
  102. return pk_callmagic(__divmod__, 2, argv);
  103. }
  104. static bool builtins_round(int argc, py_Ref argv) {
  105. py_i64 ndigits;
  106. if(argc == 1) {
  107. ndigits = -1;
  108. } else if(argc == 2) {
  109. PY_CHECK_ARG_TYPE(1, tp_int);
  110. ndigits = py_toint(py_arg(1));
  111. if(ndigits < 0) return ValueError("ndigits should be non-negative");
  112. } else {
  113. return TypeError("round() takes 1 or 2 arguments");
  114. }
  115. if(argv->type == tp_int) {
  116. py_assign(py_retval(), py_arg(0));
  117. return true;
  118. } else if(argv->type == tp_float) {
  119. py_f64 x = py_tofloat(py_arg(0));
  120. py_f64 offset = x >= 0 ? 0.5 : -0.5;
  121. if(ndigits == -1) {
  122. py_newint(py_retval(), (py_i64)(x + offset));
  123. return true;
  124. }
  125. py_f64 factor = pow(10, ndigits);
  126. py_newfloat(py_retval(), (py_i64)(x * factor + offset) / factor);
  127. return true;
  128. }
  129. return pk_callmagic(__round__, argc, argv);
  130. }
  131. static bool builtins_print(int argc, py_Ref argv) {
  132. // print(*args, sep=' ', end='\n', flush=False)
  133. py_TValue* args = py_tuple_data(argv);
  134. int length = py_tuple_len(argv);
  135. PY_CHECK_ARG_TYPE(1, tp_str);
  136. PY_CHECK_ARG_TYPE(2, tp_str);
  137. PY_CHECK_ARG_TYPE(3, tp_bool);
  138. c11_sv sep = py_tosv(py_arg(1));
  139. c11_sv end = py_tosv(py_arg(2));
  140. bool flush = py_tobool(py_arg(3));
  141. c11_sbuf buf;
  142. c11_sbuf__ctor(&buf);
  143. for(int i = 0; i < length; i++) {
  144. if(i > 0) c11_sbuf__write_sv(&buf, sep);
  145. if(!py_str(&args[i])) {
  146. c11_sbuf__dtor(&buf);
  147. return false;
  148. }
  149. c11_sbuf__write_sv(&buf, py_tosv(py_retval()));
  150. }
  151. c11_sbuf__write_sv(&buf, end);
  152. c11_string* res = c11_sbuf__submit(&buf);
  153. py_callbacks()->print(res->data);
  154. if(flush) py_callbacks()->flush();
  155. c11_string__delete(res);
  156. py_newnone(py_retval());
  157. return true;
  158. }
  159. static bool builtins_isinstance(int argc, py_Ref argv) {
  160. PY_CHECK_ARGC(2);
  161. if(py_istuple(py_arg(1))) {
  162. int length = py_tuple_len(py_arg(1));
  163. for(int i = 0; i < length; i++) {
  164. py_Ref item = py_tuple_getitem(py_arg(1), i);
  165. if(!py_checktype(item, tp_type)) return false;
  166. if(py_isinstance(py_arg(0), py_totype(item))) {
  167. py_newbool(py_retval(), true);
  168. return true;
  169. }
  170. }
  171. py_newbool(py_retval(), false);
  172. return true;
  173. }
  174. if(!py_checktype(py_arg(1), tp_type)) return false;
  175. py_newbool(py_retval(), py_isinstance(py_arg(0), py_totype(py_arg(1))));
  176. return true;
  177. }
  178. static bool builtins_issubclass(int argc, py_Ref argv) {
  179. PY_CHECK_ARGC(2);
  180. if(!py_checktype(py_arg(0), tp_type)) return false;
  181. if(!py_checktype(py_arg(1), tp_type)) return false;
  182. py_newbool(py_retval(), py_issubclass(py_totype(py_arg(0)), py_totype(py_arg(1))));
  183. return true;
  184. }
  185. static bool builtins_callable(int argc, py_Ref argv) {
  186. PY_CHECK_ARGC(1);
  187. bool res = py_callable(py_arg(0));
  188. py_newbool(py_retval(), res);
  189. return true;
  190. }
  191. static bool builtins_getattr(int argc, py_Ref argv) {
  192. PY_CHECK_ARG_TYPE(1, tp_str);
  193. py_Name name = py_namev(py_tosv(py_arg(1)));
  194. if(argc == 2) {
  195. return py_getattr(py_arg(0), name);
  196. } else if(argc == 3) {
  197. bool ok = py_getattr(py_arg(0), name);
  198. if(!ok && py_matchexc(tp_AttributeError)) {
  199. py_clearexc(NULL);
  200. py_assign(py_retval(), py_arg(2));
  201. return true; // default value
  202. }
  203. return ok;
  204. } else {
  205. return TypeError("getattr() expected 2 or 3 arguments");
  206. }
  207. return true;
  208. }
  209. static bool builtins_setattr(int argc, py_Ref argv) {
  210. PY_CHECK_ARGC(3);
  211. PY_CHECK_ARG_TYPE(1, tp_str);
  212. py_Name name = py_namev(py_tosv(py_arg(1)));
  213. py_newnone(py_retval());
  214. return py_setattr(py_arg(0), name, py_arg(2));
  215. }
  216. static bool builtins_hasattr(int argc, py_Ref argv) {
  217. PY_CHECK_ARGC(2);
  218. PY_CHECK_ARG_TYPE(1, tp_str);
  219. py_Name name = py_namev(py_tosv(py_arg(1)));
  220. bool ok = py_getattr(py_arg(0), name);
  221. if(ok) {
  222. py_newbool(py_retval(), true);
  223. return true;
  224. }
  225. if(py_matchexc(tp_AttributeError)) {
  226. py_clearexc(NULL);
  227. py_newbool(py_retval(), false);
  228. return true;
  229. }
  230. return false;
  231. }
  232. static bool builtins_delattr(int argc, py_Ref argv) {
  233. PY_CHECK_ARGC(2);
  234. PY_CHECK_ARG_TYPE(1, tp_str);
  235. py_Name name = py_namev(py_tosv(py_arg(1)));
  236. py_newnone(py_retval());
  237. return py_delattr(py_arg(0), name);
  238. }
  239. static bool builtins_chr(int argc, py_Ref argv) {
  240. PY_CHECK_ARGC(1);
  241. PY_CHECK_ARG_TYPE(0, tp_int);
  242. uint32_t val = py_toint(py_arg(0));
  243. // convert to utf-8
  244. char utf8[4];
  245. int len = c11__u32_to_u8(val, utf8);
  246. if(len == -1) return ValueError("invalid unicode code point: %d", val);
  247. py_newstrv(py_retval(), (c11_sv){utf8, len});
  248. return true;
  249. }
  250. static bool builtins_ord(int argc, py_Ref argv) {
  251. PY_CHECK_ARGC(1);
  252. PY_CHECK_ARG_TYPE(0, tp_str);
  253. c11_sv sv = py_tosv(py_arg(0));
  254. if(c11_sv__u8_length(sv) != 1) {
  255. return TypeError("ord() expected a character, but string of length %d found",
  256. c11_sv__u8_length(sv));
  257. }
  258. int u8bytes = c11__u8_header(sv.data[0], true);
  259. if(u8bytes == 0) return ValueError("invalid utf-8 char: %c", sv.data[0]);
  260. int value = c11__u8_value(u8bytes, sv.data);
  261. py_newint(py_retval(), value);
  262. return true;
  263. }
  264. static bool builtins_id(int argc, py_Ref argv) {
  265. PY_CHECK_ARGC(1);
  266. if(argv->is_ptr) {
  267. py_newint(py_retval(), (intptr_t)argv->_obj);
  268. } else {
  269. py_newnone(py_retval());
  270. }
  271. return true;
  272. }
  273. static bool builtins_globals(int argc, py_Ref argv) {
  274. PY_CHECK_ARGC(0);
  275. py_newglobals(py_retval());
  276. return true;
  277. }
  278. static bool builtins_locals(int argc, py_Ref argv) {
  279. PY_CHECK_ARGC(0);
  280. py_newlocals(py_retval());
  281. return true;
  282. }
  283. static void pk_push_special_locals() {
  284. py_Frame* frame = pk_current_vm->top_frame;
  285. if(!frame) {
  286. py_pushnil();
  287. return;
  288. }
  289. if(frame->is_locals_special) {
  290. py_push(frame->locals);
  291. } else {
  292. py_StackRef out = py_pushtmp();
  293. out->type = tp_locals;
  294. out->is_ptr = false;
  295. out->extra = 0;
  296. // this is a weak reference
  297. // which will expire when the frame is destroyed
  298. out->_ptr = frame;
  299. }
  300. }
  301. static bool _builtins_execdyn(const char* title, int argc, py_Ref argv, enum py_CompileMode mode) {
  302. switch(argc) {
  303. case 1: {
  304. py_newglobals(py_pushtmp());
  305. pk_push_special_locals();
  306. break;
  307. }
  308. case 2: {
  309. // globals
  310. if(py_isnone(py_arg(1))) {
  311. py_newglobals(py_pushtmp());
  312. } else {
  313. py_push(py_arg(1));
  314. }
  315. // locals
  316. py_pushnil();
  317. break;
  318. }
  319. case 3: {
  320. // globals
  321. if(py_isnone(py_arg(1))) {
  322. py_newglobals(py_pushtmp());
  323. } else {
  324. py_push(py_arg(1));
  325. }
  326. // locals
  327. if(py_isnone(py_arg(2))) {
  328. py_pushnil();
  329. } else {
  330. py_push(py_arg(2));
  331. }
  332. break;
  333. }
  334. default: return TypeError("%s() takes at most 3 arguments", title);
  335. }
  336. if(py_isstr(argv)) {
  337. bool ok = py_compile(py_tostr(argv), "<string>", mode, true);
  338. if(!ok) return false;
  339. py_push(py_retval());
  340. } else if(py_istype(argv, tp_code)) {
  341. py_push(argv);
  342. } else {
  343. return TypeError("%s() expected 'str' or 'code', got '%t'", title, argv->type);
  344. }
  345. py_Frame* frame = pk_current_vm->top_frame;
  346. // [globals, locals, code]
  347. CodeObject* code = py_touserdata(py_peek(-1));
  348. if(code->src->is_dynamic) {
  349. bool ok = pk_execdyn(code, frame ? frame->module : NULL, py_peek(-3), py_peek(-2));
  350. py_shrink(3);
  351. return ok;
  352. } else {
  353. if(argc != 1) {
  354. return ValueError(
  355. "code object is not dynamic, `globals` and `locals` must not be specified");
  356. }
  357. bool ok = pk_exec(code, frame ? frame->module : NULL);
  358. py_shrink(3);
  359. return ok;
  360. }
  361. }
  362. static bool builtins_exec(int argc, py_Ref argv) {
  363. bool ok = _builtins_execdyn("exec", argc, argv, EXEC_MODE);
  364. py_newnone(py_retval());
  365. return ok;
  366. }
  367. static bool builtins_eval(int argc, py_Ref argv) {
  368. return _builtins_execdyn("eval", argc, argv, EVAL_MODE);
  369. }
  370. static bool builtins_compile(int argc, py_Ref argv) {
  371. PY_CHECK_ARGC(3);
  372. for(int i = 0; i < 3; i++) {
  373. if(!py_checktype(py_arg(i), tp_str)) return false;
  374. }
  375. const char* source = py_tostr(py_arg(0));
  376. const char* filename = py_tostr(py_arg(1));
  377. const char* mode = py_tostr(py_arg(2));
  378. enum py_CompileMode compile_mode;
  379. if(strcmp(mode, "exec") == 0) {
  380. compile_mode = EXEC_MODE;
  381. } else if(strcmp(mode, "eval") == 0) {
  382. compile_mode = EVAL_MODE;
  383. } else if(strcmp(mode, "single") == 0) {
  384. compile_mode = SINGLE_MODE;
  385. } else {
  386. return ValueError("compile() mode must be 'exec', 'eval', or 'single'");
  387. }
  388. return py_compile(source, filename, compile_mode, true);
  389. }
  390. static bool builtins__import__(int argc, py_Ref argv) {
  391. PY_CHECK_ARGC(1);
  392. PY_CHECK_ARG_TYPE(0, tp_str);
  393. int res = py_import(py_tostr(argv));
  394. if(res == -1) return false;
  395. if(res) return true;
  396. return ImportError("module '%s' not found", py_tostr(argv));
  397. }
  398. static bool NoneType__repr__(int argc, py_Ref argv) {
  399. py_newstr(py_retval(), "None");
  400. return true;
  401. }
  402. static bool ellipsis__repr__(int argc, py_Ref argv) {
  403. py_newstr(py_retval(), "...");
  404. return true;
  405. }
  406. static bool NotImplementedType__repr__(int argc, py_Ref argv) {
  407. py_newstr(py_retval(), "NotImplemented");
  408. return true;
  409. }
  410. py_GlobalRef pk_builtins__register() {
  411. py_GlobalRef builtins = py_newmodule("builtins");
  412. py_bindfunc(builtins, "exit", builtins_exit);
  413. py_bindfunc(builtins, "input", builtins_input);
  414. py_bindfunc(builtins, "repr", builtins_repr);
  415. py_bindfunc(builtins, "len", builtins_len);
  416. py_bindfunc(builtins, "hex", builtins_hex);
  417. py_bindfunc(builtins, "iter", builtins_iter);
  418. py_bindfunc(builtins, "next", builtins_next);
  419. py_bindfunc(builtins, "hash", builtins_hash);
  420. py_bindfunc(builtins, "abs", builtins_abs);
  421. py_bindfunc(builtins, "divmod", builtins_divmod);
  422. py_bindfunc(builtins, "round", builtins_round);
  423. py_bind(builtins, "print(*args, sep=' ', end='\\n', flush=False)", builtins_print);
  424. py_bindfunc(builtins, "isinstance", builtins_isinstance);
  425. py_bindfunc(builtins, "issubclass", builtins_issubclass);
  426. py_bindfunc(builtins, "callable", builtins_callable);
  427. py_bindfunc(builtins, "getattr", builtins_getattr);
  428. py_bindfunc(builtins, "setattr", builtins_setattr);
  429. py_bindfunc(builtins, "hasattr", builtins_hasattr);
  430. py_bindfunc(builtins, "delattr", builtins_delattr);
  431. py_bindfunc(builtins, "chr", builtins_chr);
  432. py_bindfunc(builtins, "ord", builtins_ord);
  433. py_bindfunc(builtins, "id", builtins_id);
  434. py_bindfunc(builtins, "globals", builtins_globals);
  435. py_bindfunc(builtins, "locals", builtins_locals);
  436. py_bindfunc(builtins, "exec", builtins_exec);
  437. py_bindfunc(builtins, "eval", builtins_eval);
  438. py_bindfunc(builtins, "compile", builtins_compile);
  439. py_bindfunc(builtins, "__import__", builtins__import__);
  440. // some patches
  441. py_bindmagic(tp_NoneType, __repr__, NoneType__repr__);
  442. py_setdict(py_tpobject(tp_NoneType), __hash__, py_None());
  443. py_bindmagic(tp_ellipsis, __repr__, ellipsis__repr__);
  444. py_setdict(py_tpobject(tp_ellipsis), __hash__, py_None());
  445. py_bindmagic(tp_NotImplementedType, __repr__, NotImplementedType__repr__);
  446. py_setdict(py_tpobject(tp_NotImplementedType), __hash__, py_None());
  447. return builtins;
  448. }
  449. void function__gc_mark(void* ud, c11_vector* p_stack) {
  450. Function* func = ud;
  451. if(func->globals) pk__mark_value(func->globals);
  452. if(func->closure) {
  453. NameDict* dict = func->closure;
  454. for(int i = 0; i < dict->capacity; i++) {
  455. NameDict_KV* kv = &dict->items[i];
  456. if(kv->key == NULL) continue;
  457. pk__mark_value(&kv->value);
  458. }
  459. }
  460. FuncDecl__gc_mark(func->decl, p_stack);
  461. }
  462. static bool function__doc__(int argc, py_Ref argv) {
  463. PY_CHECK_ARGC(1);
  464. Function* func = py_touserdata(py_arg(0));
  465. if(func->decl->docstring) {
  466. py_newstr(py_retval(), func->decl->docstring);
  467. } else {
  468. py_newnone(py_retval());
  469. }
  470. return true;
  471. }
  472. static bool function__name__(int argc, py_Ref argv) {
  473. PY_CHECK_ARGC(1);
  474. Function* func = py_touserdata(py_arg(0));
  475. py_newstr(py_retval(), func->decl->code.name->data);
  476. return true;
  477. }
  478. static bool function__repr__(int argc, py_Ref argv) {
  479. // <function f at 0x10365b9c0>
  480. PY_CHECK_ARGC(1);
  481. Function* func = py_touserdata(py_arg(0));
  482. c11_sbuf buf;
  483. c11_sbuf__ctor(&buf);
  484. c11_sbuf__write_cstr(&buf, "<function ");
  485. c11_sbuf__write_cstr(&buf, func->decl->code.name->data);
  486. c11_sbuf__write_cstr(&buf, " at ");
  487. c11_sbuf__write_ptr(&buf, func);
  488. c11_sbuf__write_char(&buf, '>');
  489. c11_sbuf__py_submit(&buf, py_retval());
  490. return true;
  491. }
  492. py_Type pk_function__register() {
  493. py_Type type =
  494. pk_newtype("function", tp_object, NULL, (void (*)(void*))Function__dtor, false, true);
  495. py_bindproperty(type, "__doc__", function__doc__, NULL);
  496. py_bindproperty(type, "__name__", function__name__, NULL);
  497. py_bindmagic(type, __repr__, function__repr__);
  498. return type;
  499. }
  500. static bool nativefunc__repr__(int argc, py_Ref argv) {
  501. PY_CHECK_ARGC(1);
  502. py_newstr(py_retval(), "<nativefunc object>");
  503. return true;
  504. }
  505. py_Type pk_nativefunc__register() {
  506. py_Type type = pk_newtype("nativefunc", tp_object, NULL, NULL, false, true);
  507. py_bindmagic(type, __repr__, nativefunc__repr__);
  508. return type;
  509. }
  510. static bool super__new__(int argc, py_Ref argv) {
  511. py_Type class_arg = 0;
  512. py_Frame* frame = pk_current_vm->top_frame;
  513. py_Ref self_arg = NULL;
  514. if(argc == 1) {
  515. // super()
  516. if(!frame->is_locals_special) {
  517. py_TValue* callable = frame->p0;
  518. if(callable->type == tp_boundmethod) callable = py_getslot(frame->p0, 1);
  519. if(callable->type == tp_function) {
  520. Function* func = py_touserdata(callable);
  521. if(func->clazz != NULL) {
  522. class_arg = ((py_TypeInfo*)PyObject__userdata(func->clazz))->index;
  523. if(frame->co->nlocals > 0) { self_arg = &frame->locals[0]; }
  524. }
  525. }
  526. }
  527. if(class_arg == 0 || self_arg == NULL) return RuntimeError("super(): no arguments");
  528. if(self_arg->type == tp_type) {
  529. // f(cls, ...)
  530. class_arg = pk_typeinfo(class_arg)->base;
  531. if(class_arg == 0) return RuntimeError("super(): base class is invalid");
  532. py_assign(py_retval(), py_tpobject(class_arg));
  533. return true;
  534. }
  535. } else if(argc == 3) {
  536. // super(type[T], obj)
  537. PY_CHECK_ARG_TYPE(1, tp_type);
  538. class_arg = py_totype(py_arg(1));
  539. self_arg = py_arg(2);
  540. if(!py_isinstance(self_arg, class_arg)) {
  541. return TypeError("super(type, obj): obj must be an instance of type");
  542. }
  543. } else {
  544. return TypeError("super() takes 0 or 2 arguments");
  545. }
  546. class_arg = pk_typeinfo(class_arg)->base;
  547. if(class_arg == 0) return RuntimeError("super(): base class is invalid");
  548. py_Type* p_class_arg = py_newobject(py_retval(), tp_super, 1, sizeof(py_Type));
  549. *p_class_arg = class_arg;
  550. py_setslot(py_retval(), 0, self_arg);
  551. return true;
  552. }
  553. py_Type pk_super__register() {
  554. py_Type type = pk_newtype("super", tp_object, NULL, NULL, false, true);
  555. py_bindmagic(type, __new__, super__new__);
  556. return type;
  557. }