diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-06-13 11:32:31 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2017-06-13 11:32:31 (GMT) |
commit | 023564bf7d95f8e6a4b790491811e75ce497a071 (patch) | |
tree | c1c7d8349fdf8dd7526b2562ef23e2674f56e693 /Lib/test | |
parent | eb52ac89929bb09b15c014ab8ff60eee685e86c7 (diff) | |
download | cpython-023564bf7d95f8e6a4b790491811e75ce497a071.zip cpython-023564bf7d95f8e6a4b790491811e75ce497a071.tar.gz cpython-023564bf7d95f8e6a4b790491811e75ce497a071.tar.bz2 |
bpo-30635: Fix refleak in test_c_locale_coercion (#2126)
When checking for reference leaks, test_c_locale_coercion is run
multiple times and so _LocaleCoercionTargetsTestCase.setUpClass() is
called multiple times. setUpClass() appends new value at each call,
so it looks like a reference leak.
Moving the setup from setUpClass() to setUpModule() avoids
this, eliminating the false alarm.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_c_locale_coercion.py | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/Lib/test/test_c_locale_coercion.py b/Lib/test/test_c_locale_coercion.py index c14d820..f537f1d 100644 --- a/Lib/test/test_c_locale_coercion.py +++ b/Lib/test/test_c_locale_coercion.py @@ -143,27 +143,29 @@ CLI_COERCION_WARNING_FMT = ( "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior)." ) -class _LocaleCoercionTargetsTestCase(_ChildProcessEncodingTestCase): - # Base class for test cases that rely on coercion targets being defined - available_targets = [] - targets_required = True +AVAILABLE_TARGETS = None - @classmethod - def setUpClass(cls): - first_target_locale = None - available_targets = cls.available_targets - # Find the target locales available in the current system - for target_locale in _C_UTF8_LOCALES: - if _set_locale_in_subprocess(target_locale): - available_targets.append(target_locale) - if first_target_locale is None: - first_target_locale = target_locale - if cls.targets_required and not available_targets: - raise unittest.SkipTest("No C-with-UTF-8 locale available") - # Expect coercion to use the first available locale - warning_msg = CLI_COERCION_WARNING_FMT.format(first_target_locale) - cls.EXPECTED_COERCION_WARNING = warning_msg +def setUpModule(): + global AVAILABLE_TARGETS + + if AVAILABLE_TARGETS is not None: + # initialization already done + return + AVAILABLE_TARGETS = [] + + # Find the target locales available in the current system + for target_locale in _C_UTF8_LOCALES: + if _set_locale_in_subprocess(target_locale): + AVAILABLE_TARGETS.append(target_locale) + if not AVAILABLE_TARGETS: + raise unittest.SkipTest("No C-with-UTF-8 locale available") + + + +class _LocaleCoercionTargetsTestCase(_ChildProcessEncodingTestCase): + # Base class for test cases that rely on coercion targets being defined + pass class LocaleConfigurationTests(_LocaleCoercionTargetsTestCase): @@ -183,7 +185,7 @@ class LocaleConfigurationTests(_LocaleCoercionTargetsTestCase): "LC_ALL": "", } for env_var in ("LANG", "LC_CTYPE"): - for locale_to_set in self.available_targets: + for locale_to_set in AVAILABLE_TARGETS: with self.subTest(env_var=env_var, configured_locale=locale_to_set): var_dict = base_var_dict.copy() @@ -215,7 +217,9 @@ class LocaleCoercionTests(_LocaleCoercionTargetsTestCase): expected_warning = [] if coerce_c_locale != "0": - expected_warning.append(self.EXPECTED_COERCION_WARNING) + # Expect coercion to use the first available locale + warning_msg = CLI_COERCION_WARNING_FMT.format(AVAILABLE_TARGETS[0]) + expected_warning.append(warning_msg) base_var_dict = { "LANG": "", |