replace error handlers for stdout and co.

Python3.5 and lower throw an UnicodeEncodeError when trying to print
not-encodable characters when not using 'utf-8' as encoding.
Setting their error handlers to 'replace' should help.
This commit is contained in:
Mike Fährmann
2018-04-04 17:30:42 +02:00
parent f8168c693e
commit 0381ae5318
2 changed files with 16 additions and 1 deletions

View File

@@ -27,7 +27,6 @@ from . import version, config, option, extractor, job, util, exception
__version__ = version.__version__ __version__ = version.__version__
log = logging.getLogger("gallery-dl") log = logging.getLogger("gallery-dl")
def initialize_logging(loglevel, formatter): def initialize_logging(loglevel, formatter):
"""Setup basic logging functionality before configfiles have been loaded""" """Setup basic logging functionality before configfiles have been loaded"""
# convert levelnames to lowercase # convert levelnames to lowercase
@@ -42,6 +41,18 @@ def initialize_logging(loglevel, formatter):
root.addHandler(handler) root.addHandler(handler)
def replace_std_streams(errors="replace"):
"""Replace standard streams and set their error handlers to 'errors'"""
for name in ("stdout", "stdin", "stderr"):
stream = getattr(sys, name)
setattr(sys, name, stream.__class__(
stream.buffer,
errors=errors,
newline=stream.newlines,
line_buffering=stream.line_buffering,
))
def progress(urls, pformat): def progress(urls, pformat):
"""Wrapper around urls to output a simple progress indicator""" """Wrapper around urls to output a simple progress indicator"""
if pformat is True: if pformat is True:
@@ -142,6 +153,9 @@ def parse_inputfile(file):
def main(): def main():
try: try:
if sys.stdout.encoding.lower() != "utf-8":
replace_std_streams()
parser = option.build_parser() parser = option.build_parser()
args = parser.parse_args() args = parser.parse_args()

View File

@@ -21,6 +21,7 @@ TRAVIS_SKIP = {
# temporary issues, etc. # temporary issues, etc.
BROKEN = { BROKEN = {
"imgyt", # "Name or service not known"
"loveisover", # "Name or service not known" "loveisover", # "Name or service not known"
"luscious", # order changed, "Jump to Page" is gone "luscious", # order changed, "Jump to Page" is gone
"mangahere", # invalid SSL cert "mangahere", # invalid SSL cert