Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[3.11] gh-110388: Add tests for tty (GH-110394) #110622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-110388: Add tests for tty (GH-110394)
(cherry picked from commit 7f702b2)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
serhiy-storchaka authored and miss-islington committed Oct 10, 2023
commit 6e8135551e78fb6b86d24249b8aa4310119f49df
80 changes: 80 additions & 0 deletions 80 Lib/test/test_tty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import os
import unittest
from test.support.import_helper import import_module

termios = import_module('termios')
tty = import_module('tty')


@unittest.skipUnless(hasattr(os, 'openpty'), "need os.openpty()")
class TestTty(unittest.TestCase):

def setUp(self):
master_fd, self.fd = os.openpty()
self.addCleanup(os.close, master_fd)
self.stream = self.enterContext(open(self.fd, 'wb', buffering=0))
self.fd = self.stream.fileno()
self.mode = termios.tcgetattr(self.fd)
self.addCleanup(termios.tcsetattr, self.fd, termios.TCSANOW, self.mode)
self.addCleanup(termios.tcsetattr, self.fd, termios.TCSAFLUSH, self.mode)

def check_cbreak(self, mode):
self.assertEqual(mode[0] & termios.ICRNL, 0)
self.assertEqual(mode[3] & termios.ECHO, 0)
self.assertEqual(mode[3] & termios.ICANON, 0)
self.assertEqual(mode[6][termios.VMIN], 1)
self.assertEqual(mode[6][termios.VTIME], 0)

def check_raw(self, mode):
self.check_cbreak(mode)
self.assertEqual(mode[0] & termios.ISTRIP, 0)
self.assertEqual(mode[0] & termios.ICRNL, 0)
self.assertEqual(mode[1] & termios.OPOST, 0)
self.assertEqual(mode[2] & termios.PARENB, termios.CS8 & termios.PARENB)
self.assertEqual(mode[2] & termios.CSIZE, termios.CS8 & termios.CSIZE)
self.assertEqual(mode[2] & termios.CS8, termios.CS8)
self.assertEqual(mode[3] & termios.ECHO, 0)
self.assertEqual(mode[3] & termios.ICANON, 0)
self.assertEqual(mode[3] & termios.ISIG, 0)
self.assertEqual(mode[6][termios.VMIN], 1)
self.assertEqual(mode[6][termios.VTIME], 0)

def test_cfmakeraw(self):
mode = termios.tcgetattr(self.fd)
self.assertEqual(mode, self.mode)
tty.cfmakeraw(mode)
self.check_raw(mode)
self.assertEqual(mode[4], self.mode[4])
self.assertEqual(mode[5], self.mode[5])

def test_cfmakecbreak(self):
mode = termios.tcgetattr(self.fd)
self.assertEqual(mode, self.mode)
tty.cfmakecbreak(mode)
self.check_cbreak(mode)
self.assertEqual(mode[1], self.mode[1])
self.assertEqual(mode[2], self.mode[2])
self.assertEqual(mode[4], self.mode[4])
self.assertEqual(mode[5], self.mode[5])

def test_setraw(self):
mode = tty.setraw(self.fd)
mode2 = termios.tcgetattr(self.fd)
self.check_raw(mode2)
mode3 = tty.setraw(self.fd, termios.TCSANOW)
self.assertEqual(mode3, mode2)
tty.setraw(self.stream)
tty.setraw(fd=self.fd, when=termios.TCSANOW)

def test_setcbreak(self):
mode = tty.setcbreak(self.fd)
mode2 = termios.tcgetattr(self.fd)
self.check_cbreak(mode2)
mode3 = tty.setcbreak(self.fd, termios.TCSANOW)
self.assertEqual(mode3, mode2)
tty.setcbreak(self.stream)
tty.setcbreak(fd=self.fd, when=termios.TCSANOW)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add tests for :mod:`tty`.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.