Skip to content

After 0.22, how do I load a .so language? #251

Closed Answered by jdugan6240
realhackcraft asked this question in Q&A
Discussion options

You must be logged in to vote

Basically, you'd need to manually do what py-tree-sitter used to do itself - that is, grabbing a pointer to the tree_sitter_python function from the .so library file and passing that pointer to the Language constructor.

Something like the following should work:

from tree_sitter import Language, Parser
from ctypes import cdll, c_void_p
from os import fspath

def lang_from_so(path: str, name: str) -> Language:
    lib = cdll.LoadLibrary(fspath(path))
    language_function = getattr(lib, f"tree_sitter_{name}")
    language_function.restype = c_void_p
    language_ptr = language_function()
    return Language(language_ptr)

PY_LANG = lang_from_so(path_to_python_so, "python")
PY_PARSER = Parser(…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@Moosems
Comment options

@realhackcraft
Comment options

@jdugan6240
Comment options

Answer selected by realhackcraft
@Moosems
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants