diff --git a/tensorboard/BUILD b/tensorboard/BUILD index 23441f3ae50..5c7c2b59874 100644 --- a/tensorboard/BUILD +++ b/tensorboard/BUILD @@ -214,7 +214,6 @@ py_library( "//tensorboard/backend/event_processing:data_ingester", "//tensorboard/backend/event_processing:event_file_inspector", "//tensorboard/data:server_ingester", - "//tensorboard/util:argparse_util", "@org_pocoo_werkzeug", "@org_pythonhosted_six", ], diff --git a/tensorboard/program.py b/tensorboard/program.py index 7e2c0c4b658..e8e0636a98f 100644 --- a/tensorboard/program.py +++ b/tensorboard/program.py @@ -60,7 +60,6 @@ from tensorboard.backend.event_processing import event_file_inspector as efi from tensorboard.data import server_ingester from tensorboard.plugins.core import core_plugin -from tensorboard.util import argparse_util from tensorboard.util import tb_logging @@ -230,10 +229,7 @@ def configure(self, argv=("",), **kwargs): arg0 = argv[0] if argv else "" - with argparse_util.allow_missing_subcommand(): - flags = base_parser.parse_args( - argv[1:] - ) # Strip binary name from argv. + flags = base_parser.parse_args(argv[1:]) # Strip binary name from argv. if getattr(flags, _SUBCOMMAND_FLAG, None) is None: # Manually assign default value rather than using `set_defaults` # on the base parser to work around Python bug #9351 on old diff --git a/tensorboard/util/BUILD b/tensorboard/util/BUILD index f45257e9b84..aac98ec6224 100644 --- a/tensorboard/util/BUILD +++ b/tensorboard/util/BUILD @@ -4,23 +4,6 @@ package(default_visibility = ["//tensorboard:internal"]) licenses(["notice"]) -py_library( - name = "argparse_util", - srcs = ["argparse_util.py"], - srcs_version = "PY3", -) - -py_test( - name = "argparse_util_test", - size = "small", - srcs = ["argparse_util_test.py"], - srcs_version = "PY3", - deps = [ - ":argparse_util", - "//tensorboard:test", - ], -) - py_library( name = "encoder", srcs = ["encoder.py"], diff --git a/tensorboard/util/argparse_util.py b/tensorboard/util/argparse_util.py deleted file mode 100644 index 5bb26e84c13..00000000000 --- a/tensorboard/util/argparse_util.py +++ /dev/null @@ -1,65 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utilities for working with `argparse` in a portable way.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import contextlib -import gettext - - -@contextlib.contextmanager -def allow_missing_subcommand(): - """Make Python 2.7 behave like Python 3 w.r.t. default subcommands. - - The behavior of argparse was changed [1] [2] in Python 3.3. When a - parser defines subcommands, it used to be an error for the user to - invoke the binary without specifying a subcommand. As of Python 3.3, - this is permitted. This monkey patch backports the new behavior to - earlier versions of Python. - - This context manager need only be used around `parse_args`; parsers - may be constructed and configured outside of the context manager. - - [1]: https://github.com/python/cpython/commit/f97c59aaba2d93e48cbc6d25f7ff9f9c87f8d0b2 - [2]: https://bugs.python.org/issue16308 - """ - - real_error = argparse.ArgumentParser.error - - # This must exactly match the error message raised by Python 2.7's - # `argparse` when no subparser is given. This is `argparse.py:1954` at - # Git tag `v2.7.16`. - ignored_message = gettext.gettext("too few arguments") - - def error(*args, **kwargs): - # Expected signature is `error(self, message)`, but we retain more - # flexibility to be forward-compatible with implementation changes. - if "message" not in kwargs and len(args) < 2: - return real_error(*args, **kwargs) - message = kwargs["message"] if "message" in kwargs else args[1] - if message == ignored_message: - return None - else: - return real_error(*args, **kwargs) - - argparse.ArgumentParser.error = error - try: - yield - finally: - argparse.ArgumentParser.error = real_error diff --git a/tensorboard/util/argparse_util_test.py b/tensorboard/util/argparse_util_test.py deleted file mode 100644 index 7279da19181..00000000000 --- a/tensorboard/util/argparse_util_test.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for `argparse_util`.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse - -from tensorboard import test as tb_test -from tensorboard.util import argparse_util - - -class AllowMissingSubcommandTest(tb_test.TestCase): - def test_allows_missing_subcommands(self): - parser = argparse.ArgumentParser() - subparsers = parser.add_subparsers() - subparser = subparsers.add_parser("magic") - subparser.set_defaults(chosen="magic") - with argparse_util.allow_missing_subcommand(): - args = parser.parse_args([]) - self.assertEqual(args, argparse.Namespace()) - - def test_allows_provided_subcommands(self): - parser = argparse.ArgumentParser() - subparsers = parser.add_subparsers() - subparser = subparsers.add_parser("magic") - subparser.set_defaults(chosen="magic") - with argparse_util.allow_missing_subcommand(): - args = parser.parse_args(["magic"]) - self.assertEqual(args, argparse.Namespace(chosen="magic")) - - def test_still_complains_on_missing_arguments(self): - parser = argparse.ArgumentParser() - parser.add_argument("please_provide_me") - with argparse_util.allow_missing_subcommand(): - with self.assertRaises(SystemExit): - parser.parse_args([]) - - -if __name__ == "__main__": - tb_test.main()