Skip to content

Commit

Permalink
Fix python3 compability issue in clang binding
Browse files Browse the repository at this point in the history
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

llvm-svn: 352039
  • Loading branch information
serge-sans-paille-qb committed Jan 24, 2019
1 parent 79df859 commit fa2e927
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions clang/bindings/python/clang/cindex.py
Expand Up @@ -2814,9 +2814,9 @@ def from_source(cls, filename, args=None, unsaved_files=None, options=0,
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,
Expand Down Expand Up @@ -2993,17 +2993,13 @@ def reparse(self, unsaved_files=None, options=0):
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)

Expand Down Expand Up @@ -3057,17 +3053,13 @@ def codeComplete(self, path, line, column, unsaved_files=None,
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:
Expand Down

0 comments on commit fa2e927

Please sign in to comment.