From f16e21bc0e4e36aff8ad31677d6700ed90a1c7ec Mon Sep 17 00:00:00 2001 From: Evan Oman Date: Mon, 22 Apr 2024 21:39:40 -0500 Subject: [PATCH] Adds missing pointer reference for slices along with test --- _examples/slices/slices.go | 13 +++++++++++++ _examples/slices/test.py | 7 +++++++ bind/gen_slice.go | 9 ++++++--- main_test.go | 1 + 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/_examples/slices/slices.go b/_examples/slices/slices.go index 6c6fb1c..4bd2d08 100644 --- a/_examples/slices/slices.go +++ b/_examples/slices/slices.go @@ -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 +} \ No newline at end of file diff --git a/_examples/slices/test.py b/_examples/slices/test.py index 6dc3f22..143f453 100644 --- a/_examples/slices/test.py +++ b/_examples/slices/test.py @@ -44,4 +44,11 @@ for root, orig in zip(sqrts, cmplx): 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") diff --git a/bind/gen_slice.go b/bind/gen_slice.go index fa0ab54..c0ef885 100644 --- a/bind/gen_slice.go +++ b/bind/gen_slice.go @@ -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") } diff --git a/main_test.go b/main_test.go index 5592072..e7d4292 100644 --- a/main_test.go +++ b/main_test.go @@ -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 `), })