MCPcopy Index your code
hub / github.com/go-python/gopy / genMapInit

Method genMapInit

bind/gen_map.go:63–391  ·  view source on GitHub ↗
(slc *symbol, extTypes, pyWrapOnly bool, mpob *Map)

Source from the content-addressed store, hash-verified

61}
62
63func (g *pyGen) genMapInit(slc *symbol, extTypes, pyWrapOnly bool, mpob *Map) {
64 pkgname := g.cfg.Name
65 slNm := slc.id
66 qNm := pkgname + "." + slNm
67 typ := slc.GoType().Underlying().(*types.Map)
68 esym := current.symtype(typ.Elem())
69 ksym := current.symtype(typ.Key())
70
71 // key slice type and name
72 keyslt := types.NewSlice(typ.Key())
73 keyslsym := current.symtype(keyslt)
74 if keyslsym == nil {
75 fmt.Printf("nil key slice type!: %s map: %s\n", current.fullTypeString(keyslt), slc.goname)
76 return
77 }
78 keyslnm := ""
79 if g.pkg == nil {
80 keyslnm = keyslsym.id
81 } else {
82 keyslnm = keyslsym.pyPkgId(g.pkg.pkg)
83 }
84
85 gocl := "go."
86 if g.pkg == goPackage {
87 gocl = ""
88 }
89
90 if !extTypes || pyWrapOnly {
91 g.pywrap.Printf("def __init__(self, *args, **kwargs):\n")
92 g.pywrap.Indent()
93 g.pywrap.Printf(`"""
94handle=A Go-side object is always initialized with an explicit handle=arg
95otherwise parameter is a python list that we copy from
96"""
97`)
98 g.pywrap.Printf("self.index = 0\n")
99 g.pywrap.Printf("if len(kwargs) == 1 and 'handle' in kwargs:\n")
100 g.pywrap.Indent()
101 g.pywrap.Printf("self.handle = kwargs['handle']\n")
102 g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
103 g.pywrap.Outdent()
104 g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], %sGoClass):\n", gocl)
105 g.pywrap.Indent()
106 g.pywrap.Printf("self.handle = args[0].handle\n")
107 g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
108 g.pywrap.Outdent()
109 g.pywrap.Printf("else:\n")
110 g.pywrap.Indent()
111 g.pywrap.Printf("self.handle = _%s_CTor()\n", qNm)
112 g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
113 g.pywrap.Printf("if len(args) > 0:\n")
114 g.pywrap.Indent()
115 g.pywrap.Printf("if not isinstance(args[0], _collections_abc.Mapping):\n")
116 g.pywrap.Indent()
117 g.pywrap.Printf("raise TypeError('%s.__init__ takes a mapping as argument')\n", slNm)
118 g.pywrap.Outdent()
119 g.pywrap.Printf("for k, v in args[0].items():\n")
120 g.pywrap.Indent()

Callers 1

genMapMethod · 0.95

Calls 11

genStringerCallMethod · 0.95
isStringerFunction · 0.85
symtypeMethod · 0.80
PrintfMethod · 0.80
fullTypeStringMethod · 0.80
pyPkgIdMethod · 0.80
IndentMethod · 0.80
OutdentMethod · 0.80
PrintlnMethod · 0.80
hasHandleMethod · 0.80
GoTypeMethod · 0.65

Tested by

no test coverage detected