From patchwork Wed Mar 4 05:59:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: D8209: phabricator: avoid a stacktrace when command arguments are missing From: phabricator X-Patchwork-Id: 45444 Message-Id: To: Phabricator Cc: mercurial-devel@mercurial-scm.org Date: Wed, 4 Mar 2020 05:59:04 +0000 mharbison72 created this revision. Herald added subscribers: mercurial-devel, Kwan. Herald added a reviewer: hg-reviewers. REVISION SUMMARY Previously, the TypeError wasn't properly converted to a SignatureError when improper arguments were supplied to the inner function, because the stack depth is 2 inside the vcrcommand decorator. The `__name__` and `__doc__` attributes need to be reassigned to the new wrapper so that the help summary is available. REPOSITORY rHG Mercurial BRANCH default REVISION DETAIL https://phab.mercurial-scm.org/D8209 AFFECTED FILES hgext/phabricator.py mercurial/util.py tests/test-phabricator.t CHANGE DETAILS To: mharbison72, #hg-reviewers Cc: Kwan, mercurial-devel diff --git a/tests/test-phabricator.t b/tests/test-phabricator.t --- a/tests/test-phabricator.t +++ b/tests/test-phabricator.t @@ -29,6 +29,21 @@ > --test-vcr "$VCR/phabread-conduit-error.json" D4480 | head abort: Conduit Error (ERR-INVALID-AUTH): API token "cli-notavalidtoken" has the wrong length. API tokens should be 32 characters long. +Missing arguments print the command help + + $ hg phabread + hg phabread: invalid arguments + hg phabread DREVSPEC [OPTIONS] + + print patches from Phabricator suitable for importing + + options: + + --stack read dependencies + + (use 'hg phabread -h' to show more help) + [255] + Basic phabread: $ hg phabread --test-vcr "$VCR/phabread-4480.json" D4480 | head # HG changeset patch diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -1846,14 +1846,14 @@ return pycompat.ossep.join(([b'..'] * len(a)) + b) or b'.' -def checksignature(func): +def checksignature(func, depth=1): '''wrap a function with code to check for calling errors''' def check(*args, **kwargs): try: return func(*args, **kwargs) except TypeError: - if len(traceback.extract_tb(sys.exc_info()[2])) == 1: + if len(traceback.extract_tb(sys.exc_info()[2])) == depth: raise error.SignatureError raise diff --git a/hgext/phabricator.py b/hgext/phabricator.py --- a/hgext/phabricator.py +++ b/hgext/phabricator.py @@ -257,15 +257,17 @@ return fn(*args, **kwargs) return fn(*args, **kwargs) - inner.__name__ = fn.__name__ - inner.__doc__ = fn.__doc__ + cmd = util.checksignature(inner, depth=2) + cmd.__name__ = fn.__name__ + cmd.__doc__ = fn.__doc__ + return command( name, fullflags, spec, helpcategory=helpcategory, optionalrepo=optionalrepo, - )(inner) + )(cmd) return decorate