From 2d2d200c06fcdebc931e351810d6702769ffe7c8 Mon Sep 17 00:00:00 2001 From: Chiel ten Brinke Date: Mon, 21 Mar 2016 15:46:29 +0100 Subject: [PATCH] Fix crash related to environment variables containing unicode. Also use Python 3 instead of 2 when both are available. --- plugin/autoformat.vim | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index f47e877..df19909 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -106,10 +106,10 @@ function! s:TryAllFormatters(...) range \ echohl None return 1 endif - if has("python") - let success = s:TryFormatterPython() - else + if has("python3") let success = s:TryFormatterPython3() + else + let success = s:TryFormatterPython() endif if success if verbose @@ -173,17 +173,21 @@ function! s:TryFormatterPython() python << EOF import vim, subprocess, os from subprocess import Popen, PIPE - text = os.linesep.join(vim.current.buffer[:]) + os.linesep formatprg = vim.eval('b:formatprg') verbose = bool(int(vim.eval('verbose'))) + env = os.environ.copy() if int(vim.eval('exists("g:formatterpath")')): extra_path = vim.eval('g:formatterpath') env['PATH'] = ':'.join(extra_path) + ':' + env['PATH'] +# When an entry is unicode, Popen can't deal with it in Python 2. +# As a pragmatic fix, we'll omit that entry. +env = {key : val for key, val in env.iteritems() if type(key) == type(val) == str} p = subprocess.Popen(formatprg, env=env, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) stdoutdata, stderrdata = p.communicate(text) + if stderrdata: if verbose: formattername = vim.eval('b:formatters[s:index]')