Created
October 28, 2018 03:17
-
-
Save graphaelli/4ccdf514624a4a0d173888e594fb4ed3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// +build linux,cgo darwin,cgo | |
package main | |
import "C" | |
import ( | |
"fmt" | |
"strconv" | |
) | |
func init() { | |
fmt.Println("loaded") | |
} | |
/* | |
fn maybe_panic(a: i32) -> String { | |
if a == 0 { | |
panic!("boom!") | |
} else { | |
a.to_string() | |
} | |
} | |
*/ | |
//export maybe_panic | |
func maybe_panic(a int64) *C.char { | |
if a == 0 { | |
panic("boom!") | |
} | |
s := strconv.Itoa(int(a)) | |
return C.CString(s) | |
} | |
func main() {} | |
// go build -buildmode=c-shared -o maybe_panic.so main.go |
This file contains hidden or 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
#!/usr/bin/env python | |
import ctypes | |
from ctypes import c_int, c_char_p | |
import sys | |
lib = ctypes.CDLL('maybe_panic.so') | |
maybe_panic = lib.maybe_panic | |
maybe_panic.argtypes = [c_int] | |
maybe_panic.restype = c_char_p | |
def main(): | |
for arg in sys.argv[1:]: | |
res = maybe_panic(int(arg)) | |
print(res) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment