mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
Merge pull request #72 from koltenpearson/c_binding_api
adjusted exceptions to be back to using Exception in c bindings
This commit is contained in:
commit
387425230f
@ -13,9 +13,14 @@ using namespace pkpy;
|
|||||||
<< "this probably means pocketpy itself has a bug!\n" \
|
<< "this probably means pocketpy itself has a bug!\n" \
|
||||||
<< e.what() << "\n"; \
|
<< e.what() << "\n"; \
|
||||||
exit(2); \
|
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(...) { \
|
} catch(...) { \
|
||||||
std::cerr << "ERROR: a unknown exception was thrown " \
|
std::cerr << "ERROR: a unknown exception was thrown from " << __func__ \
|
||||||
<< "this probably means pocketpy itself has a bug!\n"; \
|
<< "\nthis probably means pocketpy itself has a bug!\n"; \
|
||||||
exit(2); \
|
exit(2); \
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,7 +307,7 @@ bool pkpy_get_global(pkpy_vm* vm_handle, const char* name) {
|
|||||||
if (o == nullptr) {
|
if (o == nullptr) {
|
||||||
o = vm->builtins->attr().try_get(name);
|
o = vm->builtins->attr().try_get(name);
|
||||||
if (o == nullptr)
|
if (o == nullptr)
|
||||||
vm->NameError("could not find requested global");
|
throw Exception("NameError", "could not find requested global");
|
||||||
}
|
}
|
||||||
|
|
||||||
vm->c_data->push(o);
|
vm->c_data->push(o);
|
||||||
|
@ -74,6 +74,12 @@ int test_error_propagate(pkpy_vm* vm) {
|
|||||||
return 1;
|
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;
|
pkpy_vm* vm;
|
||||||
|
|
||||||
@ -280,6 +286,7 @@ int main(int argc, char** argv) {
|
|||||||
//check(pkpy_vm_run(vm, "test_multiple_return()"));
|
//check(pkpy_vm_run(vm, "test_multiple_return()"));
|
||||||
//check(pkpy_stack_size(vm) == 2);
|
//check(pkpy_stack_size(vm) == 2);
|
||||||
|
|
||||||
|
|
||||||
check(pkpy_push_function(vm, test_error_propagate));
|
check(pkpy_push_function(vm, test_error_propagate));
|
||||||
check(pkpy_set_global(vm, "test_error_propagate"));
|
check(pkpy_set_global(vm, "test_error_propagate"));
|
||||||
error(pkpy_vm_run(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_call(vm, 0));
|
||||||
check(pkpy_stack_size(vm) == 2);
|
check(pkpy_stack_size(vm) == 2);
|
||||||
|
|
||||||
|
|
||||||
check(pkpy_pop(vm, 2));
|
check(pkpy_pop(vm, 2));
|
||||||
check(pkpy_stack_size(vm) == 0);
|
check(pkpy_stack_size(vm) == 0);
|
||||||
|
|
||||||
check(pkpy_check_global(vm, "test_error_propagate"));
|
check(pkpy_check_global(vm, "test_error_propagate"));
|
||||||
fail(pkpy_check_global(vm, "nonexistant"));
|
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()");
|
pkpy_vm_run(vm, "test_error_propagate()");
|
||||||
check(pkpy_check_error(vm));
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
hello world!
|
hello world!
|
||||||
successfully errored with this message:
|
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
|
testing int methods
|
||||||
11
|
11
|
||||||
@ -49,3 +50,14 @@ TypeError: expected 2 positional arguments, but got 0 (x)
|
|||||||
|
|
||||||
testing pushing functions
|
testing pushing functions
|
||||||
12
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user