]> granicus.if.org Git - python/commitdiff
Issue #12326: refactor usage of sys.platform
authorVictor Stinner <victor.stinner@haypocalc.com>
Sat, 20 Aug 2011 22:39:18 +0000 (00:39 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sat, 20 Aug 2011 22:39:18 +0000 (00:39 +0200)
 * 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')

Lib/ctypes/util.py
Lib/distutils/command/build_ext.py
Lib/packaging/command/build_ext.py
Lib/test/support.py
Lib/test/test_fcntl.py
Lib/test/test_logging.py
Lib/test/test_socket.py
setup.py

index 6815f941d568ffd464ac3405a0a74bbd4d13e4a7..97d0c2f3fa48ad9e8d3e5d25cefc526d3f572b4e 100644 (file)
@@ -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 ]
index 8d843d689fdbdebd54a725c3690e9923d3737082..8baf538f2adb53e3574d9f6736dbbe55bb16149f 100644 (file)
@@ -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
index c82033608571d216b610bdc5c329470dc0572ad5..b0c3f16e8366a5a113b7d7b95c430abc8b370cd8 100644 (file)
@@ -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
index 64c1b4e454b5f7b63d645c0b9c7e5c9ee37f1f22..b3989e52a887baf37812f29d0482578c44c9adc9 100644 (file)
@@ -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('.')))
index 6e425aa7ef57a9a223faf655d3c4c97bc14c6db3..29af99cf1624a7d7cef94bde0cedf32040eb5d9e 100644 (file)
@@ -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'
index 35beae4cb0acc560ce32da367cafba765897cb74..556bbba3131ba9a3b343a43977b60375e5f61560 100644 (file)
@@ -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)
index 320f3735b5f3568b87714b7e2d311a046f803894..4e5085ebdc57e0726477b6fbfd6d6d31524b13c5 100644 (file)
@@ -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)
index e12768590b8d80c2ae59d85cce51ac89652fbbb4..542dc496df8c03220276f292e66b8e73194b3641 100644 (file)
--- 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):