def encode_output(value, output_file):
if sys.version_info > (<NUM_LIT:3>,<NUM_LIT:0>):<EOL><INDENT>return compat.text_type(value)<EOL><DEDENT>else:<EOL><INDENT>stream_encoding = getattr(output_file, '<STR_LIT>', None)<EOL>if stream_encoding:<EOL><INDENT>if stream_encoding.upper() == '<STR_LIT>':<EOL><INDENT>return compat.text_type(value)<EOL><DEDENT>else:<EOL><INDENT>return value.encode(stream_encoding, '<STR_LIT:ignore>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if isinstance(value, compat.text_type):<EOL><INDENT>return value.encode('<STR_LIT:utf-8>')<EOL><DEDENT>else:<EOL><INDENT>return str(value)<EOL><DEDENT><DEDENT><DEDENT>
Encodes given value so it can be written to given file object.

Value may be Unicode, binary string or any other data type.

The exact behaviour depends on the Python version:

Python 3.x

    `sys.stdout` is a `_io.TextIOWrapper` instance that accepts `str`
    (unicode) and breaks on `bytes`.

    It is OK to simply assume that everything is Unicode unless special
    handling is introduced in the client code.

    Thus, no additional processing is performed.

Python 2.x

    `sys.stdout` is a file-like object that accepts `str` (bytes)
    and breaks when `unicode` is passed to `sys.stdout.write()`.

    We can expect both Unicode and bytes. They need to be encoded so as
    to match the file object encoding.

    The output is binary if the object doesn't explicitly require Unicode.