gh-128438: Use EnvironmentVarGuard for test_{builtin,io,locale}.py (#128476)

Modifying locale-related environment variables in `Lib/test/test_builtin.py`,
`Lib/test/test_io.py` and `Lib/test/test_locale.py` is now achieved by using
an `EnvironmentVarGuard` context instead of an explicit `try-finally` block.
This commit is contained in:
Yan Yanchii 2025-01-15 10:38:43 +01:00 committed by GitHub
parent 6e4f64109b
commit ae7f621c33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 30 deletions

View file

@ -2892,14 +2892,12 @@ class TextIOWrapperTest(unittest.TestCase):
@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]
env.unset(key)
current_locale_encoding = locale.getencoding()
b = self.BytesIO()
@ -2907,9 +2905,6 @@ class TextIOWrapperTest(unittest.TestCase):
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