Skip to content

Commit 5b7934a

Browse files
committed
Add EnvironmentVarGuard for test_builtin.py, test_io.py and test_locale.py
1 parent 513a4ef commit 5b7934a

File tree

3 files changed

+16
-33
lines changed

3 files changed

+16
-33
lines changed

Lib/test/test_builtin.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,14 +1567,13 @@ 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+
if key in env:
1576+
del env[key]
15781577

15791578
self.write_testfile()
15801579
current_locale_encoding = locale.getencoding()
@@ -1583,9 +1582,6 @@ def test_open_default_encoding(self):
15831582
fp = open(TESTFN, 'w')
15841583
with fp:
15851584
self.assertEqual(fp.encoding, current_locale_encoding)
1586-
finally:
1587-
os.environ.clear()
1588-
os.environ.update(old_environ)
15891585

15901586
@support.requires_subprocess()
15911587
def test_open_non_inheritable(self):

Lib/test/test_io.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,24 +2892,20 @@ 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+
if key in env:
2901+
del env[key]
29032902

29042903
current_locale_encoding = locale.getencoding()
29052904
b = self.BytesIO()
29062905
with warnings.catch_warnings():
29072906
warnings.simplefilter("ignore", EncodingWarning)
29082907
t = self.TextIOWrapper(b)
29092908
self.assertEqual(t.encoding, current_locale_encoding)
2910-
finally:
2911-
os.environ.clear()
2912-
os.environ.update(old_environ)
29132909

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

Lib/test/test_locale.py

Lines changed: 10 additions & 19 deletions
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,27 +499,18 @@ def test_defaults_UTF8(self):
499499
else:
500500
orig_getlocale = None
501501

502-
orig_env = {}
503-
try:
502+
with os_helper.EnvironmentVarGuard() as env:
504503
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]
517505

518-
if 'LC_CTYPE' not in orig_env:
519-
del os.environ['LC_CTYPE']
506+
env['LC_CTYPE'] = 'UTF-8'
520507

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
523514

524515
def test_getencoding(self):
525516
# Invoke getencoding to make sure it does not cause exceptions.

0 commit comments

Comments
 (0)