# Buggy code
def log_to_term(self, message, verbosity):
    """Write message to stdout/stderr"""
    if verbosity <= 2 or Globals.server:
        termfp = sys.stderr
    else:
        termfp = sys.stdout
    tmpstr = self.format(message, self.term_verbosity)
    if type(tmpstr) != str:  # transform bytes in string
        tmpstr = str(tmpstr, "utf-8")
    termfp.write(tmpstr)


# Traceback (UnicodeEncodeError)
2020-05-11 21:04:14.547611 +0000  <CLIENT-286>  Using rdiff-backup version 2.0.1rc0
2020-05-11 21:04:14.547685 +0000  <CLIENT-286>  	with cpython /usr/bin/python3 version 3.6.8
2020-05-11 21:04:14.550694 +0000  <CLIENT-286>  	on Linux-4.19.37-x86_64-with-centos-7.8.2003-Core, fs encoding ascii
2020-05-11 21:04:14.552330 +0000  <CLIENT-286>  Using mirror root directory /testcases
2020-05-11 21:04:14.552817 +0000  <CLIENT-286>  Unable to import win32security module. Windows ACLs
not supported by filesystem at /testcases/rdiff-backup-data
2020-05-11 21:04:14.553562 +0000  <CLIENT-286>  -----------------------------------------------------------------
Detected abilities for rdiff-backup repository (read only) file system:
Access control lists                         On
Extended attributes                          On
Windows access control lists                 Off
Case sensitivity                             On
Escape DOS devices                           Off
Escape trailing spaces                       Off
Mac OS X style resource forks                Off
Mac OS X Finder information                  Off
-----------------------------------------------------------------
2020-05-11 21:04:14.553638 +0000  <CLIENT-286>  Making directory /tmp/coucou/rdiff-backup.tmp.0
2020-05-11 21:04:14.553802 +0000  <CLIENT-286>  Touching /tmp/coucou/rdiff-backup.tmp.0/5-_ a.snapshot.gz
2020-05-11 21:04:14.553948 +0000  <CLIENT-286>  Deleting /tmp/coucou/rdiff-backup.tmp.0/5-_ a.snapshot.gz
2020-05-11 21:04:14.554872 +0000  <CLIENT-286>  Exception ''ascii' codec can't encode characters in position 91-93: ordinal not in range(128)' raised of class '<class 'UnicodeEncodeError'>':
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 371, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 697, in Restore
dest_rp.conn.fs_abilities.restore_set_globals(dest_rp)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 1072, in restore_set_globals
dest_fsa = FSAbilities('restore target').init_readwrite(rpout)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 157, in init_readwrite
self.set_extended_filenames(subdir)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 241, in set_extended_filenames
ext_rp.touch()
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/rpath.py", line 1248, in touch
log.Log("Touching %s" % self.get_safepath(), 7)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 150, in __call__
self.log_to_term(message, verbosity)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 173, in log_to_term
termfp.write(tmpstr)

Traceback (most recent call last):
File "/usr/local/bin/rdiff-backup", line 32, in <module>
rdiff_backup.Main.error_check_Main(sys.argv[1:])
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 371, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 697, in Restore
dest_rp.conn.fs_abilities.restore_set_globals(dest_rp)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 1072, in restore_set_globals
dest_fsa = FSAbilities('restore target').init_readwrite(rpout)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 157, in init_readwrite
self.set_extended_filenames(subdir)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 241, in set_extended_filenames
ext_rp.touch()
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/rpath.py", line 1248, in touch
log.Log("Touching %s" % self.get_safepath(), 7)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 150, in __call__
self.log_to_term(message, verbosity)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 173, in log_to_term
termfp.write(tmpstr)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 91-93: ordinal not in range(128)

# Fixed code
def log_to_term(self, message, verbosity):
    """Write message to stdout/stderr"""
    if verbosity <= 2 or Globals.server:
        termfp = sys.stderr.buffer
    else:
        termfp = sys.stdout.buffer
    tmpstr = self.format(message, self.term_verbosity)
    if type(tmpstr) == str:  # transform string in bytes
        tmpstr = tmpstr.encode(sys.stdout.encoding, "backslashreplace")
    termfp.write(tmpstr)