tree-sitter / py-tree-sitter

Python bindings to the Tree-sitter parsing library
https://tree-sitter.github.io/py-tree-sitter/
MIT License
817 stars 96 forks source link

Python nil pointer crash while iterating over query captures #219

Closed asutoshpalai closed 4 months ago

asutoshpalai commented 5 months ago

I am seeing SIGSEGV crash while running the following code, which runs a query over the code and tries to print the captures.

import tree_sitter
from tree_sitter_languages import get_language

code = """
;; Define a structure
(defstruct person
  name
  age)
"""

chunk_query = """
    [
        (list_lit (defun)) @defun
        (list_lit (sym_lit) @struct.keyword
            (#eq? @struct.keyword "defstruct")) @defstruct
        (list_lit (sym_lit) @class.keyword
            (#eq? @class.keyword "defclass")) @defclass
    ]
"""

language = get_language("commonlisp")

parser = tree_sitter.Parser()
parser.set_language(language)
tree = parser.parse(bytes(code, encoding="UTF-8"))

query = language.query(chunk_query)
for node, name in query.captures(tree.root_node):
    print(name)
    print(node.text)
$ python test.py
defstruct
b'(defstruct person\n name\n  age)'
struct.keyword
Segmentation fault (core dumped)