Skip to content

Commit 8fbbdf0

Browse files
authored
bpo-33671: Add support.MS_WINDOWS and support.MACOS (GH-7800)
* Add support.MS_WINDOWS: True if Python is running on Microsoft Windows. * Add support.MACOS: True if Python is running on Apple macOS. * Replace support.is_android with support.ANDROID * Replace support.is_jython with support.JYTHON * Cleanup code to initialize unix_shell
1 parent 209abf7 commit 8fbbdf0

16 files changed

+95
-91
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ def join_process(process):
103103
HAVE_GETVALUE = not getattr(_multiprocessing,
104104
'HAVE_BROKEN_SEM_GETVALUE', False)
105105

106-
WIN32 = (sys.platform == "win32")
107-
108106
from multiprocessing.connection import wait
109107

110108
def wait_for_handle(handle, timeout):
@@ -3806,7 +3804,7 @@ def record(*args):
38063804

38073805
class TestInvalidHandle(unittest.TestCase):
38083806

3809-
@unittest.skipIf(WIN32, "skipped on Windows")
3807+
@unittest.skipIf(support.MS_WINDOWS, "skipped on Windows")
38103808
def test_invalid_handles(self):
38113809
conn = multiprocessing.connection.Connection(44977608)
38123810
# check that poll() doesn't crash
@@ -4134,12 +4132,12 @@ def test_neg_timeout(self):
41344132

41354133
class TestInvalidFamily(unittest.TestCase):
41364134

4137-
@unittest.skipIf(WIN32, "skipped on Windows")
4135+
@unittest.skipIf(support.MS_WINDOWS, "skipped on Windows")
41384136
def test_invalid_family(self):
41394137
with self.assertRaises(ValueError):
41404138
multiprocessing.connection.Listener(r'\\.\test')
41414139

4142-
@unittest.skipUnless(WIN32, "skipped on non-Windows platforms")
4140+
@unittest.skipUnless(support.MS_WINDOWS, "skipped on non-Windows platforms")
41434141
def test_invalid_family_win32(self):
41444142
with self.assertRaises(ValueError):
41454143
multiprocessing.connection.Listener('/var/test.pipe')
@@ -4265,7 +4263,7 @@ def test_lock(self):
42654263
class TestCloseFds(unittest.TestCase):
42664264

42674265
def get_high_socket_fd(self):
4268-
if WIN32:
4266+
if support.MS_WINDOWS:
42694267
# The child process will not have any socket handles, so
42704268
# calling socket.fromfd() should produce WSAENOTSOCK even
42714269
# if there is a handle of the same number.
@@ -4283,7 +4281,7 @@ def get_high_socket_fd(self):
42834281
return fd
42844282

42854283
def close(self, fd):
4286-
if WIN32:
4284+
if support.MS_WINDOWS:
42874285
socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=fd).close()
42884286
else:
42894287
os.close(fd)

Lib/test/support/__init__.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@
9090
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
9191
"check__all__", "skip_unless_bind_unix_socket",
9292
# sys
93-
"is_jython", "is_android", "check_impl_detail", "unix_shell",
94-
"setswitchinterval",
93+
"JYTHON", "ANDROID", "check_impl_detail", "unix_shell",
94+
"setswitchinterval", "MS_WINDOWS", "MACOS",
9595
# network
9696
"HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource",
9797
"bind_unix_socket",
@@ -108,6 +108,21 @@
108108
"run_with_tz", "PGO", "missing_compiler_executable", "fd_count",
109109
]
110110

111+
112+
# True if Python is running on Microsoft Windows.
113+
MS_WINDOWS = (sys.platform == 'win32')
114+
115+
# True if Python is running on Apple macOS.
116+
MACOS = (sys.platform == 'darwin')
117+
118+
# True if Python runs on Jython
119+
# (Python implemented in Java running in a Java VM)
120+
JYTHON = sys.platform.startswith('java')
121+
122+
# True if Python runs on Android
123+
ANDROID = hasattr(sys, 'getandroidapilevel')
124+
125+
111126
class Error(Exception):
112127
"""Base class for regression test exceptions."""
113128

