parent
481101bc53
commit
4d06a3718a
50
guessit.go
50
guessit.go
|
@ -50,14 +50,9 @@ func init() {
|
|||
}
|
||||
|
||||
func New() (*Guessit, error) {
|
||||
oImport := 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)
|
||||
module := python3.PyImport_ImportModule("guessit") //ret val: new ref
|
||||
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()
|
||||
|
||||
|
@ -66,12 +61,13 @@ func New() (*Guessit, error) {
|
|||
return nil, fmt.Errorf("could not get dict for module")
|
||||
}
|
||||
|
||||
guessitfn := python3.PyDict_GetItemString(dict, "guessit") //retval: Borrowed
|
||||
if !(guessitfn != nil && python3.PyCallable_Check(guessitfn)) {
|
||||
fn := python3.PyDict_GetItemString(dict, "guessit") //retval: Borrowed
|
||||
if !(fn != nil && python3.PyCallable_Check(fn)) {
|
||||
return nil, fmt.Errorf("could not find function 'guessit'")
|
||||
}
|
||||
|
||||
return &Guessit{
|
||||
fn: guessitfn,
|
||||
fn: fn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -87,44 +83,27 @@ func (g Guessit) Guessit(s string, options ...string) (out Match, err error) {
|
|||
opts := python3.PyUnicode_FromString(strings.Join(options[:], " "))
|
||||
defer opts.DecRef()
|
||||
|
||||
args := python3.PyTuple_New(2) //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
|
||||
testdataPy := g.fn.CallFunctionObjArgs(item, opts) //retval: New reference
|
||||
if !(testdataPy != nil && python3.PyErr_Occurred() == nil) {
|
||||
python3.PyErr_Print()
|
||||
return
|
||||
}
|
||||
|
||||
defer testdataPy.DecRef()
|
||||
|
||||
size := python3.PyDict_Size(testdataPy)
|
||||
keys := python3.PyDict_Keys(testdataPy)
|
||||
defer keys.DecRef()
|
||||
|
||||
vals := python3.PyDict_Values(testdataPy)
|
||||
defer vals.DecRef()
|
||||
|
||||
testdataPy = nil
|
||||
|
||||
tmpmap := make(map[string]interface{})
|
||||
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)
|
||||
switch {
|
||||
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++ {
|
||||
item := python3.PyList_GetItem(val, i)
|
||||
v := python3.PyUnicode_AsUTF8(item)
|
||||
item.DecRef()
|
||||
tmp = append(tmp, v)
|
||||
}
|
||||
tmpmap[key] = tmp
|
||||
|
|
Loading…
Reference in New Issue