]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-115538: Emit warning when use bool as fd in _io.WindowsConsoleIO (GH-116925)
authorAN Long <aisk@users.noreply.github.com>
Mon, 18 Mar 2024 11:48:50 +0000 (19:48 +0800)
committerGitHub <noreply@github.com>
Mon, 18 Mar 2024 11:48:50 +0000 (11:48 +0000)
Lib/test/test_winconsoleio.py
Misc/NEWS.d/next/Library/2024-03-17-18-12-39.gh-issue-115538.PBiRQB.rst [new file with mode: 0644]
Modules/_io/winconsoleio.c

index 209e4464e1a5c0cdf07596c1d6142d430737375e..a10d63dfdc97539e6230354b38bd8bb9022b68aa 100644 (file)
@@ -43,6 +43,9 @@ class WindowsConsoleIOTests(unittest.TestCase):
             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')
@@ -55,6 +58,9 @@ class WindowsConsoleIOTests(unittest.TestCase):
             self.assertEqual(1, f.fileno())
             f.close()
             f.close()
+            with self.assertWarns(RuntimeWarning):
+                with ConIO(False):
+                    pass
 
         try:
             f = ConIO(2, 'w')
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 (file)
index 0000000..fda2ebf
--- /dev/null
@@ -0,0 +1,2 @@
+:class:`_io.WindowsConsoleIO` now emit a warning if a boolean value is
+passed as a filedescriptor argument.
index 54e1555541728745d0a162de0dd950600dc9f414..ec5c298066a587462157115d3e742fbcf487baba 100644 (file)
@@ -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()) {