diff --git a/src/allmydata/scripts/tahoe_cp.py b/src/allmydata/scripts/tahoe_cp.py index 0a6c67942..fb86ff0ec 100644 --- a/src/allmydata/scripts/tahoe_cp.py +++ b/src/allmydata/scripts/tahoe_cp.py @@ -5,7 +5,8 @@ from past.builtins import unicode import os.path from urllib.parse import quote as url_quote from collections import defaultdict -from six.moves import cStringIO as StringIO +from io import BytesIO + from twisted.python.failure import Failure from allmydata.scripts.common import get_alias, escape_path, \ DefaultAliasMarker, TahoeError @@ -200,13 +201,21 @@ class TahoeFileSource(object): def open(self, caps_only): if caps_only: - return StringIO(self.readcap) + return BytesIO(self.readcap) url = self.nodeurl + "uri/" + url_quote(self.readcap) return GET_to_file(url) def bestcap(self): return self.writecap or self.readcap + +def seekable(file_like): + """Return whether the file-like object is seekable.""" + return hasattr(file_like, "seek") and ( + not hasattr(file_like, "seekable") or file_like.seekable() + ) + + class TahoeFileTarget(object): def __init__(self, nodeurl, mutable, writecap, readcap, url): self.nodeurl = nodeurl @@ -220,7 +229,7 @@ class TahoeFileTarget(object): assert self.url # our do_http() call currently requires a string or a filehandle with # a real .seek - if not hasattr(inf, "seek"): + if not seekable(inf): inf = inf.read() PUT(self.url, inf) # TODO: this always creates immutable files. We might want an option @@ -306,7 +315,7 @@ class TahoeMissingTarget(object): def put_file(self, inf): # We want to replace this object in-place. - if not hasattr(inf, "seek"): + if not seekable(inf): inf = inf.read() PUT(self.url, inf) # TODO: this always creates immutable files. We might want an option @@ -417,7 +426,7 @@ class TahoeDirectoryTarget(object): def put_file(self, name, inf): precondition(isinstance(name, unicode), name) url = self.nodeurl + "uri" - if not hasattr(inf, "seek"): + if not seekable(inf): inf = inf.read() if self.children is None: diff --git a/src/allmydata/test/test_system.py b/src/allmydata/test/test_system.py index 8ffbf7063..12ae846eb 100644 --- a/src/allmydata/test/test_system.py +++ b/src/allmydata/test/test_system.py @@ -2579,7 +2579,7 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase): (out, err) = out_and_err x = open(os.path.join(dn_copy2, "dir1", "subdir2", "rfile4")).read() y = uri.from_string_filenode(x) - self.failUnlessEqual(y.data, "rfile4") + self.failUnlessEqual(y.data, b"rfile4") d.addCallback(_check_capsonly) # and tahoe-to-tahoe