More progress towards passing tests on Python 3.

This commit is contained in:
Itamar Turner-Trauring 2020-10-16 11:13:11 -04:00
parent f689d59a40
commit 51d472e221
4 changed files with 21 additions and 10 deletions

View File

@ -1020,8 +1020,8 @@ class _Client(node.Node, pollmixin.PollMixin):
def init_control(self):
c = ControlServer()
c.setServiceParent(self)
control_url = self.control_tub.registerReference(c).encode("utf-8")
self.config.write_private_config("control.furl", control_url + b"\n")
control_url = self.control_tub.registerReference(c)
self.config.write_private_config("control.furl", control_url + "\n")
def init_helper(self):
self.helper = Helper(self.config.get_config_path("helper"),

View File

@ -360,14 +360,16 @@ class _Config(object):
"""
privname = os.path.join(self._basedir, "private", name)
try:
value = fileutil.read(privname)
value = fileutil.read(privname, mode="r")
except EnvironmentError as e:
if e.errno != errno.ENOENT:
raise # we only care about "file doesn't exist"
if default is _None:
raise MissingConfigEntry("The required configuration file %s is missing."
% (quote_output(privname),))
if isinstance(default, (bytes, unicode)):
if isinstance(default, bytes):
default = unicode(default, "utf-8")
if isinstance(default, unicode):
value = default
else:
value = default()
@ -379,19 +381,21 @@ class _Config(object):
config file that resides within the subdirectory named 'private'), and
return it.
"""
if isinstance(value, unicode):
value = value.encode("utf-8")
privname = os.path.join(self._basedir, "private", name)
with open(privname, "wb") as f:
f.write(value)
def get_private_config(self, name, default=_None):
"""Read the (string) contents of a private config file (which is a
"""Read the (native string) contents of a private config file (a
config file that resides within the subdirectory named 'private'),
and return it. Return a default, or raise an error if one was not
given.
"""
privname = os.path.join(self._basedir, "private", name)
try:
return fileutil.read(privname).strip()
return fileutil.read(privname, mode="r").strip()
except EnvironmentError as e:
if e.errno != errno.ENOENT:
raise # we only care about "file doesn't exist"
@ -689,6 +693,9 @@ def create_main_tub(config, tub_options,
port_or_endpoint = tor_provider.get_listener()
else:
port_or_endpoint = port
if PY2 and isinstance(port_or_endpoint, unicode):
# Foolscap requires native string
port_or_endpoint = port_or_endpoint.encode("utf-8")
tub.listenOn(port_or_endpoint)
tub.setLocation(location)
log.msg("Tub location set to %s" % (location,))

View File

@ -1,3 +1,5 @@
from past.builtins import unicode
import base64
import os
import stat
@ -145,7 +147,7 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
def test_tahoe_cfg_utf8(self):
basedir = "test_node/test_tahoe_cfg_utf8"
fileutil.make_dirs(basedir)
f = open(os.path.join(basedir, 'tahoe.cfg'), 'wt')
f = open(os.path.join(basedir, 'tahoe.cfg'), 'wb')
f.write(u"\uFEFF[node]\n".encode('utf-8'))
f.write(u"nickname = \u2621\n".encode('utf-8'))
f.close()
@ -333,7 +335,7 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
ns.called = False
def call_setLogDir(logdir):
ns.called = True
self.failUnless(isinstance(logdir, str), logdir)
self.failUnless(isinstance(logdir, unicode), logdir)
self.patch(foolscap.logging.log, 'setLogDir', call_setLogDir)
create_node_dir(basedir, "nothing to see here")

View File

@ -271,11 +271,13 @@ def write_atomically(target, contents, mode="b"):
move_into_place(target+".tmp", target)
def write(path, data, mode="wb"):
if "b" in mode and isinstance(data, str):
data = data.encode("utf-8")
with open(path, mode) as f:
f.write(data)
def read(path):
with open(path, "rb") as rf:
def read(path, mode="rb"):
with open(path, mode) as rf:
return rf.read()
def put_file(path, inf):