Fix bytes handling on python3

Fixes #356
This commit is contained in:
ZyX 2013-03-24 05:45:52 +04:00
parent 49b54353a1
commit b517df774b
5 changed files with 44 additions and 8 deletions

View File

@ -1,5 +1,7 @@
# vim:fileencoding=utf-8:noet # vim:fileencoding=utf-8:noet
import sys
try: try:
import vim import vim
except ImportError: except ImportError:
@ -15,7 +17,10 @@ try:
def vim_get_func(f, rettype=None): def vim_get_func(f, rettype=None):
'''Return a vim function binding.''' '''Return a vim function binding.'''
try: try:
return vim.bindeval('function("' + f + '")') func = vim.bindeval('function("' + f + '")')
if sys.version_info >= (3,) and rettype is str:
return (lambda *args, **kwargs: func(*args, **kwargs).decode('utf-8', errors='replace'))
return func
except vim.error: except vim.error:
return None return None
except AttributeError: except AttributeError:
@ -47,4 +52,12 @@ except AttributeError:
vim_get_func = VimFunc vim_get_func = VimFunc
getbufvar = vim_get_func('getbufvar') if sys.version_info < (3,) or not hasattr(vim, 'bindeval'):
getbufvar = vim_get_func('getbufvar')
else:
_getbufvar = vim_get_func('getbufvar')
def getbufvar(*args):
r = _getbufvar(*args)
if type(r) is bytes:
return r.decode('utf-8')
return r

View File

@ -7,7 +7,7 @@ from powerline.bindings.vim import getbufvar
def help(matcher_info): def help(matcher_info):
return getbufvar(matcher_info['bufnr'], '&buftype') == 'help' return str(getbufvar(matcher_info['bufnr'], '&buftype')) == 'help'
def cmdwin(matcher_info): def cmdwin(matcher_info):

View File

@ -10,9 +10,7 @@ from powerline.theme import Theme
import vim import vim
vim_mode = vim_get_func('mode') vim_mode = vim_get_func('mode', rettype=str)
vim_getwinvar = vim_get_func('getwinvar')
vim_setwinvar = vim_get_func('setwinvar')
mode_translations = { mode_translations = {
chr(ord('V') - 0x40): '^V', chr(ord('V') - 0x40): '^V',
chr(ord('S') - 0x40): '^S', chr(ord('S') - 0x40): '^S',

View File

@ -19,8 +19,8 @@ from collections import defaultdict
vim_funcs = { vim_funcs = {
'virtcol': vim_get_func('virtcol', rettype=int), 'virtcol': vim_get_func('virtcol', rettype=int),
'fnamemodify': vim_get_func('fnamemodify'), 'fnamemodify': vim_get_func('fnamemodify', rettype=str),
'expand': vim_get_func('expand'), 'expand': vim_get_func('expand', rettype=str),
'bufnr': vim_get_func('bufnr', rettype=int), 'bufnr': vim_get_func('bufnr', rettype=int),
'line2byte': vim_get_func('line2byte', rettype=int), 'line2byte': vim_get_func('line2byte', rettype=int),
} }

View File

@ -31,6 +31,26 @@ def _logged(func):
return f return f
def _construct_result(r):
import sys
if sys.version_info < (3,):
return r
else:
if type(r) is str:
return r.encode('utf-8')
elif type(r) is dict or type(r) is list:
raise NotImplementedError
return r
def _str_func(func):
from functools import wraps
@wraps(func)
def f(*args, **kwargs):
return _construct_result(func(*args, **kwargs))
return f
def _log_print(): def _log_print():
import sys import sys
for entry in _log: for entry in _log:
@ -75,6 +95,7 @@ def bindeval(expr):
@_logged @_logged
@_str_func
def _emul_mode(*args): def _emul_mode(*args):
if args and args[0]: if args and args[0]:
return _mode return _mode
@ -83,6 +104,7 @@ def _emul_mode(*args):
@_logged @_logged
@_str_func
def _emul_getbufvar(bufnr, varname): def _emul_getbufvar(bufnr, varname):
if varname[0] == '&': if varname[0] == '&':
if bufnr not in _buf_options: if bufnr not in _buf_options:
@ -98,6 +120,7 @@ def _emul_getbufvar(bufnr, varname):
@_logged @_logged
@_str_func
def _emul_getwinvar(winnr, varname): def _emul_getwinvar(winnr, varname):
return _win_scopes[winnr][varname] return _win_scopes[winnr][varname]
@ -115,6 +138,7 @@ def _emul_virtcol(expr):
@_logged @_logged
@_str_func
def _emul_fnamemodify(path, modstring): def _emul_fnamemodify(path, modstring):
import os import os
_modifiers = { _modifiers = {
@ -130,6 +154,7 @@ def _emul_fnamemodify(path, modstring):
@_logged @_logged
@_str_func
def _emul_expand(expr): def _emul_expand(expr):
if expr == '<abuf>': if expr == '<abuf>':
return _buffer() return _buffer()