5

I have a python code that use the git lib. For some commands the git lib throw an exception (CommandError exception). The issue is that this exception is thrown from another thread. I did try/except and catch the exception and did some workaround. However, the other thread is still printing the exception error msg.

This is the code in git lib

    def pump_stream(cmdline, name, stream, is_decode, handler):
        try:
            for line in stream:
                if handler:
                    if is_decode:
                        line = line.decode(defenc)
                    handler(line)
        except Exception as ex:
            log.error("Pumping %r of cmd(%s) failed due to: %r", name, cmdline, ex)
            raise CommandError(['<%s-pump>' % name] + cmdline, ex) from ex
        finally:
            stream.close()

And it create the thread like this:

    threads = []

    for name, stream, handler in pumps:
        t = threading.Thread(target=pump_stream,
                             args=(cmdline, name, stream, decode_streams, handler))
        t.setDaemon(True)
        t.start()
        threads.append(t)

    ## FIXME: Why Join??  Will block if `stdin` needs feeding...
    #
    for t in threads:
        t.join()

    if finalizer:
        return finalizer(process)

And this what I wrote:

try:
    <do something in git that will raise the CommandError exception>
except git.exc.CommandError:
    print('>>>>>>>>>>>>>>>> I GOT IT')

And this is the output on the Terminal:

Exception in thread Thread-796:
Traceback (most recent call last):
  File "/my/src/lib/git/cmd.py", line 88, in pump_stream
    raise CommandError('<error from the command>')
git.exc.CommandError: Cmd('<the command>') failed!
  cmdline: <the command>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/my/src/lib/git/cmd.py", line 91, in pump_stream
    raise Exception('<error from the command>')
Exception: <error from the command>

>>>>>>>>>>>>>>>> I GOT IT

<... then it finish the rest of the code>

How do I stop the printing of the exception error msg?

torek
  • 448,244
  • 59
  • 642
  • 775
Sondos.S
  • 76
  • 3

0 Answers0