Skip to content

Commit

Permalink
Add EnvironmentVarGuard for test_builtin.py, test_io.py and `te…
Browse files Browse the repository at this point in the history
…st_locale.py`
  • Loading branch information
WolframAlph committed Jan 4, 2025
1 parent 513a4ef commit 5b7934a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
10 changes: 3 additions & 7 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand Down
10 changes: 3 additions & 7 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2892,24 +2892,20 @@ 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()
with warnings.catch_warnings():
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
Expand Down
29 changes: 10 additions & 19 deletions Lib/test/test_locale.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5b7934a

Please sign in to comment.