-
-
Notifications
You must be signed in to change notification settings - Fork 372
Fixes #3275; handle reraise of KI or SystemExit without losing err code #3280
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
Conversation
…sing err msg and exit code
for more information, see https://pre-commit.ci
A5rocks
left a comment
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.
Thanks for doing this!
src/trio/_tests/test_util.py
Outdated
| # and same for SystemExit | ||
| # and same for SystemExit but verify code too | ||
| sysexit_exc = SystemExit("preserve error code") | ||
| sysexit_exc.code = 1 # can't set it in constructor |
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.
Use a code that isn't 1 because that's what it defaults to iirc
(Also I think code is the 1st arg of SystemExit?)
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.
Now that I played a bit more with SystemExit, I agree with what this is testing, I think the comment should be changed though.
EDIT: ouch, I saw you just updated things based on my incorrect understanding of things. Sorry!
src/trio/_util.py
Outdated
| raise type(e) from eg | ||
| new_exc = type(e)(e.args) | ||
| if isinstance(e, SystemExit): | ||
| new_exc.code = e.code # code can't be set in SystemExit constructor |
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 don't think this is right:
The constructor accepts the same optional argument passed to sys.exit(). If the value is an integer, it specifies the system exit status (passed to C’s exit() function); if it is None, the exit status is zero; if it has another type (such as a string), the object’s value is printed and the exit status is one.
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.
Actually Python's behavior here is interesting. The comment still seems incorrect, but I agree with this extra work:
>>> se = SystemExit("test")
>>> se.code
'test'
>>> se.code = 1
>>> se
SystemExit('test')
>>> se.args
('test',)
>>> se.code
1(you probably knew this which is why you added this, and just wrote the comment wrong or something.)
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 I figured why it was not working for me and fixed
systemexit is not supposed to get actual string error msg, it's just SystemExit(4)
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.
Well it supports it so Trio should probably do the right thing! (I do think it's a very silly use case though, and if it were more than a 5 line change then maybe we shouldn't support it)
…trio into fix/issue-3275-sysexit
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3280 +/- ##
===============================================
Coverage 100.00000% 100.00000%
===============================================
Files 127 127
Lines 19257 19258 +1
Branches 1301 1301
===============================================
+ Hits 19257 19258 +1
🚀 New features to boost your workflow:
|
for more information, see https://pre-commit.ci
you're welcome please re-review @A5rocks , fixed requested things failed tests on py3.13 seem unrelated to my changes, some typing mismatch in some other files |
It may have to do with the pytest version bump. I can't find anything named I still think we can avoid having to increase the minimum required pytest version (though if you want, you can bump in I don't really know what review status to mark a review comment as though, so this is just a regular comment. |
A5rocks
left a comment
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.
This looks good to me! As stated before I'm conflicted about whether to handle mutation of SystemExit's code attribute, but I'm fine if we don't. Users probably don't do that?
ok fixed it to work without pytest upgrade, as you suggested, thanks for comments! |
I don't think one's supposed to instantiate SystemExit class directly at all, or to mutate their fields, one is supposed to use sys.exit. However for the purposes of, pardon, a bit weird things happening in I suppose this PR resolves reporter's issue of missing error code so that's a small change for good don't think I can merge it though so maybe somebody else can merge? |
Yeah let's make sure others agree (and they can merge it). |
|
@seowalex I assume this works for you? |
|
Well if there's any issues, we can make a follow up PR. |
|
Hey @CheViana, it looks like that was the first time we merged one of your PRs! Thanks so much! 🎉 🎂 If you want to keep contributing, we'd love to have you. So, I just sent you an invitation to join the python-trio organization on Github! If you accept, then here's what will happen:
If you want to read more, here's the relevant section in our contributing guide. Alternatively, you're free to decline or ignore the invitation. You'll still be able to contribute as much or as little as you like, and I won't hassle you about joining again. But if you ever change your mind, just let us know and we'll send another invitation. We'd love to have you, but more importantly we want you to do whatever's best for you. If you have any questions, well... I am just a humble Python script, so I probably can't help. But please do post a comment here, or in our chat, or on our forum, whatever's easiest, and someone will help you out! |
Fixes #3275
Handle reraise of KeyboardInterrupt and SystemExit more gracefully when reraising last exception in exception group.