[Python-checkins] bpo-35178: Fix warnings._formatwarnmsg() (GH-12033)

Victor Stinner webhook-mailer at python.org
Fri Mar 1 12:17:59 EST 2019


https://github.com/python/cpython/commit/be7c460fb50efe3b88a00281025d76acc62ad2fd
commit: be7c460fb50efe3b88a00281025d76acc62ad2fd
branch: master
author: Xtreak <tir.karthi at gmail.com>
committer: Victor Stinner <vstinner at redhat.com>
date: 2019-03-01T18:17:55+01:00
summary:

bpo-35178: Fix warnings._formatwarnmsg() (GH-12033)

Ensure custom formatwarning function can receive line as positional argument.

Co-Authored-By: Tashrif Billah <tashrifbillah at gmail.com>

files:
A Misc/NEWS.d/next/Library/2019-02-25-23-04-00.bpo-35178.NA_rXa.rst
M Lib/test/test_warnings/__init__.py
M Lib/warnings.py

diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index 2c54e6137ba8..86c2f226ebcf 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -877,6 +877,25 @@ def test_showwarning(self):
                                 file_object, expected_file_line)
         self.assertEqual(expect, file_object.getvalue())
 
+    def test_formatwarning_override(self):
+        # 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}'
+
+        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
diff --git a/Lib/warnings.py b/Lib/warnings.py
index cf88131f87b4..00f740ca3a95 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 000000000000..2593199cfe9c
--- /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.



More information about the Python-checkins mailing list