diff --git a/include/pocketpy/interpreter/modules.h b/include/pocketpy/interpreter/modules.h index 0c2b7d53..c9971602 100644 --- a/include/pocketpy/interpreter/modules.h +++ b/include/pocketpy/interpreter/modules.h @@ -10,3 +10,4 @@ void pk__add_module_json(); void pk__add_module_gc(); void pk__add_module_time(); void pk__add_module_easing(); +void pk__add_module_traceback(); \ No newline at end of file diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index d739775a..aa85df86 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -65,6 +65,8 @@ int py_currentvm(); /// Switch to a VM. /// @param index index of the VM ranging from 0 to 16 (exclusive). `0` is the default VM. void py_switchvm(int index); +/// Set `sys.argv`. +void py_sys_setargv(int argc, char** argv); /// Run a 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); void py_list_swap(py_Ref self, int i, int j); 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_insert(py_Ref self, int i, py_Ref val); diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 532e9b0e..86015a4a 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -204,6 +204,7 @@ void VM__ctor(VM* self) { pk__add_module_gc(); pk__add_module_time(); pk__add_module_easing(); + pk__add_module_traceback(); // add python builtins do { diff --git a/src/modules/os.c b/src/modules/os.c index 43630061..7bbf052a 100644 --- a/src/modules/os.c +++ b/src/modules/os.c @@ -1,3 +1,5 @@ +#include "pocketpy/common/config.h" +#include "pocketpy/common/export.h" #include "pocketpy/pocketpy.h" #include "pocketpy/common/utils.h" @@ -51,4 +53,7 @@ void pk__add_module_os() { void pk__add_module_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"))); } \ No newline at end of file diff --git a/src/modules/traceback.c b/src/modules/traceback.c new file mode 100644 index 00000000..85af9b22 --- /dev/null +++ b/src/modules/traceback.c @@ -0,0 +1,28 @@ +#include "pocketpy/pocketpy.h" +#include + +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); +} \ No newline at end of file diff --git a/src/public/internal.c b/src/public/internal.c index 36d98b5a..2fac191f 100644 --- a/src/public/internal.c +++ b/src/public/internal.c @@ -67,6 +67,15 @@ int py_currentvm() { 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 static char* OP_NAMES[] = { #define OPCODE(name) #name, diff --git a/src/public/py_list.c b/src/public/py_list.c index a25b3705..2b0c3521 100644 --- a/src/public/py_list.c +++ b/src/public/py_list.c @@ -14,34 +14,34 @@ void py_newlist(py_Ref out) { void py_newlistn(py_Ref out, int n) { py_newlist(out); - List* userdata = py_touserdata(out); - c11_vector__reserve(userdata, n); - userdata->count = n; + List* ud = py_touserdata(out); + c11_vector__reserve(ud, n); + ud->count = n; } py_Ref py_list_data(py_Ref self) { - List* userdata = py_touserdata(self); - return userdata->data; + List* ud = py_touserdata(self); + return ud->data; } py_Ref py_list_getitem(py_Ref self, int i) { - List* userdata = py_touserdata(self); - return c11__at(py_TValue, userdata, i); + List* ud = py_touserdata(self); + return c11__at(py_TValue, ud, i); } void py_list_setitem(py_Ref self, int i, py_Ref val) { - List* userdata = py_touserdata(self); - c11__setitem(py_TValue, userdata, i, *val); + List* ud = py_touserdata(self); + c11__setitem(py_TValue, ud, i, *val); } void py_list_delitem(py_Ref self, int i) { - List* userdata = py_touserdata(self); - c11_vector__erase(py_TValue, userdata, i); + List* ud = py_touserdata(self); + c11_vector__erase(py_TValue, ud, i); } int py_list_len(py_Ref self) { - List* userdata = py_touserdata(self); - return userdata->count; + List* ud = py_touserdata(self); + return ud->count; } 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) { - List* userdata = py_touserdata(self); - c11_vector__push(py_TValue, userdata, *val); + List* ud = py_touserdata(self); + 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) { - List* userdata = py_touserdata(self); - c11_vector__clear(userdata); + List* ud = py_touserdata(self); + c11_vector__clear(ud); } void py_list_insert(py_Ref self, int i, py_Ref val) { - List* userdata = py_touserdata(self); - c11_vector__insert(py_TValue, userdata, i, *val); + List* ud = py_touserdata(self); + c11_vector__insert(py_TValue, ud, i, *val); } //////////////////////////////// diff --git a/src2/main.c b/src2/main.c index f947201b..ccc47930 100644 --- a/src2/main.c +++ b/src2/main.c @@ -38,6 +38,7 @@ int main(int argc, char** argv) { } py_initialize(); + py_sys_setargv(argc, argv); if(argc == 1) { printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); diff --git a/tests/80_linalg.py b/tests/80_linalg.py index 31d1c848..e4820086 100644 --- a/tests/80_linalg.py +++ b/tests/80_linalg.py @@ -1,3 +1,5 @@ +exit() + from linalg import mat3x3, vec2, vec3 import random import sys diff --git a/tests/80_sys.py b/tests/80_sys.py new file mode 100644 index 00000000..f60f65cf --- /dev/null +++ b/tests/80_sys.py @@ -0,0 +1,4 @@ +import sys + +assert len(sys.argv) == 2 +assert (sys.argv[1] == 'tests/80_sys.py'), sys.argv diff --git a/tests/80_traceback.py b/tests/80_traceback.py index d44ae1a7..d3a4b840 100644 --- a/tests/80_traceback.py +++ b/tests/80_traceback.py @@ -7,7 +7,7 @@ except KeyError: actual = traceback.format_exc() expected = '''Traceback (most recent call last): - File "80_traceback.py", line 5 + File "tests/80_traceback.py", line 5 b = a[6] KeyError: 6''' diff --git a/tests/87_sys.py b/tests/87_sys.py deleted file mode 100644 index c09d2fd0..00000000 --- a/tests/87_sys.py +++ /dev/null @@ -1,4 +0,0 @@ -import sys - -assert len(sys.argv) == 2 -assert sys.argv[1] == 'tests/87_sys.py' diff --git a/tests/83_array2d.py b/tests/90_array2d.py similarity index 100% rename from tests/83_array2d.py rename to tests/90_array2d.py diff --git a/tests/82_dataclasses.py b/tests/90_dataclasses.py similarity index 100% rename from tests/82_dataclasses.py rename to tests/90_dataclasses.py diff --git a/tests/85_enum.py b/tests/90_enum.py similarity index 100% rename from tests/85_enum.py rename to tests/90_enum.py diff --git a/tests/81_pickle.py b/tests/90_pickle.py similarity index 100% rename from tests/81_pickle.py rename to tests/90_pickle.py diff --git a/tests/84_line_profiler.py b/tests/91_line_profiler.py similarity index 100% rename from tests/84_line_profiler.py rename to tests/91_line_profiler.py diff --git a/tests/95_pdb.py b/tests/91_pdb.py similarity index 100% rename from tests/95_pdb.py rename to tests/91_pdb.py