Improve python code, hopefully.

pull/1/head v0.0.3
Stein Ivar Berghei 2021-11-21 02:05:35 +01:00
parent 481101bc53
commit 4d06a3718a
1 changed files with 14 additions and 36 deletions

View File

@ -50,14 +50,9 @@ func init() {
} }
func New() (*Guessit, error) { func New() (*Guessit, error) {
oImport := python3.PyImport_ImportModule("guessit") //ret val: new ref module := python3.PyImport_ImportModule("guessit") //ret val: new ref
if !(oImport != nil && python3.PyErr_Occurred() == nil) {
return nil, fmt.Errorf("failed to import module 'guessit'")
}
module := python3.PyImport_AddModule("guessit") //ret val: borrowed ref (from oImport)
if !(module != nil && python3.PyErr_Occurred() == nil) { if !(module != nil && python3.PyErr_Occurred() == nil) {
return nil, fmt.Errorf("failed to add module 'guessit'") return nil, fmt.Errorf("failed to import module 'guessit'")
} }
defer module.DecRef() defer module.DecRef()
@ -66,12 +61,13 @@ func New() (*Guessit, error) {
return nil, fmt.Errorf("could not get dict for module") return nil, fmt.Errorf("could not get dict for module")
} }
guessitfn := python3.PyDict_GetItemString(dict, "guessit") //retval: Borrowed fn := python3.PyDict_GetItemString(dict, "guessit") //retval: Borrowed
if !(guessitfn != nil && python3.PyCallable_Check(guessitfn)) { if !(fn != nil && python3.PyCallable_Check(fn)) {
return nil, fmt.Errorf("could not find function 'guessit'") return nil, fmt.Errorf("could not find function 'guessit'")
} }
return &Guessit{ return &Guessit{
fn: guessitfn, fn: fn,
}, nil }, nil
} }
@ -87,44 +83,27 @@ func (g Guessit) Guessit(s string, options ...string) (out Match, err error) {
opts := python3.PyUnicode_FromString(strings.Join(options[:], " ")) opts := python3.PyUnicode_FromString(strings.Join(options[:], " "))
defer opts.DecRef() defer opts.DecRef()
args := python3.PyTuple_New(2) //retval: New reference testdataPy := g.fn.CallFunctionObjArgs(item, opts) //retval: New reference
if args == nil {
return
}
defer args.DecRef()
ret := python3.PyTuple_SetItem(args, 0, item) //steals ref to pylist
if ret != 0 {
if python3.PyErr_Occurred() != nil {
python3.PyErr_Print()
}
return
}
ret = python3.PyTuple_SetItem(args, 1, opts) //steals ref to pylist
if ret != 0 {
if python3.PyErr_Occurred() != nil {
python3.PyErr_Print()
}
return
}
testdataPy := g.fn.CallObject(args) //retval: New reference
if !(testdataPy != nil && python3.PyErr_Occurred() == nil) { if !(testdataPy != nil && python3.PyErr_Occurred() == nil) {
python3.PyErr_Print() python3.PyErr_Print()
return return
} }
defer testdataPy.DecRef()
size := python3.PyDict_Size(testdataPy) size := python3.PyDict_Size(testdataPy)
keys := python3.PyDict_Keys(testdataPy) keys := python3.PyDict_Keys(testdataPy)
defer keys.DecRef() defer keys.DecRef()
vals := python3.PyDict_Values(testdataPy) vals := python3.PyDict_Values(testdataPy)
defer vals.DecRef() defer vals.DecRef()
testdataPy = nil
tmpmap := make(map[string]interface{}) tmpmap := make(map[string]interface{})
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
key := python3.PyUnicode_AsUTF8(python3.PyList_GetItem(keys, i)) kitem := python3.PyList_GetItem(keys, i)
key := python3.PyUnicode_AsUTF8(kitem)
kitem = nil
val := python3.PyList_GetItem(vals, i) val := python3.PyList_GetItem(vals, i)
switch { switch {
case python3.PyLong_Check(val): case python3.PyLong_Check(val):
@ -136,7 +115,6 @@ func (g Guessit) Guessit(s string, options ...string) (out Match, err error) {
for i := 0; i < python3.PyList_Size(val); i++ { for i := 0; i < python3.PyList_Size(val); i++ {
item := python3.PyList_GetItem(val, i) item := python3.PyList_GetItem(val, i)
v := python3.PyUnicode_AsUTF8(item) v := python3.PyUnicode_AsUTF8(item)
item.DecRef()
tmp = append(tmp, v) tmp = append(tmp, v)
} }
tmpmap[key] = tmp tmpmap[key] = tmp