Explorar el Código

adjusted exceptions to be back to how they were before, implementation
is brittle but functional for current use cases

Kolten Pearson hace 2 años
padre
commit
8abaebbf9f
Se han modificado 3 ficheros con 61 adiciones y 4 borrados
  1. 8 3
      c_bindings/pocketpy_c.cpp
  2. 40 0
      c_bindings/test.c
  3. 13 1
      c_bindings/test_answers.txt

+ 8 - 3
c_bindings/pocketpy_c.cpp

@@ -13,9 +13,14 @@ using namespace pkpy;
         << "this probably means pocketpy itself has a bug!\n" \
         << e.what() << "\n"; \
         exit(2); \
+    } catch(std::runtime_error& e) { \
+        std::cerr << "ERROR: a std::runtime_error " \
+        << "this probably means pocketpy itself has a bug!\n" \
+        << e.what() << "\n"; \
+        exit(2); \
     } catch(...) { \
-        std::cerr << "ERROR: a unknown exception was thrown " \
-        << "this probably means pocketpy itself has a bug!\n"; \
+        std::cerr << "ERROR: a unknown exception was thrown from " << __func__ \
+        << "\nthis probably means pocketpy itself has a bug!\n"; \
         exit(2); \
     }
 
@@ -302,7 +307,7 @@ bool pkpy_get_global(pkpy_vm* vm_handle, const char* name) {
     if (o == nullptr) {
         o = vm->builtins->attr().try_get(name);
         if (o == nullptr)
-            vm->NameError("could not find requested global");
+            throw Exception("NameError", "could not find requested global");
     }
 
     vm->c_data->push(o);

+ 40 - 0
c_bindings/test.c

@@ -74,6 +74,12 @@ int test_error_propagate(pkpy_vm* vm) {
     return 1;
 }
 
+int test_nested_error(pkpy_vm* vm) {
+    pkpy_get_global(vm, "error_from_python");
+    pkpy_call(vm, 0);
+    return 0;
+}
+
 
 pkpy_vm* vm;
 
@@ -280,6 +286,7 @@ int main(int argc, char** argv) {
     //check(pkpy_vm_run(vm, "test_multiple_return()"));
     //check(pkpy_stack_size(vm) == 2);
 
+
     check(pkpy_push_function(vm, test_error_propagate));
     check(pkpy_set_global(vm, "test_error_propagate"));
     error(pkpy_vm_run(vm, "test_error_propagate()"));
@@ -288,13 +295,46 @@ int main(int argc, char** argv) {
     check(pkpy_call(vm, 0));
     check(pkpy_stack_size(vm) == 2);
 
+    
     check(pkpy_pop(vm, 2));
     check(pkpy_stack_size(vm) == 0);
 
     check(pkpy_check_global(vm, "test_error_propagate"));
     fail(pkpy_check_global(vm, "nonexistant"));
+
+    error(pkpy_vm_run(vm, "raise NameError('testing error throwing from python')"));
+
     pkpy_vm_run(vm, "test_error_propagate()");
     check(pkpy_check_error(vm));
+    fprintf(stderr, "testing code going to standard error, can ignore next error\n");
+    pkpy_clear_error(vm, NULL);
+
+    //with the current way execptions are handled, this will fail and pass the
+    //error clean through, ignoring the python handling
+    //
+    //maybe worth fixing someday, but for now it is functionating as implemented
+    error(pkpy_vm_run(vm, "try : test_error_propagate(); except NameError : pass"));
+
+
+    //more complicated error handling
+    //
+    //at the moment this is disabled, as this use case is not supported
+    //it will cause the program to abort 
+    //
+    //a python exception thrown from python can not pass through a c binding
+    //
+    //this means for now the api can only be used to make shallow bindings, or
+    //else care must be taken to not let an exception go through a c binding by
+    //catching it in python first 
+    //
+    //at such a time this interferes with a real world use case of the bindings
+    //we can revisit it
+    //
+    //check(pkpy_vm_run(vm, "def error_from_python() : raise NotImplementedError()"));
+    //check(pkpy_push_function(vm, test_nested_error));
+    //check(pkpy_set_global(vm, "test_nested_error"));
+    //fail(pkpy_vm_run(vm, "test_nested_error()"));
+
 
     return 0;
 }

+ 13 - 1
c_bindings/test_answers.txt

@@ -1,6 +1,7 @@
 hello world!
 successfully errored with this message: 
-NameError: name 'could not find requested global' is not defined
+Traceback (most recent call last):
+NameError: could not find requested global
 
 testing int methods
 11
@@ -49,3 +50,14 @@ TypeError: expected 2 positional arguments, but got 0 (x)
 
 testing pushing functions
 12
+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):
+  File "<c-bound>", line 1
+    raise NameError('testing error throwing from python')
+NameError: testing error throwing from python
+successfully errored with this message: 
+Traceback (most recent call last):
+NameError: could not find requested global