From 777da5b7ad42a7c6db73b56ca9ae1e74ebbb070c Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 28 Apr 2025 11:24:57 +0300 Subject: [PATCH 1/3] gh-132742: Fix tcflush() tests on Android --- Lib/test/test_ioctl.py | 6 ++++-- Lib/test/test_termios.py | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_ioctl.py b/Lib/test/test_ioctl.py index 9fd3b6196b81c2..f5831ff5ddd6eb 100644 --- a/Lib/test/test_ioctl.py +++ b/Lib/test/test_ioctl.py @@ -143,7 +143,9 @@ def setUp(self): def test_ioctl_clear_input_or_output(self): wfd = self.slave_fd rfd = self.master_fd - inbuf = sys.platform == 'linux' + # The data is buffered in input buffer on Linux, and in + # output buffer on other platforms. + inbuf = sys.platform in ('linux', 'android') os.write(wfd, b'abcdef') self.assertEqual(os.read(rfd, 2), b'ab') @@ -163,7 +165,7 @@ def test_ioctl_clear_input_or_output(self): os.write(wfd, b'ABCDEF') self.assertEqual(os.read(rfd, 1024), b'ABCDEF') - @unittest.skipUnless(sys.platform == 'linux', 'only works on Linux') + @unittest.skipUnless(sys.platform in ('linux', 'android'), 'only works on Linux') @unittest.skipUnless(hasattr(termios, 'TCXONC'), 'requires termios.TCXONC') def test_ioctl_suspend_and_resume_output(self): wfd = self.slave_fd diff --git a/Lib/test/test_termios.py b/Lib/test/test_termios.py index 18965f09a2569e..78d4d2c7ec2d1f 100644 --- a/Lib/test/test_termios.py +++ b/Lib/test/test_termios.py @@ -152,7 +152,9 @@ def test_tcflush_errors(self): def test_tcflush_clear_input_or_output(self): wfd = self.fd rfd = self.master_fd - inbuf = sys.platform == 'linux' + # The data is buffered in input buffer on Linux, and in + # output buffer on other platforms. + inbuf = sys.platform in ('linux', 'android') os.write(wfd, b'abcdef') self.assertEqual(os.read(rfd, 2), b'ab') @@ -190,7 +192,8 @@ def test_tcflow_errors(self): self.assertRaises(TypeError, termios.tcflow, object(), termios.TCOON) self.assertRaises(TypeError, termios.tcflow, self.fd) - @unittest.skipUnless(sys.platform == 'linux', 'only works on Linux') + @support.skip_android_selinux('tcflow') + @unittest.skipUnless(sys.platform in ('linux', 'android'), 'only works on Linux') def test_tcflow_suspend_and_resume_output(self): wfd = self.fd rfd = self.master_fd From 3642a32513c1036b7691e9a5cebccfc0dc73e08a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 28 Apr 2025 14:17:21 +0300 Subject: [PATCH 2/3] Skip test_ioctl_suspend_and_resume_output on Android too. --- Lib/test/test_ioctl.py | 19 +++++++++++-------- Lib/test/test_termios.py | 14 ++++++++------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Lib/test/test_ioctl.py b/Lib/test/test_ioctl.py index f5831ff5ddd6eb..da5b6b5ea55cce 100644 --- a/Lib/test/test_ioctl.py +++ b/Lib/test/test_ioctl.py @@ -4,7 +4,7 @@ import sys import threading import unittest -from test.support import get_attribute +from test import support from test.support import threading_helper from test.support.import_helper import import_module fcntl = import_module('fcntl') @@ -13,7 +13,7 @@ class IoctlTestsTty(unittest.TestCase): @classmethod def setUpClass(cls): - TIOCGPGRP = get_attribute(termios, 'TIOCGPGRP') + TIOCGPGRP = support.get_attribute(termios, 'TIOCGPGRP') try: tty = open("/dev/tty", "rb") except OSError: @@ -143,8 +143,8 @@ def setUp(self): def test_ioctl_clear_input_or_output(self): wfd = self.slave_fd rfd = self.master_fd - # The data is buffered in input buffer on Linux, and in - # output buffer on other platforms. + # The data is buffered in the input buffer on Linux, and in + # the output buffer on other platforms. inbuf = sys.platform in ('linux', 'android') os.write(wfd, b'abcdef') @@ -165,6 +165,7 @@ def test_ioctl_clear_input_or_output(self): os.write(wfd, b'ABCDEF') self.assertEqual(os.read(rfd, 1024), b'ABCDEF') + @support.skip_android_selinux('tcflow') @unittest.skipUnless(sys.platform in ('linux', 'android'), 'only works on Linux') @unittest.skipUnless(hasattr(termios, 'TCXONC'), 'requires termios.TCXONC') def test_ioctl_suspend_and_resume_output(self): @@ -175,20 +176,22 @@ def test_ioctl_suspend_and_resume_output(self): def writer(): os.write(wfd, b'abc') - write_suspended.wait() + self.assertTrue(write_suspended.wait(5)) os.write(wfd, b'def') write_finished.set() with threading_helper.start_threads([threading.Thread(target=writer)]): self.assertEqual(os.read(rfd, 3), b'abc') try: - fcntl.ioctl(wfd, termios.TCXONC, termios.TCOOFF) - write_suspended.set() + try: + fcntl.ioctl(wfd, termios.TCXONC, termios.TCOOFF) + finally: + write_suspended.set() self.assertFalse(write_finished.wait(0.5), 'output was not suspended') finally: fcntl.ioctl(wfd, termios.TCXONC, termios.TCOON) - self.assertTrue(write_finished.wait(0.5), + self.assertTrue(write_finished.wait(5), 'output was not resumed') self.assertEqual(os.read(rfd, 1024), b'def') diff --git a/Lib/test/test_termios.py b/Lib/test/test_termios.py index 78d4d2c7ec2d1f..b5e919012ab0fc 100644 --- a/Lib/test/test_termios.py +++ b/Lib/test/test_termios.py @@ -152,8 +152,8 @@ def test_tcflush_errors(self): def test_tcflush_clear_input_or_output(self): wfd = self.fd rfd = self.master_fd - # The data is buffered in input buffer on Linux, and in - # output buffer on other platforms. + # The data is buffered in the input buffer on Linux, and in + # the output buffer on other platforms. inbuf = sys.platform in ('linux', 'android') os.write(wfd, b'abcdef') @@ -202,20 +202,22 @@ def test_tcflow_suspend_and_resume_output(self): def writer(): os.write(wfd, b'abc') - write_suspended.wait() + self.assertTrue(write_suspended.wait(5)) os.write(wfd, b'def') write_finished.set() with threading_helper.start_threads([threading.Thread(target=writer)]): self.assertEqual(os.read(rfd, 3), b'abc') try: - termios.tcflow(wfd, termios.TCOOFF) - write_suspended.set() + try: + termios.tcflow(wfd, termios.TCOOFF) + finally: + write_suspended.set() self.assertFalse(write_finished.wait(0.5), 'output was not suspended') finally: termios.tcflow(wfd, termios.TCOON) - self.assertTrue(write_finished.wait(0.5), + self.assertTrue(write_finished.wait(5), 'output was not resumed') self.assertEqual(os.read(rfd, 1024), b'def') From c3a0c3db5634ddce0bb2713b9dfd0dae20018717 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 28 Apr 2025 17:28:34 +0300 Subject: [PATCH 3/3] Use support.SHORT_TIMEOUT. --- Lib/test/test_ioctl.py | 4 ++-- Lib/test/test_termios.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_ioctl.py b/Lib/test/test_ioctl.py index da5b6b5ea55cce..7a986048bdac2a 100644 --- a/Lib/test/test_ioctl.py +++ b/Lib/test/test_ioctl.py @@ -176,7 +176,7 @@ def test_ioctl_suspend_and_resume_output(self): def writer(): os.write(wfd, b'abc') - self.assertTrue(write_suspended.wait(5)) + self.assertTrue(write_suspended.wait(support.SHORT_TIMEOUT)) os.write(wfd, b'def') write_finished.set() @@ -191,7 +191,7 @@ def writer(): 'output was not suspended') finally: fcntl.ioctl(wfd, termios.TCXONC, termios.TCOON) - self.assertTrue(write_finished.wait(5), + self.assertTrue(write_finished.wait(support.SHORT_TIMEOUT), 'output was not resumed') self.assertEqual(os.read(rfd, 1024), b'def') diff --git a/Lib/test/test_termios.py b/Lib/test/test_termios.py index b5e919012ab0fc..e5d11cf84d2a66 100644 --- a/Lib/test/test_termios.py +++ b/Lib/test/test_termios.py @@ -202,7 +202,7 @@ def test_tcflow_suspend_and_resume_output(self): def writer(): os.write(wfd, b'abc') - self.assertTrue(write_suspended.wait(5)) + self.assertTrue(write_suspended.wait(support.SHORT_TIMEOUT)) os.write(wfd, b'def') write_finished.set() @@ -217,7 +217,7 @@ def writer(): 'output was not suspended') finally: termios.tcflow(wfd, termios.TCOON) - self.assertTrue(write_finished.wait(5), + self.assertTrue(write_finished.wait(support.SHORT_TIMEOUT), 'output was not resumed') self.assertEqual(os.read(rfd, 1024), b'def')