Skip to content

Commit

Permalink
replace error handlers for stdout and co.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mikf committed Apr 4, 2018
1 parent f8168c6 commit 0381ae5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion gallery_dl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
__version__ = version.__version__
log = logging.getLogger("gallery-dl")


def initialize_logging(loglevel, formatter):
"""Setup basic logging functionality before configfiles have been loaded"""
# convert levelnames to lowercase
Expand All @@ -42,6 +41,18 @@ def initialize_logging(loglevel, formatter):
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):
"""Wrapper around urls to output a simple progress indicator"""
if pformat is True:
Expand Down Expand Up @@ -142,6 +153,9 @@ def parse_inputfile(file):

def main():
try:
if sys.stdout.encoding.lower() != "utf-8":
replace_std_streams()

parser = option.build_parser()
args = parser.parse_args()

Expand Down
1 change: 1 addition & 0 deletions test/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

# temporary issues, etc.
BROKEN = {
"imgyt", # "Name or service not known"
"loveisover", # "Name or service not known"
"luscious", # order changed, "Jump to Page" is gone
"mangahere", # invalid SSL cert
Expand Down

0 comments on commit 0381ae5

Please sign in to comment.