You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
exceptgit.GitCommandErrorase:
Because you reassigned git, this is like:
exceptrepo.git.GitCommandErrorase:
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.
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.
Activity
Byron commentedon Jun 1, 2024
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.
EliahKagan commentedon Jun 1, 2024
The problem is not with the
GitCommandErrorexception, which works fine inexceptclauses and, like all exceptions in Python code, does derive ultimately fromBaseException.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
exceptclause. The problem is this assignment statement:Before that,
gitwas thegitmodule, whoseGitCommandErrorattribute is an exception. After that,gitis an instance of theGitclass. All subsequent uses ofgitare as ifrepo.githad been used. So then you have:Because you reassigned
git, this is like:In most cases, a bug like this would result in an
AttributeError. But the attributes of aGitinstance, unless otherwise present, are dynamically generated functions that, when called, invoke thegitexecutable to rungitcommands. For example,repo.git.gcis a function that, when called, runs agit gccommand. Likewise,repo.git.GitCommandErroris a function that, when called, runs agit GitCommandErrorcommand.(Of course, the
gitexecutable has noGitCommandErrorsubcommand. So calling it would fail, unless aGitCommandErroralias has been defined, agit-GitCommandErrorscript or other executable can be found in$PATH, or a future or alternative version ofgitis in use that has such a subcommand. Being open-ended in this way allowsGitinstances to be usable for all those things. That thegitexecutable recognizes noGitCommandErrorsubcommand is not relevant to what you are observing, though, since you you are attempting to catchrepo.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
gitstill refers to the module that it referred to due to theimportstatement.zerothi commentedon Jun 3, 2024
Ha, so true!!! How did I miss that :) Thanks!
zerothi commentedon Jun 3, 2024
Might I propose that https://gitpython.readthedocs.io/en/latest/tutorial.html#using-git-directly changes
git = repo.gitto something else? That was my inspiration ;)Byron commentedon Jun 4, 2024
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.
fixed doc to not faulty do gitpython-developers#1924
zerothi commentedon Jun 13, 2024
done!
Merge pull request #1932 from zerothi/1924-docs