Fix an uncaught BrokenPipeError for the python 3 case.

This commit is contained in:
Chiel ten Brinke 2017-03-06 18:33:02 +01:00
parent 56170ff0d3
commit 129f17d807

View File

@ -113,11 +113,17 @@ function! s:TryAllFormatters(...) range
return 1
endif
if has("python3")
if verbose
echomsg "Using python 3 code..."
endif
let success = s:TryFormatterPython3()
else
if verbose
echomsg "Using python 2 code..."
endif
let success = s:TryFormatterPython()
endif
if success
if success == 0
if verbose
echomsg "Definition in '".formatdef_var."' was successful."
endif
@ -170,8 +176,8 @@ endfunction
" Call formatter
" If stderr is empty, apply result, return 1
" Otherwise, return 0
" If stderr is empty, apply result, return 0
" Otherwise, return 1
" +python version
function! s:TryFormatterPython()
@ -205,7 +211,7 @@ if stderrdata:
formattername = vim.eval('b:formatters[s:index]')
print('Formatter {} has errors: {} Skipping.'.format(formattername, stderrdata))
print('Failing config: {}'.format(repr(formatprg), stderrdata))
vim.command('return 0')
vim.command('return 1')
else:
# It is not certain what kind of line endings are being used by the format program.
# Therefore we simply split on all possible eol characters.
@ -226,7 +232,7 @@ else:
vim.current.buffer[:] = lines
EOF
return 1
return 0
endfunction
" +python3 version
@ -238,6 +244,9 @@ python3 << EOF
import vim, subprocess, os
from subprocess import Popen, PIPE
# The return code is `failure`, unless otherwise specified
vim.command('return 1')
text = bytes(os.linesep.join(vim.current.buffer[:]) + os.linesep, 'utf-8')
formatprg = vim.eval('b:formatprg')
verbose = bool(int(vim.eval('verbose')))
@ -246,14 +255,22 @@ if int(vim.eval('exists("g:formatterpath")')):
extra_path = vim.eval('g:formatterpath')
env['PATH'] = ':'.join(extra_path) + ':' + env['PATH']
try:
p = subprocess.Popen(formatprg, env=env, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdoutdata, stderrdata = p.communicate(text)
except (BrokenPipeError, IOError):
if verbose:
raise
else:
if stderrdata:
if verbose:
formattername = vim.eval('b:formatters[s:index]')
print('Formatter {} has errors: {} Skipping.'.format(formattername, stderrdata))
print('Failing config: {}'.format(repr(formatprg), stderrdata))
vim.command('return 0')
elif not stdoutdata:
if verbose:
print('Formatter {} gives empty result: {} Skipping.'.format(formattername, stderrdata))
print('Failing config: {}'.format(repr(formatprg), stderrdata))
else:
# It is not certain what kind of line endings are being used by the format program.
# Therefore we simply split on all possible eol characters.
@ -274,9 +291,8 @@ else:
lines = [splitline for line in lines for splitline in line.split(eol)]
vim.current.buffer[:] = lines
vim.command('return 0')
EOF
return 1
endfunction