MCPcopy Index your code
hub / github.com/python/cpython / test_openpty

Method test_openpty

Lib/test/test_pty.py:111–194  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

109
110 @expectedFailureIfStdinIsTTY
111 def test_openpty(self):
112 try:
113 mode = tty.tcgetattr(pty.STDIN_FILENO)
114 except tty.error:
115 # Not a tty or bad/closed fd.
116 debug("tty.tcgetattr(pty.STDIN_FILENO) failed")
117 mode = None
118
119 new_dim = None
120 if self.stdin_dim:
121 try:
122 # Modify pty.STDIN_FILENO window size; we need to
123 # check if pty.openpty() is able to set pty slave
124 # window size accordingly.
125 debug("Setting pty.STDIN_FILENO window size.")
126 debug(f"original size: (row, col) = {self.stdin_dim}")
127 target_dim = (self.stdin_dim[0] + 1, self.stdin_dim[1] + 1)
128 debug(f"target size: (row, col) = {target_dim}")
129 tty.tcsetwinsize(pty.STDIN_FILENO, target_dim)
130
131 # Were we able to set the window size
132 # of pty.STDIN_FILENO successfully?
133 new_dim = tty.tcgetwinsize(pty.STDIN_FILENO)
134 self.assertEqual(new_dim, target_dim,
135 "pty.STDIN_FILENO window size unchanged")
136 except OSError as e:
137 logging.getLogger(__name__).warning(
138 "Failed to set pty.STDIN_FILENO window size.", exc_info=e,
139 )
140 pass
141
142 try:
143 debug("Calling pty.openpty()")
144 try:
145 master_fd, slave_fd, slave_name = pty.openpty(mode, new_dim,
146 True)
147 except TypeError:
148 master_fd, slave_fd = pty.openpty()
149 slave_name = None
150 debug(f"Got {master_fd=}, {slave_fd=}, {slave_name=}")
151 except OSError:
152 # " An optional feature could not be imported " ... ?
153 raise unittest.SkipTest("Pseudo-terminals (seemingly) not functional.")
154
155 # closing master_fd can raise a SIGHUP if the process is
156 # the session leader: we installed a SIGHUP signal handler
157 # to ignore this signal.
158 self.addCleanup(os.close, master_fd)
159 self.addCleanup(os.close, slave_fd)
160
161 self.assertTrue(os.isatty(slave_fd), "slave_fd is not a tty")
162
163 if mode:
164 self.assertEqual(tty.tcgetattr(slave_fd), mode,
165 "openpty() failed to set slave termios")
166 if new_dim:
167 self.assertEqual(tty.tcgetwinsize(slave_fd), new_dim,
168 "openpty() failed to set slave window size")

Callers

nothing calls this directly

Calls 11

write_allFunction · 0.85
_readlineFunction · 0.85
normalize_outputFunction · 0.85
getLoggerMethod · 0.80
addCleanupMethod · 0.80
assertTrueMethod · 0.80
debugFunction · 0.70
assertEqualMethod · 0.45
warningMethod · 0.45
isattyMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected