Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime Error when use shared lib in python #185

Closed
ananan opened this issue Jan 8, 2019 · 3 comments
Closed

Runtime Error when use shared lib in python #185

ananan opened this issue Jan 8, 2019 · 3 comments
Labels

Comments

@ananan
Copy link

ananan commented Jan 8, 2019

Hey Guys,

I got error when using enry as shared lib in python, it looks like below:

here is the way i built it:
image

here is the way i used it:
image

even tried in cffi:
image

can anyone tell me what't wrong?
thx...

@creachadair
Copy link
Contributor

It's a little difficult to tell exactly what's happening from the screencaps, but I would be very surprised for this to work without more explicit type annotations. The Go types of the arguments and results are generated as structures, and the FFI library is unlikely to map correctly from str and bytes values without help.

I was able to make the following work as of f28fc12.

Setup:

git clone [email protected]:src-d/enry.git
cd enry
go generate
go build -buildmode=c-shared -o enry.so shared/enry.go

Test:

import ctypes

lib = ctypes.CDLL('enry.so')

with file('common.go', 'rb') as fp:
    data = fp.read()

class GoString (ctypes.Structure):
    _fields_ = [
        ('p', ctypes.c_char_p),
        ('n', ctypes.c_long),
    ]

class GoSlice (ctypes.Structure):
    _fields_ = [
        ('data', ctypes.c_char_p),
        ('len', ctypes.c_int64),
        ('cap', ctypes.c_int64),
    ]

lib.GetLanguage.argtypes = (GoString, GoSlice)
lib.GetLanguage.restype = GoString

def GetLanguage(path, data):
    s = GoString(p=path, n=len(path))
    d = GoSlice(data=data, len=len(data), cap=len(data))
    res = lib.GetLanguage(s, d)
    if res.p is not None:
        return res.p[:res.n]

print GetLanguage('common.go', data)

I implemented the type wrappers by hand based on inspection of enry.h. I am not very familiar with the cffi package, but in theory it should be able to do something similar.

@bzz
Copy link
Contributor

bzz commented Jan 9, 2019

I think @creachadair is right.

And we have #154 open for a Adding CFFI-based python bindings for Enry, that I'm also hoping to find some time soon to grapple with.

@bzz bzz added the question label Jan 9, 2019
@bzz
Copy link
Contributor

bzz commented Jan 29, 2019

If no objections, going to close this as the question seems to be answered, further work on bindings is planned and there is no further discussion.

@bzz bzz closed this as completed Jan 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants