https://github.com/python/cpython/commit/cd2ed917801b93fb46d1dcf19dd480e5146932d8
commit: cd2ed917801b93fb46d1dcf19dd480e5146932d8
branch: main
author: AN Long <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-03-18T11:48:50Z
summary:
gh-115538: Emit warning when use bool as fd in _io.WindowsConsoleIO (GH-116925)
files:
A Misc/NEWS.d/next/Library/2024-03-17-18-12-39.gh-issue-115538.PBiRQB.rst
M Lib/test/test_winconsoleio.py
M Modules/_io/winconsoleio.c
diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_winconsoleio.py
index 209e4464e1a5c0..a10d63dfdc9753 100644
--- a/Lib/test/test_winconsoleio.py
+++ b/Lib/test/test_winconsoleio.py
@@ -43,6 +43,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')
@@ -55,6 +58,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')
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()) {
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]