# Buggy 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)


# Traceback (TypeError)
UpdateError: 'data/some/sub/dir/changed-file/rdiff-backup.tmp.266' does not match source
Exception 'a bytes-like object is required, not 'str'' raised of class '<class 'TypeError'>':
File "/usr/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 390, in error_check_Main
Main(arglist)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 412, in Main
take_action(rps)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 348, in take_action
Backup(rps[0], rps[1])
File "/usr/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 434, in Backup
backup.Mirror_and_increment(rpin, rpout, incdir)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 55, in Mirror_and_increment
DestS.patch_and_increment(dest_rpath, source_diffiter, inc_rpath)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 281, in patch_and_increment
ITR(diff.index, diff)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/rorpiter.py", line 313, in __call__
last_branch.fast_process(*args)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 781, in fast_process
if self.patch_to_temp(mirror_rp, diff_rorp, tf):
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 637, in patch_to_temp
return self.matches_cached_rorp(diff_rorp, new)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 691, in matches_cached_rorp
"temp file '%s' does not match source" % new_rp.get_safepath())
File "/usr/lib64/python3.6/site-packages/rdiff_backup/log.py", line 313, in write_if_open
cls.write(error_type, rp, exc)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/log.py", line 293, in write
cls._log_fileobj.write(logstr)

Traceback (most recent call last):
File "/usr/bin/rdiff-backup", line 32, in <module>
rdiff_backup.Main.error_check_Main(sys.argv[1:])
File "/usr/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 390, in error_check_Main
Main(arglist)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 412, in Main
take_action(rps)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 348, in take_action
Backup(rps[0], rps[1])
File "/usr/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 434, in Backup
backup.Mirror_and_increment(rpin, rpout, incdir)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 55, in Mirror_and_increment
DestS.patch_and_increment(dest_rpath, source_diffiter, inc_rpath)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 281, in patch_and_increment
ITR(diff.index, diff)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/rorpiter.py", line 313, in __call__
last_branch.fast_process(*args)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 781, in fast_process
if self.patch_to_temp(mirror_rp, diff_rorp, tf):
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 637, in patch_to_temp
return self.matches_cached_rorp(diff_rorp, new)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/backup.py", line 691, in matches_cached_rorp
"temp file '%s' does not match source" % new_rp.get_safepath())
File "/usr/lib64/python3.6/site-packages/rdiff_backup/log.py", line 313, in write_if_open
cls.write(error_type, rp, exc)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/log.py", line 293, in write
cls._log_fileobj.write(logstr)
TypeError: a bytes-like object is required, not 'str'

# 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)
    termfp.write(_to_bytes(tmpstr, encoding=sys.stdout.encoding))