8000 tests: add module import example · foundations/go-python@3c15159 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 3c15159

Browse files
committed
tests: add module import example
Add a simple example on how to import and use `cPickle` from `go-python`. Updates sbinet#29.
1 parent 7991d39 commit 
10000
3c15159

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

tests/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
56
"github.com/sbinet/go-python"
67
)
78

@@ -17,4 +18,20 @@ func main() {
1718
pystr := python.PyString_FromString(gostr)
1819
str := python.PyString_AsString(pystr)
1920
fmt.Println("hello [", str, "]")
21+
22+
pickle := python.PyImport_ImportModule("cPickle")
23+
if pickle == nil {
24+
panic("could not import 'cPickle'")
25+
}
26+
dumps := pickle.GetAttrString("dumps")
27+
if dumps == nil {
28+
panic("could not retrieve 'cPickle.dumps'")
29+
}
30+
out := dumps.CallFunctionObjArgs("O", pystr)
31+
if out == nil {
32+
panic("could not dump pystr")
33+
}
34+
fmt.Printf("cPickle.dumps(%s) = %q\n", gostr,
35+
python.PyString_AsString(out),
36+
)
2037
}

0 commit comments

Comments
 (0)
0