Skip to content

Commit ae7f621

Browse files
authored
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.
1 parent 6e4f641 commit ae7f621

File tree

3 files changed

+11
-30
lines changed

3 files changed

+11
-30
lines changed

Lib/test/test_builtin.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -1567,14 +1567,12 @@ def test_open(self):
15671567

15681568
@unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled")
15691569
def test_open_default_encoding(self):
1570-
old_environ = dict(os.environ)
1571-
try:
1570+
with EnvironmentVarGuard() as env:
15721571
# try to get a user preferred encoding different than the current
15731572
# locale encoding to check that open() uses the current locale
15741573
# encoding and not the user preferred encoding
15751574
for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
1576-
if key in os.environ:
1577-
del os.environ[key]
1575+
env.unset(key)
15781576

15791577
self.write_testfile()
15801578
current_locale_encoding = locale.getencoding()
@@ -1583,9 +1581,6 @@ def test_open_default_encoding(self):
15831581
fp = open(TESTFN, 'w')
15841582
with fp:
15851583
self.assertEqual(fp.encoding, current_locale_encoding)
1586-
finally:
1587-
os.environ.clear()
1588-
os.environ.update(old_environ)
15891584

15901585
@support.requires_subprocess()
15911586
def test_open_non_inheritable(self):

Lib/test/test_io.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -2892,24 +2892,19 @@ def test_reconfigure_line_buffering(self):
28922892

28932893
@unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled")
28942894
def test_default_encoding(self):
2895-
old_environ = dict(os.environ)
2896-
try:
2895+
with os_helper.EnvironmentVarGuard() as env:
28972896
# try to get a user preferred encoding different than the current
28982897
# locale encoding to check that TextIOWrapper() uses the current
28992898
# locale encoding and not the user preferred encoding
29002899
for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
2901-
if key in os.environ:
2902-
del os.environ[key]
2900+
env.unset(key)
29032901

29042902
current_locale_encoding = locale.getencoding()
29052903
b = self.BytesIO()
29062904
with warnings.catch_warnings():
29072905
warnings.simplefilter("ignore", EncodingWarning)
29082906
t = self.TextIOWrapper(b)
29092907
self.assertEqual(t.encoding, current_locale_encoding)
2910-
finally:
2911-
os.environ.clear()
2912-
os.environ.update(old_environ)
29132908

29142909
def test_encoding(self):
29152910
# Check the encoding attribute is always set, and valid

Lib/test/test_locale.py

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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
33
from test.support.warnings_helper import check_warnings
44
from test.support.import_helper import import_fresh_module
55
from unittest import mock
@@ -499,25 +499,16 @@ def test_defaults_UTF8(self):
499499
else:
500500
orig_getlocale = None
501501

502-
orig_env = {}
503502
try:
504-
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]
503+
with os_helper.EnvironmentVarGuard() as env:
504+
for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
505+
env.unset(key)
508506

509-
os.environ['LC_CTYPE'] = 'UTF-8'
510-
511-
with check_warnings(('', DeprecationWarning)):
512-
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
507+
env.set('LC_CTYPE', 'UTF-8')
513508

509+
with check_warnings(('', DeprecationWarning)):
510+
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
514511
finally:
515-
for k in orig_env:
516-
os.environ[k] = orig_env[k]
517-
518-
if 'LC_CTYPE' not in orig_env:
519-
del os.environ['LC_CTYPE']
520-
521512
if orig_getlocale is not None:
522513
_locale._getdefaultlocale = orig_getlocale
523514

0 commit comments

Comments
 (0)