| 1 | """Used in jedi-vim's jedi#debug_info()""" |
| 2 | import sys |
| 3 | |
| 4 | import vim |
| 5 | from jedi_vim import PythonToVimStr, jedi |
| 6 | |
| 7 | |
| 8 | def echo(msg): |
| 9 | vim.command('echo %r' % PythonToVimStr(msg)) |
| 10 | |
| 11 | |
| 12 | def echo_error(msg): |
| 13 | vim.command('echohl ErrorMsg') |
| 14 | echo(msg) |
| 15 | vim.command('echohl None') |
| 16 | |
| 17 | |
| 18 | def format_exc_info(exc_info=None, tb_indent=2): |
| 19 | import traceback |
| 20 | |
| 21 | if exc_info is None: |
| 22 | exc_info = sys.exc_info() |
| 23 | |
| 24 | exc_msg = traceback.format_exception_only(exc_info[0], exc_info[1]) |
| 25 | lines = ''.join(exc_msg).rstrip('\n').split('\n') |
| 26 | |
| 27 | lines.append('Traceback (most recent call last):') |
| 28 | tb = traceback.format_tb(exc_info[2]) |
| 29 | lines.extend(''.join(tb).rstrip('\n').split('\n')) |
| 30 | |
| 31 | indent = ' ' * tb_indent |
| 32 | return '{0}'.format(('\n' + indent).join(lines)) |
| 33 | |
| 34 | |
| 35 | def get_known_environments(): |
| 36 | """Get known Jedi environments.""" |
| 37 | envs = list(jedi.find_virtualenvs()) |
| 38 | envs.extend(jedi.find_system_environments()) |
| 39 | return envs |
| 40 | |
| 41 | |
| 42 | def display_debug_info(): |
| 43 | echo(' - global sys.executable: `{0}`'.format(sys.executable)) |
| 44 | echo(' - global sys.version: `{0}`'.format( |
| 45 | ', '.join([x.strip() |
| 46 | for x in sys.version.split('\n')]))) |
| 47 | echo(' - global site module: `{0}`'.format(__import__('site').__file__)) |
| 48 | |
| 49 | try: |
| 50 | import jedi_vim |
| 51 | except Exception: |
| 52 | echo_error('ERROR: could not import jedi_vim: {0}'.format( |
| 53 | format_exc_info())) |
| 54 | return |
| 55 | |
| 56 | if jedi_vim.jedi is None: |
| 57 | if hasattr(jedi_vim, 'jedi_import_error'): |
| 58 | error_msg = format_exc_info(jedi_vim.jedi_import_error) |
| 59 | else: |
| 60 | error_msg = 'unknown error' |
| 61 | echo_error('ERROR: could not import the "jedi" Python module: {0}'.format( |
| 62 | error_msg)) |
| 63 | else: |
| 64 | echo('\n##### Jedi\n\n - path: `{0}`'.format(jedi_vim.jedi.__file__)) |
| 65 | echo(' - version: {0}'.format(jedi_vim.jedi.__version__)) |
| 66 | |
| 67 | try: |
| 68 | project = jedi_vim.get_project() |
| 69 | environment = project.get_environment() |
| 70 | except AttributeError: |
| 71 | script_evaluator = jedi_vim.jedi.Script('')._evaluator |
| 72 | try: |
| 73 | sys_path = script_evaluator.project.sys_path |
| 74 | except AttributeError: |
| 75 | sys_path = script_evaluator.sys_path |
| 76 | else: |
| 77 | echo('\n##### Jedi environment: {0}\n\n'.format(environment)) |
| 78 | echo(' - executable: {0}'.format(environment.executable)) |
| 79 | try: |
| 80 | sys_path = environment.get_sys_path() |
| 81 | except Exception: |
| 82 | echo_error('ERROR: failed to get sys path from environment: {0}'.format( |
| 83 | format_exc_info())) |
| 84 | return |
| 85 | |
| 86 | echo(' - sys_path:') |
| 87 | for p in sys_path: |
| 88 | echo(' - `{0}`'.format(p)) |
| 89 | |
| 90 | if environment: |
| 91 | echo('\n##### Known environments\n\n') |
| 92 | for environment in get_known_environments(): |
| 93 | echo(' - {0} ({1})\n'.format( |
| 94 | environment, |
| 95 | environment.executable, |
| 96 | )) |