Skip to content

Commit d8380f7

Browse files
authored
Fix test_tty.py tests (RustPython#8065)
* Align patches of `test_tty` * Fix failing tests at `test_tty.py` * Align patches of `test_termios.py` * Unmark passing tests under `test_termios.py` * Unmark another passing test
1 parent 110f27b commit d8380f7

4 files changed

Lines changed: 25 additions & 18 deletions

File tree

Lib/test/test_termios.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def assertRaisesTermiosError(self, err, callable, *args):
3232
callable(*args)
3333
self.assertIn(cm.exception.args[0], errs)
3434

35-
@unittest.expectedFailure # TODO: RUSTPYTHON; TypeError: Expected type 'int' but 'FileIO' found.
3635
def test_tcgetattr(self):
3736
attrs = termios.tcgetattr(self.fd)
3837
self.assertIsInstance(attrs, list)
@@ -51,23 +50,20 @@ def test_tcgetattr(self):
5150
self.assertEqual(len(x), 1)
5251
self.assertEqual(termios.tcgetattr(self.stream), attrs)
5352

54-
@unittest.skip("TODO: RUSTPYTHON segfault")
5553
def test_tcgetattr_errors(self):
5654
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcgetattr, self.bad_fd)
5755
self.assertRaises(ValueError, termios.tcgetattr, -1)
5856
self.assertRaises(OverflowError, termios.tcgetattr, 2**1000)
5957
self.assertRaises(TypeError, termios.tcgetattr, object())
6058
self.assertRaises(TypeError, termios.tcgetattr)
6159

62-
@unittest.expectedFailure # TODO: RUSTPYTHON; TypeError: Expected type 'int' but 'FileIO' found.
6360
def test_tcsetattr(self):
6461
attrs = termios.tcgetattr(self.fd)
6562
termios.tcsetattr(self.fd, termios.TCSANOW, attrs)
6663
termios.tcsetattr(self.fd, termios.TCSADRAIN, attrs)
6764
termios.tcsetattr(self.fd, termios.TCSAFLUSH, attrs)
6865
termios.tcsetattr(self.stream, termios.TCSANOW, attrs)
6966

70-
@unittest.skip("TODO: RUSTPYTHON segfault")
7167
def test_tcsetattr_errors(self):
7268
attrs = termios.tcgetattr(self.fd)
7369
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, tuple(attrs))
@@ -103,7 +99,6 @@ def test_tcsetattr_errors(self):
10399
self.assertRaises(TypeError, termios.tcsetattr, object(), termios.TCSANOW, attrs)
104100
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW)
105101

106-
@unittest.expectedFailure # TODO: RUSTPYTHON; TypeError: Expected type 'int' but 'FileIO' found.
107102
@support.skip_android_selinux('tcsendbreak')
108103
def test_tcsendbreak(self):
109104
try:
@@ -115,7 +110,6 @@ def test_tcsendbreak(self):
115110
raise
116111
termios.tcsendbreak(self.stream, 1)
117112

118-
@unittest.skip("TODO: RUSTPYTHON segfault")
119113
@support.skip_android_selinux('tcsendbreak')
120114
def test_tcsendbreak_errors(self):
121115
self.assertRaises(OverflowError, termios.tcsendbreak, self.fd, 2**1000)
@@ -127,13 +121,11 @@ def test_tcsendbreak_errors(self):
127121
self.assertRaises(TypeError, termios.tcsendbreak, object(), 0)
128122
self.assertRaises(TypeError, termios.tcsendbreak, self.fd)
129123

130-
@unittest.expectedFailure # TODO: RUSTPYTHON; TypeError: Expected type 'int' but 'FileIO' found.
131124
@support.skip_android_selinux('tcdrain')
132125
def test_tcdrain(self):
133126
termios.tcdrain(self.fd)
134127
termios.tcdrain(self.stream)
135128

136-
@unittest.skip("TODO: RUSTPYTHON segfault")
137129
@support.skip_android_selinux('tcdrain')
138130
def test_tcdrain_errors(self):
139131
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcdrain, self.bad_fd)
@@ -147,7 +139,6 @@ def test_tcflush(self):
147139
termios.tcflush(self.fd, termios.TCOFLUSH)
148140
termios.tcflush(self.fd, termios.TCIOFLUSH)
149141

150-
@unittest.skip("TODO: RUSTPYTHON segfault")
151142
def test_tcflush_errors(self):
152143
self.assertRaisesTermiosError(errno.EINVAL, termios.tcflush, self.fd, -1)
153144
self.assertRaises(OverflowError, termios.tcflush, self.fd, 2**1000)
@@ -190,7 +181,6 @@ def test_tcflow(self):
190181
termios.tcflow(self.fd, termios.TCIOFF)
191182
termios.tcflow(self.fd, termios.TCION)
192183

193-
@unittest.skip("TODO: RUSTPYTHON segfault")
194184
@support.skip_android_selinux('tcflow')
195185
def test_tcflow_errors(self):
196186
self.assertRaisesTermiosError(errno.EINVAL, termios.tcflow, self.fd, -1)

Lib/test/test_tty.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def test_cfmakecbreak(self):
6464
self.assertEqual(mode[tty.IFLAG] & termios.ICRNL, 0,
6565
msg="ICRNL should not be set by cbreak")
6666

