Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix test_os on Windows
  • Loading branch information
vstinner committed Sep 26, 2025
commit 206e5001089e1f774660b5f70ddf91e279be5ce2
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_dirfd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

import errno
import os
import posix
import stat
import time
import unittest
from contextlib import contextmanager
from test import support
from test.support import os_helper

try:
import posix
except ImportError:
import nt as posix


class TestPosixDirFd(unittest.TestCase):
count = 0
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import errno
import itertools
import os
import posix
import shutil
import stat
import subprocess
Expand All @@ -16,6 +15,11 @@
from test.support import os_helper
from .utils import create_file

try:
import posix
except ImportError:
import nt as posix


class WalkTests(unittest.TestCase):
"""Tests for os.walk()."""
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_os/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_bad_fd(self):
(hasattr(locale, 'nl_langinfo') and hasattr(locale, 'CODESET'))),
'test requires a tty and either Windows or nl_langinfo(CODESET)')
def test_device_encoding(self):
ncoding = os.device_encoding(0)
encoding = os.device_encoding(0)
self.assertIsNotNone(encoding)
self.assertTrue(codecs.lookup(encoding))

Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

import os
import posix
import subprocess
import sys
import unittest
Expand All @@ -13,6 +12,11 @@
from test.support import os_helper
from test.support import unix_shell

try:
import posix
except ImportError:
import nt as posix


class EnvironTests(mapping_tests.BasicTestMappingProtocol):
"""check that os.environ object conform to mapping protocol"""
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_fd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import errno
import itertools
import os
import posix
import select
import struct
import sys
Expand All @@ -14,6 +13,11 @@
from test import support
from test.support import os_helper

try:
import posix
except ImportError:
import nt as posix


@unittest.skipIf(support.is_wasi, "Cannot create invalid FD on WASI.")
class TestInvalidFD(unittest.TestCase):
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

import errno
import os
import posix
import sys
import unittest
from test import support
from test.support import os_helper
from .utils import create_file

try:
import posix
except ImportError:
import nt as posix

try:
import fcntl
except ImportError:
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_file_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import fractions
import os
import platform
import posix
import stat
import sys
import tempfile
Expand All @@ -16,6 +15,11 @@
from test.support import os_helper
from .utils import create_file

try:
import posix
except ImportError:
import nt as posix

try:
import pwd
all_users = [u.pw_uid for u in pwd.getpwall()]
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
"""

import os
import posix
import shutil
import sys
import unittest
from test.support import os_helper
from test.support.os_helper import FakePath
from .utils import create_file

try:
import posix
except ImportError:
import nt as posix


@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
class LinkTests(unittest.TestCase):
Expand Down
12 changes: 9 additions & 3 deletions Lib/test/test_os/test_macos_weaklink.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
Test macOS weak linking.
"""

import os
import posix
import sys
import unittest
if sys.platform != "darwin":
raise unittest.SkipTest("test weak linking on macOS")

import os
from test.support import os_helper

try:
import posix
except ImportError:
import nt as posix


@unittest.skipUnless(sys.platform == "darwin", "test weak linking on macOS")
class TestPosixWeaklinking(unittest.TestCase):
# These test cases verify that weak linking support on macOS works
# as expected. These cases only test new behaviour introduced by weak linking,
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_os/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import errno
import os
import platform
import posix
import signal
import stat
import subprocess
Expand All @@ -22,6 +21,10 @@
from test.support.script_helper import assert_python_ok
from .utils import requires_sched

try:
import posix
except ImportError:
import nt as posix
try:
import _testcapi
except ImportError:
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

import errno
import os
import resource
import sys
import sysconfig
import unittest
from test.support import os_helper
from test.support.script_helper import assert_python_ok
from .utils import create_file

try:
import resource
except ImportError:
resource = None


@unittest.skipUnless(hasattr(os, 'getrandom'), 'need os.getrandom()')
class GetRandomTests(unittest.TestCase):
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import errno
import os
import pickle
import posix
import sys
import unittest
from test import support
from test.support.script_helper import assert_python_ok
from .utils import requires_sched

try:
import posix
except ImportError:
import nt as posix


class PosixTester(unittest.TestCase):

Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import errno
import os
import pickle
import posix
import stat
import subprocess
import sys
import unittest
from test.support import os_helper
from .utils import create_file

try:
import posix
except ImportError:
import nt as posix


# Test attributes on return values from os.*stat* family.
class StatAttributeTests(unittest.TestCase):
Expand Down
12 changes: 10 additions & 2 deletions Lib/test/test_os/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@

import errno
import os
import posix
import pwd
import subprocess
import sys
import unittest
from test import support

try:
import posix
except ImportError:
import nt as posix

try:
import pwd
except ImportError:
pwd = None

# Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group.
HAVE_WHEEL_GROUP = (sys.platform.startswith('freebsd') and os.getgid() == 0)

Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_os/test_utime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import decimal
import fractions
import os
import posix
import sys
import time
import unittest
from test import support
from test.support import os_helper
from .utils import create_file

try:
import posix
except ImportError:
import nt as posix


class UtimeTests(unittest.TestCase):
def setUp(self):
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_os/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from test import support
from test.support import import_helper
from test.support import os_helper
from test.support import warnings_helper
from .utils import create_file


Expand All @@ -45,6 +46,25 @@ def test_write_windows_console(self):


class PidTests(unittest.TestCase):
@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
def check_waitpid(self, code, exitcode, callback=None):
if sys.platform == 'win32':
# On Windows, os.spawnv() simply joins arguments with spaces:
# arguments need to be quoted
args = [f'"{sys.executable}"', '-c', f'"{code}"']
else:
args = [sys.executable, '-c', code]
pid = os.spawnv(os.P_NOWAIT, sys.executable, args)

if callback is not None:
callback(pid)

# don't use support.wait_process() to test directly os.waitpid()
# and os.waitstatus_to_exitcode()
pid2, status = os.waitpid(pid, 0)
self.assertEqual(os.waitstatus_to_exitcode(status), exitcode)
self.assertEqual(pid2, pid)

def test_waitpid_windows(self):
# bpo-40138: test os.waitpid() and os.waitstatus_to_exitcode()
# with exit code larger than INT_MAX.
Expand Down