From a620a0ef549e31ae2c538beb914101dd36b20248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Mon, 21 Jan 2013 21:50:30 +0100 Subject: [PATCH] Fix various Python 3 related issues Powerline now detects the Python version and verifies that the user has Python 3.3 before enabling Python 3 support. To enable Python 3 support, use `python3` to execute source_plugin() in your vimrc instead of `python2`. --- powerline/bindings/vim/__init__.py | 9 ++++++++- .../bindings/vim/plugin/source_plugin.vim | 12 ++++++------ powerline/bindings/vim/powerline.vim | 18 +++++++----------- powerline/theme.py | 14 ++++++++------ 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/powerline/bindings/vim/__init__.py b/powerline/bindings/vim/__init__.py index aa2c652f..ef46b4ae 100644 --- a/powerline/bindings/vim/__init__.py +++ b/powerline/bindings/vim/__init__.py @@ -4,9 +4,16 @@ import vim def source_plugin(): + import sys import os + if sys.version_info[:2] == (3, 3): + vim.command('let g:powerline_pycmd = "python3"') + vim.command('let g:powerline_pyeval = "py3eval"') + else: + vim.command('let g:powerline_pycmd = "python"') + vim.command('let g:powerline_pyeval = "pyeval"') fnameescape = vim_get_func('fnameescape') - vim.command('source ' + fnameescape(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'powerline.vim'))) + vim.command('source ' + fnameescape(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'powerline.vim')).decode('utf-8')) try: _vim_globals = vim.bindeval('g:') diff --git a/powerline/bindings/vim/plugin/source_plugin.vim b/powerline/bindings/vim/plugin/source_plugin.vim index 0d1fe429..51c85ea0 100644 --- a/powerline/bindings/vim/plugin/source_plugin.vim +++ b/powerline/bindings/vim/plugin/source_plugin.vim @@ -5,9 +5,9 @@ if ! has('python') && ! has('python3') finish endif -let s:pycmd = has('python3') ? 'python3' : 'python' - -exec s:pycmd ' import sys, vim' -exec s:pycmd ' sys.path.append(vim.eval(''expand(":h:h:h:h:h")''))' - -source :h:h/powerline.vim +python <:h:h:h:h:h")')) +from powerline.bindings.vim import source_plugin +source_plugin() +EOF diff --git a/powerline/bindings/vim/powerline.vim b/powerline/bindings/vim/powerline.vim index 502ef837..ade94c7b 100644 --- a/powerline/bindings/vim/powerline.vim +++ b/powerline/bindings/vim/powerline.vim @@ -1,17 +1,13 @@ -let s:pycmd = has('python3') ? 'python3' : 'python' +exec g:powerline_pycmd 'import uuid' +exec g:powerline_pycmd 'from powerline.core import Powerline' +exec g:powerline_pycmd 'powerline = Powerline("vim")' -exec s:pycmd ' import uuid' -exec s:pycmd ' from powerline.core import Powerline' -exec s:pycmd ' powerline = Powerline("vim")' - -if exists('*py3eval') - let s:pyeval = function('py3eval') -elseif exists('*pyeval') - let s:pyeval = function('pyeval') +if exists('*'. g:powerline_pyeval) + let s:pyeval = function(g:powerline_pyeval) else - exec s:pycmd ' import json, vim' + exec g:powerline_pycmd 'import json, vim' function! s:pyeval(e) - exec s:pycmd ' vim.command("return " + json.dumps(eval(vim.eval("a:e"))))' + exec g:powerline_pycmd 'vim.command("return " + json.dumps(eval(vim.eval("a:e"))))' endfunction endif diff --git a/powerline/theme.py b/powerline/theme.py index 5bca316f..7ef45615 100644 --- a/powerline/theme.py +++ b/powerline/theme.py @@ -5,6 +5,12 @@ import copy from .segment import Segment +try: + unicode() +except NameError: + unicode = str + + class Theme(object): def __init__(self, ext, colorscheme, theme_config, common_config): self.colorscheme = colorscheme @@ -12,7 +18,7 @@ class Theme(object): self.segments = [] self.EMPTY_SEGMENT = { 'contents': None, - 'highlight': defaultdict(lambda : {'fg': False, 'bg': False, 'attr': 0}) + 'highlight': defaultdict(lambda: {'fg': False, 'bg': False, 'attr': 0}) } get_segment = Segment(ext, common_config['paths'], colorscheme, theme_config.get('default_module')).get for side in ['left', 'right']: @@ -42,11 +48,7 @@ class Theme(object): pass else: continue - try: - contents = unicode(segment['contents']) - except NameError: - contents = str(segment['contents']) - segment['contents'] = (segment['before'] + contents + segment['after'])\ + segment['contents'] = (segment['before'] + unicode(segment['contents']) + segment['after'])\ .ljust(segment['ljust'])\ .rjust(segment['rjust']) # We need to yield a copy of the segment, or else mode-dependent