Skip to content

Commit 074f93e

Browse files
DeinAlptraumgithub-actions[bot]
authored andcommitted
Automerge: [libclang/python] Return None instead of null cursors from Token.cursor (#163183)
Since llvm/llvm-project#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 #163180
2 parents 641cb34 + 38a5282 commit 074f93e

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
@@ -3939,6 +3939,8 @@ def cursor(self):
39393939
cursor._tu = self._tu
39403940

39413941
conf.lib.clang_annotateTokens(self._tu, byref(self), 1, byref(cursor))
3942+
if cursor.is_null():
3943+
return None
39423944

39433945
return cursor
39443946

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
@@ -136,6 +136,7 @@ Clang Frontend Potentially Breaking Changes
136136

137137
Clang Python Bindings Potentially Breaking Changes
138138
--------------------------------------------------
139+
- Return ``None`` instead of null cursors from ``Token.cursor``
139140
- TypeKind ``ELABORATED`` is not used anymore, per clang AST changes removing
140141
ElaboratedTypes. The value becomes unused, and all the existing users should
141142
expect the former underlying type to be reported instead.

0 commit comments

Comments
 (0)