问题
Short description: using libclang
to autocomplete code does not work with python that comes bundled with Sublime Text 3.
Details: A small verifiable example is in the repo on Github
In essence, there is a script that uses a slightly changed cindex.py
(compatible with python 3 and clang 3.8) and builds a Translation Unit from a test source file. It then reparses it and tries to complete.
The script works as expected on using Python 3.3.5 from Powershell.
When put into Packages folder on Sublime Text 3 it produces an error. Python version as reported by Sublime Text 3 is 3.3.6. The error:
Traceback (most recent call last):
File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 78, in reload_plugin
m = importlib.import_module(modulename)
File "./python3.3/importlib/__init__.py", line 90, in import_module
File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper
File "<frozen importlib._bootstrap>", line 1022, in load_module
File "<frozen importlib._bootstrap>", line 1003, in load_module
File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper
File "<frozen importlib._bootstrap>", line 868, in _load_module
File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
File "C:\Users\igor\AppData\Roaming\Sublime Text 3\Packages\test_clang\script.py", line 21, in <module>
tu = TU.from_source(filename=filename)
File "C:\Users\igor\AppData\Roaming\Sublime Text 3\Packages\test_clang\clang\cindex38.py", line 2372, in from_source
raise TranslationUnitLoadError("Error parsing translation unit.")
clang.cindex38.TranslationUnitLoadError: Error parsing translation unit.
This is happening because the ptr to tranlation unit returned by libclang inside cindex.py
is None
. The only thing that is strange for me is that it happens only with python bundled with sublime text 3.
Does it happen also to other people? Does anyone have any idea what could be the cause or how to debug it?
Also feel free to ping me if you cannot run the example provided here.
UPD: in the issues of the test project we have found out that it is not ctypes that is bundled in sublime text. Replacing the ones from sublime text to the ones installed in the system produces the same error.
UPD2: I have stripped down the cindex.py
file in the test repository to only contain a bare minimum of code that is needed to run into the same issue as described in the question. Maybe this will help generating new ideas on what can be wrong? Also, I want to explicitly point out that the same code works exactly as expected on both Linux and OSX.
回答1:
Unfortunately for now the answer to this question is that it is due to a bug in Python 3.3 that is bundled with Sublime Text 3. See discussion on Sublime Text forum.
This means that we won't see a fix to this until whenever a version of sublime text comes with a newer python bundled within it.
来源:https://stackoverflow.com/questions/37774599/python-libclang-bindings-on-windows-fail-to-initialize-a-translation-unit-from-s