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

Segmentation Error in Python #265

Closed
Goldziher opened this issue Jul 8, 2024 · 2 comments · Fixed by #319
Closed

Segmentation Error in Python #265

Goldziher opened this issue Jul 8, 2024 · 2 comments · Fixed by #319
Labels
bug Something isn't working

Comments

@Goldziher
Copy link

Goldziher commented Jul 8, 2024

Describe the bug
Hi, I try to split code in python and I get a segmentation fault. I tried several tree-sitter libraries, all return segmentation error. I identified the issue - it happens if language is not instantiated.

def test_code_splitter() -> None:
    from tree_sitter_cpp import language

    code_splitter = CodeSplitter(language, capacity=100)

    cpp_code = """
        #include <bits/stdc++.h>
        using namespace std;
         
        int main()
        {
            vector <int> arr1 = {1, 2, 3, 4};
            vector <int> arr2 = {};
            vector <float> arr3 = {1.2, 3.8, 3.0, 2.7, 6.6};
         
            cout << "Size of arr1: " << arr1.size() << endl;
            cout << "Size of arr2: " << arr2.size() << endl;
            cout << "Size of arr3: " << arr3.size() << endl;
         
            return 0;
        }
    """

    chunks = list(code_splitter.chunks(cpp_code))
    assert len(chunks) == 7

This fails - because language is not called -> language().

This works:

def test_code_splitter() -> None:
    from tree_sitter_cpp import language

    code_splitter = CodeSplitter(language(), capacity=100)

    cpp_code = """
        #include <bits/stdc++.h>
        using namespace std;
         
        int main()
        {
            vector <int> arr1 = {1, 2, 3, 4};
            vector <int> arr2 = {};
            vector <float> arr3 = {1.2, 3.8, 3.0, 2.7, 6.6};
         
            cout << "Size of arr1: " << arr1.size() << endl;
            cout << "Size of arr2: " << arr2.size() << endl;
            cout << "Size of arr3: " << arr3.size() << endl;
         
            return 0;
        }
    """

    chunks = list(code_splitter.chunks(cpp_code))
    assert len(chunks) == 7

If you are open to some adjustments of the python bindings, I can improve the typing and ensure this issue is handled by allowing a Language instance to be passed in.

@benbrandt
Copy link
Owner

Hi @Goldziher thanks for reporting. I'll give it a thought, I think I'd rather give a better error message because the language is supposed to be instantiated, but the error case is admittedly quite severe.

@benbrandt benbrandt linked a pull request Aug 11, 2024 that will close this issue
@benbrandt benbrandt added the bug Something isn't working label Aug 11, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in text-splitter Roadmap Aug 11, 2024
@benbrandt
Copy link
Owner

@Goldziher this should now return a more helpful error (and not crash python) if encountered. Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants