Fix crash related to environment variables containing unicode.
Also use Python 3 instead of 2 when both are available.
This commit is contained in:
parent
383fbdb716
commit
2d2d200c06
@ -106,10 +106,10 @@ function! s:TryAllFormatters(...) range
|
|||||||
\ echohl None
|
\ echohl None
|
||||||
return 1
|
return 1
|
||||||
endif
|
endif
|
||||||
if has("python")
|
if has("python3")
|
||||||
let success = s:TryFormatterPython()
|
|
||||||
else
|
|
||||||
let success = s:TryFormatterPython3()
|
let success = s:TryFormatterPython3()
|
||||||
|
else
|
||||||
|
let success = s:TryFormatterPython()
|
||||||
endif
|
endif
|
||||||
if success
|
if success
|
||||||
if verbose
|
if verbose
|
||||||
@ -173,17 +173,21 @@ function! s:TryFormatterPython()
|
|||||||
python << EOF
|
python << EOF
|
||||||
import vim, subprocess, os
|
import vim, subprocess, os
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
text = os.linesep.join(vim.current.buffer[:]) + os.linesep
|
text = os.linesep.join(vim.current.buffer[:]) + os.linesep
|
||||||
formatprg = vim.eval('b:formatprg')
|
formatprg = vim.eval('b:formatprg')
|
||||||
verbose = bool(int(vim.eval('verbose')))
|
verbose = bool(int(vim.eval('verbose')))
|
||||||
|
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
if int(vim.eval('exists("g:formatterpath")')):
|
if int(vim.eval('exists("g:formatterpath")')):
|
||||||
extra_path = vim.eval('g:formatterpath')
|
extra_path = vim.eval('g:formatterpath')
|
||||||
env['PATH'] = ':'.join(extra_path) + ':' + env['PATH']
|
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)
|
p = subprocess.Popen(formatprg, env=env, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
||||||
stdoutdata, stderrdata = p.communicate(text)
|
stdoutdata, stderrdata = p.communicate(text)
|
||||||
|
|
||||||
if stderrdata:
|
if stderrdata:
|
||||||
if verbose:
|
if verbose:
|
||||||
formattername = vim.eval('b:formatters[s:index]')
|
formattername = vim.eval('b:formatters[s:index]')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user