Comments (6)
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.
from gitpython.
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.
from gitpython.
done!
from gitpython.
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.
from gitpython.
Ha, so true!!! How did I miss that :) Thanks!
from gitpython.
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 ;)
from gitpython.
Related Issues (20)
- Module docstring says TagReference but means TagObject
- Diffable.diff is misleadingly annotated regarding special `other` values HOT 3
- Top-level refresh function not listed in __all__ HOT 1
- Bad git executable HOT 2
- Unable to specify lines of context while doing diff (-U flag) HOT 1
- γremote().exist()γ exception when lack remote section in config file
- Unhandled IndexError when calling .read() on a malformed config file HOT 6
- Detailed xfail output sometimes distracts from unexpected failures
- Some xfail markings fail to validate their exception types HOT 1
- How can i set create_patch=true, and meanwhile get change_type in Diff
- git.Repo.clone_from() not working correctly in WSL HOT 3
- Fuzz Tests Are Crashing at Start-up on ClusterFuzz HOT 2
- GitConfigParser misparses quotes in options HOT 1
- Command's DEBUG reveals username/password from URL HOT 3
- submodule: Reference at 'HEAD' does not exist HOT 3
- Git diff pathspec missing -- HOT 4
- mypy is confused by property aliases HOT 2
- Get annotated tags
- UDP Port conflict
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gitpython.