Skip to content

Commit

Permalink
pythongh-115538: Emit warning when use bool as fd in _io.WindowsConso…
Browse files Browse the repository at this point in the history
…leIO (pythonGH-116925)
  • Loading branch information
aisk authored and vstinner committed Mar 20, 2024
1 parent 796345a commit f736c19
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Lib/test/test_winconsoleio.py
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
@@ -0,0 +1,2 @@
:class:`_io.WindowsConsoleIO` now emit a warning if a boolean value is
passed as a filedescriptor argument.
7 changes: 7 additions & 0 deletions Modules/_io/winconsoleio.c
Expand Up @@ -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()) {
Expand Down

0 comments on commit f736c19

Please sign in to comment.