Skip to content

fix(node): fix text handling for bytes-like callbacks#465

Merged
ObserverOfTime merged 1 commit into
tree-sitter:masterfrom
zahid-arbisoft:bugfix/fix-node-text-memory-view
Jul 8, 2026
Merged

fix(node): fix text handling for bytes-like callbacks#465
ObserverOfTime merged 1 commit into
tree-sitter:masterfrom
zahid-arbisoft:bugfix/fix-node-text-memory-view

Conversation

@zahid-arbisoft

Copy link
Copy Markdown
Contributor

Summary

Fix a segfault in Node.text when the parse source comes from a callback returning a bytes-like object.

Related to #435.

What changed

  • Updated the callable-source branch in node_get_text to use the normalized bytearray buffer when computing length and reading bytes.
  • Added regression coverage for callback sources returning:
    • bytes
    • bytearray
    • memoryview

Why

The previous implementation accepted bytes-like callback results, but later treated the original callback return value as if it were always a bytes object. That could segfault when Node.text was accessed.

This change addresses a node_get_text crash in the same area discussed in #435.

Testing

  • python -m unittest discover -s tests -v

@ObserverOfTime ObserverOfTime changed the title Fix node text handling for bytes-like callbacks fix(node): fix text handling for bytes-like callbacks Jul 8, 2026
@ObserverOfTime ObserverOfTime merged commit 4fb9f66 into tree-sitter:master Jul 8, 2026
82 of 87 checks passed
@ObserverOfTime ObserverOfTime linked an issue Jul 8, 2026 that may be closed by this pull request
@ObserverOfTime

Copy link
Copy Markdown
Member

The added test case fails sometimes:

======================================================================
ERROR: test_text_with_callback_source (tests.test_node.TestNode.test_text_with_callback_source) (type='bytes')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/runner/work/py-tree-sitter/py-tree-sitter/tests/test_node.py", line 484, in test_text_with_callback_source
    self.assertEqual(tree.root_node.text, source_code)
                     ^^^^^^^^^^^^^^^^^^^
TypeError: cannot convert 'NoneType' object to bytearray

======================================================================
ERROR: test_text_with_callback_source (tests.test_node.TestNode.test_text_with_callback_source) (type='bytearray')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/runner/work/py-tree-sitter/py-tree-sitter/tests/test_node.py", line 484, in test_text_with_callback_source
    self.assertEqual(tree.root_node.text, source_code)
                     ^^^^^^^^^^^^^^^^^^^
TypeError: cannot convert 'NoneType' object to bytearray

======================================================================
ERROR: test_text_with_callback_source (tests.test_node.TestNode.test_text_with_callback_source) (type='memoryview')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/runner/work/py-tree-sitter/py-tree-sitter/tests/test_node.py", line 484, in test_text_with_callback_source
    self.assertEqual(tree.root_node.text, source_code)
                     ^^^^^^^^^^^^^^^^^^^
TypeError: cannot convert 'NoneType' object to bytearray

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fatal Python error: Segmentation fault

2 participants