Skip to content

Commit ceeb930

Browse files
DeinAlptraumc-rhodes
authored andcommitted
[libclang/python] Return None instead of null cursors from Token.cursor (llvm#163183)
Since llvm#138103 , the `Cursor` class throws an error when any of its methods is called on a null cursor. Simultaneously, we adapted all methods to return `None` instead of a null cursor, so users should not encounter these. We have overlooked one way to end up with null cursors, namely the `Token.cursor` property, which may return null cursors under some circumstances. Fixes llvm#163180
1 parent 7e153f5 commit ceeb930

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3853,6 +3853,8 @@ def cursor(self):
38533853
cursor._tu = self._tu
38543854

38553855
conf.lib.clang_annotateTokens(self._tu, byref(self), 1, byref(cursor))
3856+
if cursor.is_null():
3857+
return None
38563858

38573859
return cursor
38583860

clang/bindings/python/tests/cindex/test_tokens.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ def test_token_extent(self):
5353

5454
self.assertEqual(extent.start.offset, 4)
5555
self.assertEqual(extent.end.offset, 7)
56+
57+
def test_null_cursor(self):
58+
"""Ensure that the cursor property converts null cursors to None"""
59+
tu = get_tu("int i = 5;")
60+
tokens = list(tu.get_tokens(extent=tu.cursor.extent))
61+
self.assertEqual(tokens[-1].cursor, None)

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Clang Frontend Potentially Breaking Changes
9494

9595
Clang Python Bindings Potentially Breaking Changes
9696
--------------------------------------------------
97+
- Return ``None`` instead of null cursors from ``Token.cursor``
9798
- ``Cursor.from_location`` now returns ``None`` instead of a null cursor.
9899
This eliminates the last known source of null cursors.
99100
- Almost all ``Cursor`` methods now assert that they are called on non-null cursors.

0 commit comments

Comments
 (0)