Skip to content

Commit

Permalink
Merge pull request #120 from davidlatwe/improve-subprocessing
Browse files Browse the repository at this point in the history
Better subprocess listening experience
  • Loading branch information
davidlatwe authored Nov 4, 2020
2 parents 0a3e964 + ccbd40b commit 43655aa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
45 changes: 45 additions & 0 deletions allzpark/allzparkconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,48 @@ def themes():
"""
return []


def subprocess_encoding():
"""Codec that should be used to decode subprocess stdout/stderr
See https://docs.python.org/3/library/codecs.html#standard-encodings
Returns:
str: name of codec
"""
# nerdvegas/rez sets `encoding='utf-8'` when `universal_newlines=True` and
# `encoding` is not in Popen kwarg.
return "utf-8"


def unicode_decode_error_handler():
"""Error handler for handling UnicodeDecodeError in subprocess
See https://docs.python.org/3/library/codecs.html#error-handlers
Returns:
str: name of registered error handler
"""
import codecs
import locale

def decode_with_preferred_encoding(exception):
encoding = locale.getpreferredencoding(do_setlocale=False)
invalid_bytes = exception.object[exception.start:]

text = invalid_bytes.decode(encoding,
# second fallback
errors="backslashreplace")

return text, len(exception.object)

handler_name = "decode_with_preferred_encoding"
try:
codecs.lookup_error(handler_name)
except LookupError:
codecs.register_error(handler_name, decode_with_preferred_encoding)

return handler_name
9 changes: 9 additions & 0 deletions allzpark/control.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Orchestrates view.py and model.py"""

import os
import sys
import time
import errno
import shutil
Expand Down Expand Up @@ -1193,6 +1194,14 @@ def _execute(self):
"parent_environ": None,
"startupinfo": startupinfo
}
if rez.project == "rez":
# bleeding-rez adds `universal_newlines=True` when spawning shell,
# nerdvegas/rez doesn't.
kwargs["universal_newlines"] = True

if sys.version_info[:2] >= (3, 6):
kwargs["encoding"] = allzparkconfig.subprocess_encoding()
kwargs["errors"] = allzparkconfig.unicode_decode_error_handler()

context = self.context

Expand Down

0 comments on commit 43655aa

Please sign in to comment.