8000 tests/modify-values: first import · ActNotSign/go-python@990e749 · GitHub
[go: up one dir, main page]

Skip to content

Commit 990e749

Browse files
committed
tests/modify-values: first import
1 parent 4941be0 commit 990e749

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

python_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,18 @@ func TestErrFetch(t *testing.T) {
115115
want: []byte("exc=&{<nil>}\nval=&{<nil>}\ntb=&{<nil>}\n"),
116116
})
117117
}
118+
119+
func TestModifyValues(t *testing.T) {
120+
t.Parallel()
121+
testPkg(t, pkg{
122+
path: "tests/modify-values",
123+
want: []byte(`values.__name__: "values"
124+
values.sval: "42"
125+
values.ival: 666
126+
sval='42'
127+
ival=666
128+
sval='42 is the answer'
129+
ival=1666
130+
`),
131+
})
132+
}

tests/modify-values/main.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/sbinet/go-python"
8+
)
9+
10+
func init() {
11+
err := python.Initialize()
12+
if err != nil {
13+
log.Panic(err.Error())
14+
}
15+
}
16+
17+
func main() {
18+
module := python.PyImport_ImportModule("values")
19+
if module == nil {
20+
log.Fatal("could not import 'values'")
21+
}
22+
23+
name := module.GetAttrString("__name__")
24+
if name == nil {
25+
log.Fatal("could not getattr '__name__'")
26+
}
27+
defer name.DecRef()
28+
fmt.Printf("values.__name__: %q\n", python.PyString_AsString(name))
29+
30+
sval := module.GetAttrString("sval")
31+
if sval == nil {
32+
log.Fatal("could not getattr 'sval'")
33+
}
34+
defer sval.DecRef()
35+
fmt.Printf("values.sval: %q\n", python.PyString_AsString(sval))
36+
37+
pyival := module.GetAttrString("ival")
38+
if pyival == nil {
39+
log.Fatal("could not getattr 'ival'")
40+
}
41+
defer pyival.DecRef()
42+
43+
ival := python.PyInt_AsLong(pyival)
44+
fmt.Printf("values.ival: %d\n", ival)
45+
46+
myfunc := module.GetAttrString("myfunc")
47+
if myfunc == nil {
48+
log.Fatal("could not getattr 'myfunc'")
49+
}
50+
defer myfunc.DecRef()
51+
52+
o1 := myfunc.CallFunction()
53+
if o1 == nil {
54+
log.Fatal("could not call 'values.myfunc()'")
55+
}
56+
fmt.Printf("%s\n", python.PyString_AsString(o1))
57+
o1.DecRef()
58+
59+
// modify 'test.ival' and 'test.sval'
60+
{
61+
pyival := python.PyInt_FromLong(ival + 1000)
62+
module.SetAttrString("ival", pyival)
63+
pyival.DecRef()
64+
65+
pysval := python.PyString_FromString(python.PyString_AsString(sval) + " is the answer")
66+
module.SetAttrString("sval", pysval)
67+
pysval.DecRef()
68+
}
69+
70+
o2 := myfunc.CallFunction()
71+
if o2 == nil {
72+
log.Fatal("could not call 'values.myfunc()'")
73+
}
74+
fmt.Printf("%s\n", python.PyString_AsString(o2))
75+
o2.DecRef()
76+
}

tests/modify-values/values.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python2
2+
3+
sval = "42"
4+
ival = 666
5+
6+
def myfunc():
7+
return "sval='%s'\nival=%d" %(sval,ival)
8+

0 commit comments

Comments
 (0)
0