Skip to content

Commit 46b27b3

Browse files
authored
Merge pull request #297 from NoamK-CR/fix/slice-element-is-pyobject
Fix - slice element is pyobject
2 parents 1018dfd + 51cf93e commit 46b27b3

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

_examples/slices/slices.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package slices
66

7-
import "fmt"
7+
import (
8+
"fmt"
9+
"math/cmplx"
10+
)
811

912
func IntSum(s []int) int {
1013
sum := 0
@@ -28,6 +31,8 @@ type SliceInt16 []int16
2831
type SliceInt32 []int32
2932
type SliceInt64 []int64
3033

34+
type SliceComplex []complex128
35+
3136
type SliceIface []interface{}
3237

3338
type S struct {
@@ -47,3 +52,11 @@ func PrintSSlice(ss []*S) {
4752
func PrintS(s *S) {
4853
fmt.Printf("%v\n", s.Name)
4954
}
55+
56+
func CmplxSqrt(arr SliceComplex) SliceComplex {
57+
res := make([]complex128, len(arr))
58+
for i, el := range arr {
59+
res[i] = cmplx.Sqrt(el)
60+
}
61+
return res
62+
}

_examples/slices/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# license that can be found in the LICENSE file.
44

55
from __future__ import print_function
6+
import math
7+
import random
68
import slices, go
79

810
a = [1,2,3,4]
@@ -35,4 +37,11 @@
3537
slices.PrintS(ss[0])
3638
slices.PrintS(ss[1])
3739

40+
cmplx = slices.SliceComplex([(random.random() + random.random() * 1j) for _ in range(16)])
41+
sqrts = slices.CmplxSqrt(cmplx)
42+
for root, orig in zip(sqrts, cmplx):
43+
root_squared = root * root
44+
assert math.isclose(root_squared.real, orig.real)
45+
assert math.isclose(root_squared.imag, orig.imag)
46+
3847
print("OK")

bind/gen_slice.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,13 @@ otherwise parameter is a python list that we copy from
313313
g.gofile.Outdent()
314314
g.gofile.Printf("}\n\n")
315315

316-
g.pybuild.Printf("mod.add_function('%s_elem', retval('%s'), [param('%s', 'handle'), param('int', 'idx')])\n", slNm, esym.cpyname, PyHandle)
316+
var caller_owns_ret string
317+
var transfer_ownership string
318+
if esym.cpyname == "PyObject*" {
319+
caller_owns_ret = ", caller_owns_return=True"
320+
transfer_ownership = ", transfer_ownership=False"
321+
}
322+
g.pybuild.Printf("mod.add_function('%s_elem', retval('%s'%s), [param('%s', 'handle'), param('int', 'idx')])\n", slNm, esym.cpyname, caller_owns_ret, PyHandle)
317323

318324
if slc.isSlice() {
319325
g.gofile.Printf("//export %s_subslice\n", slNm)
@@ -340,7 +346,7 @@ otherwise parameter is a python list that we copy from
340346
g.gofile.Outdent()
341347
g.gofile.Printf("}\n\n")
342348

343-
g.pybuild.Printf("mod.add_function('%s_set', None, [param('%s', 'handle'), param('int', 'idx'), param('%v', 'value')])\n", slNm, PyHandle, esym.cpyname)
349+
g.pybuild.Printf("mod.add_function('%s_set', None, [param('%s', 'handle'), param('int', 'idx'), param('%v', 'value'%s)])\n", slNm, PyHandle, esym.cpyname, transfer_ownership)
344350

345351
if slc.isSlice() {
346352
g.gofile.Printf("//export %s_append\n", slNm)
@@ -355,7 +361,7 @@ otherwise parameter is a python list that we copy from
355361
g.gofile.Outdent()
356362
g.gofile.Printf("}\n\n")
357363

358-
g.pybuild.Printf("mod.add_function('%s_append', None, [param('%s', 'handle'), param('%s', 'value')])\n", slNm, PyHandle, esym.cpyname)
364+
g.pybuild.Printf("mod.add_function('%s_append', None, [param('%s', 'handle'), param('%s', 'value'%s)])\n", slNm, PyHandle, esym.cpyname, transfer_ownership)
359365
}
360366
}
361367
}

0 commit comments

Comments
 (0)