mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
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:
parent
6e4f64109b
commit
ae7f621c33
3 changed files with 11 additions and 30 deletions
|
@ -1567,14 +1567,12 @@ class BuiltinTest(ComplexesAreIdenticalMixin, unittest.TestCase):
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled")
|
@unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled")
|
||||||
def test_open_default_encoding(self):
|
def test_open_default_encoding(self):
|
||||||
old_environ = dict(os.environ)
|
with EnvironmentVarGuard() as env:
|
||||||
try:
|
|
||||||
# try to get a user preferred encoding different than the current
|
# try to get a user preferred encoding different than the current
|
||||||
# locale encoding to check that open() uses the current locale
|
# locale encoding to check that open() uses the current locale
|
||||||
# encoding and not the user preferred encoding
|
# encoding and not the user preferred encoding
|
||||||
for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
|
for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
|
||||||
if key in os.environ:
|
env.unset(key)
|
||||||
del os.environ[key]
|
|
||||||
|
|
||||||
self.write_testfile()
|
self.write_testfile()
|
||||||
current_locale_encoding = locale.getencoding()
|
current_locale_encoding = locale.getencoding()
|
||||||
|
@ -1583,9 +1581,6 @@ class BuiltinTest(ComplexesAreIdenticalMixin, unittest.TestCase):
|
||||||
fp = open(TESTFN, 'w')
|
fp = open(TESTFN, 'w')
|
||||||
with fp:
|
with fp:
|
||||||
self.assertEqual(fp.encoding, current_locale_encoding)
|
self.assertEqual(fp.encoding, current_locale_encoding)
|
||||||
finally:
|
|
||||||
os.environ.clear()
|
|
||||||
os.environ.update(old_environ)
|
|
||||||
|
|
||||||
@support.requires_subprocess()
|
@support.requires_subprocess()
|
||||||
def test_open_non_inheritable(self):
|
def test_open_non_inheritable(self):
|
||||||
|
|
|
@ -2892,14 +2892,12 @@ class TextIOWrapperTest(unittest.TestCase):
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled")
|
@unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled")
|
||||||
def test_default_encoding(self):
|
def test_default_encoding(self):
|
||||||
old_environ = dict(os.environ)
|
with os_helper.EnvironmentVarGuard() as env:
|
||||||
try:
|
|
||||||
# try to get a user preferred encoding different than the current
|
# try to get a user preferred encoding different than the current
|
||||||
# locale encoding to check that TextIOWrapper() uses the current
|
# locale encoding to check that TextIOWrapper() uses the current
|
||||||
# locale encoding and not the user preferred encoding
|
# locale encoding and not the user preferred encoding
|
||||||
for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
|
for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
|
||||||
if key in os.environ:
|
env.unset(key)
|
||||||
del os.environ[key]
|
|
||||||
|
|
||||||
current_locale_encoding = locale.getencoding()
|
current_locale_encoding = locale.getencoding()
|
||||||
b = self.BytesIO()
|
b = self.BytesIO()
|
||||||
|
@ -2907,9 +2905,6 @@ class TextIOWrapperTest(unittest.TestCase):
|
||||||
warnings.simplefilter("ignore", EncodingWarning)
|
warnings.simplefilter("ignore", EncodingWarning)
|
||||||
t = self.TextIOWrapper(b)
|
t = self.TextIOWrapper(b)
|
||||||
self.assertEqual(t.encoding, current_locale_encoding)
|
self.assertEqual(t.encoding, current_locale_encoding)
|
||||||
finally:
|
|
||||||
os.environ.clear()
|
|
||||||
os.environ.update(old_environ)
|
|
||||||
|
|
||||||
def test_encoding(self):
|
def test_encoding(self):
|
||||||
# Check the encoding attribute is always set, and valid
|
# Check the encoding attribute is always set, and valid
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from decimal import Decimal
|
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.warnings_helper import check_warnings
|
||||||
from test.support.import_helper import import_fresh_module
|
from test.support.import_helper import import_fresh_module
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
@ -499,25 +499,16 @@ class TestMiscellaneous(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
orig_getlocale = None
|
orig_getlocale = None
|
||||||
|
|
||||||
orig_env = {}
|
|
||||||
try:
|
try:
|
||||||
for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
|
with os_helper.EnvironmentVarGuard() as env:
|
||||||
if key in os.environ:
|
for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
|
||||||
orig_env[key] = os.environ[key]
|
env.unset(key)
|
||||||
del os.environ[key]
|
|
||||||
|
|
||||||
os.environ['LC_CTYPE'] = 'UTF-8'
|
env.set('LC_CTYPE', 'UTF-8')
|
||||||
|
|
||||||
with check_warnings(('', DeprecationWarning)):
|
|
||||||
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
|
|
||||||
|
|
||||||
|
with check_warnings(('', DeprecationWarning)):
|
||||||
|
self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
|
||||||
finally:
|
finally:
|
||||||
for k in orig_env:
|
|
||||||
os.environ[k] = orig_env[k]
|
|
||||||
|
|
||||||
if 'LC_CTYPE' not in orig_env:
|
|
||||||
del os.environ['LC_CTYPE']
|
|
||||||
|
|
||||||
if orig_getlocale is not None:
|
if orig_getlocale is not None:
|
||||||
_locale._getdefaultlocale = orig_getlocale
|
_locale._getdefaultlocale = orig_getlocale
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue