Skip to content

Commit 128244d

Browse files
graingertsobolevnbrettcannon
authored andcommitted
pythongh-128770: raise warnings as errors in test suite - except for test_socket which still logs warnings (pythonGH-128726)
(cherry picked from commit 7807b40) Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: sobolevn <[email protected]> Co-authored-by: Brett Cannon <[email protected]>
1 parent c927fd9 commit 128244d

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

Lib/test/libregrtest/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,9 @@ def _add_ci_python_opts(self, python_opts, keep_environ):
639639
if not sys.stdout.write_through:
640640
python_opts.append('-u')
641641

642-
# Add warnings filter 'default'
642+
# Add warnings filter 'error'
643643
if 'default' not in sys.warnoptions:
644-
python_opts.extend(('-W', 'default'))
644+
python_opts.extend(('-W', 'error'))
645645

646646
# Error on bytes/str comparison
647647
if sys.flags.bytes_warning < 2:

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,22 +1324,21 @@ def test_readline_history_file(self):
13241324
if readline.backend != "editline":
13251325
self.skipTest("GNU readline is not affected by this issue")
13261326

1327-
hfile = tempfile.NamedTemporaryFile()
1328-
self.addCleanup(unlink, hfile.name)
1329-
env = os.environ.copy()
1330-
env["PYTHON_HISTORY"] = hfile.name
1331-
1332-
env["PYTHON_BASIC_REPL"] = "1"
1333-
output, exit_code = self.run_repl("spam \nexit()\n", env=env)
1334-
self.assertEqual(exit_code, 0)
1335-
self.assertIn("spam ", output)
1336-
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0)
1337-
self.assertIn("spam\\040", pathlib.Path(hfile.name).read_text())
1338-
1339-
env.pop("PYTHON_BASIC_REPL", None)
1340-
output, exit_code = self.run_repl("exit\n", env=env)
1341-
self.assertEqual(exit_code, 0)
1342-
self.assertNotIn("\\040", pathlib.Path(hfile.name).read_text())
1327+
with tempfile.NamedTemporaryFile() as hfile:
1328+
env = os.environ.copy()
1329+
env["PYTHON_HISTORY"] = hfile.name
1330+
1331+
env["PYTHON_BASIC_REPL"] = "1"
1332+
output, exit_code = self.run_repl("spam \nexit()\n", env=env)
1333+
self.assertEqual(exit_code, 0)
1334+
self.assertIn("spam ", output)
1335+
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0)
1336+
self.assertIn("spam\\040", pathlib.Path(hfile.name).read_text())
1337+
1338+
env.pop("PYTHON_BASIC_REPL", None)
1339+
output, exit_code = self.run_repl("exit\n", env=env)
1340+
self.assertEqual(exit_code, 0)
1341+
self.assertNotIn("\\040", pathlib.Path(hfile.name).read_text())
13431342

13441343
def test_keyboard_interrupt_after_isearch(self):
13451344
output, exit_code = self.run_repl(["\x12", "\x03", "exit"])

Lib/test/test_socket.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import unittest
2+
import warnings
23
from test import support
34
from test.support import (
4-
is_apple, os_helper, refleak_helper, socket_helper, threading_helper
5+
is_apple, os_helper, refleak_helper, socket_helper, threading_helper,
56
)
67
import _thread as thread
78
import array
@@ -199,6 +200,24 @@ def socket_setdefaulttimeout(timeout):
199200
socket.setdefaulttimeout(old_timeout)
200201

201202

203+
@contextlib.contextmanager
204+
def downgrade_malformed_data_warning():
205+
# This warning happens on macos and win, but does not always happen on linux.
206+
if sys.platform not in {"win32", "darwin"}:
207+
yield
208+
return
209+
210+
with warnings.catch_warnings():
211+
# TODO: gh-110012, we should investigate why this warning is happening
212+
# and fix it properly.
213+
warnings.filterwarnings(
214+
action="always",
215+
message=r"received malformed or improperly-truncated ancillary data",
216+
category=RuntimeWarning,
217+
)
218+
yield
219+
220+
202221
HAVE_SOCKET_CAN = _have_socket_can()
203222

204223
HAVE_SOCKET_CAN_ISOTP = _have_socket_can_isotp()
@@ -3941,8 +3960,9 @@ def checkTruncatedArray(self, ancbuf, maxdata, mindata=0):
39413960
# mindata and maxdata bytes when received with buffer size
39423961
# ancbuf, and that any complete file descriptor numbers are
39433962
# valid.
3944-
msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
3945-
len(MSG), ancbuf)
3963+
with downgrade_malformed_data_warning(): # TODO: gh-110012
3964+
msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
3965+
len(MSG), ancbuf)
39463966
self.assertEqual(msg, MSG)
39473967
self.checkRecvmsgAddress(addr, self.cli_addr)
39483968
self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC)
@@ -4293,8 +4313,9 @@ def testSingleCmsgTruncInData(self):
42934313
self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
42944314
socket.IPV6_RECVHOPLIMIT, 1)
42954315
self.misc_event.set()
4296-
msg, ancdata, flags, addr = self.doRecvmsg(
4297-
self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1)
4316+
with downgrade_malformed_data_warning(): # TODO: gh-110012
4317+
msg, ancdata, flags, addr = self.doRecvmsg(
4318+
self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1)
42984319

42994320
self.assertEqual(msg, MSG)
43004321
self.checkRecvmsgAddress(addr, self.cli_addr)
@@ -4397,9 +4418,10 @@ def testSecondCmsgTruncInData(self):
43974418
self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
43984419
socket.IPV6_RECVTCLASS, 1)
43994420
self.misc_event.set()
4400-
msg, ancdata, flags, addr = self.doRecvmsg(
4401-
self.serv_sock, len(MSG),
4402-
socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1)
4421+
with downgrade_malformed_data_warning(): # TODO: gh-110012
4422+
msg, ancdata, flags, addr = self.doRecvmsg(
4423+
self.serv_sock, len(MSG),
4424+
socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1)
44034425

44044426
self.assertEqual(msg, MSG)
44054427
self.checkRecvmsgAddress(addr, self.cli_addr)

0 commit comments

Comments
 (0)