From 13cd49fa46eca0f3ce20fa48b28baa268c4343a1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 1 Oct 2025 16:43:43 +0200 Subject: [PATCH 1/4] gh-139322: Remove test_os Win32ErrorTests test_os OSErrorTests already covers the OSError class and is more complete than Win32ErrorTests. --- Lib/test/test_os/test_os.py | 8 ++++++++ Lib/test/test_os/test_windows.py | 35 -------------------------------- 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 623d05235835c2..aa5d2f668f39a7 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -4080,6 +4080,7 @@ def test_oserror_filename(self): (self.filenames, os.listdir,), (self.filenames, os.rename, "dst"), (self.filenames, os.replace, "dst"), + (self.filenames, os.utime, None), ] if os_helper.can_chmod(): funcs.append((self.filenames, os.chmod, 0o777)) @@ -4120,6 +4121,13 @@ def test_oserror_filename(self): else: self.fail(f"No exception thrown by {func}") + def test_mkdir(self): + filename = os_helper.TESTFN + self.addCleanup(os_helper.unlink, filename) + create_file(filename) + self.assertRaises(OSError, os.mkdir, filename) + + class CPUCountTests(unittest.TestCase): def check_cpu_count(self, cpus): if cpus is None: diff --git a/Lib/test/test_os/test_windows.py b/Lib/test/test_os/test_windows.py index b306a037399fc8..f1c6283f60d35e 100644 --- a/Lib/test/test_os/test_windows.py +++ b/Lib/test/test_os/test_windows.py @@ -21,41 +21,6 @@ from .utils import create_file -class Win32ErrorTests(unittest.TestCase): - def setUp(self): - try: - os.stat(os_helper.TESTFN) - except FileNotFoundError: - exists = False - except OSError as exc: - exists = True - self.fail("file %s must not exist; os.stat failed with %s" - % (os_helper.TESTFN, exc)) - else: - self.fail("file %s must not exist" % os_helper.TESTFN) - - def test_rename(self): - self.assertRaises(OSError, os.rename, os_helper.TESTFN, os_helper.TESTFN+".bak") - - def test_remove(self): - self.assertRaises(OSError, os.remove, os_helper.TESTFN) - - def test_chdir(self): - self.assertRaises(OSError, os.chdir, os_helper.TESTFN) - - def test_mkdir(self): - self.addCleanup(os_helper.unlink, os_helper.TESTFN) - - with open(os_helper.TESTFN, "x") as f: - self.assertRaises(OSError, os.mkdir, os_helper.TESTFN) - - def test_utime(self): - self.assertRaises(OSError, os.utime, os_helper.TESTFN, None) - - def test_chmod(self): - self.assertRaises(OSError, os.chmod, os_helper.TESTFN, 0) - - class Win32KillTests(unittest.TestCase): def _kill(self, sig): # Start sys.executable as a subprocess and communicate from the From 2b28f272e1c567f14812d42934a9586c74d2528c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 1 Oct 2025 18:03:36 +0200 Subject: [PATCH 2/4] Use FileExistsError in test_mkdir() --- Lib/test/test_os/test_os.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index aa5d2f668f39a7..f4c06b86b5daf4 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -4125,7 +4125,7 @@ def test_mkdir(self): filename = os_helper.TESTFN self.addCleanup(os_helper.unlink, filename) create_file(filename) - self.assertRaises(OSError, os.mkdir, filename) + self.assertRaises(FileExistsError, os.mkdir, filename) class CPUCountTests(unittest.TestCase): From 23de82367a4b00e3346f3045c51aa4124bb4f455 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 2 Oct 2025 11:50:12 +0200 Subject: [PATCH 3/4] Add more tests --- Lib/test/test_os/test_os.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index f4c06b86b5daf4..ca24e7d64e8012 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -4123,10 +4123,15 @@ def test_oserror_filename(self): def test_mkdir(self): filename = os_helper.TESTFN + subdir = os.path.join(filename, 'subdir') + self.assertRaises(FileNotFoundError, os.mkdir, subdir) + self.addCleanup(os_helper.unlink, filename) create_file(filename) self.assertRaises(FileExistsError, os.mkdir, filename) + self.assertRaises(NotADirectoryError, os.mkdir, subdir) + class CPUCountTests(unittest.TestCase): def check_cpu_count(self, cpus): From 89326fcb6c836d4d79e15f84619a0486df9f5173 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 2 Oct 2025 18:15:03 +0200 Subject: [PATCH 4/4] Fix test_mkdir() on Windows --- Lib/test/test_os/test_os.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index ca24e7d64e8012..43f1a79a7c7328 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -4130,7 +4130,8 @@ def test_mkdir(self): create_file(filename) self.assertRaises(FileExistsError, os.mkdir, filename) - self.assertRaises(NotADirectoryError, os.mkdir, subdir) + self.assertRaises((NotADirectoryError, FileNotFoundError), + os.mkdir, subdir) class CPUCountTests(unittest.TestCase):