@@ -484,7 +499,7 @@ class USEROBJECTFLAGS(ctypes.Structure):
484499
raise ctypes.WinError()
485500
if not bool(uof.dwFlags & WSF_VISIBLE):
486501
reason = "gui not available (WSF_VISIBLE flag not set)"
487-
elif sys.platform == 'darwin':
502+
elif MACOS:
488503
# The Aqua Tk implementations on OS X can abort the process if
489504
# being called in an environment where a window server connection
490505
# cannot be made, for instance when invoked by a buildbot or ssh
@@ -600,7 +615,7 @@ def requires_mac_ver(*min_version):
600615
def decorator(func):
601616
@functools.wraps(func)
602617
def wrapper(*args, **kw):
603-
if sys.platform == 'darwin':
618+
if MACOS:
604619
version_txt = platform.mac_ver()[0]
605620
try:
606621
version = tuple(map(int, version_txt.split('.')))
@@ -788,14 +803,12 @@ def dec(*args, **kwargs):
788803

789804
requires_lzma = unittest.skipUnless(lzma, 'requires lzma')
790805

791-
is_jython = sys.platform.startswith('java')
792-
793-
is_android = hasattr(sys, 'getandroidapilevel')
794-
795-
if sys.platform != 'win32':
796-
unix_shell = '/system/bin/sh' if is_android else '/bin/sh'
797-
else:
806+
if MS_WINDOWS:
798807
unix_shell = None
808+
elif ANDROID:
809+
unix_shell = '/system/bin/sh'
810+
else:
811+
unix_shell = '/bin/sh'
799812

800813
# Filename used for testing
801814
if os.name == 'java':
@@ -854,7 +867,7 @@ def dec(*args, **kwargs):
854867

855868
# TESTFN_UNICODE is a non-ascii filename
856869
TESTFN_UNICODE = TESTFN + "-\xe0\xf2\u0258\u0141\u011f"
857-
if sys.platform == 'darwin':
870+
if MACOS:
858871
# In Mac OS X's VFS API file names are, by definition, canonically
859872
# decomposed Unicode, encoded using UTF-8. See QA1173:
860873
# http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
@@ -866,7 +879,7 @@ def dec(*args, **kwargs):
866879
# encoded by the filesystem encoding (in strict mode). It can be None if we
867880
# cannot generate such filename.
868881
TESTFN_UNENCODABLE = None
869-
if os.name == 'nt':
882+
if MS_WINDOWS:
870883
# skip win32s (0) or Windows 9x/ME (1)
871884
if sys.getwindowsversion().platform >= 2:
872885
# Different kinds of characters from various languages to minimize the
@@ -881,8 +894,8 @@ def dec(*args, **kwargs):
881894
'Unicode filename tests may not be effective'
882895
% (TESTFN_UNENCODABLE, TESTFN_ENCODING))
883896
TESTFN_UNENCODABLE = None
884-
# Mac OS X denies unencodable filenames (invalid utf-8)
885-
elif sys.platform != 'darwin':
897+
# macOS denies unencodable filenames (invalid utf-8)
898+
elif not MACOS:
886899
try:
887900
# ascii and utf-8 cannot encode the byte 0xff
888901
b'\xff'.decode(TESTFN_ENCODING)
@@ -1523,7 +1536,7 @@ def gc_collect():
15231536
objects to disappear.
15241537
"""
15251538
gc.collect()
1526-
if is_jython:
1539+
if JYTHON:
15271540
time.sleep(0.1)
15281541
gc.collect()
15291542
gc.collect()
@@ -1982,7 +1995,7 @@ def _check_docstrings():
19821995
"""Just used to check if docstrings are enabled"""
19831996

19841997
MISSING_C_DOCSTRINGS = (check_impl_detail() and
1985-
sys.platform != 'win32' and
1998+
not MS_WINDOWS and
19861999
not sysconfig.get_config_var('WITH_DOC_STRINGS'))
19872000

19882001
HAVE_DOCSTRINGS = (_check_docstrings.__doc__ is not None and
@@ -2592,7 +2605,7 @@ def __enter__(self):
25922605
except (ValueError, OSError):
25932606
pass
25942607

2595-
if sys.platform == 'darwin':
2608+
if MACOS:
25962609
# Check if the 'Crash Reporter' on OSX was configured
25972610
# in 'Developer' mode and warn that it will get triggered
25982611
# when it is.
@@ -2736,7 +2749,7 @@ def setswitchinterval(interval):
27362749
# Setting a very low gil interval on the Android emulator causes python
27372750
# to hang (issue #26939).
27382751
minimum_interval = 1e-5
2739-
if is_android and interval < minimum_interval:
2752+
if ANDROID and interval < minimum_interval:
27402753
global _is_android_emulator
27412754
if _is_android_emulator is None:
27422755
_is_android_emulator = (subprocess.check_output(
@@ -2782,7 +2795,7 @@ def fd_count():
27822795
pass
27832796

27842797
old_modes = None
2785-
if sys.platform == 'win32':
2798+
if MS_WINDOWS:
27862799
# bpo-25306, bpo-31009: Call CrtSetReportMode() to not kill the process
27872800
# on invalid file descriptor if Python is compiled in debug mode
27882801
try:

Lib/test/test_c_locale_coercion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# Apply some platform dependent overrides
2929
if sys.platform.startswith("linux"):
30-
if test.support.is_android:
30+
if test.support.ANDROID:
3131
# Android defaults to using UTF-8 for all system interfaces
3232
EXPECTED_C_LOCALE_STREAM_ENCODING = "utf-8"
3333
EXPECTED_C_LOCALE_FS_ENCODING = "utf-8"
@@ -335,7 +335,7 @@ def _check_c_locale_coercion(self,
335335
# locale environment variables are undefined or empty. When
336336
# this code path is run with environ['LC_ALL'] == 'C', then
337337
# LEGACY_LOCALE_WARNING is printed.
338-
if (test.support.is_android and
338+
if (test.support.ANDROID and
339339
_expected_warnings == [CLI_COERCION_WARNING]):
340340
_expected_warnings = None
341341
self._check_child_encoding_details(base_var_dict,

Lib/test/test_cmd_line.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ def test_undecodable_code(self):
187187
if not stdout.startswith(pattern):
188188
raise AssertionError("%a doesn't start with %a" % (stdout, pattern))
189189

190-
@unittest.skipUnless((sys.platform == 'darwin' or
191-
support.is_android), 'test specific to Mac OS X and Android')
190+
@unittest.skipUnless((support.MACOS or support.ANDROID),
191+
'test specific to macOS and Android')
192192
def test_osx_android_utf8(self):
193193
def check_output(text):
194194
decoded = text.decode('utf-8', 'surrogateescape')

Lib/test/test_codeop.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
Nick Mathewson
44
"""
55
import unittest
6-
from test.support import is_jython
6+
from test import support
77

