From: Victor Stinner Date: Sat, 20 Aug 2011 22:39:18 +0000 (+0200) Subject: Issue #12326: refactor usage of sys.platform X-Git-Tag: v3.3.0a1~1657 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e67474725b5b13b48ba0007e89314214a6bdca0b;p=python Issue #12326: refactor usage of sys.platform * Use str.startswith(tuple): I didn't know this Python feature, Python rocks! * Replace sometimes sys.platform.startswith('linux') with sys.platform == 'linux' * sys.platform doesn't contain the major version on Cygwin on Mac OS X (it's just 'cygwin' and 'darwin') --- diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 6815f941d5..97d0c2f3fa 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -142,9 +142,7 @@ elif os.name == "posix": return None return res.group(1) - if (sys.platform.startswith("freebsd") - or sys.platform.startswith("openbsd") - or sys.platform.startswith("dragonfly")): + if sys.platform.startswith(("freebsd", "openbsd", "dragonfly")): def _num_version(libname): # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ] diff --git a/Lib/distutils/command/build_ext.py b/Lib/distutils/command/build_ext.py index 8d843d689f..8baf538f2a 100644 --- a/Lib/distutils/command/build_ext.py +++ b/Lib/distutils/command/build_ext.py @@ -240,8 +240,7 @@ class build_ext(Command): # for extensions under Linux or Solaris with a shared Python library, # Python's library directory must be appended to library_dirs sysconfig.get_config_var('Py_ENABLE_SHARED') - if ((sys.platform.startswith('linux') or sys.platform.startswith('gnu') - or sys.platform.startswith('sunos')) + if (sys.platform.startswith(('linux', 'gnu', 'sunos')) and sysconfig.get_config_var('Py_ENABLE_SHARED')): if sys.executable.startswith(os.path.join(sys.exec_prefix, "bin")): # building third party extensions diff --git a/Lib/packaging/command/build_ext.py b/Lib/packaging/command/build_ext.py index c820336085..b0c3f16e83 100644 --- a/Lib/packaging/command/build_ext.py +++ b/Lib/packaging/command/build_ext.py @@ -244,8 +244,7 @@ class build_ext(Command): # for extensions under Linux or Solaris with a shared Python library, # Python's library directory must be appended to library_dirs sysconfig.get_config_var('Py_ENABLE_SHARED') - if ((sys.platform.startswith('linux') or sys.platform.startswith('gnu') - or sys.platform.startswith('sunos')) + if (sys.platform.startswith(('linux', 'gnu', 'sunos')) and sysconfig.get_config_var('Py_ENABLE_SHARED')): if sys.executable.startswith(os.path.join(sys.exec_prefix, "bin")): # building third party extensions diff --git a/Lib/test/support.py b/Lib/test/support.py index 64c1b4e454..b3989e52a8 100644 --- a/Lib/test/support.py +++ b/Lib/test/support.py @@ -311,7 +311,7 @@ def requires_linux_version(*min_version): def decorator(func): @functools.wraps(func) def wrapper(*args, **kw): - if sys.platform.startswith('linux'): + if sys.platform == 'linux': version_txt = platform.release().split('-', 1)[0] try: version = tuple(map(int, version_txt.split('.'))) diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py index 6e425aa7ef..29af99cf16 100644 --- a/Lib/test/test_fcntl.py +++ b/Lib/test/test_fcntl.py @@ -23,9 +23,8 @@ def get_lockdata(): else: start_len = "qq" - if (any(sys.platform.startswith(prefix) - for prefix in ('netbsd', 'freebsd', 'openbsd', 'bsdos')) - or sys.platform in ('Darwin1.2', 'darwin')): + if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos')) + or sys.platform == 'darwin'): if struct.calcsize('l') == 8: off_t = 'l' pid_t = 'i' diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 35beae4cb0..556bbba313 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -527,7 +527,7 @@ class HandlerTest(BaseTest): def test_builtin_handlers(self): # We can't actually *use* too many handlers in the tests, # but we can try instantiating them with various options - if sys.platform.startswith('linux') or sys.platform == 'darwin': + if sys.platform in ('linux', 'darwin'): for existing in (True, False): fd, fn = tempfile.mkstemp() os.close(fd) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 320f3735b5..4e5085ebdc 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -442,10 +442,8 @@ class GeneralModuleTests(unittest.TestCase): # Find one service that exists, then check all the related interfaces. # I've ordered this by protocols that have both a tcp and udp # protocol, at least for modern Linuxes. - if (sys.platform.startswith('linux') or - sys.platform.startswith('freebsd') or - sys.platform.startswith('netbsd') or - sys.platform == 'darwin'): + if (sys.platform.startswith(('freebsd', 'netbsd')) + or sys.platform in ('linux', 'darwin')): # avoid the 'echo' service on this platform, as there is an # assumption breaking non-standard port/protocol entry services = ('daytime', 'qotd', 'domain') @@ -2074,7 +2072,7 @@ def test_main(): ]) if hasattr(socket, "socketpair"): tests.append(BasicSocketPairTest) - if sys.platform.startswith('linux'): + if sys.platform == 'linux': tests.append(TestLinuxAbstractNamespace) if isTipcAvailable(): tests.append(TIPCTest) diff --git a/setup.py b/setup.py index e12768590b..542dc496df 100644 --- a/setup.py +++ b/setup.py @@ -363,9 +363,8 @@ class PyBuildExt(build_ext): def get_platform(self): # Get value of sys.platform - for platform in ['cygwin', 'darwin', 'osf1']: - if sys.platform.startswith(platform): - return platform + if sys.platform.startswith('osf1'): + return 'osf1' return sys.platform def add_multiarch_paths(self):