From 5b7934aa907561771381c99a26b4b10b91e30a6d Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Sat, 4 Jan 2025 11:17:31 +0100 Subject: [PATCH 1/4] Add `EnvironmentVarGuard` for `test_builtin.py`, `test_io.py` and `test_locale.py` --- Lib/test/test_builtin.py | 10 +++------- Lib/test/test_io.py | 10 +++------- Lib/test/test_locale.py | 29 ++++++++++------------------- 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 5f4eac5267622f..795e5e2006c7b8 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1567,14 +1567,13 @@ def test_open(self): @unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled") def test_open_default_encoding(self): - old_environ = dict(os.environ) - try: + with EnvironmentVarGuard() as env: # try to get a user preferred encoding different than the current # locale encoding to check that open() uses the current locale # encoding and not the user preferred encoding for key in ('LC_ALL', 'LANG', 'LC_CTYPE'): - if key in os.environ: - del os.environ[key] + if key in env: + del env[key] self.write_testfile() current_locale_encoding = locale.getencoding() @@ -1583,9 +1582,6 @@ def test_open_default_encoding(self): fp = open(TESTFN, 'w') with fp: self.assertEqual(fp.encoding, current_locale_encoding) - finally: - os.environ.clear() - os.environ.update(old_environ) @support.requires_subprocess() def test_open_non_inheritable(self): diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 81c17b2731cc58..d37c43d57c3593 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2892,14 +2892,13 @@ def test_reconfigure_line_buffering(self): @unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled") def test_default_encoding(self): - old_environ = dict(os.environ) - try: + with os_helper.EnvironmentVarGuard() as env: # try to get a user preferred encoding different than the current # locale encoding to check that TextIOWrapper() uses the current # locale encoding and not the user preferred encoding for key in ('LC_ALL', 'LANG', 'LC_CTYPE'): - if key in os.environ: - del os.environ[key] + if key in env: + del env[key] current_locale_encoding = locale.getencoding() b = self.BytesIO() @@ -2907,9 +2906,6 @@ def test_default_encoding(self): warnings.simplefilter("ignore", EncodingWarning) t = self.TextIOWrapper(b) self.assertEqual(t.encoding, current_locale_encoding) - finally: - os.environ.clear() - os.environ.update(old_environ) def test_encoding(self): # Check the encoding attribute is always set, and valid diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py index 00e93d8e78443d..d97def81a37ef3 100644 --- a/Lib/test/test_locale.py +++ b/Lib/test/test_locale.py @@ -1,5 +1,5 @@ from decimal import Decimal -from test.support import verbose, is_android, is_emscripten, is_wasi +from test.support import verbose, is_android, is_emscripten, is_wasi, os_helper from test.support.warnings_helper import check_warnings from test.support.import_helper import import_fresh_module from unittest import mock @@ -499,27 +499,18 @@ def test_defaults_UTF8(self): else: orig_getlocale = None - orig_env = {} - try: + with os_helper.EnvironmentVarGuard() as env: for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'): - if key in os.environ: - orig_env[key] = os.environ[key] - del os.environ[key] - - os.environ['LC_CTYPE'] = 'UTF-8' - - with check_warnings(('', DeprecationWarning)): - self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8')) - - finally: - for k in orig_env: - os.environ[k] = orig_env[k] + del env[key] - if 'LC_CTYPE' not in orig_env: - del os.environ['LC_CTYPE'] + env['LC_CTYPE'] = 'UTF-8' - if orig_getlocale is not None: - _locale._getdefaultlocale = orig_getlocale + try: + with check_warnings(('', DeprecationWarning)): + self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8')) + finally: + if orig_getlocale is not None: + _locale._getdefaultlocale = orig_getlocale def test_getencoding(self): # Invoke getencoding to make sure it does not cause exceptions. From 5c2ba31f9ef6f0fc63cead887c8c68cf389a2cae Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Sat, 4 Jan 2025 11:20:00 +0100 Subject: [PATCH 2/4] remove redundant condition --- Lib/test/test_builtin.py | 3 +-- Lib/test/test_io.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 795e5e2006c7b8..07f65970f22bfa 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1572,8 +1572,7 @@ def test_open_default_encoding(self): # locale encoding to check that open() uses the current locale # encoding and not the user preferred encoding for key in ('LC_ALL', 'LANG', 'LC_CTYPE'): - if key in env: - del env[key] + del env[key] self.write_testfile() current_locale_encoding = locale.getencoding() diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index d37c43d57c3593..9d2643e19ba4ec 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2897,8 +2897,7 @@ def test_default_encoding(self): # locale encoding to check that TextIOWrapper() uses the current # locale encoding and not the user preferred encoding for key in ('LC_ALL', 'LANG', 'LC_CTYPE'): - if key in env: - del env[key] + del env[key] current_locale_encoding = locale.getencoding() b = self.BytesIO() From 80f3bed4cac735d1ff3c7e694cafe5f36d71bb2d Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Sun, 5 Jan 2025 21:57:52 +0100 Subject: [PATCH 3/4] use `unset` instead of del --- Lib/test/test_builtin.py | 2 +- Lib/test/test_io.py | 2 +- Lib/test/test_locale.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 07f65970f22bfa..73b139e405ae59 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1572,7 +1572,7 @@ def test_open_default_encoding(self): # locale encoding to check that open() uses the current locale # encoding and not the user preferred encoding for key in ('LC_ALL', 'LANG', 'LC_CTYPE'): - del env[key] + env.unset(key) self.write_testfile() current_locale_encoding = locale.getencoding() diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 9d2643e19ba4ec..0abd19a5a4596a 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2897,7 +2897,7 @@ def test_default_encoding(self): # locale encoding to check that TextIOWrapper() uses the current # locale encoding and not the user preferred encoding for key in ('LC_ALL', 'LANG', 'LC_CTYPE'): - del env[key] + env.unset(key) current_locale_encoding = locale.getencoding() b = self.BytesIO() diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py index d97def81a37ef3..4e0a425b1ac764 100644 --- a/Lib/test/test_locale.py +++ b/Lib/test/test_locale.py @@ -501,7 +501,7 @@ def test_defaults_UTF8(self): with os_helper.EnvironmentVarGuard() as env: for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'): - del env[key] + env.unset(key) env['LC_CTYPE'] = 'UTF-8' From 9ff69e207d853d82680ff6354bcd0644ef9102c9 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Tue, 14 Jan 2025 13:03:50 +0100 Subject: [PATCH 4/4] address comments --- Lib/test/test_locale.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py index 4e0a425b1ac764..c025ed4108fb58 100644 --- a/Lib/test/test_locale.py +++ b/Lib/test/test_locale.py @@ -499,18 +499,18 @@ def test_defaults_UTF8(self): else: orig_getlocale = None - with os_helper.EnvironmentVarGuard() as env: - for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'): - env.unset(key) + try: + with os_helper.EnvironmentVarGuard() as env: + for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'): + env.unset(key) - env['LC_CTYPE'] = 'UTF-8' + env.set('LC_CTYPE', 'UTF-8') - try: with check_warnings(('', DeprecationWarning)): self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8')) - finally: - if orig_getlocale is not None: - _locale._getdefaultlocale = orig_getlocale + finally: + if orig_getlocale is not None: + _locale._getdefaultlocale = orig_getlocale def test_getencoding(self): # Invoke getencoding to make sure it does not cause exceptions.