88
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
99
import io
1010

11-
if is_jython:
11+
if support.JYTHON:
1212
import sys
1313

1414
def unify_callables(d):
@@ -21,7 +21,7 @@ class CodeopTests(unittest.TestCase):
2121

2222
def assertValid(self, str, symbol='single'):
2323
'''succeed iff str is a valid piece of code'''
24-
if is_jython:
24+
if support.JYTHON:
2525
code = compile_command(str, "<input>", symbol)
2626
self.assertTrue(code)
2727
if symbol == "single":
@@ -60,7 +60,7 @@ def test_valid(self):
6060
av = self.assertValid
6161

6262
# special case
63-
if not is_jython:
63+
if not support.JYTHON:
6464
self.assertEqual(compile_command(""),
6565
compile("pass", "<input>", 'single',
6666
PyCF_DONT_IMPLY_DEDENT))

Lib/test/test_faulthandler.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import subprocess
77
import sys
88
from test import support
9-
from test.support import script_helper, is_android
9+
from test.support import script_helper
1010
import tempfile
1111
import threading
1212
import unittest
@@ -18,7 +18,6 @@
1818
_testcapi = None
1919

2020
TIMEOUT = 0.5
21-
MS_WINDOWS = (os.name == 'nt')
2221

2322
def expected_traceback(lineno1, lineno2, header, min_count=1):
2423
regex = header
@@ -31,7 +30,7 @@ def expected_traceback(lineno1, lineno2, header, min_count=1):
3130

