From 501e91f3b7bc8d06f245696ff327ea6aa153ec47 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Sun, 24 Jan 2010 21:20:05 +0000 Subject: [PATCH] cindex/Python: Return null cursors as None instead of exposing this notion. llvm-svn: 94385 --- clang/bindings/python/clang/cindex.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 115ea9fbfd11..8b4ea5bc323b 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -133,11 +133,6 @@ class Cursor(Structure): def __ne__(self, other): return not Cursor_eq(self, other) - @staticmethod - def null(): - """Return the null cursor object.""" - return Cursor_null() - def is_declaration(self): """Return True if the cursor points to a declaration.""" return Cursor_is_decl(self.kind) @@ -204,16 +199,27 @@ class Cursor(Structure): return Cursor_extent(self) def get_children(self): - """Return an iterator for the accessing children of this cursor.""" + """Return an iterator for the accessing the children of this cursor.""" # FIXME: Expose iteration from CIndex, PR6125. def visitor(child, parent, children): + # FIXME: Document this assertion in API. + # FIXME: There should just be an isNull method. + assert child != Cursor_null() children.append(child) return 1 # continue children = [] Cursor_visit(self, Callback(visitor), children) return iter(children) + @staticmethod + def from_result(res, fn, args): + assert isinstance(res, Cursor) + # FIXME: There should just be an isNull method. + if res == Cursor_null(): + return None + return res + ## CIndex Objects ## # CIndex objects (derived from ClangObject) are essentially lightweight @@ -391,6 +397,7 @@ Cursor_is_def.restype = bool Cursor_def = lib.clang_getCursorDefinition Cursor_def.argtypes = [Cursor] Cursor_def.restype = Cursor +Cursor_def.errcheck = Cursor.from_result Cursor_eq = lib.clang_equalCursors Cursor_eq.argtypes = [Cursor, Cursor] @@ -412,6 +419,7 @@ Cursor_extent.restype = SourceRange Cursor_ref = lib.clang_getCursorReferenced Cursor_ref.argtypes = [Cursor] Cursor_ref.restype = Cursor +Cursor_ref.errcheck = Cursor.from_result Cursor_visit = lib.clang_visitChildren Cursor_visit.argtypes = [Cursor, Callback, py_object] @@ -438,6 +446,7 @@ TranslationUnit_parse.restype = c_object_p TranslationUnit_cursor = lib.clang_getTranslationUnitCursor TranslationUnit_cursor.argtypes = [TranslationUnit] TranslationUnit_cursor.restype = Cursor +TranslationUnit_cursor.errcheck = Cursor.from_result TranslationUnit_spelling = lib.clang_getTranslationUnitSpelling TranslationUnit_spelling.argtypes = [TranslationUnit]