
from __future__ import print_function
import sys

try:
    import __builtin__
except ImportError:
    # Python 3
    import builtins as __builtin__


def print(*args, **kwargs):
    conv_str = ()
    for s in args:
        conv_str = s.encode('utf-8').decode(sys.stdout.encoding, 'ignore')
    return __builtin__.print(conv_str, **kwargs)


from __future__ import print_function
from sys import stdout

try:
    import __builtin__
except ImportError:
    # Python 3
    import builtins as __builtin__


def print(*args, **kwargs):
    ret_str = ()
    # when stdout is redirected to a file, python 2 uses ascii for the writer;
    # python 3 uses what is configured in the system (i.e. 'utf-8')
    str_encoding = stdout.encoding if stdout.encoding is not None else 'ascii'
    for s in args:
        ret_str = s.encode('utf-8').decode(str_encoding, 'ignore')

    return __builtin__.print(ret_str, **kwargs)

Fix out redirection in python2
Fix out redirection in python2

  
  from __future__ import print_function
- import sys
+ from sys import stdout
  
  try:
      import __builtin__
  except ImportError:
      # Python 3
      import builtins as __builtin__
  
  
  def print(*args, **kwargs):
-     conv_str = ()
+     ret_str = ()
+     # when stdout is redirected to a file, python 2 uses ascii for the writer;
+     # python 3 uses what is configured in the system (i.e. 'utf-8')
+     str_encoding = stdout.encoding if stdout.encoding is not None else 'ascii'
      for s in args:
-         conv_str = s.encode('utf-8').decode(sys.stdout.encoding, 'ignore')
+         ret_str = s.encode('utf-8').decode(str_encoding, 'ignore')
-     return __builtin__.print(conv_str, **kwargs)
  
+     return __builtin__.print(ret_str, **kwargs)
+ 
Fix out redirection in python2
## Code Before:

from __future__ import print_function
import sys

try:
    import __builtin__
except ImportError:
    # Python 3
    import builtins as __builtin__


def print(*args, **kwargs):
    conv_str = ()
    for s in args:
        conv_str = s.encode('utf-8').decode(sys.stdout.encoding, 'ignore')
    return __builtin__.print(conv_str, **kwargs)

## Instruction:
Fix out redirection in python2
## Code After:

from __future__ import print_function
from sys import stdout

try:
    import __builtin__
except ImportError:
    # Python 3
    import builtins as __builtin__


def print(*args, **kwargs):
    ret_str = ()
    # when stdout is redirected to a file, python 2 uses ascii for the writer;
    # python 3 uses what is configured in the system (i.e. 'utf-8')
    str_encoding = stdout.encoding if stdout.encoding is not None else 'ascii'
    for s in args:
        ret_str = s.encode('utf-8').decode(str_encoding, 'ignore')

    return __builtin__.print(ret_str, **kwargs)
