Skip to content

Commit 7c8a930

Browse files
author
Randall C. O'Reilly
committed
support pass-through of nil value for slices and maps -- otherwise causes immediate error in generated boilerplate for nil values passed for these. also fix gopy: comment parsing.
1 parent 1ae2f24 commit 7c8a930

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

bind/gen_func.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (g *pyGen) genMethod(s *symbol, o *Func) {
221221

222222
func isIfaceHandle(gdoc string) (bool, string) {
223223
const PythonIface = "\ngopy:interface=handle"
224-
if idx := strings.Index(gdoc, PythonIface); idx > 0 {
224+
if idx := strings.Index(gdoc, PythonIface); idx >= 0 {
225225
gdoc = gdoc[:idx] + gdoc[idx+len(PythonIface)+1:]
226226
return true, gdoc
227227
}

bind/gen_map.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ otherwise parameter is a python list that we copy from
281281
g.gofile.Printf("//export %s_len\n", slNm)
282282
g.gofile.Printf("func %s_len(handle CGoHandle) int {\n", slNm)
283283
g.gofile.Indent()
284-
g.gofile.Printf("return len(*ptrFromHandle_%s(handle))\n", slNm)
284+
g.gofile.Printf("return len(deptrFromHandle_%s(handle))\n", slNm)
285285
g.gofile.Outdent()
286286
g.gofile.Printf("}\n\n")
287287

@@ -291,7 +291,7 @@ otherwise parameter is a python list that we copy from
291291
g.gofile.Printf("//export %s_elem\n", slNm)
292292
g.gofile.Printf("func %s_elem(handle CGoHandle, _ky %s) %s {\n", slNm, ksym.cgoname, esym.cgoname)
293293
g.gofile.Indent()
294-
g.gofile.Printf("s := *ptrFromHandle_%s(handle)\n", slNm)
294+
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
295295
if ksym.py2go != "" {
296296
g.gofile.Printf("v, ok := s[%s(_ky)%s]\n", ksym.py2go, ksym.py2goParenEx)
297297
} else {
@@ -316,7 +316,7 @@ otherwise parameter is a python list that we copy from
316316
g.gofile.Printf("//export %s_contains\n", slNm)
317317
g.gofile.Printf("func %s_contains(handle CGoHandle, _ky %s) C.char {\n", slNm, ksym.cgoname)
318318
g.gofile.Indent()
319-
g.gofile.Printf("s := *ptrFromHandle_%s(handle)\n", slNm)
319+
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
320320
if ksym.py2go != "" {
321321
g.gofile.Printf("_, ok := s[%s(_ky)%s]\n", ksym.py2go, ksym.py2goParenEx)
322322
} else {
@@ -332,7 +332,7 @@ otherwise parameter is a python list that we copy from
332332
g.gofile.Printf("//export %s_set\n", slNm)
333333
g.gofile.Printf("func %s_set(handle CGoHandle, _ky %s, _vl %s) {\n", slNm, ksym.cgoname, esym.cgoname)
334334
g.gofile.Indent()
335-
g.gofile.Printf("s := *ptrFromHandle_%s(handle)\n", slNm)
335+
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
336336
if ksym.py2go != "" {
337337
g.gofile.Printf("s[%s(_ky)%s] = ", ksym.py2go, ksym.py2goParenEx)
338338
} else {
@@ -352,7 +352,7 @@ otherwise parameter is a python list that we copy from
352352
g.gofile.Printf("//export %s_delete\n", slNm)
353353
g.gofile.Printf("func %s_delete(handle CGoHandle, _ky %s) {\n", slNm, ksym.cgoname)
354354
g.gofile.Indent()
355-
g.gofile.Printf("s := *ptrFromHandle_%s(handle)\n", slNm)
355+
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
356356
if ksym.py2go != "" {
357357
g.gofile.Printf("delete(s, %s(_ky)%s)\n", ksym.py2go, ksym.py2goParenEx)
358358
} else {
@@ -367,7 +367,7 @@ otherwise parameter is a python list that we copy from
367367
g.gofile.Printf("//export %s_keys\n", slNm)
368368
g.gofile.Printf("func %s_keys(handle CGoHandle) CGoHandle {\n", slNm)
369369
g.gofile.Indent()
370-
g.gofile.Printf("s := *ptrFromHandle_%s(handle)\n", slNm)
370+
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
371371
g.gofile.Printf("kys := make(%s, 0, len(s))\n", keyslsym.goname)
372372
g.gofile.Printf("for k := range(s) {\n")
373373
g.gofile.Indent()

bind/gen_slice.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ otherwise parameter is a python list that we copy from
270270
g.gofile.Printf("//export %s_len\n", slNm)
271271
g.gofile.Printf("func %s_len(handle CGoHandle) int {\n", slNm)
272272
g.gofile.Indent()
273-
g.gofile.Printf("return len(*ptrFromHandle_%s(handle))\n", slNm)
273+
g.gofile.Printf("return len(deptrFromHandle_%s(handle))\n", slNm)
274274
g.gofile.Outdent()
275275
g.gofile.Printf("}\n\n")
276276

@@ -279,7 +279,7 @@ otherwise parameter is a python list that we copy from
279279
g.gofile.Printf("//export %s_elem\n", slNm)
280280
g.gofile.Printf("func %s_elem(handle CGoHandle, _idx int) %s {\n", slNm, esym.cgoname)
281281
g.gofile.Indent()
282-
g.gofile.Printf("s := *ptrFromHandle_%s(handle)\n", slNm)
282+
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
283283
if esym.go2py != "" {
284284
if !esym.isPointer() && esym.isStruct() {
285285
g.gofile.Printf("return %s(&(s[_idx]))%s\n", esym.go2py, esym.go2pyParenEx)
@@ -297,7 +297,7 @@ otherwise parameter is a python list that we copy from
297297
g.gofile.Printf("//export %s_set\n", slNm)
298298
g.gofile.Printf("func %s_set(handle CGoHandle, _idx int, _vl %s) {\n", slNm, esym.cgoname)
299299
g.gofile.Indent()
300-
g.gofile.Printf("s := *ptrFromHandle_%s(handle)\n", slNm)
300+
g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm)
301301
if esym.py2go != "" {
302302
g.gofile.Printf("s[_idx] = %s(_vl)%s\n", esym.py2go, esym.py2goParenEx)
303303
} else {

bind/gen_type.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ func (g *pyGen) genType(sym *symbol, extTypes, pyWrapOnly bool) {
2525
}
2626

2727
if !pyWrapOnly {
28-
if sym.isPointer() || sym.isInterface() {
28+
switch {
29+
case sym.isPointer() || sym.isInterface():
2930
g.genTypeHandlePtr(sym)
30-
} else {
31+
case sym.isSlice() || sym.isMap() || sym.isArray():
32+
g.genTypeHandleImplPtr(sym)
33+
default:
3134
g.genTypeHandle(sym)
3235
}
3336
}
@@ -84,6 +87,48 @@ func (g *pyGen) genTypeHandlePtr(sym *symbol) {
8487
g.gofile.Printf("}\n")
8588
}
8689

90+
// implicit pointer types: slice, map, array
91+
func (g *pyGen) genTypeHandleImplPtr(sym *symbol) {
92+
gonm := sym.gofmt()
93+
ptrnm := gonm
94+
nptrnm := gonm
95+
if ptrnm[0] != '*' {
96+
ptrnm = "*" + ptrnm
97+
} else {
98+
nptrnm = gonm[1:]
99+
}
100+
g.gofile.Printf("\n// Converters for implicit pointer handles for type: %s\n", gonm)
101+
g.gofile.Printf("func ptrFromHandle_%s(h CGoHandle) %s {\n", sym.id, ptrnm)
102+
g.gofile.Indent()
103+
g.gofile.Printf("p := gopyh.VarFromHandle((gopyh.CGoHandle)(h), %[1]q)\n", gonm)
104+
g.gofile.Printf("if p == nil {\n")
105+
g.gofile.Indent()
106+
g.gofile.Printf("return nil\n")
107+
g.gofile.Outdent()
108+
g.gofile.Printf("}\n")
109+
g.gofile.Printf("return p.(%s)\n", ptrnm)
110+
g.gofile.Outdent()
111+
g.gofile.Printf("}\n")
112+
g.gofile.Printf("func deptrFromHandle_%s(h CGoHandle) %s {\n", sym.id, nptrnm)
113+
g.gofile.Indent()
114+
g.gofile.Printf("p := ptrFromHandle_%s(h)\n", sym.id)
115+
if !sym.isArray() {
116+
g.gofile.Printf("if p == nil {\n")
117+
g.gofile.Indent()
118+
g.gofile.Printf("return nil\n")
119+
g.gofile.Outdent()
120+
g.gofile.Printf("}\n")
121+
}
122+
g.gofile.Printf("return *p\n")
123+
g.gofile.Outdent()
124+
g.gofile.Printf("}\n")
125+
g.gofile.Printf("func %s(p interface{})%s CGoHandle {\n", sym.go2py, sym.go2pyParenEx)
126+
g.gofile.Indent()
127+
g.gofile.Printf("return CGoHandle(gopyh.Register(\"%s\", p))\n", gonm)
128+
g.gofile.Outdent()
129+
g.gofile.Printf("}\n")
130+
}
131+
87132
func nonPtrName(nm string) string {
88133
if nm[0] == '*' {
89134
return nm[1:]

bind/symbols.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ func (sym *symtab) addArrayType(pkg *types.Package, obj types.Object, t types.Ty
907907
cpyname: PyHandle,
908908
pysig: "[]" + elsym.pysig,
909909
go2py: "handleFromPtr_" + id,
910-
py2go: "*ptrFromHandle_" + id,
910+
py2go: "deptrFromHandle_" + id,
911911
zval: "nil",
912912
}
913913
return nil
@@ -943,7 +943,7 @@ func (sym *symtab) addMapType(pkg *types.Package, obj types.Object, t types.Type
943943
cpyname: PyHandle,
944944
pysig: "object",
945945
go2py: "handleFromPtr_" + id,
946-
py2go: "*ptrFromHandle_" + id,
946+
py2go: "deptrFromHandle_" + id,
947947
zval: "nil",
948948
}
949949
return nil
@@ -972,7 +972,7 @@ func (sym *symtab) addSliceType(pkg *types.Package, obj types.Object, t types.Ty
972972
cpyname: PyHandle,
973973
pysig: "[]" + elsym.pysig,
974974
go2py: "handleFromPtr_" + id,
975-
py2go: "*ptrFromHandle_" + id,
975+
py2go: "deptrFromHandle_" + id,
976976
zval: "nil",
977977
}
978978
return nil

0 commit comments

Comments
 (0)