-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-35178: Ensure custom formatwarning function can receive line as positional argument #12033
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -877,6 +877,24 @@ def test_showwarning(self): | |
file_object, expected_file_line) | ||
self.assertEqual(expect, file_object.getvalue()) | ||
|
||
def test_formatwarning_override(self): | ||
# bpo-35178: Test custom formatwarning can receive line as positional | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It isn't obvious that the last parameter of myformatwarning() is usually called line, but here it's called "text" to test that the argument is passed as a position argument and not as a keyword argument. Try to rephrase the comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about indicating line could be keyword or positional argument inferring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like this?
(I let you format it properly) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make a minor edit of "not only as a keyword argument" since this reads a little like line as a keyword argument is not allowed. How about below ?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's more explicit than the current comment ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rephrased the comment as noted. |
||
def myformatwarning(message, category, filename, lineno, text): | ||
return f'm={message}:c={category}:f={filename}:l={lineno}:t={text}' | ||
|
||
file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' | ||
line_num = 3 | ||
file_line = linecache.getline(file_name, line_num).strip() | ||
message = 'msg' | ||
category = Warning | ||
file_object = StringIO() | ||
expected = f'm={message}:c={category}:f={file_name}:l={line_num}' + \ | ||
f':t={file_line}' | ||
with support.swap_attr(self.module, 'formatwarning', myformatwarning): | ||
self.module.showwarning(message, category, file_name, line_num, | ||
file_object, file_line) | ||
self.assertEqual(file_object.getvalue(), expected) | ||
|
||
|
||
class CWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase): | ||
module = c_warnings | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Ensure custom :func:`warnings.formatwarning` function can receive `line` as | ||
positional argument. Based on patch by Tashrif Billah. |
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 would prefer to put the new test in a new method:
def test_formatwarning_override(self):
And maybe move the new test inside test_formatwarning().
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 initially had it in
test_showwarning
since the exception was triggered byshowwarning
. I moved this into a new testtest_formatwarning_override
. Let me know if I need to move the test intotest_formatwarning
.