For your consideration:
Attached is a patch that adds Python 3 compatibility (without losing Python
2 compatibility) to Clang’s Python bindings.
The patch also includes PEP8 formatting cleanups, and a setup.py file to
make it easier to install the bindings into a working Python development
environment.
Yours,
Russell Keith-Magee %-)
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index e4b3876..b1a9abe 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -6,6 +6,7 @@
# License. See LICENSE.TXT for details.
#
#======#
+from __future__ import unicode_literals
r"""
Clang Indexing Library Bindings
@@ -75,6 +76,7 @@ c_object_p = POINTER(c_void_p)
callbacks = {}
+
### Exception Classes ###
class TranslationUnitLoadError(Exception):
@@ -87,6 +89,7 @@ class TranslationUnitLoadError(Exception):
"""
pass
+
class TranslationUnitSaveError(Exception):
"""Represents an error that occurred when saving a TranslationUnit.
@@ -117,6 +120,7 @@ class TranslationUnitSaveError(Exception):
self.save_error = enumeration
Exception.__init__(self, 'Error %d: %s' % (enumeration, message))
+
### Structures and Utility Classes ###
class CachedProperty(object):
@@ -157,6 +161,7 @@ class _CXString(Structure):
assert isinstance(res, _CXString)
return conf.lib.clang_getCString(res)
+
class SourceLocation(Structure):
"""
A SourceLocation represents a particular location within a source file.
@@ -167,8 +172,9 @@ class SourceLocation(Structure):
def _get_instantiation(self):
if self._data is None:
f, l, c, o = c_object_p(), c_uint(), c_uint(), c_uint()
-conf.lib.clang_getInstantiationLocation(self, byref(f), byref(l),
-byref(c), byref(o))
+conf.lib.clang_getInstantiationLocation(
+self, byref(f), byref(l), byref(c), byref(o)
+)
if f:
f = File(f)
else:
@@ -228,6 +234,7 @@ class SourceLocation(Structure):
return "" % (
filename, self.line, self.column)
+
class SourceRange(Structure):
"""
A SourceRange describes a range of source locations within the source
@@ -272,8 +279,8 @@ class SourceRange(Structure):
return False
if other.file is None and self.start.file is None:
pass
-elif ( self.start.file.name != other.file.name or
- other.file.name != self.end.file.name):
+elif (self.start.file.name != other.file.name or
+ other.file.name != self.end.file.name):
# same file name
return False
# same file, in between lines
@@ -292,6 +299,7 @@ class SourceRange(Structure):
def __repr__(self):
return "" % (self.start, self.end)
+
class Diagnostic(object):
"""
A Diagnostic is a single instance of a Clang diagnostic. It includes the
@@ -300,10 +308,10 @@ class Diagnostic(object):
"""
Ignored = 0
-Note= 1
+Note = 1
Warning = 2
-Error = 3
-Fatal = 4
+Error = 3
+Fatal = 4
def __init__(self, ptr):
self.ptr = ptr
@@ -321,7 +329,7 @@ class Diagnostic(object):
@property
def spelling(self):
-return conf.lib.clang_getDiagnosticSpelling(self)
+return conf.lib.clang_getDiagnosticSpelling(self).decode('utf-8')
@property
def ranges(self):
@@ -350,8 +358,9 @@ class Diagnostic(object):
def __getitem__(self, key):
range = SourceRange()
-value = conf.lib.clang_getDiagnosticFixIt(self.diag, key,
-byref(range))
+value = conf.lib.clang_getDiagnosticFixIt(
+self.diag, key, byref(range)
+)
if len(value) == 0:
raise IndexError
@@ -367,12 +376,12 @@ class Diagnostic(object):
@property
def category_name(self):
"""The string name of the category for this diagnostic."""
-return conf.lib.clang_getDiagnosticCategoryText(self)
+return conf.lib.clang_getDiagnosticCategoryText(self).decode('utf-8')
@property
def option(self):
"""The command-line option that enables this diagnostic."""
-return conf.lib.clang_getDiagnosticOption(self, None)
+return conf.lib.clang_getDiagnosticOption(self, None).decode('utf-8')
@property
def disable_option(self):
@@ -380,14 +389,15 @@ class Diagnostic(object):
disable = _CXString()
conf.lib.clang_getDiagnosticOption(self, byref(disable))
-return conf.lib.clang_getCString(disable)
+return conf.lib.clang_getCString(disable).decode('utf-8')
def __repr__(self):
return "" % (
self.severity, self.location, self.spelli