11from decimal import Decimal
2- from test .support import verbose , is_android , is_emscripten , is_wasi
2+ from test .support import cpython_only , verbose , is_android , linked_to_musl , os_helper
33from test .support .warnings_helper import check_warnings
4- from test .support .import_helper import import_fresh_module
4+ from test .support .import_helper import ensure_lazy_imports , import_fresh_module
55from unittest import mock
66import unittest
77import locale
88import os
99import sys
1010import codecs
1111
12+ class LazyImportTest (unittest .TestCase ):
13+ @cpython_only
14+ def test_lazy_import (self ):
15+ ensure_lazy_imports ("locale" , {"re" , "warnings" })
16+
1217
1318class BaseLocalizedTest (unittest .TestCase ):
1419 #
@@ -351,21 +356,15 @@ def setUp(self):
351356
352357 @unittest .skipIf (sys .platform .startswith ('aix' ),
353358 'bpo-29972: broken test on AIX' )
354- @unittest .skipIf (
355- is_emscripten or is_wasi ,
356- "musl libc issue on Emscripten/WASI, bpo-46390"
357- )
359+ @unittest .skipIf (linked_to_musl (), "musl libc issue, bpo-46390" )
358360 @unittest .skipIf (sys .platform .startswith ("netbsd" ),
359361 "gh-124108: NetBSD doesn't support UTF-8 for LC_COLLATE" )
360362 def test_strcoll_with_diacritic (self ):
361363 self .assertLess (locale .strcoll ('à' , 'b' ), 0 )
362364
363365 @unittest .skipIf (sys .platform .startswith ('aix' ),
364366 'bpo-29972: broken test on AIX' )
365- @unittest .skipIf (
366- is_emscripten or is_wasi ,
367- "musl libc issue on Emscripten/WASI, bpo-46390"
368- )
367+ @unittest .skipIf (linked_to_musl (), "musl libc issue, bpo-46390" )
369368 @unittest .skipIf (sys .platform .startswith ("netbsd" ),
370369 "gh-124108: NetBSD doesn't support UTF-8 for LC_COLLATE" )
371370 def test_strxfrm_with_diacritic (self ):
@@ -541,7 +540,6 @@ def test_defaults_UTF8(self):
541540 # valid. Furthermore LC_CTYPE=UTF is used by the UTF-8 locale coercing
542541 # during interpreter startup (on macOS).
543542 import _locale
544- import os
545543
546544 self .assertEqual (locale ._parse_localename ('UTF-8' ), (None , 'UTF-8' ))
547545
@@ -551,25 +549,14 @@ def test_defaults_UTF8(self):
551549 else :
552550 orig_getlocale = None
553551
554- orig_env = {}
555552 try :
556- for key in ('LC_ALL' , 'LC_CTYPE' , 'LANG' , 'LANGUAGE' ):
557- if key in os .environ :
558- orig_env [key ] = os .environ [key ]
559- del os .environ [key ]
560-
561- os .environ ['LC_CTYPE' ] = 'UTF-8'
562-
563- with check_warnings (('' , DeprecationWarning )):
564- self .assertEqual (locale .getdefaultlocale (), (None , 'UTF-8' ))
553+ with os_helper .EnvironmentVarGuard () as env :
554+ env .unset ('LC_ALL' , 'LC_CTYPE' , 'LANG' , 'LANGUAGE' )
555+ env .set ('LC_CTYPE' , 'UTF-8' )
565556
557+ with check_warnings (('' , DeprecationWarning )):
558+ self .assertEqual (locale .getdefaultlocale (), (None , 'UTF-8' ))
566559 finally :
567- for k in orig_env :
568- os .environ [k ] = orig_env [k ]
569-
570- if 'LC_CTYPE' not in orig_env :
571- del os .environ ['LC_CTYPE' ]
572-
573560 if orig_getlocale is not None :
574561 _locale ._getdefaultlocale = orig_getlocale
575562
0 commit comments