Skip to content

Commit ba16058

Browse files
committed
gh-157899: Factorize support._requires_unix_version() code
Reuse the same code for _requires_unix_version() and _requires_apple_os_version(). Check sys.platform instead of platform.system().
1 parent c7a1716 commit ba16058

1 file changed

Lines changed: 26 additions & 33 deletions

File tree

‎Lib/test/support/__init__.py‎

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -348,16 +348,17 @@ def requires(resource, msg=None):
348348
if resource == 'gui' and not _is_gui_available():
349349
raise ResourceDenied(_is_gui_available.reason)
350350

351-
def _requires_unix_version(sysname, min_version):
352-
"""Decorator raising SkipTest if the OS is `sysname` and the version is less
351+
352+
def _requires_platform_version(name, sys_platform, get_version_text, min_version):
353+
"""
354+
Decorator raising SkipTest if the OS is `sysname` and the version is less
353355
than `min_version`.
354356
355-
For example, @_requires_unix_version('FreeBSD', (7, 2)) raises SkipTest if
356-
the FreeBSD version is less than 7.2.
357+
For example, @_requires_platform_version('FreeBSD', 'freebsd', (7, 2))
358+
raises SkipTest if the FreeBSD version is less than 7.2.
357359
"""
358-
min_version_txt = '.'.join(map(str, min_version))
359-
version_txt = platform.release().split('-', 1)[0]
360-
if platform.system() == sysname:
360+
version_txt = get_version_text()
361+
if sys.platform == sys_platform:
361362
try:
362363
version = tuple(map(int, version_txt.split('.')))
363364
except ValueError:
@@ -367,21 +368,31 @@ def _requires_unix_version(sysname, min_version):
367368
else:
368369
skip = False
369370

371+
min_version_txt = '.'.join(map(str, min_version))
370372
return unittest.skipIf(
371373
skip,
372-
f"{sysname} version {min_version_txt} or higher required, not "
374+
f"{name} version {min_version_txt} or higher required, not "
373375
f"{version_txt}"
374376
)
375377

376378

379+
def _get_platform_release():
380+
# Replace '7.2.8-200.fc44.x86_64' with '7.2.8'
381+
return platform.release().partition('-')[0]
382+
383+
377384
def requires_freebsd_version(*min_version):
378385
"""Decorator raising SkipTest if the OS is FreeBSD and the FreeBSD version is
379386
less than `min_version`.
380387
381388
For example, @requires_freebsd_version(7, 2) raises SkipTest if the FreeBSD
382389
version is less than 7.2.
383390
"""
384-
return _requires_unix_version('FreeBSD', min_version)
391+
return _requires_platform_version(
392+
'FreeBSD', 'freebsd',
393+
get_version_text=_get_platform_release,
394+
min_version=min_version)
395+
385396

386397
def requires_linux_version(*min_version):
387398
"""Decorator raising SkipTest if the OS is Linux and the Linux version is
@@ -390,28 +401,10 @@ def requires_linux_version(*min_version):
390401
For example, @requires_linux_version(2, 6, 32) raises SkipTest if the Linux
391402
version is less than 2.6.32.
392403
"""
393-
return _requires_unix_version('Linux', min_version)
394-
395-
def _requires_apple_os_version(name, sys_platform, get_version_text, min_version):
396-
def decorator(func):
397-
@functools.wraps(func)
398-
def wrapper(*args, **kw):
399-
if sys.platform == sys_platform:
400-
version_txt = get_version_text()
401-
try:
402-
version = tuple(map(int, version_txt.split('.')))
403-
except ValueError:
404-
pass
405-
else:
406-
if version < min_version:
407-
min_version_txt = '.'.join(map(str, min_version))
408-
raise unittest.SkipTest(
409-
f"{name} {min_version_txt} or higher required, "
410-
+ f"not {version_txt}")
411-
return func(*args, **kw)
412-
wrapper.min_version = min_version
413-
return wrapper
414-
return decorator
404+
return _requires_platform_version(
405+
"Linux", "linux",
406+
get_version_text=_get_platform_release,
407+
min_version=min_version)
415408

416409

417410
def requires_mac_ver(*min_version):
@@ -421,7 +414,7 @@ def requires_mac_ver(*min_version):
421414
For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version
422415
is lesser than 10.5.
423416
"""
424-
return _requires_apple_os_version(
417+
return _requires_platform_version(
425418
"macOS", "darwin",
426419
get_version_text=lambda: platform.mac_ver()[0],
427420
min_version=min_version)
@@ -434,7 +427,7 @@ def requires_ios_ver(*min_version):
434427
For example, requires_ios_ver(16, 0) raises SkipTest if the iOS version
435428
is lesser than 16.0.
436429
"""
437-
return _requires_apple_os_version(
430+
return _requires_platform_version(
438431
"iOS", "ios",
439432
get_version_text=lambda: platform.ios_ver().release,
440433
min_version=min_version)

0 commit comments

Comments
 (0)