From bfef77a39683d698450d69151059bc15ac507c45 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 15 Feb 2021 14:38:11 -0500 Subject: [PATCH 1/6] Port runner to Python 3. --- src/allmydata/scripts/runner.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/allmydata/scripts/runner.py b/src/allmydata/scripts/runner.py index ada3c2dfc..9d5df478d 100644 --- a/src/allmydata/scripts/runner.py +++ b/src/allmydata/scripts/runner.py @@ -1,5 +1,11 @@ from __future__ import print_function +from __future__ import absolute_import +from __future__ import division +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 warnings import os, sys from six.moves import StringIO From 110e77b56031b111f48f1827826927dfc9b0f26f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 26 Mar 2021 09:48:46 -0400 Subject: [PATCH 2/6] Mark module as ported --- src/allmydata/util/_python3.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/allmydata/util/_python3.py b/src/allmydata/util/_python3.py index f26b77185..7b3226bc5 100644 --- a/src/allmydata/util/_python3.py +++ b/src/allmydata/util/_python3.py @@ -77,6 +77,7 @@ PORTED_MODULES = [ "allmydata.node", "allmydata.nodemaker", "allmydata.scripts.create_node", + "allmydata.scripts.runner", "allmydata.scripts.types_", "allmydata.stats", "allmydata.storage_client", From a6147b05b1d52a32e981521e6e3fa07e339ff394 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 26 Mar 2021 11:24:39 -0400 Subject: [PATCH 3/6] Fix test failure in test_unicode_arguments_and_output on Python 2. --- src/allmydata/scripts/runner.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/allmydata/scripts/runner.py b/src/allmydata/scripts/runner.py index 9d5df478d..dc83e2ff0 100644 --- a/src/allmydata/scripts/runner.py +++ b/src/allmydata/scripts/runner.py @@ -22,7 +22,7 @@ from twisted.internet import defer, task, threads from allmydata.scripts.common import get_default_nodedir from allmydata.scripts import debug, create_node, cli, \ admin, tahoe_run, tahoe_invite -from allmydata.util.encodingutil import quote_local_unicode_path +from allmydata.util.encodingutil import quote_local_unicode_path, argv_to_unicode from allmydata.util.eliotutil import ( opt_eliot_destination, opt_help_eliot_destinations, @@ -118,6 +118,22 @@ def parse_options(argv, config=None): config.parseOptions(argv) # may raise usage.error return config + +def _safety_encode(msg): + """ + Previously, on Python 2, argv was utf-8 encoded bytes and + Options.parseOptions would pass through those unicode + characters (such as in a UsageError unknown command). + + In a Unicode-preferred world, the argv is decoded to + Unicode early and that's what's passed through, but + print still needs an encoded value. + """ + if PY2: + return msg.encode('utf-8') + return msg + + def parse_or_exit_with_explanation(argv, stdout=sys.stdout): config = Options() try: @@ -127,7 +143,7 @@ def parse_or_exit_with_explanation(argv, stdout=sys.stdout): while hasattr(c, 'subOptions'): c = c.subOptions print(str(c), file=stdout) - print("%s: %s\n" % (sys.argv[0], e), file=stdout) + print(_safety_encode("%s: %s\n" % (sys.argv[0], e)), file=stdout) sys.exit(1) return config @@ -235,7 +251,8 @@ def _run_with_reactor(reactor): _setup_coverage(reactor) - d = defer.maybeDeferred(parse_or_exit_with_explanation, sys.argv[1:]) + argv = list(map(argv_to_unicode, sys.argv[1:])) + d = defer.maybeDeferred(parse_or_exit_with_explanation, argv) d.addCallback(_maybe_enable_eliot_logging, reactor) d.addCallback(dispatch) def _show_exception(f): From f8dddcd1d031b6e187e215a464a4118882bf8612 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 26 Mar 2021 11:27:53 -0400 Subject: [PATCH 4/6] Add newsfragment --- newsfragments/3603.minor.2 | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 newsfragments/3603.minor.2 diff --git a/newsfragments/3603.minor.2 b/newsfragments/3603.minor.2 new file mode 100644 index 000000000..e69de29bb From 43f1f115cbe8b288a83a6bfe4b47121a0ab565a8 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Tue, 30 Mar 2021 09:46:56 -0400 Subject: [PATCH 5/6] Simplify. --- src/allmydata/scripts/runner.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/allmydata/scripts/runner.py b/src/allmydata/scripts/runner.py index dc83e2ff0..7122e499e 100644 --- a/src/allmydata/scripts/runner.py +++ b/src/allmydata/scripts/runner.py @@ -119,21 +119,6 @@ def parse_options(argv, config=None): return config -def _safety_encode(msg): - """ - Previously, on Python 2, argv was utf-8 encoded bytes and - Options.parseOptions would pass through those unicode - characters (such as in a UsageError unknown command). - - In a Unicode-preferred world, the argv is decoded to - Unicode early and that's what's passed through, but - print still needs an encoded value. - """ - if PY2: - return msg.encode('utf-8') - return msg - - def parse_or_exit_with_explanation(argv, stdout=sys.stdout): config = Options() try: @@ -143,7 +128,10 @@ def parse_or_exit_with_explanation(argv, stdout=sys.stdout): while hasattr(c, 'subOptions'): c = c.subOptions print(str(c), file=stdout) - print(_safety_encode("%s: %s\n" % (sys.argv[0], e)), file=stdout) + # On Python 2 the string may turn into a unicode string, e.g. the error + # may be unicode, in which case it will print funny. Once we're on + # Python 3 we can just drop the ensure_str(). + print(six.ensure_str("%s: %s\n" % (sys.argv[0], e)), file=stdout) sys.exit(1) return config From cef53c40d12e27472e0f1f0e3e44ed302e327d92 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Wed, 31 Mar 2021 09:05:18 -0400 Subject: [PATCH 6/6] New ticket number. --- newsfragments/{3603.minor.2 => 3656.minor} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename newsfragments/{3603.minor.2 => 3656.minor} (100%) diff --git a/newsfragments/3603.minor.2 b/newsfragments/3656.minor similarity index 100% rename from newsfragments/3603.minor.2 rename to newsfragments/3656.minor