traceback.c 640 B

12345678910111213141516171819202122232425262728
  1. #include "pocketpy/pocketpy.h"
  2. #include <stdlib.h>
  3. static bool traceback_format_exc(int argc, py_Ref argv) {
  4. PY_CHECK_ARGC(0);
  5. char* s = py_formatexc();
  6. if(!s) {
  7. py_newnone(py_retval());
  8. } else {
  9. py_newstr(py_retval(), s);
  10. free(s);
  11. }
  12. return true;
  13. }
  14. static bool traceback_print_exc(int argc, py_Ref argv) {
  15. PY_CHECK_ARGC(0);
  16. py_printexc();
  17. py_newnone(py_retval());
  18. return true;
  19. }
  20. void pk__add_module_traceback() {
  21. py_Ref mod = py_newmodule("traceback");
  22. py_bindfunc(mod, "format_exc", traceback_format_exc);
  23. py_bindfunc(mod, "print_exc", traceback_print_exc);
  24. }