Skip to content

Commit

Permalink
Merge pull request #351 from EvanOman/346/fix-handleFromPtr-missing-r…
Browse files Browse the repository at this point in the history
…eference-slices

Adds missing pointer reference for slices along with test
  • Loading branch information
rcoreilly authored May 3, 2024
2 parents cdad836 + f16e21b commit 50c71dc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
13 changes: 13 additions & 0 deletions _examples/slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ func CmplxSqrt(arr SliceComplex) SliceComplex {
}
return res
}

func GetEmptyMatrix(xSize int, ySize int) [][]bool {
result := [][]bool{}

for i := 0; i < xSize; i++ {
result = append(result, []bool{})
for j := 0; j < ySize; j++ {
result[i] = append(result[i], false)
}
}

return result
}
7 changes: 7 additions & 0 deletions _examples/slices/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@
assert math.isclose(root_squared.real, orig.real)
assert math.isclose(root_squared.imag, orig.imag)


matrix = slices.GetEmptyMatrix(4,4)
for i in range(4):
for j in range(4):
assert not matrix[i][j]
print("[][]bool working as expected")

print("OK")
9 changes: 6 additions & 3 deletions bind/gen_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,14 @@ otherwise parameter is a python list that we copy from
g.gofile.Indent()
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
if esym.go2py != "" {
if !esym.isPointer() && esym.isStruct() {
g.gofile.Printf("return %s(&(s[_idx]))%s\n", esym.go2py, esym.go2pyParenEx)
// If the go2py starts with handleFromPtr_, use reference &, otherwise just return the value
val_str := ""
if strings.HasPrefix(esym.go2py, "handleFromPtr_") {
val_str = "&(s[_idx])"
} else {
g.gofile.Printf("return %s(s[_idx])%s\n", esym.go2py, esym.go2pyParenEx)
val_str = "s[_idx]"
}
g.gofile.Printf("return %s(%s)%s\n", esym.go2py, val_str, esym.go2pyParenEx)
} else {
g.gofile.Printf("return s[_idx]\n")
}
Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ struct slice: slices.Slice_Ptr_slices_S len: 3 handle: 11 [slices.S{Name=S0, ha
struct slice[0]: slices.S{Name=S0, handle=15}
struct slice[1]: slices.S{Name=S1, handle=16}
struct slice[2].Name: S2
[][]bool working as expected
OK
`),
})
Expand Down

0 comments on commit 50c71dc

Please sign in to comment.