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): def init_control(self):
c = ControlServer() c = ControlServer()
c.setServiceParent(self) c.setServiceParent(self)
control_url = self.control_tub.registerReference(c).encode("utf-8") control_url = self.control_tub.registerReference(c)
self.config.write_private_config("control.furl", control_url + b"\n") self.config.write_private_config("control.furl", control_url + "\n")
def init_helper(self): def init_helper(self):
self.helper = Helper(self.config.get_config_path("helper"), 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) privname = os.path.join(self._basedir, "private", name)
try: try:
value = fileutil.read(privname) value = fileutil.read(privname, mode="r")
except EnvironmentError as e: except EnvironmentError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise # we only care about "file doesn't exist" raise # we only care about "file doesn't exist"
if default is _None: if default is _None:
raise MissingConfigEntry("The required configuration file %s is missing." raise MissingConfigEntry("The required configuration file %s is missing."
% (quote_output(privname),)) % (quote_output(privname),))
if isinstance(default, (bytes, unicode)): if isinstance(default, bytes):
default = unicode(default, "utf-8")
if isinstance(default, unicode):
value = default value = default
else: else:
value = default() value = default()
@ -379,19 +381,21 @@ class _Config(object):
config file that resides within the subdirectory named 'private'), and config file that resides within the subdirectory named 'private'), and
return it. return it.
""" """
if isinstance(value, unicode):
value = value.encode("utf-8")
privname = os.path.join(self._basedir, "private", name) privname = os.path.join(self._basedir, "private", name)
with open(privname, "wb") as f: with open(privname, "wb") as f:
f.write(value) f.write(value)
def get_private_config(self, name, default=_None): 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'), config file that resides within the subdirectory named 'private'),
and return it. Return a default, or raise an error if one was not and return it. Return a default, or raise an error if one was not
given. given.
""" """
privname = os.path.join(self._basedir, "private", name) privname = os.path.join(self._basedir, "private", name)
try: try:
return fileutil.read(privname).strip() return fileutil.read(privname, mode="r").strip()
except EnvironmentError as e: except EnvironmentError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise # we only care about "file doesn't exist" 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() port_or_endpoint = tor_provider.get_listener()
else: else:
port_or_endpoint = port 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.listenOn(port_or_endpoint)
tub.setLocation(location) tub.setLocation(location)
log.msg("Tub location set to %s" % (location,)) log.msg("Tub location set to %s" % (location,))

View File

@ -1,3 +1,5 @@
from past.builtins import unicode
import base64 import base64
import os import os
import stat import stat
@ -145,7 +147,7 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
def test_tahoe_cfg_utf8(self): def test_tahoe_cfg_utf8(self):
basedir = "test_node/test_tahoe_cfg_utf8" basedir = "test_node/test_tahoe_cfg_utf8"
fileutil.make_dirs(basedir) 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"\uFEFF[node]\n".encode('utf-8'))
f.write(u"nickname = \u2621\n".encode('utf-8')) f.write(u"nickname = \u2621\n".encode('utf-8'))
f.close() f.close()
@ -333,7 +335,7 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
ns.called = False ns.called = False
def call_setLogDir(logdir): def call_setLogDir(logdir):
ns.called = True ns.called = True
self.failUnless(isinstance(logdir, str), logdir) self.failUnless(isinstance(logdir, unicode), logdir)
self.patch(foolscap.logging.log, 'setLogDir', call_setLogDir) self.patch(foolscap.logging.log, 'setLogDir', call_setLogDir)
create_node_dir(basedir, "nothing to see here") 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) move_into_place(target+".tmp", target)
def write(path, data, mode="wb"): 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: with open(path, mode) as f:
f.write(data) f.write(data)
def read(path): def read(path, mode="rb"):
with open(path, "rb") as rf: with open(path, mode) as rf:
return rf.read() return rf.read()
def put_file(path, inf): def put_file(path, inf):