mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-04-08 19:34:18 +00:00
Merge pull request #1013 from tahoe-lafs/3647.more-cli-python-3
Port allmydata.test.cli.test_invite to Python 3 Fixes ticket:3647
This commit is contained in:
commit
8099929dec
0
newsfragments/3647.minor
Normal file
0
newsfragments/3647.minor
Normal file
@ -11,7 +11,6 @@ if PY2:
|
||||
|
||||
import io
|
||||
import os
|
||||
import json
|
||||
|
||||
try:
|
||||
from allmydata.scripts.types_ import (
|
||||
@ -36,7 +35,7 @@ from allmydata.scripts.common import (
|
||||
from allmydata.scripts.default_nodedir import _default_nodedir
|
||||
from allmydata.util.assertutil import precondition
|
||||
from allmydata.util.encodingutil import listdir_unicode, argv_to_unicode, quote_local_unicode_path, get_io_encoding
|
||||
from allmydata.util import fileutil, i2p_provider, iputil, tor_provider
|
||||
from allmydata.util import fileutil, i2p_provider, iputil, tor_provider, jsonbytes as json
|
||||
|
||||
from wormhole import wormhole
|
||||
|
||||
@ -393,7 +392,7 @@ def _get_config_via_wormhole(config):
|
||||
"client-v1": {},
|
||||
}
|
||||
}
|
||||
wh.send_message(json.dumps(intro))
|
||||
wh.send_message(json.dumps_bytes(intro))
|
||||
|
||||
server_intro = yield wh.get_message()
|
||||
server_intro = json.loads(server_intro)
|
||||
|
@ -1,7 +1,5 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import json
|
||||
|
||||
try:
|
||||
from allmydata.scripts.types_ import SubCommands
|
||||
except ImportError:
|
||||
@ -13,6 +11,7 @@ from twisted.internet import defer, reactor
|
||||
from wormhole import wormhole
|
||||
|
||||
from allmydata.util.encodingutil import argv_to_abspath
|
||||
from allmydata.util import jsonbytes as json
|
||||
from allmydata.scripts.common import get_default_nodedir, get_introducer_furl
|
||||
from allmydata.node import read_config
|
||||
|
||||
@ -54,7 +53,7 @@ def _send_config_via_wormhole(options, config):
|
||||
code = yield wh.get_code()
|
||||
print("Invite Code for client: {}".format(code), file=out)
|
||||
|
||||
wh.send_message(json.dumps({
|
||||
wh.send_message(json.dumps_bytes({
|
||||
u"abilities": {
|
||||
u"server-v1": {},
|
||||
}
|
||||
@ -71,7 +70,7 @@ def _send_config_via_wormhole(options, config):
|
||||
defer.returnValue(1)
|
||||
|
||||
print(" transmitting configuration", file=out)
|
||||
wh.send_message(json.dumps(config))
|
||||
wh.send_message(json.dumps_bytes(config))
|
||||
yield wh.close()
|
||||
|
||||
|
||||
|
@ -1,3 +1,15 @@
|
||||
"""
|
||||
Ported to Pythn 3.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from future.utils import PY2
|
||||
if PY2:
|
||||
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
|
||||
|
||||
import os
|
||||
import mock
|
||||
import json
|
||||
@ -16,6 +28,8 @@ class _FakeWormhole(object):
|
||||
|
||||
def __init__(self, outgoing_messages):
|
||||
self.messages = []
|
||||
for o in outgoing_messages:
|
||||
assert isinstance(o, bytes)
|
||||
self._outgoing = outgoing_messages
|
||||
|
||||
def get_code(self):
|
||||
@ -26,15 +40,16 @@ class _FakeWormhole(object):
|
||||
|
||||
def get_welcome(self):
|
||||
return defer.succeed(
|
||||
json.dumps({
|
||||
{
|
||||
u"welcome": {},
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
def allocate_code(self):
|
||||
return None
|
||||
|
||||
def send_message(self, msg):
|
||||
assert isinstance(msg, bytes)
|
||||
self.messages.append(msg)
|
||||
|
||||
def get_message(self):
|
||||
@ -45,6 +60,10 @@ class _FakeWormhole(object):
|
||||
|
||||
|
||||
def _create_fake_wormhole(outgoing_messages):
|
||||
outgoing_messages = [
|
||||
m.encode("utf-8") if isinstance(m, str) else m
|
||||
for m in outgoing_messages
|
||||
]
|
||||
return _FakeWormhole(outgoing_messages)
|
||||
|
||||
|
||||
|
@ -507,7 +507,6 @@ class JSONBytes(unittest.TestCase):
|
||||
self.assertEqual(json.loads(encoded), expected)
|
||||
self.assertEqual(jsonbytes.loads(encoded), expected)
|
||||
|
||||
|
||||
def test_encode_unicode(self):
|
||||
"""BytesJSONEncoder encodes Unicode string as usual."""
|
||||
expected = {
|
||||
@ -515,3 +514,10 @@ class JSONBytes(unittest.TestCase):
|
||||
}
|
||||
encoded = jsonbytes.dumps(expected)
|
||||
self.assertEqual(json.loads(encoded), expected)
|
||||
|
||||
def test_dumps_bytes(self):
|
||||
"""jsonbytes.dumps_bytes always returns bytes."""
|
||||
x = {u"def\N{SNOWMAN}\uFF00": 123}
|
||||
encoded = jsonbytes.dumps_bytes(x)
|
||||
self.assertIsInstance(encoded, bytes)
|
||||
self.assertEqual(json.loads(encoded, encoding="utf-8"), x)
|
||||
|
@ -146,6 +146,7 @@ PORTED_TEST_MODULES = [
|
||||
"allmydata.test.cli.test_alias",
|
||||
"allmydata.test.cli.test_backupdb",
|
||||
"allmydata.test.cli.test_create",
|
||||
"allmydata.test.cli.test_invite",
|
||||
"allmydata.test.cli.test_status",
|
||||
|
||||
"allmydata.test.mutable.test_checker",
|
||||
|
@ -9,7 +9,7 @@ from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
from future.utils import PY2
|
||||
from future.utils import PY2, PY3
|
||||
if PY2:
|
||||
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
|
||||
|
||||
@ -51,6 +51,14 @@ def dumps(obj, *args, **kwargs):
|
||||
return json.dumps(obj, cls=BytesJSONEncoder, *args, **kwargs)
|
||||
|
||||
|
||||
def dumps_bytes(obj, *args, **kwargs):
|
||||
"""Encode to JSON, then encode as bytes."""
|
||||
result = dumps(obj, *args, **kwargs)
|
||||
if PY3:
|
||||
result = result.encode("utf-8")
|
||||
return result
|
||||
|
||||
|
||||
# To make this module drop-in compatible with json module:
|
||||
loads = json.loads
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user