FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-157899: Factorize support._requires_unix_version() code by vstinner · Pull Request #159523 · python/cpython · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
59 changes: 26 additions & 33 deletions Lib/test/support/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,17 @@ def requires(resource, msg=None):
if resource == 'gui' and not _is_gui_available():
raise ResourceDenied(_is_gui_available.reason)

def _requires_unix_version(sysname, min_version):
"""Decorator raising SkipTest if the OS is `sysname` and the version is less

def _requires_platform_version(name, sys_platform, get_version_text, min_version):
"""
Decorator raising SkipTest if the OS is `sysname` and the version is less
than `min_version`.

For example, @_requires_unix_version('FreeBSD', (7, 2)) raises SkipTest if
the FreeBSD version is less than 7.2.
For example, @_requires_platform_version('FreeBSD', 'freebsd', (7, 2))
raises SkipTest if the FreeBSD version is less than 7.2.
"""
min_version_txt = '.'.join(map(str, min_version))
version_txt = platform.release().split('-', 1)[0]
if platform.system() == sysname:
version_txt = get_version_text()
if sys.platform == sys_platform:
try:
version = tuple(map(int, version_txt.split('.')))
except ValueError:
Expand All @@ -367,21 +368,31 @@ def _requires_unix_version(sysname, min_version):
else:
skip = False

min_version_txt = '.'.join(map(str, min_version))
return unittest.skipIf(
skip,
f"{sysname} version {min_version_txt} or higher required, not "
f"{name} version {min_version_txt} or higher required, not "
f"{version_txt}"
)


def _get_platform_release():
# Replace '7.2.8-200.fc44.x86_64' with '7.2.8'
return platform.release().partition('-')[0]


def requires_freebsd_version(*min_version):
"""Decorator raising SkipTest if the OS is FreeBSD and the FreeBSD version is
less than `min_version`.

For example, @requires_freebsd_version(7, 2) raises SkipTest if the FreeBSD
version is less than 7.2.
"""
return _requires_unix_version('FreeBSD', min_version)
return _requires_platform_version(
'FreeBSD', 'freebsd',
get_version_text=_get_platform_release,
min_version=min_version)


def requires_linux_version(*min_version):
"""Decorator raising SkipTest if the OS is Linux and the Linux version is
Expand All @@ -390,28 +401,10 @@ def requires_linux_version(*min_version):
For example, @requires_linux_version(2, 6, 32) raises SkipTest if the Linux
version is less than 2.6.32.
"""
return _requires_unix_version('Linux', min_version)

def _requires_apple_os_version(name, sys_platform, get_version_text, min_version):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
if sys.platform == sys_platform:
version_txt = get_version_text()
try:
version = tuple(map(int, version_txt.split('.')))
except ValueError:
pass
else:
if version < min_version:
min_version_txt = '.'.join(map(str, min_version))
raise unittest.SkipTest(
f"{name} {min_version_txt} or higher required, "
+ f"not {version_txt}")
return func(*args, **kw)
wrapper.min_version = min_version
return wrapper
return decorator
return _requires_platform_version(
"Linux", "linux",
get_version_text=_get_platform_release,
min_version=min_version)


def requires_mac_ver(*min_version):
Expand All @@ -421,7 +414,7 @@ def requires_mac_ver(*min_version):
For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version
is lesser than 10.5.
"""
return _requires_apple_os_version(
return _requires_platform_version(
"macOS", "darwin",
get_version_text=lambda: platform.mac_ver()[0],
min_version=min_version)
Expand All @@ -434,7 +427,7 @@ def requires_ios_ver(*min_version):
For example, requires_ios_ver(16, 0) raises SkipTest if the iOS version
is lesser than 16.0.
"""
return _requires_apple_os_version(
return _requires_platform_version(
"iOS", "ios",
get_version_text=lambda: platform.ios_ver().release,
min_version=min_version)
Expand Down
Loading

Back | FazBrowse Home | New Git URL