mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
add traceback
This commit is contained in:
parent
11f709b768
commit
0af8b4c7d2
@ -10,3 +10,4 @@ void pk__add_module_json();
|
|||||||
void pk__add_module_gc();
|
void pk__add_module_gc();
|
||||||
void pk__add_module_time();
|
void pk__add_module_time();
|
||||||
void pk__add_module_easing();
|
void pk__add_module_easing();
|
||||||
|
void pk__add_module_traceback();
|
@ -65,6 +65,8 @@ int py_currentvm();
|
|||||||
/// Switch to a VM.
|
/// Switch to a VM.
|
||||||
/// @param index index of the VM ranging from 0 to 16 (exclusive). `0` is the default VM.
|
/// @param index index of the VM ranging from 0 to 16 (exclusive). `0` is the default VM.
|
||||||
void py_switchvm(int index);
|
void py_switchvm(int index);
|
||||||
|
/// Set `sys.argv`.
|
||||||
|
void py_sys_setargv(int argc, char** argv);
|
||||||
|
|
||||||
/// Run a source string.
|
/// Run a source string.
|
||||||
/// @param source source string.
|
/// @param source source string.
|
||||||
@ -506,6 +508,7 @@ void py_list_delitem(py_Ref self, int i);
|
|||||||
int py_list_len(py_Ref self);
|
int py_list_len(py_Ref self);
|
||||||
void py_list_swap(py_Ref self, int i, int j);
|
void py_list_swap(py_Ref self, int i, int j);
|
||||||
void py_list_append(py_Ref self, py_Ref val);
|
void py_list_append(py_Ref self, py_Ref val);
|
||||||
|
py_ItemRef py_list_emplace(py_Ref self);
|
||||||
void py_list_clear(py_Ref self);
|
void py_list_clear(py_Ref self);
|
||||||
void py_list_insert(py_Ref self, int i, py_Ref val);
|
void py_list_insert(py_Ref self, int i, py_Ref val);
|
||||||
|
|
||||||
|
@ -204,6 +204,7 @@ void VM__ctor(VM* self) {
|
|||||||
pk__add_module_gc();
|
pk__add_module_gc();
|
||||||
pk__add_module_time();
|
pk__add_module_time();
|
||||||
pk__add_module_easing();
|
pk__add_module_easing();
|
||||||
|
pk__add_module_traceback();
|
||||||
|
|
||||||
// add python builtins
|
// add python builtins
|
||||||
do {
|
do {
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#include "pocketpy/common/config.h"
|
||||||
|
#include "pocketpy/common/export.h"
|
||||||
#include "pocketpy/pocketpy.h"
|
#include "pocketpy/pocketpy.h"
|
||||||
|
|
||||||
#include "pocketpy/common/utils.h"
|
#include "pocketpy/common/utils.h"
|
||||||
@ -51,4 +53,7 @@ void pk__add_module_os() {
|
|||||||
|
|
||||||
void pk__add_module_sys() {
|
void pk__add_module_sys() {
|
||||||
py_Ref mod = py_newmodule("sys");
|
py_Ref mod = py_newmodule("sys");
|
||||||
|
py_newstr(py_emplacedict(mod, py_name("platform")), PY_SYS_PLATFORM_STRING);
|
||||||
|
py_newstr(py_emplacedict(mod, py_name("version")), PK_VERSION);
|
||||||
|
py_newlist(py_emplacedict(mod, py_name("argv")));
|
||||||
}
|
}
|
28
src/modules/traceback.c
Normal file
28
src/modules/traceback.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include "pocketpy/pocketpy.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static bool traceback_format_exc(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(0);
|
||||||
|
char* s = py_formatexc();
|
||||||
|
if(!s) {
|
||||||
|
py_newnone(py_retval());
|
||||||
|
} else {
|
||||||
|
py_newstr(py_retval(), s);
|
||||||
|
free(s);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool traceback_print_exc(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(0);
|
||||||
|
py_printexc();
|
||||||
|
py_newnone(py_retval());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pk__add_module_traceback() {
|
||||||
|
py_Ref mod = py_newmodule("traceback");
|
||||||
|
|
||||||
|
py_bindfunc(mod, "format_exc", traceback_format_exc);
|
||||||
|
py_bindfunc(mod, "print_exc", traceback_print_exc);
|
||||||
|
}
|
@ -67,6 +67,15 @@ int py_currentvm() {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void py_sys_setargv(int argc, char** argv) {
|
||||||
|
py_GlobalRef sys = py_getmodule("sys");
|
||||||
|
py_Ref argv_list = py_getdict(sys, py_name("argv"));
|
||||||
|
py_list_clear(argv_list);
|
||||||
|
for(int i = 0; i < argc; i++) {
|
||||||
|
py_newstr(py_list_emplace(argv_list), argv[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const char* pk_opname(Opcode op) {
|
const char* pk_opname(Opcode op) {
|
||||||
const static char* OP_NAMES[] = {
|
const static char* OP_NAMES[] = {
|
||||||
#define OPCODE(name) #name,
|
#define OPCODE(name) #name,
|
||||||
|
@ -14,34 +14,34 @@ void py_newlist(py_Ref out) {
|
|||||||
|
|
||||||
void py_newlistn(py_Ref out, int n) {
|
void py_newlistn(py_Ref out, int n) {
|
||||||
py_newlist(out);
|
py_newlist(out);
|
||||||
List* userdata = py_touserdata(out);
|
List* ud = py_touserdata(out);
|
||||||
c11_vector__reserve(userdata, n);
|
c11_vector__reserve(ud, n);
|
||||||
userdata->count = n;
|
ud->count = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_list_data(py_Ref self) {
|
py_Ref py_list_data(py_Ref self) {
|
||||||
List* userdata = py_touserdata(self);
|
List* ud = py_touserdata(self);
|
||||||
return userdata->data;
|
return ud->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref py_list_getitem(py_Ref self, int i) {
|
py_Ref py_list_getitem(py_Ref self, int i) {
|
||||||
List* userdata = py_touserdata(self);
|
List* ud = py_touserdata(self);
|
||||||
return c11__at(py_TValue, userdata, i);
|
return c11__at(py_TValue, ud, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list_setitem(py_Ref self, int i, py_Ref val) {
|
void py_list_setitem(py_Ref self, int i, py_Ref val) {
|
||||||
List* userdata = py_touserdata(self);
|
List* ud = py_touserdata(self);
|
||||||
c11__setitem(py_TValue, userdata, i, *val);
|
c11__setitem(py_TValue, ud, i, *val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list_delitem(py_Ref self, int i) {
|
void py_list_delitem(py_Ref self, int i) {
|
||||||
List* userdata = py_touserdata(self);
|
List* ud = py_touserdata(self);
|
||||||
c11_vector__erase(py_TValue, userdata, i);
|
c11_vector__erase(py_TValue, ud, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int py_list_len(py_Ref self) {
|
int py_list_len(py_Ref self) {
|
||||||
List* userdata = py_touserdata(self);
|
List* ud = py_touserdata(self);
|
||||||
return userdata->count;
|
return ud->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list_swap(py_Ref self, int i, int j) {
|
void py_list_swap(py_Ref self, int i, int j) {
|
||||||
@ -52,18 +52,24 @@ void py_list_swap(py_Ref self, int i, int j) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void py_list_append(py_Ref self, py_Ref val) {
|
void py_list_append(py_Ref self, py_Ref val) {
|
||||||
List* userdata = py_touserdata(self);
|
List* ud = py_touserdata(self);
|
||||||
c11_vector__push(py_TValue, userdata, *val);
|
c11_vector__push(py_TValue, ud, *val);
|
||||||
|
}
|
||||||
|
|
||||||
|
py_ItemRef py_list_emplace(py_Ref self) {
|
||||||
|
List* ud = py_touserdata(self);
|
||||||
|
c11_vector__emplace(ud);
|
||||||
|
return &c11_vector__back(py_TValue, ud);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list_clear(py_Ref self) {
|
void py_list_clear(py_Ref self) {
|
||||||
List* userdata = py_touserdata(self);
|
List* ud = py_touserdata(self);
|
||||||
c11_vector__clear(userdata);
|
c11_vector__clear(ud);
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_list_insert(py_Ref self, int i, py_Ref val) {
|
void py_list_insert(py_Ref self, int i, py_Ref val) {
|
||||||
List* userdata = py_touserdata(self);
|
List* ud = py_touserdata(self);
|
||||||
c11_vector__insert(py_TValue, userdata, i, *val);
|
c11_vector__insert(py_TValue, ud, i, *val);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
|
@ -38,6 +38,7 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
py_initialize();
|
py_initialize();
|
||||||
|
py_sys_setargv(argc, argv);
|
||||||
|
|
||||||
if(argc == 1) {
|
if(argc == 1) {
|
||||||
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
exit()
|
||||||
|
|
||||||
from linalg import mat3x3, vec2, vec3
|
from linalg import mat3x3, vec2, vec3
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
|
4
tests/80_sys.py
Normal file
4
tests/80_sys.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
assert len(sys.argv) == 2
|
||||||
|
assert (sys.argv[1] == 'tests/80_sys.py'), sys.argv
|
@ -7,7 +7,7 @@ except KeyError:
|
|||||||
actual = traceback.format_exc()
|
actual = traceback.format_exc()
|
||||||
|
|
||||||
expected = '''Traceback (most recent call last):
|
expected = '''Traceback (most recent call last):
|
||||||
File "80_traceback.py", line 5
|
File "tests/80_traceback.py", line 5
|
||||||
b = a[6]
|
b = a[6]
|
||||||
KeyError: 6'''
|
KeyError: 6'''
|
||||||
|
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
import sys
|
|
||||||
|
|
||||||
assert len(sys.argv) == 2
|
|
||||||
assert sys.argv[1] == 'tests/87_sys.py'
|
|
Loading…
x
Reference in New Issue
Block a user