Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e674747

Browse files
author
Victor Stinner
committed
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')
1 parent a993188 commit e674747

8 files changed

Lines changed: 12 additions & 20 deletions

File tree

Lib/ctypes/util.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ def _get_soname(f):
142142
return None
143143
return res.group(1)
144144

145-
if (sys.platform.startswith("freebsd")
146-
or sys.platform.startswith("openbsd")
147-
or sys.platform.startswith("dragonfly")):
145+
if sys.platform.startswith(("freebsd", "openbsd", "dragonfly")):
148146

149147
def _num_version(libname):
150148
# "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]

Lib/distutils/command/build_ext.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ def finalize_options(self):
240240
# for extensions under Linux or Solaris with a shared Python library,
241241
# Python's library directory must be appended to library_dirs
242242
sysconfig.get_config_var('Py_ENABLE_SHARED')
243-
if ((sys.platform.startswith('linux') or sys.platform.startswith('gnu')
244-
or sys.platform.startswith('sunos'))
243+
if (sys.platform.startswith(('linux', 'gnu', 'sunos'))
245244
and sysconfig.get_config_var('Py_ENABLE_SHARED')):
246245
if sys.executable.startswith(os.path.join(sys.exec_prefix, "bin")):
247246
# building third party extensions

Lib/packaging/command/build_ext.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ def finalize_options(self):
244244
# for extensions under Linux or Solaris with a shared Python library,
245245
# Python's library directory must be appended to library_dirs
246246
sysconfig.get_config_var('Py_ENABLE_SHARED')
247-
if ((sys.platform.startswith('linux') or sys.platform.startswith('gnu')
248-
or sys.platform.startswith('sunos'))
247+
if (sys.platform.startswith(('linux', 'gnu', 'sunos'))
249248
and sysconfig.get_config_var('Py_ENABLE_SHARED')):
250249
if sys.executable.startswith(os.path.join(sys.exec_prefix, "bin")):
251250
# building third party extensions

Lib/test/support.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def requires_linux_version(*min_version):
311311
def decorator(func):
312312
@functools.wraps(func)
313313
def wrapper(*args, **kw):
314-
if sys.platform.startswith('linux'):
314+
if sys.platform == 'linux':
315315
version_txt = platform.release().split('-', 1)[0]
316316
try:
317317
version = tuple(map(int, version_txt.split('.')))

Lib/test/test_fcntl.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ def get_lockdata():
2323
else:
2424
start_len = "qq"
2525

26-
if (any(sys.platform.startswith(prefix)
27-
for prefix in ('netbsd', 'freebsd', 'openbsd', 'bsdos'))
28-
or sys.platform in ('Darwin1.2', 'darwin')):
26+
if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
27+
or sys.platform == 'darwin'):
2928
if struct.calcsize('l') == 8:
3029
off_t = 'l'
3130
pid_t = 'i'

Lib/test/test_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ def test_name(self):
527527
def test_builtin_handlers(self):
528528
# We can't actually *use* too many handlers in the tests,
529529
# but we can try instantiating them with various options
530-
if sys.platform.startswith('linux') or sys.platform == 'darwin':
530+
if sys.platform in ('linux', 'darwin'):
531531
for existing in (True, False):
532532
fd, fn = tempfile.mkstemp()
533533
os.close(fd)

Lib/test/test_socket.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,8 @@ def testGetServBy(self):
442442
# Find one service that exists, then check all the related interfaces.
443443
# I've ordered this by protocols that have both a tcp and udp
444444
# protocol, at least for modern Linuxes.
445-
if (sys.platform.startswith('linux') or
446-
sys.platform.startswith('freebsd') or
447-
sys.platform.startswith('netbsd') or
448-
sys.platform == 'darwin'):
445+
if (sys.platform.startswith(('freebsd', 'netbsd'))
446+
or sys.platform in ('linux', 'darwin')):
449447
# avoid the 'echo' service on this platform, as there is an
450448
# assumption breaking non-standard port/protocol entry
451449
services = ('daytime', 'qotd', 'domain')
@@ -2074,7 +2072,7 @@ def test_main():
20742072
])
20752073
if hasattr(socket, "socketpair"):
20762074
tests.append(BasicSocketPairTest)
2077-
if sys.platform.startswith('linux'):
2075+
if sys.platform == 'linux':
20782076
tests.append(TestLinuxAbstractNamespace)
20792077
if isTipcAvailable():
20802078
tests.append(TIPCTest)

setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,8 @@ def build_extension(self, ext):
363363

364364
def get_platform(self):
365365
# Get value of sys.platform
366-
for platform in ['cygwin', 'darwin', 'osf1']:
367-
if sys.platform.startswith(platform):
368-
return platform
366+
if sys.platform.startswith('osf1'):
367+
return 'osf1'
369368
return sys.platform
370369

371370
def add_multiarch_paths(self):

0 commit comments

Comments
 (0)