From 68301160237e16e5c7d698b350b1be5ab1f3d7cf Mon Sep 17 00:00:00 2001 From: xtreak Date: Mon, 25 Feb 2019 23:20:15 +0530 Subject: [PATCH 1/3] Ensure custom formatwarning function can receive line as positional argument. Co-Authored-By: Tashrif Billah Co-Authored-By: Xtreak --- Lib/test/test_warnings/__init__.py | 12 ++++++++++++ Lib/warnings.py | 2 +- .../Library/2019-02-25-23-04-00.bpo-35178.NA_rXa.rst | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-02-25-23-04-00.bpo-35178.NA_rXa.rst diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 2c54e6137ba80b..8d732edb82468d 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -877,6 +877,18 @@ def test_showwarning(self): file_object, expected_file_line) self.assertEqual(expect, file_object.getvalue()) + # bpo-35178: Test custom formatwarning can receive line as positional + def formatwarning(message, category, filename, lineno, text): + return text + + file_object = StringIO() + original = self.module.formatwarning + self.module.formatwarning = formatwarning + self.module.showwarning(message, category, file_name, line_num, + file_object, expected_file_line) + self.assertEqual(file_object.getvalue(), expected_file_line) + self.addCleanup(setattr, self.module, 'formatwarning', original) + class CWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase): module = c_warnings diff --git a/Lib/warnings.py b/Lib/warnings.py index cf88131f87b4f1..00f740ca3a95ba 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -124,7 +124,7 @@ def _formatwarnmsg(msg): if fw is not _formatwarning_orig: # warnings.formatwarning() was replaced return fw(msg.message, msg.category, - msg.filename, msg.lineno, line=msg.line) + msg.filename, msg.lineno, msg.line) return _formatwarnmsg_impl(msg) def filterwarnings(action, message="", category=Warning, module="", lineno=0, diff --git a/Misc/NEWS.d/next/Library/2019-02-25-23-04-00.bpo-35178.NA_rXa.rst b/Misc/NEWS.d/next/Library/2019-02-25-23-04-00.bpo-35178.NA_rXa.rst new file mode 100644 index 00000000000000..2593199cfe9cec --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-02-25-23-04-00.bpo-35178.NA_rXa.rst @@ -0,0 +1,2 @@ +Ensure custom :func:`warnings.formatwarning` function can receive `line` as +positional argument. Based on patch by Tashrif Billah. From 5a3f4221edd24b2b282e53ab17f34531038acce8 Mon Sep 17 00:00:00 2001 From: xtreak Date: Tue, 26 Feb 2019 10:46:39 +0530 Subject: [PATCH 2/3] Refactor test into a separate test. Co-Authored-By: Tashrif Billah Co-Authored-By: Xtreak --- Lib/test/test_warnings/__init__.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 8d732edb82468d..e7f7ad8f5cd268 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -877,17 +877,23 @@ 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 - def formatwarning(message, category, filename, lineno, text): - return text + 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() - original = self.module.formatwarning - self.module.formatwarning = formatwarning - self.module.showwarning(message, category, file_name, line_num, - file_object, expected_file_line) - self.assertEqual(file_object.getvalue(), expected_file_line) - self.addCleanup(setattr, self.module, 'formatwarning', original) + 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): From 5a018aea1c1a77c5fe134bfc8c6421791a62274e Mon Sep 17 00:00:00 2001 From: xtreak Date: Fri, 1 Mar 2019 10:03:54 +0530 Subject: [PATCH 3/3] Reword comment --- Lib/test/test_warnings/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index e7f7ad8f5cd268..86c2f226ebcf1f 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -878,7 +878,8 @@ def test_showwarning(self): self.assertEqual(expect, file_object.getvalue()) def test_formatwarning_override(self): - # bpo-35178: Test custom formatwarning can receive line as positional + # bpo-35178: Test that a custom formatwarning function gets the 'line' + # argument as a positional argument, and not only as a keyword argument def myformatwarning(message, category, filename, lineno, text): return f'm={message}:c={category}:f={filename}:l={lineno}:t={text}'