-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python error handling #215
Merged
+174
−60
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e4648ec
Initial commit
308ad80
Make __str__ work
7992947
Suggestions!
c17692c
Update test cases for Kotlin and Swift
76d04c9
try!
4a9d401
Add error template
0fd87eb
Add specific import for ArithmeticError
d54a3ee
Default return for __raise__
b8d0d69
Fix error message
ec4fd6d
Stateless error helper
2452de8
Remove functools import
cdb194a
Merge remote-tracking branch 'upstream/main' into py-err-handling
553324b
if func.arguments()
8f28c5d
Suggestions!
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
|
||
enum Overflow { | ||
"WRAPPING", | ||
"SATURATING", | ||
[Error] | ||
enum ArithmeticError { | ||
"IntegerOverflow", | ||
}; | ||
|
||
namespace arithmetic { | ||
u64 add(u64 a, u64 b, optional Overflow overflow = "WRAPPING"); | ||
u64 sub(u64 a, u64 b, optional Overflow overflow = "WRAPPING"); | ||
[Throws=ArithmeticError] | ||
u64 add(u64 a, u64 b); | ||
|
||
[Throws=ArithmeticError] | ||
u64 sub(u64 a, u64 b); | ||
|
||
boolean equal(u64 a, u64 b); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,20 @@ | ||
import uniffi.arithmetic.*; | ||
|
||
assert(add(2, 3, Overflow.SATURATING) == 5L) | ||
assert(add(2, 4) == 6L) | ||
assert(add(4, 8) == 12L) | ||
|
||
try { | ||
sub(0, 2) | ||
throw RuntimeException("Should have thrown a IntegerOverflow exception!") | ||
} catch (e: ArithmeticErrorException) { | ||
// It's okay! | ||
} | ||
|
||
assert(sub(4, 2) == 2L) | ||
assert(sub(8, 4) == 4L) | ||
|
||
assert(equal(2, 2)) | ||
assert(equal(4, 4)) | ||
assert(!equal(4, 5)) | ||
|
||
assert(!equal(2, 4)) | ||
assert(!equal(4, 8)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,27 @@ | ||
from arithmetic import * | ||
|
||
assert add(2, 3, Overflow.SATURATING) == 5 | ||
try: | ||
add(18446744073709551615, 1) | ||
assert(not("Should have thrown a IntegerOverflow exception!")) | ||
except ArithmeticError.IntegerOverflow: | ||
# It's okay! | ||
pass | ||
|
||
assert add(2, 4) == 6 | ||
assert add(4, 8) == 12 | ||
|
||
try: | ||
sub(0, 1) | ||
assert(not("Should have thrown a IntegerOverflow exception!")) | ||
except ArithmeticError.IntegerOverflow: | ||
# It's okay! | ||
pass | ||
|
||
assert sub(4, 2) == 2 | ||
assert sub(8, 4) == 4 | ||
|
||
assert equal(2, 2) | ||
assert equal(4, 4) | ||
assert not equal(4,5) | ||
|
||
assert not equal(2, 4) | ||
assert not equal(4, 8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,27 @@ | ||
import arithmetic | ||
|
||
assert(add(a: 2, b: 3, overflow: .saturating) == 5, "addition works") | ||
do { | ||
let _ = try add(a: 18446744073709551615, b: 1) | ||
fatalError("Should have thrown a IntegerOverflow exception!") | ||
} catch ArithmeticError.IntegerOverflow { | ||
// It's okay! | ||
} | ||
|
||
assert(equal(a: 4, b: 4), "equality works") | ||
assert(!equal(a: 4, b: 5), "non-equality works") | ||
assert(try! add(a: 2, b: 4) == 6, "add work") | ||
assert(try! add(a: 4, b: 8) == 12, "add work") | ||
|
||
do { | ||
let _ = try sub(a: 0, b: 1) | ||
fatalError("Should have thrown a IntegerOverflow exception!") | ||
} catch ArithmeticError.IntegerOverflow { | ||
// It's okay! | ||
} | ||
|
||
assert(try! sub(a: 4, b: 2) == 2, "sub work") | ||
assert(try! sub(a: 8, b: 4) == 4, "sub work") | ||
|
||
assert(equal(a: 2, b: 2), "equal works") | ||
assert(equal(a: 4, b: 4), "equal works") | ||
|
||
assert(!equal(a: 2, b: 4), "non-equal works") | ||
assert(!equal(a: 4, b: 8), "non-equal works") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
uniffi_bindgen/src/bindings/python/templates/ErrorTemplate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class RustError(ctypes.Structure): | ||
_fields_ = [ | ||
("code", ctypes.c_int32), | ||
("message", ctypes.c_void_p), | ||
] | ||
|
||
def free(self): | ||
return _UniFFILib.{{ ci.ffi_string_free().name() }}(self.message) | ||
|
||
def __str__(self): | ||
return "RustError(code={}, message={})".format( | ||
self.code, | ||
str(ctypes.cast(self.message, ctypes.c_char_p).value, "utf-8"), | ||
) | ||
|
||
{% for e in ci.iter_error_definitions() %} | ||
class {{ e.name()|class_name_py }}: | ||
{%- for value in e.values() %} | ||
class {{ value|class_name_py }}(Exception): | ||
pass | ||
{%- endfor %} | ||
weslenng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@staticmethod | ||
def raise_err(code, message): | ||
{%- for value in e.values() %} | ||
if code == {{ loop.index }}: | ||
raise {{ e.name()|class_name_py }}.{{ value|class_name_py }}(message) | ||
{% endfor %} | ||
raise Exception("Unknown error code") | ||
{% endfor %} | ||
|
||
def rust_call_with_error(error_class, fn, *args): | ||
error = RustError() | ||
weslenng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
error.code = 0 | ||
|
||
args_with_error = args + (ctypes.byref(error),) | ||
result = fn(*args_with_error) | ||
if error.code != 0: | ||
message = str(error) | ||
error.free() | ||
|
||
error_class.raise_err(error.code, message) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
uniffi_bindgen/src/bindings/python/templates/TopLevelFunctionTemplate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
{%- match func.return_type() -%} | ||
{%- when Some with (return_type) %} | ||
|
||
def {{ func.name()|fn_name_py }}({%- call py::arg_list_decl(func.arguments()) -%}): | ||
{%- call py::coerce_args(func.arguments()) %} | ||
def {{ func.name()|fn_name_py }}({%- call py::arg_list_decl(func) -%}): | ||
{%- call py::coerce_args(func) %} | ||
_retval = {% call py::to_ffi_call(func) %} | ||
return {{ "_retval"|lift_py(return_type) }} | ||
|
||
{% when None -%} | ||
|
||
def {{ func.name()|fn_name_py }}({%- call py::arg_list_decl(func.arguments()) -%}): | ||
{%- call py::coerce_args(func.arguments()) %} | ||
def {{ func.name()|fn_name_py }}({%- call py::arg_list_decl(func) -%}): | ||
{%- call py::coerce_args(func) %} | ||
{% call py::to_ffi_call(func) %} | ||
{% endmatch %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
uniffi_bindgen/src/bindings/swift/templates/ErrorTemplate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In swift and python we explicitly test that this was the
IntegerOverflow
variant, but here we test only that it was anArithmeticError
. Does it work if we catchArithmeticErrorException.IntegerOverflow
here like the other languages do?(I note that the todolist example has a similar problem, but we don't need to fix that here)