Skip to content

libclang Python bindings return null Cursors via Token.cursor property #163180

@DeinAlptraum

Description

@DeinAlptraum

In #138103, we changed the behavior of the Cursor class in order to prevent usage of null Cursors by throwing an error when any of its member methods is accessed. At the same time, we adjusted all known places that return cursors to convert null Cursors to None, so that users would not encounter null Cursors. See discussion on the PR here: #138103 (comment)

It seems that we have overlooked one function that returns cursors: the TokenGroup.get_tokens function sometimes returns null Cursors.

Reproducer:
test.cpp

struct A;

int main() {
  return 0;
}

main.py

import clang.cindex as clci
clci.Config.set_library_path("/data/shared/programming/repos/llvm-project/build/lib")
tu = clci.TranslationUnit.from_source("test.cpp")

cs = list(tu.get_tokens(extent=tu.cursor.extent))
for c in cs:
    if c.cursor.is_null():
        print("Null cursor found")

Running main.py should find one null cursor.

get_tokens should be adapted to convert null Cursors into None as done everywhere else.

Originally reported here: jnikula/hawkmoth#291

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions