Index: bindings/python/clang/cindex.py =================================================================== --- bindings/python/clang/cindex.py +++ bindings/python/clang/cindex.py @@ -2803,7 +2803,7 @@ unsaved_array[i].name = b(name) unsaved_array[i].contents = b(contents) - unsaved_array[i].length = len(contents) + unsaved_array[i].length = len(unsaved_array[i].contents) ptr = conf.lib.clang_parseTranslationUnit(index, filename, args_array, len(args), unsaved_array, @@ -2983,9 +2983,10 @@ print(value) if not isinstance(value, str): raise TypeError('Unexpected unsaved file contents.') - unsaved_files_array[i].name = name - unsaved_files_array[i].contents = value - unsaved_files_array[i].length = len(value) + unsaved_files_array[i].name = b(name) + unsaved_files_array[i].contents = b(value) + unsaved_files_array[i].length = \ + len(unsaved_files_array[i].contents) ptr = conf.lib.clang_reparseTranslationUnit(self, len(unsaved_files), unsaved_files_array, options) @@ -3049,7 +3050,8 @@ raise TypeError('Unexpected unsaved file contents.') unsaved_files_array[i].name = b(name) unsaved_files_array[i].contents = b(value) - unsaved_files_array[i].length = len(value) + unsaved_files_array[i].length = \ + len(unsaved_files_array[i].contents) ptr = conf.lib.clang_codeCompleteAt(self, path, line, column, unsaved_files_array, len(unsaved_files), options) if ptr: Index: bindings/python/tests/cindex/test_translation_unit.py =================================================================== --- bindings/python/tests/cindex/test_translation_unit.py +++ bindings/python/tests/cindex/test_translation_unit.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + from contextlib import contextmanager import gc import os @@ -84,6 +86,16 @@ spellings = [c.spelling for c in tu.cursor.get_children()] self.assertEqual(spellings[-1], 'x') + def test_unsaved_files_encoding(self): + tu = TranslationUnit.from_source('fake.c', ['-I./'], unsaved_files = [ + ('fake.c', """ +// 😀 +int x; +""") + ]) + spellings = [c.spelling for c in tu.cursor.get_children()] + self.assertEqual(spellings[-1], 'x') + def assert_normpaths_equal(self, path1, path2): """ Compares two paths for equality after normalizing them with os.path.normpath