def replace_stdout():
 stdout = sys.stdout
 sys.stdout = open(stdout.fileno(), 'w', encoding=stdout.encoding, errors='backslashreplace', closefd=False, newline='\n')
 def restore_stdout():
  sys.stdout.close()
  sys.stdout = stdout
 atexit.register(restore_stdout)

def replace_stdout ( ) : stdout = sys . stdout sys . stdout = open ( stdout . fileno ( ) , 'w' , encoding = stdout . encoding , errors = 'backslashreplace' , closefd = False , newline = '\n' ) def restore_stdout ( ) : sys . stdout . close ( ) sys . stdout = stdout atexit . register ( restore_stdout )
set stdout encoder error handler to backslashreplace to avoid unicodeencodeerror when printing a traceback .