Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Popen UnicodeDecodeError partial fix #779

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/rez/utils/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Popen(_PopenBase):
Allows for Popen to be used as a context in both py2 and py3.
"""
def __init__(self, args, **kwargs):

# Avoids python bug described here: https://bugs.python.org/issue3905.
# This can arise when apps (maya) install a non-standard stdin handler.
#
Expand All @@ -59,15 +58,36 @@ def __init__(self, args, **kwargs):
if file_no not in (0, 1, 2):
kwargs["stdin"] = subprocess.PIPE

self._set_text_encoding_kwargs(kwargs)
super(Popen, self).__init__(args, **kwargs)

def _set_text_encoding_kwargs(self, kwargs):
""" Adds support for py3 :py:obj:`subprocess.Popen`
keywords `text`, `encoding` for more consistent handling of stdout/stderr.

Args:
kwargs (dict): keyword arguments for :py:obj:`subprocess.Popen`
"""
# Add support for the new py3 "text" arg, which is equivalent to
# "universal_newlines".
# https://docs.python.org/3/library/subprocess.html#frequently-used-arguments
#
if "text" in kwargs:
kwargs["universal_newlines"] = True
del kwargs["text"]
text = kwargs.pop('text', None)
universal_newlines = kwargs.pop('universal_newlines', None)
if not any((text, universal_newlines)):
return
kwargs["universal_newlines"] = True

super(Popen, self).__init__(args, **kwargs)
# fixes py3/cmd.exe UnicodeDecodeError() with some characters.
# UnicodeDecodeError: 'charmap' codec can't decode byte
# 0x8d in position 1023172: character maps to <undefined>

# NOTE: currently no solution for `python3+<3.6`
#
if sys.version_info[:2] >= (3, 6):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do this only if encoding is not already in kwargs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh thank you! fixing now!

if 'encoding' in kwargs:
return
kwargs['encoding'] = 'utf-8'


class ExecutableScriptMode(Enum):
Expand Down