# Buggy code
def display(self, msg, color=None, stderr=False, screen_only=False, log_only=False):
    """Display a message to the user

    Note: msg *must* be a unicode string to prevent UnicodeError tracebacks.
    """

    nocolor = msg
    if color:
        msg = stringc(msg, color)

    if not log_only:
        if not msg.endswith("\n"):
            msg2 = msg + "\n"
        else:
            msg2 = msg

        msg2 = to_bytes(msg2, encoding=self._output_encoding(stderr=stderr))
        if sys.version_info >= (3,):
            # Convert back to text string on python3
            # We first convert to a byte string so that we get rid of
            # characters that are invalid in the user's locale
            msg2 = to_text(msg2, self._output_encoding(stderr=stderr), errors="replace")

        if not stderr:
            fileobj = sys.stdout
        else:
            fileobj = sys.stderr

        fileobj.write(msg2)

        try:
            fileobj.flush()
        except IOError as e:
            # Ignore EPIPE in case fileobj has been prematurely closed, eg.
            # when piping to "head -n1"
            if e.errno != errno.EPIPE:
                raise

    if logger and not screen_only:
        msg2 = nocolor.lstrip("\n")

        msg2 = to_bytes(msg2)
        if sys.version_info >= (3,):
            # Convert back to text string on python3
            # We first convert to a byte string so that we get rid of
            # characters that are invalid in the user's locale
            msg2 = to_text(msg2, self._output_encoding(stderr=stderr))

        if color == C.COLOR_ERROR:
            logger.error(msg2)
        else:
            logger.info(msg2)


# Traceback (ValueError)
The full traceback is:
Traceback (most recent call last):
File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/executor/task_executor.py", line 119, in run
res = self._execute()
File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/executor/task_executor.py", line 523, in _execute
result = self._handler.run(task_vars=variables)
File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/plugins/action/junos.py", line 73, in run
socket_path = connection.run()
File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/plugins/connection/persistent.py", line 68, in run
socket_path = self._start_connection()
File "/Users/gnalawad/ansible/ganeshrn/ansible/lib/ansible/plugins/connection/persistent.py", line 96, in _start_connection
result = json.loads(to_text(stdout, errors='surrogate_then_replace'))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 369, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 8 - line 4 column 121 (char 7 - 405)

fatal: [38.145.35.195]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}

# Fixed code
def display(self, msg, color=None, stderr=False, screen_only=False, log_only=False):
    """Display a message to the user

    Note: msg *must* be a unicode string to prevent UnicodeError tracebacks.
    """

    nocolor = msg
    if color:
        msg = stringc(msg, color)

    if not log_only:
        if not msg.endswith("\n"):
            msg2 = msg + "\n"
        else:
            msg2 = msg

        msg2 = to_bytes(msg2, encoding=self._output_encoding(stderr=stderr))
        if sys.version_info >= (3,):
            # Convert back to text string on python3
            # We first convert to a byte string so that we get rid of
            # characters that are invalid in the user's locale
            msg2 = to_text(msg2, self._output_encoding(stderr=stderr), errors="replace")

        # Note: After Display() class is refactored need to update the log capture
        # code in 'bin/ansible-connection' (and other relevant places).
        if not stderr:
            fileobj = sys.stdout
        else:
            fileobj = sys.stderr

        fileobj.write(msg2)

        try:
            fileobj.flush()
        except IOError as e:
            # Ignore EPIPE in case fileobj has been prematurely closed, eg.
            # when piping to "head -n1"
            if e.errno != errno.EPIPE:
                raise

    if logger and not screen_only:
        msg2 = nocolor.lstrip("\n")

        msg2 = to_bytes(msg2)
        if sys.version_info >= (3,):
            # Convert back to text string on python3
            # We first convert to a byte string so that we get rid of
            # characters that are invalid in the user's locale
            msg2 = to_text(msg2, self._output_encoding(stderr=stderr))

        if color == C.COLOR_ERROR:
            logger.error(msg2)
        else:
            logger.info(msg2)