From eabea36bee430d56bd32f5f14967e2fe435d6f28 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 17 Mar 2024 18:09:22 +0800 Subject: [PATCH 1/5] gh-115538: Emit warning when use bool as fd in _io.WindowsConsoleIO --- .../Library/2024-03-17-18-12-39.gh-issue-115538.PBiRQB.rst | 2 ++ Modules/_io/winconsoleio.c | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-03-17-18-12-39.gh-issue-115538.PBiRQB.rst diff --git a/Misc/NEWS.d/next/Library/2024-03-17-18-12-39.gh-issue-115538.PBiRQB.rst b/Misc/NEWS.d/next/Library/2024-03-17-18-12-39.gh-issue-115538.PBiRQB.rst new file mode 100644 index 00000000000000..fda2ebf7593ed5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-03-17-18-12-39.gh-issue-115538.PBiRQB.rst @@ -0,0 +1,2 @@ +:class:`_io.WindowsConsoleIO` now emit a warning if a boolean value is +passed as a filedescriptor argument. diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c index 54e15555417287..ec5c298066a587 100644 --- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -298,6 +298,13 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj, self->fd = -1; } + if (PyBool_Check(nameobj)) { + if (PyErr_WarnEx(PyExc_RuntimeWarning, + "bool is used as a file descriptor", 1)) + { + return -1; + } + } fd = PyLong_AsInt(nameobj); if (fd < 0) { if (!PyErr_Occurred()) { From ba181204c514eb136a7eeed4e18fe23f5193a859 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 17 Mar 2024 22:44:59 +0800 Subject: [PATCH 2/5] Add test --- Lib/test/test_winconsoleio.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_winconsoleio.py index 209e4464e1a5c0..aee4f5bd7da110 100644 --- a/Lib/test/test_winconsoleio.py +++ b/Lib/test/test_winconsoleio.py @@ -6,6 +6,7 @@ import sys import tempfile import unittest +import warnings from test.support import os_helper, requires_resource if sys.platform != 'win32': @@ -68,6 +69,18 @@ def test_open_fd(self): f.close() f.close() + with warnings.catch_warnings(): + warnings.simplefilter("error", RuntimeWarning) + try: + with self.assertRaises(RuntimeWarning): + f = ConIO(False) + with self.assertRaises(RuntimeWarning): + f = ConIO(True, mode='w') + except ValueError: + # cannot open console because it's not a real console + pass + + def test_open_name(self): self.assertRaises(ValueError, ConIO, sys.executable) From d6e37e89493c00ee9ca8ed9eef36723d16f2a2cb Mon Sep 17 00:00:00 2001 From: AN Long Date: Mon, 18 Mar 2024 19:14:30 +0800 Subject: [PATCH 3/5] Update the test case --- Lib/test/test_winconsoleio.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_winconsoleio.py index aee4f5bd7da110..17eef58df128e4 100644 --- a/Lib/test/test_winconsoleio.py +++ b/Lib/test/test_winconsoleio.py @@ -44,6 +44,9 @@ def test_open_fd(self): self.assertEqual(0, f.fileno()) f.close() # multiple close should not crash f.close() + with self.assertWarns(RuntimeWarning): + with ConIO(False): + pass try: f = ConIO(1, 'w') @@ -56,6 +59,9 @@ def test_open_fd(self): self.assertEqual(1, f.fileno()) f.close() f.close() + with self.assertWarns(RuntimeWarning): + with ConIO(False): + pass try: f = ConIO(2, 'w') @@ -69,17 +75,6 @@ def test_open_fd(self): f.close() f.close() - with warnings.catch_warnings(): - warnings.simplefilter("error", RuntimeWarning) - try: - with self.assertRaises(RuntimeWarning): - f = ConIO(False) - with self.assertRaises(RuntimeWarning): - f = ConIO(True, mode='w') - except ValueError: - # cannot open console because it's not a real console - pass - def test_open_name(self): self.assertRaises(ValueError, ConIO, sys.executable) From 3b74b2553f80ad4acf6009909862828dacbade47 Mon Sep 17 00:00:00 2001 From: AN Long Date: Mon, 18 Mar 2024 19:18:55 +0800 Subject: [PATCH 4/5] Remove unused import statement --- Lib/test/test_winconsoleio.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_winconsoleio.py index 17eef58df128e4..f9ea6442e3ecf2 100644 --- a/Lib/test/test_winconsoleio.py +++ b/Lib/test/test_winconsoleio.py @@ -6,7 +6,6 @@ import sys import tempfile import unittest -import warnings from test.support import os_helper, requires_resource if sys.platform != 'win32': From 3bcfbe65700a650db1cee493f96fb87ac0d66046 Mon Sep 17 00:00:00 2001 From: AN Long Date: Mon, 18 Mar 2024 19:19:43 +0800 Subject: [PATCH 5/5] Remove unnessesary blank --- Lib/test/test_winconsoleio.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_winconsoleio.py index f9ea6442e3ecf2..a10d63dfdc9753 100644 --- a/Lib/test/test_winconsoleio.py +++ b/Lib/test/test_winconsoleio.py @@ -74,7 +74,6 @@ def test_open_fd(self): f.close() f.close() - def test_open_name(self): self.assertRaises(ValueError, ConIO, sys.executable)