67-
@unittest.expectedFailure # TODO: RUSTPYTHON TypeError: Expected type "int" but "FileIO" found.
6867
def test_setraw(self):
6968
mode0 = termios.tcgetattr(self.fd)
7069
mode1 = tty.setraw(self.fd)
@@ -76,7 +75,6 @@ def test_setraw(self):
7675
tty.setraw(self.stream)
7776
tty.setraw(fd=self.fd, when=termios.TCSANOW)
7877

79-
@unittest.expectedFailure # TODO: RUSTPYTHON TypeError: Expected type "int" but "FileIO" found.
8078
def test_setcbreak(self):
8179
mode0 = termios.tcgetattr(self.fd)
8280
mode1 = tty.setcbreak(self.fd)

crates/stdlib/src/termios.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod termios {
88
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
99
builtins::{PyBaseExceptionRef, PyBytes, PyInt, PyListRef, PyTypeRef},
1010
convert::ToPyObject,
11+
stdlib::_io::Fildes,
1112
};
1213
use rustpython_host_env::{os::ErrorExt, termios as host_termios};
1314

@@ -166,7 +167,8 @@ mod termios {
166167
};
167168

168169
#[pyfunction]
169-
fn tcgetattr(fd: i32, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
170+
fn tcgetattr(fd: PyObjectRef, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
171+
let fd = Fildes::try_from_object(vm, fd).map(Into::into)?;
170172
let termios = host_termios::tcgetattr(fd).map_err(|e| termios_error(e, vm))?;
171173
let noncanon = (termios.c_lflag & host_termios::ICANON) == 0;
172174
let cc = termios
@@ -191,7 +193,13 @@ mod termios {
191193
}
192194

193195
#[pyfunction]
194-
fn tcsetattr(fd: i32, when: i32, attributes: PyListRef, vm: &VirtualMachine) -> PyResult<()> {
196+
fn tcsetattr(
197+
fd: PyObjectRef,
198+
when: i32,
199+
attributes: PyListRef,
200+
vm: &VirtualMachine,
201+
) -> PyResult<()> {
202+
let fd = Fildes::try_from_object(vm, fd).map(Into::into)?;
195203
let [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] =
196204
<&[PyObjectRef; 7]>::try_from(&*attributes.borrow_vec())
197205
.map_err(|_| vm.new_type_error("tcsetattr, arg 3: must be 7 element list"))?
@@ -233,25 +241,29 @@ mod termios {
233241
}
234242

235243
#[pyfunction]
236-
fn tcsendbreak(fd: i32, duration: i32, vm: &VirtualMachine) -> PyResult<()> {
244+
fn tcsendbreak(fd: PyObjectRef, duration: i32, vm: &VirtualMachine) -> PyResult<()> {
245+
let fd = Fildes::try_from_object(vm, fd).map(Into::into)?;
237246
host_termios::tcsendbreak(fd, duration).map_err(|e| termios_error(e, vm))?;
238247
Ok(())
239248
}
240249

241250
#[pyfunction]
242-
fn tcdrain(fd: i32, vm: &VirtualMachine) -> PyResult<()> {
251+
fn tcdrain(fd: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
252+
let fd = Fildes::try_from_object(vm, fd).map(Into::into)?;
243253
host_termios::tcdrain(fd).map_err(|e| termios_error(e, vm))?;
244254
Ok(())
245255
}
246256

247257
#[pyfunction]
248-
fn tcflush(fd: i32, queue: i32, vm: &VirtualMachine) -> PyResult<()> {
258+
fn tcflush(fd: PyObjectRef, queue: i32, vm: &VirtualMachine) -> PyResult<()> {
259+
let fd = Fildes::try_from_object(vm, fd).map(Into::into)?;
249260
host_termios::tcflush(fd, queue).map_err(|e| termios_error(e, vm))?;
250261
Ok(())
251262
}
252263

253264
#[pyfunction]
254-
fn tcflow(fd: i32, action: i32, vm: &VirtualMachine) -> PyResult<()> {
265+
fn tcflow(fd: PyObjectRef, action: i32, vm: &VirtualMachine) -> PyResult<()> {
266+
let fd = Fildes::try_from_object(vm, fd).map(Into::into)?;
255267
host_termios::tcflow(fd, action).map_err(|e| termios_error(e, vm))?;
256268
Ok(())
257269
}

crates/vm/src/stdlib/_io.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ impl TryFromObject for Fildes {
8989
}
9090
}
9191

92+
impl From<Fildes> for i32 {
93+
fn from(fildes: Fildes) -> Self {
94+
fildes.0
95+
}
96+
}
97+
9298
#[cfg(unix)]
9399
impl std::os::fd::AsFd for Fildes {
94100
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
@@ -97,6 +103,7 @@ impl std::os::fd::AsFd for Fildes {
97103
unsafe { std::os::fd::BorrowedFd::borrow_raw(self.0) }
98104
}
99105
}
106+
100107
#[cfg(unix)]
101108
impl std::os::fd::AsRawFd for Fildes {
102109
fn as_raw_fd(&self) -> std::os::fd::RawFd {

0 commit comments

Comments
 (0)