From: Serge Guelton Date: Thu, 24 Jan 2019 10:34:44 +0000 (+0000) Subject: Fix python3 compability issue in clang binding X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5f7b8d9df4b1023830f011f20999bb205a75ac27;p=clang Fix python3 compability issue in clang binding The file contents could be of str type. Should use byte length instead of str length, otherwise utf-8 encoded files may not get properly parsed for completion. Source issue: https://github.com/ncm2/ncm2-pyclang#2 Commited on behalf of `roxma' Differential Revision: https://reviews.llvm.org/D56429 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352039 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py index 0be83ec998..2c97d01a10 100644 --- a/bindings/python/clang/cindex.py +++ b/bindings/python/clang/cindex.py @@ -2814,9 +2814,9 @@ class TranslationUnit(ClangObject): for i, (name, contents) in enumerate(unsaved_files): if hasattr(contents, "read"): contents = contents.read() - + contents = b(contents) unsaved_array[i].name = b(fspath(name)) - unsaved_array[i].contents = b(contents) + unsaved_array[i].contents = contents unsaved_array[i].length = len(contents) ptr = conf.lib.clang_parseTranslationUnit(index, @@ -2993,17 +2993,13 @@ class TranslationUnit(ClangObject): unsaved_files_array = 0 if len(unsaved_files): unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))() - for i,(name,value) in enumerate(unsaved_files): - if not isinstance(value, str): - # FIXME: It would be great to support an efficient version - # of this, one day. - value = value.read() - print(value) - if not isinstance(value, str): - raise TypeError('Unexpected unsaved file contents.') - unsaved_files_array[i].name = fspath(name) - unsaved_files_array[i].contents = value - unsaved_files_array[i].length = len(value) + for i,(name,contents) in enumerate(unsaved_files): + if hasattr(contents, "read"): + contents = contents.read() + contents = b(contents) + unsaved_files_array[i].name = b(fspath(name)) + unsaved_files_array[i].contents = contents + unsaved_files_array[i].length = len(contents) ptr = conf.lib.clang_reparseTranslationUnit(self, len(unsaved_files), unsaved_files_array, options) @@ -3057,17 +3053,13 @@ class TranslationUnit(ClangObject): unsaved_files_array = 0 if len(unsaved_files): unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))() - for i,(name,value) in enumerate(unsaved_files): - if not isinstance(value, str): - # FIXME: It would be great to support an efficient version - # of this, one day. - value = value.read() - print(value) - if not isinstance(value, str): - raise TypeError('Unexpected unsaved file contents.') + for i,(name,contents) in enumerate(unsaved_files): + if hasattr(contents, "read"): + contents = contents.read() + contents = b(contents) unsaved_files_array[i].name = b(fspath(name)) - unsaved_files_array[i].contents = b(value) - unsaved_files_array[i].length = len(value) + unsaved_files_array[i].contents = contents + unsaved_files_array[i].length = len(contents) ptr = conf.lib.clang_codeCompleteAt(self, fspath(path), line, column, unsaved_files_array, len(unsaved_files), options) if ptr: