1919import types
2020import unittest
2121import warnings
22+ lazy import platform
2223
2324
2425__all__ = [
3334 "is_resource_enabled" , "get_resource_value" , "requires" , "requires_resource" ,
3435 "requires_freebsd_version" ,
3536 "requires_gil_enabled" , "requires_linux_version" , "requires_mac_ver" ,
37+ "requires_ios_ver" ,
3638 "check_syntax_error" ,
3739 "requires_gzip" , "requires_bz2" , "requires_lzma" , "requires_zstd" ,
3840 "bigmemtest" , "nomemtest" , "bigaddrspacetest" , "cpython_only" , "get_attribute" ,
@@ -229,7 +231,6 @@ def _force_run(path, func, *args):
229231def _is_gui_available ():
230232 if hasattr (_is_gui_available , 'result' ):
231233 return _is_gui_available .result
232- import platform
233234 reason = None
234235 if sys .platform .startswith ('win' ) and platform .win32_is_iot ():
235236 reason = "gui is not available on Windows IoT Core"
@@ -354,7 +355,6 @@ def _requires_unix_version(sysname, min_version):
354355 For example, @_requires_unix_version('FreeBSD', (7, 2)) raises SkipTest if
355356 the FreeBSD version is less than 7.2.
356357 """
357- import platform
358358 min_version_txt = '.' .join (map (str , min_version ))
359359 version_txt = platform .release ().split ('-' , 1 )[0 ]
360360 if platform .system () == sysname :
@@ -392,19 +392,12 @@ def requires_linux_version(*min_version):
392392 """
393393 return _requires_unix_version ('Linux' , min_version )
394394
395- def requires_mac_ver (* min_version ):
396- """Decorator raising SkipTest if the OS is Mac OS X and the OS X
397- version if less than min_version.
398-
399- For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version
400- is lesser than 10.5.
401- """
395+ def _requires_apple_os_version (name , sys_platform , get_version_text , min_version ):
402396 def decorator (func ):
403397 @functools .wraps (func )
404398 def wrapper (* args , ** kw ):
405- if sys .platform == 'darwin' :
406- import platform
407- version_txt = platform .mac_ver ()[0 ]
399+ if sys .platform == sys_platform :
400+ version_txt = get_version_text ()
408401 try :
409402 version = tuple (map (int , version_txt .split ('.' )))
410403 except ValueError :
@@ -413,14 +406,40 @@ def wrapper(*args, **kw):
413406 if version < min_version :
414407 min_version_txt = '.' .join (map (str , min_version ))
415408 raise unittest .SkipTest (
416- "Mac OS X %s or higher required, not %s "
417- % ( min_version_txt , version_txt ) )
409+ f" { name } { min_version_txt } or higher required, "
410+ + f"not { version_txt } " )
418411 return func (* args , ** kw )
419412 wrapper .min_version = min_version
420413 return wrapper
421414 return decorator
422415
423416
417+ def requires_mac_ver (* min_version ):
418+ """Decorator raising SkipTest if the OS is Mac OS X and the OS X
419+ version if less than min_version.
420+
421+ For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version
422+ is lesser than 10.5.
423+ """
424+ return _requires_apple_os_version (
425+ "macOS" , "darwin" ,
426+ get_version_text = lambda : platform .mac_ver ()[0 ],
427+ min_version = min_version )
428+
429+
430+ def requires_ios_ver (* min_version ):
431+ """Decorator raising SkipTest if the OS is iOS and the OS version
432+ version if less than min_version.
433+
434+ For example, requires_ios_ver(16, 0) raises SkipTest if the iOS version
435+ is lesser than 16.0.
436+ """
437+ return _requires_apple_os_version (
438+ "iOS" , "ios" ,
439+ get_version_text = lambda : platform .ios_ver ().release ,
440+ min_version = min_version )
441+
442+
424443def thread_unsafe (reason ):
425444 """Mark a test as not thread safe. When the test runner is run with
426445 --parallel-threads=N, the test will be run in a single thread."""
@@ -2223,7 +2242,6 @@ def setswitchinterval(interval):
22232242 if is_android and interval < minimum_interval :
22242243 global _old_android_emulator
22252244 if _old_android_emulator is None :
2226- import platform
22272245 av = platform .android_ver ()
22282246 _old_android_emulator = av .is_emulator and av .api_level < 24
22292247 if _old_android_emulator :
@@ -3480,7 +3498,6 @@ def linked_to_musl():
34803498
34813499 # On linux, we'll depend on the platform module to do the check, so new
34823500 # musl platforms should add support in that module if possible.
3483- import platform
34843501 lib , version = platform .libc_ver ()
34853502 if lib != 'musl' :
34863503 _linked_to_musl = False
0 commit comments