From c7e947030083a2873de6838ca85168ad46346727 Mon Sep 17 00:00:00 2001 From: "Fabien C. Y. Benureau" Date: Sun, 1 Apr 2018 19:30:13 +0900 Subject: [PATCH] implement requested changes for #133 --- pubs/commands/remove_cmd.py | 1 - pubs/p3.py | 6 ++---- pubs/uis.py | 19 ++++++++++--------- tests/test_usecase.py | 12 ++++++++---- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/pubs/commands/remove_cmd.py b/pubs/commands/remove_cmd.py index 6d843d1..ed8e23c 100644 --- a/pubs/commands/remove_cmd.py +++ b/pubs/commands/remove_cmd.py @@ -23,7 +23,6 @@ def command(conf, args): ui = get_ui() force = args.force rp = repo.Repository(conf) - print(type(args.citekeys[0]), args.citekeys[0]) keys = resolve_citekey_list(repo=rp, citekeys=args.citekeys, ui=ui, exit_on_fail=True) diff --git a/pubs/p3.py b/pubs/p3.py index 4b43a46..5e10e02 100644 --- a/pubs/p3.py +++ b/pubs/p3.py @@ -2,8 +2,7 @@ import io import sys import argparse -from six import b, u - +from six import b if sys.version_info[0] == 2: @@ -56,7 +55,7 @@ if sys.version_info[0] == 2: def to_utf8(s): return b(s) - # for details, seehttp://bugs.python.org/issue9779 + # for details, see http://bugs.python.org/issue9779 class ArgumentParser(argparse.ArgumentParser): def _print_message(self, message, file=None): """Fixes the lack of a buffer interface in unicode object """ @@ -66,7 +65,6 @@ if sys.version_info[0] == 2: file.write(message.encode('utf-8')) - else: ustr = str uchr = chr diff --git a/pubs/uis.py b/pubs/uis.py index 21b323f..c2a8bfc 100644 --- a/pubs/uis.py +++ b/pubs/uis.py @@ -14,7 +14,8 @@ from .p3 import _get_raw_stdout, _get_raw_stderr, input, ustr from .content import check_file, read_text_file, write_file, system_path -DEBUG = False +DEBUG = False # unhandled exceptions traces are printed +DEBUG_ALL_TRACES = False # handled exceptions traces are printed # package-shared ui that can be accessed using : # from uis import get_ui # ui = get_ui() @@ -109,10 +110,10 @@ class PrintUI(object): def error(self, message, **kwargs): kwargs['file'] = self._stderr print('{}: {}'.format(color.dye_err('error', 'error'), message), **kwargs) - # if an exception has been raised and debug is on, raise it. - if DEBUG or self.debug: + + if DEBUG_ALL_TRACES: # if an exception has been raised, print the trace. if sys.exc_info()[0] is not None: - raise + traceback.print_exception(*sys.exc_info) def exit(self, error_code=1): sys.exit(error_code) @@ -122,12 +123,12 @@ class PrintUI(object): :returns: True if exception has been handled (currently never happens) """ - if (not DEBUG) and (not self.debug): - self.error(ustr(exc)) - self.exit() self.error(ustr(exc)) - return False - + if DEBUG or self.debug: + raise + else: + self.exit() + return True # never happens class InputUI(PrintUI): """UI class. Stores configuration parameters and system information. diff --git a/tests/test_usecase.py b/tests/test_usecase.py index e408371..a59a773 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -116,10 +116,9 @@ class CommandTestCase(fake_env.TestFakeFs): input.as_global() try: if capture_output: - actual_cmd.split() - _, stdout, stderr = fake_env.capture(pubs_cmd.execute, - verbose=PRINT_OUTPUT)( - actual_cmd.split()) + capture_wrap = fake_env.capture(pubs_cmd.execute, + verbose=PRINT_OUTPUT) + _, stdout, stderr = capture_wrap(actual_cmd.split()) actual_out = color.undye(stdout) actual_err = color.undye(stderr) if expected_out is not None: @@ -202,6 +201,11 @@ class TestAlone(CommandTestCase): self.execute_cmds(['pubs']) self.assertEqual(cm.exception.code, 2) +<<<<<<< HEAD +======= + + @unittest.skipIf(sys.version_info.major == 2, "not supported for Python2") +>>>>>>> implement requested changes for #133 def test_alone_prints_help(self): # capturing the output of `pubs --help` is difficult because argparse # raises as SystemExit(0) after calling `print_help`, and this gets