sbinet / go-python

naive go bindings to the CPython2 C-API
Other
1.52k stars 138 forks source link

Exception SystemError: '/builddir/build/BUILD/Python-2.7.5/Objects/classobject.c:521: bad argument to internal function' #117

Open anye88 opened 2 years ago

anye88 commented 2 years ago

Hi, I have a problem and I need help !!!

When used python.PyInstance_New to instance my python script Class, if Class inherit other Class, then have bad error

Exception SystemError: '/builddir/build/BUILD/Python-2.7.5/Objects/classobject.c:521: bad argument to internal function'

my golang programmer

package main

import (
    "fmt"
    "github.com/sbinet/go-python"
    "os"
)

var PyStr = python.PyString_FromString

func init() {
    os.Setenv("PYTHONPATH", ".")
    fmt.Println("PYTHONPATH:", os.Getenv("PYTHONPATH"))
    err := python.Initialize()
    if err != nil {
        panic(err.Error())
    }
}

func import_and_use_obj() {
    // Import class from module
    objsModule := python.PyImport_ImportModule("objs")
    if objsModule == nil {
        panic("Error importing module: objs")
    }
    fmt.Printf("[MODULE REF] repr(objsModule) = %s\n", python.PyString_AS_STRING(objsModule.Repr()))

    obj := objsModule.GetAttrString("obj")
    if obj == nil {
        panic("[CLASS REF] Error importing object: obj")
    }
    fmt.Printf("[CLASS REF] repr(obj) = %s\n", python.PyString_AS_STRING(obj.Repr()))

    // Instantiate obj
    newObj := python.PyInstance_New(obj, python.PyTuple_New(0), nil)
    if newObj == nil {
        panic("[INSTANCE REF] Error instantiating object: obj")
    }
    fmt.Printf("[INSTANCE REF] repr(newObj) = %s\n", python.PyString_AS_STRING(newObj.Repr()))
}

func main() {
    err := python.Initialize()
    if err != nil {
        panic(err.Error())
    }
    defer python.Finalize()
    import_and_use_obj()
    python.Finalize()
}

my python script

#!/usr/bin/env python2
import pickle
import json

class MyObj(object):
    def __init__(self):
        self.name = "ABC"

class obj(MyObj):
    def __init__(self): pass

    def obj_method(self):
        print("Python object method called!")
        return 1

    def obj_method_arg(self, arg1):
        print("Python object method called, with argument: %s" % arg1)
        return 1

    def obj_method_args(self, arg1, arg2):
        print("Python object method called with arguments: %s" % arg1, arg2)
        return 1

    def obj_method_kwargs(self, **kwargs):
        print("Python object method called with arguments:" )
        print(kwargs)
        return 1

Error

[root@localhost demo]# go run application/demo1/main.go 
PYTHONPATH: .
[MODULE REF] repr(objsModule) = <module 'objs' from '/root/demo/objs.py'>
[CLASS REF] repr(obj) = <class 'objs.obj'>
Exception SystemError: '/builddir/build/BUILD/Python-2.7.5/Objects/classobject.c:521: bad argument to internal function' in <module 'threading' from '/usr/lib64/python2.7/threading.pyc'> ignored
panic: [INSTANCE REF] Error instantiating object: obj

goroutine 1 [running]:
main.import_and_use_obj()
        /root/demo/application/demo1/main.go:37 +0x195
main.main()
        /root/demo/application/demo1/main.go:48 +0x49
exit status 2