[cfe-commits] r94384 - in /cfe/trunk/bindings/python: clang/cindex.py tests/cindex/test_translation_unit.py
Daniel Dunbar
daniel at zuster.org
Sun Jan 24 13:19:58 PST 2010
Author: ddunbar
Date: Sun Jan 24 15:19:57 2010
New Revision: 94384
URL: http://llvm.org/viewvc/llvm-project?rev=94384&view=rev
Log:
cindex/Python: Convert CXString objects to regular Python strings below API.
Modified:
cfe/trunk/bindings/python/clang/cindex.py
cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py
Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=94384&r1=94383&r2=94384&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Sun Jan 24 15:19:57 2010
@@ -61,24 +61,19 @@
### Structures and Utility Classes ###
-class String(Structure):
- """
- The String class is a simple wrapper around constant string data returned
- from functions in the CIndex library.
+class _CXString(Structure):
+ """Helper for transforming CXString results."""
- String objects do not provide any of the operations that Python strings
- support. However, these objects can be explicitly cast using the str()
- function.
- """
_fields_ = [("spelling", c_char_p), ("free", c_int)]
def __del__(self):
- if self.free:
- String_dispose(self)
-
- def __str__(self):
- return self.spelling
+ _CXString_dispose(self)
+ @staticmethod
+ def from_result(res, fn, args):
+ assert isinstance(res, _CXString)
+ return _CXString_getCString(res)
+
class SourceLocation(Structure):
"""
A SourceLocation represents a particular location within a source file.
@@ -328,8 +323,12 @@
Callback = CFUNCTYPE(c_int, Cursor, Cursor, py_object)
# String Functions
-String_dispose = lib.clang_disposeString
-String_dispose.argtypes = [String]
+_CXString_dispose = lib.clang_disposeString
+_CXString_dispose.argtypes = [_CXString]
+
+_CXString_getCString = lib.clang_getCString
+_CXString_getCString.argtypes = [_CXString]
+_CXString_getCString.restype = c_char_p
# Source Location Functions
SourceLocation_loc = lib.clang_getInstantiationLocation
@@ -399,7 +398,8 @@
Cursor_spelling = lib.clang_getCursorSpelling
Cursor_spelling.argtypes = [Cursor]
-Cursor_spelling.restype = String
+Cursor_spelling.restype = _CXString
+Cursor_spelling.errcheck = _CXString.from_result
Cursor_loc = lib.clang_getCursorLocation
Cursor_loc.argtypes = [Cursor]
@@ -441,7 +441,8 @@
TranslationUnit_spelling = lib.clang_getTranslationUnitSpelling
TranslationUnit_spelling.argtypes = [TranslationUnit]
-TranslationUnit_spelling.restype = String
+TranslationUnit_spelling.restype = _CXString
+TranslationUnit_spelling.errcheck = _CXString.from_result
TranslationUnit_dispose = lib.clang_disposeTranslationUnit
TranslationUnit_dispose.argtypes = [TranslationUnit]
Modified: cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py?rev=94384&r1=94383&r2=94384&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py Sun Jan 24 15:19:57 2010
@@ -7,7 +7,7 @@
path = os.path.join(kInputsDir, 'hello.cpp')
index = Index.create()
tu = index.parse(path)
- assert str(tu.spelling) == path
+ assert tu.spelling == path
def test_cursor():
path = os.path.join(kInputsDir, 'hello.cpp')
More information about the cfe-commits
mailing list