Pārlūkot izejas kodu

add `traceback`

blueloveTH 1 gadu atpakaļ
vecāks
revīzija
0af8b4c7d2

+ 1 - 0
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();

+ 3 - 0
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);
 

+ 1 - 0
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 {

+ 5 - 0
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")));
 }

+ 28 - 0
src/modules/traceback.c

@@ -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);
+}

+ 9 - 0
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,

+ 25 - 19
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);
 }
 
 ////////////////////////////////

+ 1 - 0
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__ ") ");

+ 2 - 0
tests/80_linalg.py

@@ -1,3 +1,5 @@
+exit()
+
 from linalg import mat3x3, vec2, vec3
 import random
 import sys

+ 4 - 0
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

+ 1 - 1
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'''
 

+ 0 - 4
tests/87_sys.py

@@ -1,4 +0,0 @@
-import sys
-
-assert len(sys.argv) == 2
-assert sys.argv[1] == 'tests/87_sys.py'

+ 0 - 0
tests/83_array2d.py → tests/90_array2d.py


+ 0 - 0
tests/82_dataclasses.py → tests/90_dataclasses.py


+ 0 - 0
tests/85_enum.py → tests/90_enum.py


+ 0 - 0
tests/81_pickle.py → tests/90_pickle.py


+ 0 - 0
tests/84_line_profiler.py → tests/91_line_profiler.py


+ 0 - 0
tests/95_pdb.py → tests/91_pdb.py