MCPcopy Create free account
hub / github.com/go-python/gopy / genSliceInit

Method genSliceInit

bind/gen_slice.go:62–424  ·  view source on GitHub ↗
(slc *symbol, extTypes, pyWrapOnly bool, slob *Slice)

Source from the content-addressed store, hash-verified

60}
61
62func (g *pyGen) genSliceInit(slc *symbol, extTypes, pyWrapOnly bool, slob *Slice) {
63 pkgname := slc.gopkg.Name()
64 slNm := slc.id
65 qNm := g.cfg.Name + "." + slNm // this is only for referring to the _ .go package!
66 var esym *symbol
67 if typ, ok := slc.GoType().Underlying().(*types.Slice); ok {
68 esym = current.symtype(typ.Elem())
69 } else if typ, ok := slc.GoType().Underlying().(*types.Array); ok {
70 esym = current.symtype(typ.Elem())
71 }
72
73 gocl := "go."
74 if g.pkg == goPackage {
75 gocl = ""
76 }
77
78 pysnm := slc.id
79 if !strings.Contains(pysnm, "Slice_") {
80 pysnm = strings.TrimPrefix(pysnm, pkgname+"_")
81 }
82
83 if !extTypes || pyWrapOnly {
84 g.pywrap.Printf("def __init__(self, *args, **kwargs):\n")
85 g.pywrap.Indent()
86 g.pywrap.Printf(`"""
87handle=A Go-side object is always initialized with an explicit handle=arg
88otherwise parameter is a python list that we copy from
89"""
90`)
91 g.pywrap.Printf("self.index = 0\n")
92 g.pywrap.Printf("if len(kwargs) == 1 and 'handle' in kwargs:\n")
93 g.pywrap.Indent()
94 g.pywrap.Printf("self.handle = kwargs['handle']\n")
95 g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
96 g.pywrap.Outdent()
97 g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], %sGoClass):\n", gocl)
98 g.pywrap.Indent()
99 g.pywrap.Printf("self.handle = args[0].handle\n")
100 g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
101 g.pywrap.Outdent()
102 if slc.isSlice() {
103 g.pywrap.Printf("else:\n")
104 g.pywrap.Indent()
105 g.pywrap.Printf("self.handle = _%s_CTor()\n", qNm)
106 g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
107 g.pywrap.Printf("if len(args) > 0:\n")
108 g.pywrap.Indent()
109 g.pywrap.Printf("if not isinstance(args[0], _collections_abc.Iterable):\n")
110 g.pywrap.Indent()
111 g.pywrap.Printf("raise TypeError('%s.__init__ takes a sequence as argument')\n", slNm)
112 g.pywrap.Outdent()
113 g.pywrap.Printf("for elt in args[0]:\n")
114 g.pywrap.Indent()
115 g.pywrap.Printf("self.append(elt)\n")
116 g.pywrap.Outdent()
117 g.pywrap.Outdent()
118 g.pywrap.Outdent()
119 }

Callers 1

genSliceMethod · 0.95

Calls 12

genStringerCallMethod · 0.95
hasHandleMethod · 0.95
pyPkgIdMethod · 0.95
isStringerFunction · 0.85
symtypeMethod · 0.80
PrintfMethod · 0.80
IndentMethod · 0.80
OutdentMethod · 0.80
isSliceMethod · 0.80
PrintlnMethod · 0.80
GoTypeMethod · 0.65
NameMethod · 0.45

Tested by

no test coverage detected