|  | 
| 1 | 1 | from decimal import Decimal | 
| 2 |  | -from test.support import verbose, is_android, is_emscripten, is_wasi | 
|  | 2 | +from test.support import verbose, is_android, is_emscripten, is_wasi, os_helper | 
| 3 | 3 | from test.support.warnings_helper import check_warnings | 
| 4 | 4 | from test.support.import_helper import import_fresh_module | 
| 5 | 5 | from unittest import mock | 
| @@ -499,27 +499,18 @@ def test_defaults_UTF8(self): | 
| 499 | 499 |         else: | 
| 500 | 500 |             orig_getlocale = None | 
| 501 | 501 | 
 | 
| 502 |  | -        orig_env = {} | 
| 503 |  | -        try: | 
|  | 502 | +        with os_helper.EnvironmentVarGuard() as env: | 
| 504 | 503 |             for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'): | 
| 505 |  | -                if key in os.environ: | 
| 506 |  | -                    orig_env[key] = os.environ[key] | 
| 507 |  | -                    del os.environ[key] | 
| 508 |  | - | 
| 509 |  | -            os.environ['LC_CTYPE'] = 'UTF-8' | 
| 510 |  | - | 
| 511 |  | -            with check_warnings(('', DeprecationWarning)): | 
| 512 |  | -                self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8')) | 
| 513 |  | - | 
| 514 |  | -        finally: | 
| 515 |  | -            for k in orig_env: | 
| 516 |  | -                os.environ[k] = orig_env[k] | 
|  | 504 | +                del env[key] | 
| 517 | 505 | 
 | 
| 518 |  | -            if 'LC_CTYPE' not in orig_env: | 
| 519 |  | -                del os.environ['LC_CTYPE'] | 
|  | 506 | +            env['LC_CTYPE'] = 'UTF-8' | 
| 520 | 507 | 
 | 
| 521 |  | -            if orig_getlocale is not None: | 
| 522 |  | -                _locale._getdefaultlocale = orig_getlocale | 
|  | 508 | +            try: | 
|  | 509 | +                with check_warnings(('', DeprecationWarning)): | 
|  | 510 | +                    self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8')) | 
|  | 511 | +            finally: | 
|  | 512 | +                if orig_getlocale is not None: | 
|  | 513 | +                    _locale._getdefaultlocale = orig_getlocale | 
| 523 | 514 | 
 | 
| 524 | 515 |     def test_getencoding(self): | 
| 525 | 516 |         # Invoke getencoding to make sure it does not cause exceptions. | 
|  | 
0 commit comments