mozilla-releng/redo

use function name in the log output if possible

Closed this issue · 6 comments

Hey there, first thanks for the redo package!

So basically when using the retry function, We have this kind of lines:

INFO | retry: Calling <function _download at 0x7fad39b54b18> with args: (), kwargs: {}, attempt #2

It would be good to just print "Calling _download ..." - i.e. the name of the function (if available) instead of the function object.

What do you think @bhearsum ? I can create a PR for that.

If you're only talking about getting right of the function identity part, I agree. I do think it's important to continue printing the args and kwargs, though maybe only at DEBUG level logging? Could be something like:
INFO | retry: Calling _download, attempt #2
DEBUG | retry: args: (), kwargs: {}

Yes, I was only talking about the function part. But since I was at it, I made a minor change to only display args/kwargs if we have at least one of them. I just created the PR #36.

Seems to work well on python 2.7.10 and 3.5.0.

(venv)dev/redo [ retry -v -s 1 cat something
cat: something: Aucun fichier ou dossier de ce type
INFO:redo:retry: calling check_call with args: (['cat', 'something'],), kwargs: {}, attempt #2
cat: something: Aucun fichier ou dossier de ce type
INFO:redo:retry: calling check_call with args: (['cat', 'something'],), kwargs: {}, attempt #3
cat: something: Aucun fichier ou dossier de ce type
INFO:redo:retry: calling check_call with args: (['cat', 'something'],), kwargs: {}, attempt #4
cat: something: Aucun fichier ou dossier de ce type
INFO:redo:retry: calling check_call with args: (['cat', 'something'],), kwargs: {}, attempt #5
cat: something: Aucun fichier ou dossier de ce type
INFO:redo:retry: Giving up on check_call
ERROR:redo.cmd:Unable to run command after 5 attempts
Traceback (most recent call last):
  File "/home/jp/dev/redo/redo/cmd.py", line 44, in main
    r_check_call(args.cmd)
  File "/home/jp/dev/redo/redo/__init__.py", line 206, in _retriable_wrapper
    **retry_kwargs)
  File "/home/jp/dev/redo/redo/__init__.py", line 161, in retry
    return action(*args, **kwargs)
  File "/usr/lib64/python2.7/subprocess.py", line 540, in check_call
    raise CalledProcessError(retcode, cmd)
CalledProcessError: Command '['cat', 'something']' returned non-zero exit status 1

I also tried with a lambda function (without a name attribute, and broken on purpose):

DEBUG:redo:attempt 5/5
INFO:redo:retry: calling <lambda>, attempt #5
DEBUG:redo:retry: Caught exception: 
Traceback (most recent call last):
  File "redo/__init__.py", line 161, in retry
    return action(*args, **kwargs)
TypeError: <lambda>() takes exactly 1 argument (0 given)
INFO:redo:retry: Giving up on <lambda>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "redo/__init__.py", line 161, in retry
    return action(*args, **kwargs)
TypeError: <lambda>() takes exactly 1 argument (0 given)

Breaking support for lambda's is a regression, so that case will need to be handled. It's probably OK to fallback to printing the function identity instead of the name in that case?

Sorry that was not explicit enough, this is what the patch does already:

https://github.com/bhearsum/redo/pull/36/files#diff-9adb60c4e52347a6dae527b7cd3c6ee6R141

The example above just show that it works (what is inside the lambda test function is broken so it is retried, that's all).

Ah, I see! Sorry, I hadn't noticed the PR!

This was fixed in #36