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