-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updater: Log info message when receiving signal #951
Conversation
Codecov Report
@@ Coverage Diff @@
## master #951 +/- ##
==========================================
- Coverage 91.8% 91.69% -0.12%
==========================================
Files 103 103
Lines 4040 4046 +6
Branches 638 639 +1
==========================================
+ Hits 3709 3710 +1
- Misses 193 197 +4
- Partials 138 139 +1
|
Wondering why coverage decreased, haven't introduced any new branches... |
@Makman2 Sometimes coverage fluctuates because errors do or don't happen. The important part is that the diff is 100% covered. I'll look more detailed after Christmas. Thanks for contributing. |
Looks better. |
Okay that shifts lookup effort to each call instead of once preparing a dict inside the module. But we shouldn't raise an implicit try:
return next(...)
except StopIteration:
raise KeyError('Unknown signal number {}'.format(...)) May I call it |
@Makman2 you're right about StopIteration. You can name your method as you wish, i'm not this repo developer, just proposed less code solution. |
We in the dev team are hesitant about adding new dependencies (even just for tests). Especially since in this case it doesn't seem to be required as log can already be captured using pure pytest. See Loggging - pytest documentation for details. |
|
||
# From https://stackoverflow.com/questions/2549939/get-signal-names-from-numbers-in-python | ||
_signames = {v: k | ||
for k, v in reversed(sorted(vars(signal).items())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpicking here. the reversed
& sorted
are useless here as you're creating a dict. please remove them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the sorted
was there for signals with different names but same number, so there's a deterministic outcome. However I haven't understood the reversed
call too^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And kk reversed
then picks signal names that are sorted at the beginning. On my system I get these differences when I don't use reversed
:
SIGIOT vs SIGABRT
SIGCLD vs SIGCHLD
SIGPOLL vs SIGIO
Though not sure if it's that important here^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check manpage of signal(7)
all the above are synonyms. If one name is chosen over another it's merely a matter of luck, as dictionary order is not guaranteed.
On my system I had different results on different executions.
Another option is to leave the signal name alone and just print the number.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If one name is chosen over another it's merely a matter of luck, as dictionary order is not guaranteed.
Looks like it's true, I'm just wondering: the order we iterate over should always produce a guaranteed outcome. vars(signal).items()
has unique mappings from name -> number
. Using sorted
we get always the same list with the same order, and then we are filling the dict with those values, while switching name
and number
. If there are numbers that are equal, later elements in the list should overwrite the old ones. Although using dict-comprehension, citing the dict
-docs:
If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option is to leave the signal name alone and just print the number.
I wouldn't do that, signal numbers differ on OSes for the "same" signals. I think people identify those signals usually by their name, like SIGINT
/ SIGTERM
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. not worth the argument, lets keep it as is.
Sure no problem 👍 |
About
I have tried using this feature, however:
So I believe using |
@Makman2 we'll try to work on it together before using |
This makes tests fail though...
Done 👍 |
The problem with pytest in the test you've sent is that it catches the signal itself / prevents Updater from catching it. |
Note to self: need to make sure that this PR works properly on windows as the unitest in question is skipped on appveyor. |
TODO: Fix warning about deprecated fixture declaration
tests/test_updater.py
Outdated
'telegram.ext.updater', 'INFO', 'Received signal {} (SIGTERM), ' | ||
'stopping...'.format(signal.SIGTERM))] | ||
rec = caplog.records()[-1] | ||
assert rec.msg.startswith('Received signal {}'.format(signal.SIGTERM)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious, why not asserting for the complete message? :)
Works fine on windows. |
Build fails due to unrelated problems fixed in a different PR. @Makman2 Thanks for your contribution. |
Thanks for helping me out and bearing with me ;D |
@Makman2 All the thanks are for you |
Closes #946