Can't catch GitCommandError
zerothi opened this issue · comments
I am trying to do a cherry-pick, but abort when it fails:
import git
...
git = repo.git
try:
git.cherry_pick(commit)
except git.GitCommandError as e:
git.cherry_pick(abort=True)
...
but I get this:
except git.GitCommandError as e:
TypeError: catching classes that do not inherit from BaseException is not allowed
Now I can obviously do except Exception as e
... But, I would like to be explicit to defer code-paths.
Thanks for reporting!
This seems fixable and testable, and even though I don't know what's the best way to fix this, somebody coming along will.
Thanks for helping.
The problem is not with the GitCommandError
exception, which works fine in except
clauses and, like all exceptions in Python code, does derive ultimately from BaseException
.
This is instead a bug in the code you showed. That code does not catch that exception, because that exception is not what really appears in the except
clause. The problem is this assignment statement:
git = repo.git
Before that, git
was the git
module, whose GitCommandError
attribute is an exception. After that, git
is an instance of the Git
class. All subsequent uses of git
are as if repo.git
had been used. So then you have:
except git.GitCommandError as e:
Because you reassigned git
, this is like:
except repo.git.GitCommandError as e:
In most cases, a bug like this would result in an AttributeError
. But the attributes of a Git
instance, unless otherwise present, are dynamically generated functions that, when called, invoke the git
executable to run git
commands. For example, repo.git.gc
is a function that, when called, runs a git gc
command. Likewise, repo.git.GitCommandError
is a function that, when called, runs a git GitCommandError
command.
(Of course, the git
executable has no GitCommandError
subcommand. So calling it would fail, unless a GitCommandError
alias has been defined, a git-GitCommandError
script or other executable can be found in $PATH
, or a future or alternative version of git
is in use that has such a subcommand. Being open-ended in this way allows Git
instances to be usable for all those things. That the git
executable recognizes no GitCommandError
subcommand is not relevant to what you are observing, though, since you you are attempting to catch repo.git.GitCommandError
, not to call it. The key is that attempting to catch this function as though it were an exception type definitely fails: not only is it not an exception type, it is not even a type.)
One way to fix the bug in the code you showed would be to use a different variable name in the assignment, so that git
still refers to the module that it referred to due to the import
statement.
Ha, so true!!! How did I miss that :) Thanks!
Might I propose that https://gitpython.readthedocs.io/en/latest/tutorial.html#using-git-directly changes git = repo.git
to something else? That was my inspiration ;)
Thanks so much for sharing, I wasn't aware!
Do you think you could give that change a try? It's in test/test_doc.py
, and it's CI-tested so nothing can go wrong.
I would love it if that could be contributed.
done!