Code Monkey home page Code Monkey logo

Comments (6)

EliahKagan avatar EliahKagan commented on August 15, 2024 1

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.

Byron avatar Byron commented on August 15, 2024 1

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.

zerothi avatar zerothi commented on August 15, 2024 1

done!

from gitpython.

Byron avatar Byron commented on August 15, 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.

from gitpython.

zerothi avatar zerothi commented on August 15, 2024

Ha, so true!!! How did I miss that :) Thanks!

from gitpython.

zerothi avatar zerothi commented on August 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.