Skip to content

Commit f802d38

Browse files
author
Randall C. O'Reilly
committed
support subslicing of slices that returns the new Go slice -- was just returning a Python list from elements -- Go-based code then broke with that.
1 parent 7c8a930 commit f802d38

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

bind/gen_slice.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ func (g *pyGen) genSliceInit(slc *symbol, extTypes, pyWrapOnly bool, slob *Slice
7575
gocl = ""
7676
}
7777

78+
pysnm := slc.id
79+
if !strings.Contains(pysnm, "Slice_") {
80+
pysnm = strings.TrimPrefix(pysnm, pkgname+"_")
81+
}
82+
7883
if !extTypes || pyWrapOnly {
7984
g.pywrap.Printf("def __init__(self, *args, **kwargs):\n")
8085
g.pywrap.Indent()
@@ -154,6 +159,22 @@ otherwise parameter is a python list that we copy from
154159
g.pywrap.Indent()
155160
g.pywrap.Printf("if isinstance(key, slice):\n")
156161
g.pywrap.Indent()
162+
if slc.isSlice() {
163+
g.pywrap.Printf("if key.step == None or key.step == 1:\n")
164+
g.pywrap.Indent()
165+
g.pywrap.Printf("st = key.start\n")
166+
g.pywrap.Printf("ed = key.stop\n")
167+
g.pywrap.Printf("if st == None:\n")
168+
g.pywrap.Indent()
169+
g.pywrap.Printf("st = 0\n")
170+
g.pywrap.Outdent()
171+
g.pywrap.Printf("if ed == None:\n")
172+
g.pywrap.Indent()
173+
g.pywrap.Printf("ed = _%s_len(self.handle)\n", qNm)
174+
g.pywrap.Outdent()
175+
g.pywrap.Printf("return %s(handle=_%s_subslice(self.handle, st, ed))\n", pysnm, qNm)
176+
g.pywrap.Outdent()
177+
}
157178
g.pywrap.Printf("return [self[ii] for ii in range(*key.indices(len(self)))]\n")
158179
g.pywrap.Outdent()
159180
g.pywrap.Printf("elif isinstance(key, int):\n")
@@ -294,6 +315,19 @@ otherwise parameter is a python list that we copy from
294315

295316
g.pybuild.Printf("mod.add_function('%s_elem', retval('%s'), [param('%s', 'handle'), param('int', 'idx')])\n", slNm, esym.cpyname, PyHandle)
296317

318+
if slc.isSlice() {
319+
g.gofile.Printf("//export %s_subslice\n", slNm)
320+
g.gofile.Printf("func %s_subslice(handle CGoHandle, _st, _ed int) CGoHandle {\n", slNm)
321+
g.gofile.Indent()
322+
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
323+
g.gofile.Printf("ss := s[_st:_ed]\n")
324+
g.gofile.Printf("return CGoHandle(handleFromPtr_%s(&ss))\n", slNm)
325+
g.gofile.Outdent()
326+
g.gofile.Printf("}\n\n")
327+
328+
g.pybuild.Printf("mod.add_function('%s_subslice', retval('%s'), [param('%s', 'handle'), param('int', 'st'), param('int', 'ed')])\n", slNm, PyHandle, PyHandle)
329+
}
330+
297331
g.gofile.Printf("//export %s_set\n", slNm)
298332
g.gofile.Printf("func %s_set(handle CGoHandle, _idx int, _vl %s) {\n", slNm, esym.cgoname)
299333
g.gofile.Indent()

0 commit comments

Comments
 (0)