diff --git a/c_bindings/pocketpy_c.cpp b/c_bindings/pocketpy_c.cpp index 7bbad43d..91daea48 100644 --- a/c_bindings/pocketpy_c.cpp +++ b/c_bindings/pocketpy_c.cpp @@ -550,3 +550,12 @@ bool pkpy_push(pkpy_vm* vm_handle, int index) { vm->c_data->push(vm->c_data->begin()[index]); return true; } + + +bool pkpy_error(pkpy_vm* vm_handle, const char* message) { + CVM* vm = (CVM*) vm_handle; + ERRHANDLER_OPEN + throw Exception("CBindingError", message); + ERRHANDLER_CLOSE +} + diff --git a/c_bindings/pocketpy_c.h b/c_bindings/pocketpy_c.h index 143f5019..96b969c6 100644 --- a/c_bindings/pocketpy_c.h +++ b/c_bindings/pocketpy_c.h @@ -22,6 +22,13 @@ typedef struct pkpy_vm_handle pkpy_vm; bool pkpy_clear_error(pkpy_vm*, char** message); //NOTE you are responsible for freeing message +//this will cause the vm to enter an error state and report the given message +//when queried +//note that at the moment this is more like a panic than throwing an error +//the user will not be able to catch it with python code +bool pkpy_error(pkpy_vm*, const char* message); + + pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os); bool pkpy_vm_run(pkpy_vm*, const char* source); void pkpy_vm_destroy(pkpy_vm*); diff --git a/c_bindings/test.c b/c_bindings/test.c index d95a1d44..2fdac240 100644 --- a/c_bindings/test.c +++ b/c_bindings/test.c @@ -317,6 +317,8 @@ int main(int argc, char** argv) { //maybe worth fixing someday, but for now it is functionating as implemented error(pkpy_vm_run(vm, "try : test_error_propagate(); except NameError : pass")); + error(pkpy_error(vm, "test direct error mechanism")); + //more complicated error handling // diff --git a/c_bindings/test_answers.txt b/c_bindings/test_answers.txt index 253c664e..94b5762e 100644 --- a/c_bindings/test_answers.txt +++ b/c_bindings/test_answers.txt @@ -61,3 +61,6 @@ NameError: testing error throwing from python successfully errored with this message: Traceback (most recent call last): NameError: could not find requested global +successfully errored with this message: +Traceback (most recent call last): +CBindingError: test direct error mechanism diff --git a/run_c_binding_test.sh b/run_c_binding_test.sh index 172e1660..116d4c65 100644 --- a/run_c_binding_test.sh +++ b/run_c_binding_test.sh @@ -19,8 +19,8 @@ echo "checking results (they should be identical)" diff -q -s binding_test_scratch c_bindings/test_answers.txt echo "cleaning up" -rm pocketpy_c.o +#rm pocketpy_c.o rm test.o rm binding_test_scratch -rm c_binding_test +#rm c_binding_test