3231
def skip_segfault_on_android(test):
3332
# Issue #32138: Raising SIGSEGV on Android may not cause a crash.
34-
return unittest.skipIf(is_android,
33+
return unittest.skipIf(support.ANDROID,
3534
'raising SIGSEGV on Android is unreliable')(test)
3635

3736
@contextmanager
@@ -121,7 +120,7 @@ def check_windows_exception(self, code, line_number, name_regex, **kw):
121120
@unittest.skipIf(sys.platform.startswith('aix'),
122121
"the first page of memory is a mapped read-only on AIX")
123122
def test_read_null(self):
124-
if not MS_WINDOWS:
123+
if not support.MS_WINDOWS:
125124
self.check_fatal_error("""
126125
import faulthandler
127126
faulthandler.enable()
@@ -732,7 +731,7 @@ def test_stderr_None(self):
732731
with self.check_stderr_none():
733732
faulthandler.register(signal.SIGUSR1)
734733

735-
@unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
734+
@unittest.skipUnless(support.MS_WINDOWS, 'specific to Windows')
736735
def test_raise_exception(self):
737736
for exc, name in (
738737
('EXCEPTION_ACCESS_VIOLATION', 'access violation'),
@@ -747,7 +746,7 @@ def test_raise_exception(self):
747746
3,
748747
name)
749748

750-
@unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
749+
@unittest.skipUnless(support.MS_WINDOWS, 'specific to Windows')
751750
def test_ignore_exception(self):
752751
for exc_code in (
753752
0xE06D7363, # MSC exception ("Emsc")
@@ -763,7 +762,7 @@ def test_ignore_exception(self):
763762
self.assertEqual(output, [])
764763
self.assertEqual(exitcode, exc_code)
765764

766-
@unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
765+
@unittest.skipUnless(support.MS_WINDOWS, 'specific to Windows')
767766
def test_raise_nonfatal_exception(self):
768767
# These exceptions are not strictly errors. Letting
769768
# faulthandler display the traceback when they are
@@ -791,7 +790,7 @@ def test_raise_nonfatal_exception(self):
791790
self.assertIn(exitcode,
792791
(exc, exc & ~0x10000000))
793792

794-
@unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
793+
@unittest.skipUnless(support.MS_WINDOWS, 'specific to Windows')
795794
def test_disable_windows_exc_handler(self):
796795
code = dedent("""
797796
import faulthandler

Lib/test/test_import/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import test.support
2222
from test.support import (
23-
EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython,
23+
EnvironmentVarGuard, TESTFN, check_warnings, forget, JYTHON,
2424
make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask,
2525
unlink, unload, create_empty_file, cpython_only, TESTFN_UNENCODABLE,
2626
temp_dir, DirsOnSysPath)
@@ -148,7 +148,7 @@ def test_import(self):
148148
def test_with_extension(ext):
149149
# The extension is normally ".py", perhaps ".pyw".
150150
source = TESTFN + ext
151-
if is_jython:
151+
if JYTHON:
152152
pyc = TESTFN + "$py.class"
153153
else:
154154
pyc = TESTFN + ".pyc"

Lib/test/test_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ def test_large_file_ops(self):
583583
# On Windows and Mac OSX this test consumes large resources; It takes
584584
# a long time to build the >2 GiB file and takes >2 GiB of disk space
585585
# therefore the resource must be enabled to run this test.
586-
if sys.platform[:3] == 'win' or sys.platform == 'darwin':
586+
if support.MS_WINDOWS or support.MACOS:
587587
support.requires(
588588
'largefile',
589589
'test requires %s bytes and a long time to run' % self.LARGE)

Lib/test/test_largefile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import stat
66
import sys
77
import unittest
8+
from test import support
89
from test.support import TESTFN, requires, unlink
910
import io # C implementation of io
1011
import _pyio as pyio # Python implementation of io
@@ -145,7 +146,7 @@ def setUpModule():
145146
# takes a long time to build the >2 GiB file and takes >2 GiB of disk
146147
# space therefore the resource must be enabled to run this test.
147148
# If not, nothing after this line stanza will be executed.
148-
if sys.platform[:3] == 'win' or sys.platform == 'darwin':
149+
if support.MS_WINDOWS or support.MACOS:
149150
requires('largefile',
150151
'test requires %s bytes and a long time to run' % str(size))
151152
else:

0 commit comments

Comments
 (0)