@@ -53,23 +53,30 @@ in which case a new Go object is constructed first
53
53
g .pywrap .Printf ("if len(kwargs) == 1 and 'handle' in kwargs:\n " )
54
54
g .pywrap .Indent ()
55
55
g .pywrap .Printf ("self.handle = kwargs['handle']\n " )
56
+ g .pywrap .Printf ("_%s.IncRef(self.handle)\n " , g .outname )
56
57
g .pywrap .Outdent ()
57
58
g .pywrap .Printf ("elif len(args) == 1 and isinstance(args[0], go.GoClass):\n " )
58
59
g .pywrap .Indent ()
59
60
g .pywrap .Printf ("self.handle = args[0].handle\n " )
61
+ g .pywrap .Printf ("_%s.IncRef(self.handle)\n " , g .outname )
60
62
g .pywrap .Outdent ()
61
63
g .pywrap .Printf ("else:\n " )
62
64
g .pywrap .Indent ()
63
65
g .pywrap .Printf ("self.handle = _%s.%s_CTor()\n " , pkgname , s .ID ())
66
+ g .pywrap .Printf ("_%s.IncRef(self.handle)\n " , g .outname )
64
67
65
68
for i := 0 ; i < numFields ; i ++ {
66
69
f := s .Struct ().Field (i )
67
70
if _ , err := isPyCompatField (f ); err != nil {
68
71
continue
69
72
}
70
- // TODO: this will accept int args for any handles / object fields
71
- // need some kind of additional type-checking logic to prevent that in way
72
- // that also allows valid handles to be used..
73
+ // NOTE: this will accept int args for any handles / object fields so
74
+ // some kind of additional type-checking logic to prevent that in a way
75
+ // that also allows valid handles to be used as required. This is
76
+ // achieved in the per-field setters (see below) with checks to ensure
77
+ // that a struct field that is a gopy managed object is only
78
+ // assigned gopy managed objects. Fields of basic types (e.g int, string)
79
+ // etc can be assigned to directly.
73
80
g .pywrap .Printf ("if %[1]d < len(args):\n " , i )
74
81
g .pywrap .Indent ()
75
82
g .pywrap .Printf ("self.%s = args[%d]\n " , f .Name (), i )
@@ -82,6 +89,11 @@ in which case a new Go object is constructed first
82
89
g .pywrap .Outdent ()
83
90
g .pywrap .Outdent ()
84
91
92
+ g .pywrap .Printf ("def __del__(self):\n " )
93
+ g .pywrap .Indent ()
94
+ g .pywrap .Printf ("_%s.DecRef(self.handle)\n " , s .Package ().Name ())
95
+ g .pywrap .Outdent ()
96
+
85
97
if s .prots & ProtoStringer != 0 {
86
98
for _ , m := range s .meths {
87
99
if ! isStringer (m .obj ) {
@@ -223,7 +235,14 @@ func (g *pyGen) genStructMemberSetter(s *Struct, i int, f types.Object) {
223
235
g .pywrap .Outdent ()
224
236
g .pywrap .Printf ("else:\n " )
225
237
g .pywrap .Indent ()
226
- g .pywrap .Printf ("_%s.%s(self.handle, value)\n " , pkgname , cgoFn )
238
+ // See comment in genStructInit about ensuring that gopy managed
239
+ // objects are only assigned to from gopy managed objects.
240
+ switch f .Type ().(type ) {
241
+ case * types.Basic :
242
+ g .pywrap .Printf ("_%s.%s(self.handle, value)\n " , pkgname , cgoFn )
243
+ default :
244
+ g .pywrap .Printf ("raise TypeError(\" supplied argument type {t} is not a go.GoClass\" .format(t=type(value)))\n " )
245
+ }
227
246
g .pywrap .Outdent ()
228
247
g .pywrap .Outdent ()
229
248
@@ -279,10 +298,12 @@ handle=A Go-side object is always initialized with an explicit handle=arg
279
298
g .pywrap .Printf ("if len(kwargs) == 1 and 'handle' in kwargs:\n " )
280
299
g .pywrap .Indent ()
281
300
g .pywrap .Printf ("self.handle = kwargs['handle']\n " )
301
+ g .pywrap .Printf ("_%s.IncRef(self.handle)\n " , g .outname )
282
302
g .pywrap .Outdent ()
283
303
g .pywrap .Printf ("elif len(args) == 1 and isinstance(args[0], go.GoClass):\n " )
284
304
g .pywrap .Indent ()
285
305
g .pywrap .Printf ("self.handle = args[0].handle\n " )
306
+ g .pywrap .Printf ("_%s.IncRef(self.handle)\n " , g .outname )
286
307
g .pywrap .Outdent ()
287
308
g .pywrap .Printf ("else:\n " )
288
309
g .pywrap .Indent ()
0 commit comments