Skip to content

Commit f16e21b

Browse files
committed
Adds missing pointer reference for slices along with test
1 parent cdad836 commit f16e21b

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

_examples/slices/slices.go

+13
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,16 @@ func CmplxSqrt(arr SliceComplex) SliceComplex {
6060
}
6161
return res
6262
}
63+
64+
func GetEmptyMatrix(xSize int, ySize int) [][]bool {
65+
result := [][]bool{}
66+
67+
for i := 0; i < xSize; i++ {
68+
result = append(result, []bool{})
69+
for j := 0; j < ySize; j++ {
70+
result[i] = append(result[i], false)
71+
}
72+
}
73+
74+
return result
75+
}

_examples/slices/test.py

+7
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@
4444
assert math.isclose(root_squared.real, orig.real)
4545
assert math.isclose(root_squared.imag, orig.imag)
4646

47+
48+
matrix = slices.GetEmptyMatrix(4,4)
49+
for i in range(4):
50+
for j in range(4):
51+
assert not matrix[i][j]
52+
print("[][]bool working as expected")
53+
4754
print("OK")

bind/gen_slice.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,14 @@ otherwise parameter is a python list that we copy from
325325
g.gofile.Indent()
326326
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
327327
if esym.go2py != "" {
328-
if !esym.isPointer() && esym.isStruct() {
329-
g.gofile.Printf("return %s(&(s[_idx]))%s\n", esym.go2py, esym.go2pyParenEx)
328+
// If the go2py starts with handleFromPtr_, use reference &, otherwise just return the value
329+
val_str := ""
330+
if strings.HasPrefix(esym.go2py, "handleFromPtr_") {
331+
val_str = "&(s[_idx])"
330332
} else {
331-
g.gofile.Printf("return %s(s[_idx])%s\n", esym.go2py, esym.go2pyParenEx)
333+
val_str = "s[_idx]"
332334
}
335+
g.gofile.Printf("return %s(%s)%s\n", esym.go2py, val_str, esym.go2pyParenEx)
333336
} else {
334337
g.gofile.Printf("return s[_idx]\n")
335338
}

main_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ struct slice: slices.Slice_Ptr_slices_S len: 3 handle: 11 [slices.S{Name=S0, ha
616616
struct slice[0]: slices.S{Name=S0, handle=15}
617617
struct slice[1]: slices.S{Name=S1, handle=16}
618618
struct slice[2].Name: S2
619+
[][]bool working as expected
619620
OK
620621
`),
621622
})

0 commit comments

Comments
 (0)