Fix #767: safely encode SphinxErrors when printing to sys.stderr.

This commit is contained in:
Georg Brandl 2011-09-21 10:46:39 +02:00
parent a4b8b81712
commit c05063d0a5
2 changed files with 11 additions and 5 deletions

View File

@ -22,6 +22,7 @@ from sphinx.errors import SphinxError
from sphinx.application import Sphinx
from sphinx.util import Tee, format_exception_cut_frames, save_traceback
from sphinx.util.console import red, nocolor, color_terminal
from sphinx.util.pycompat import terminal_safe
def usage(argv, msg=None):
@ -190,8 +191,7 @@ def main(argv):
except KeyboardInterrupt:
if use_pdb:
import pdb
print >>error, red('Interrupted while building, '
'starting debugger:')
print >>error, red('Interrupted while building, starting debugger:')
traceback.print_exc()
pdb.post_mortem(sys.exc_info()[2])
return 1
@ -199,17 +199,17 @@ def main(argv):
if use_pdb:
import pdb
print >>error, red('Exception occurred while building, '
'starting debugger:')
'starting debugger:')
traceback.print_exc()
pdb.post_mortem(sys.exc_info()[2])
else:
print >>error
if isinstance(err, SystemMessage):
print >>error, red('reST markup error:')
print >>error, err.args[0].encode('ascii', 'backslashreplace')
print >>error, terminal_safe(err.args[0])
elif isinstance(err, SphinxError):
print >>error, red('%s:' % err.category)
print >>error, err
print >>error, terminal_safe(unicode(err))
else:
print >>error, red('Exception occurred:')
print >>error, format_exception_cut_frames().rstrip()

View File

@ -27,6 +27,9 @@ if sys.version_info >= (3, 0):
u = ''
# StringIO/BytesIO classes
from io import StringIO, BytesIO, TextIOWrapper
# safely encode a string for printing to the terminal
def terminal_safe(s):
return s.encode('ascii', 'backslashreplace').decode('ascii')
# support for running 2to3 over config files
def convert_with_2to3(filepath):
from lib2to3.refactor import RefactoringTool, get_fixers_from_package
@ -56,6 +59,9 @@ else:
convert_with_2to3 = None
def TextIOWrapper(stream, encoding):
return codecs.lookup(encoding or 'ascii')[2](stream)
# safely encode a string for printing to the terminal
def terminal_safe(s):
return s.encode('ascii', 'backslashreplace')
# ------------------------------------------------------------------------------