diff --git a/src/borg/archiver.py b/src/borg/archiver.py index 06d8d0085..f262e4eb2 100644 --- a/src/borg/archiver.py +++ b/src/borg/archiver.py @@ -738,6 +738,14 @@ def do_mount(self, args, repository, manifest, key): @with_repository() def do_list(self, args, repository, manifest, key): """List archive or repository contents""" + if not hasattr(sys.stdout, 'buffer'): + # This is a shim for supporting unit tests replacing sys.stdout with e.g. StringIO, + # which doesn't have an underlying buffer (= lower file object). + def write(bytestring): + sys.stdout.write(bytestring.decode('utf-8', errors='replace')) + else: + write = sys.stdout.buffer.write + if args.location.archive: matcher, _ = self.build_matcher(args.excludes, args.paths) with Cache(repository, key, manifest, lock_wait=self.lock_wait) as cache: @@ -751,13 +759,6 @@ def do_list(self, args, repository, manifest, key): format = "{mode} {user:6} {group:6} {size:8} {isomtime} {path}{extra}{NL}" formatter = ItemFormatter(archive, format) - if not hasattr(sys.stdout, 'buffer'): - # This is a shim for supporting unit tests replacing sys.stdout with e.g. StringIO, - # which doesn't have an underlying buffer (= lower file object). - def write(bytestring): - sys.stdout.write(bytestring.decode('utf-8', errors='replace')) - else: - write = sys.stdout.buffer.write for item in archive.iter_items(lambda item: matcher.match(item.path)): write(safe_encode(formatter.format_item(item))) else: