From 40c06a27a1404e55a9ed1fd9d23a894f026be347 Mon Sep 17 00:00:00 2001 From: devRD Date: Thu, 16 Feb 2023 10:52:05 +0530 Subject: [PATCH 1/6] Fix unintended space after comma as a decimal separator --- lib/matplotlib/tests/test_ticker.py | 15 +++++++++++++++ lib/matplotlib/ticker.py | 10 ++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 07cd414d1609..3c183d589b92 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1449,6 +1449,21 @@ def test_latex(self, is_latex, usetex, expected): assert fmt.format_pct(50, 100) == expected +def test_locale_comma(): + import locale + + currentLocale = locale.getlocale() + try: + locale.setlocale(locale.LC_ALL, 'fy_DE.UTF-8') + except locale.Error as err: + pytest.fail("Locale 'fy_DE.UTF-8' is not supported") + ticks = mticker.ScalarFormatter(useMathText=True, useLocale=True) + fmt = '$\\mathdefault{%1.1f}$' + x = ticks._format_maybe_minus_and_locale(fmt, 0.5) + locale.setlocale(locale.LC_ALL, currentLocale) + assert x == '$\\mathdefault{0{,}5}$' + + def test_majformatter_type(): fig, ax = plt.subplots() with pytest.raises(TypeError): diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 339a8c6d9293..c36f3906977f 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -510,8 +510,14 @@ def _format_maybe_minus_and_locale(self, fmt, arg): """ Format *arg* with *fmt*, applying Unicode minus and locale if desired. """ - return self.fix_minus(locale.format_string(fmt, (arg,), True) - if self._useLocale else fmt % arg) + return self.fix_minus( + # Escape commas introduced by format_string but not those present + # from the beginning in fmt. + ",".join(locale.format_string(part, (arg,), True) + .replace(",", "{,}") + for part in fmt.split(",")) + if self._useLocale + else fmt % arg) def get_useMathText(self): """ From f5aa275ae5903245aee45c7da0751a910e1ebaaf Mon Sep 17 00:00:00 2001 From: devRD Date: Thu, 16 Feb 2023 12:48:11 +0530 Subject: [PATCH 2/6] Handle locale unsupported error --- lib/matplotlib/tests/test_ticker.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 3c183d589b92..fbbbfb3c358c 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1449,14 +1449,11 @@ def test_latex(self, is_latex, usetex, expected): assert fmt.format_pct(50, 100) == expected +@pytest.mark.xfail(locale != "fy_DE.UTF-8", + reason="Locale 'fy_DE.UTF-8' is not supported") def test_locale_comma(): - import locale - currentLocale = locale.getlocale() - try: - locale.setlocale(locale.LC_ALL, 'fy_DE.UTF-8') - except locale.Error as err: - pytest.fail("Locale 'fy_DE.UTF-8' is not supported") + locale.setlocale(locale.LC_ALL, 'fy_DE.UTF-8') ticks = mticker.ScalarFormatter(useMathText=True, useLocale=True) fmt = '$\\mathdefault{%1.1f}$' x = ticks._format_maybe_minus_and_locale(fmt, 0.5) From 7d059b5e999f435f8f61d2106e863fe53684abf1 Mon Sep 17 00:00:00 2001 From: Ratnabali Dutta Date: Tue, 21 Feb 2023 12:06:42 +0530 Subject: [PATCH 3/6] Check format_string comma is unchanged Co-authored-by: Oscar Gustafsson --- lib/matplotlib/tests/test_ticker.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index fbbbfb3c358c..e46ca65a508b 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1457,8 +1457,12 @@ def test_locale_comma(): ticks = mticker.ScalarFormatter(useMathText=True, useLocale=True) fmt = '$\\mathdefault{%1.1f}$' x = ticks._format_maybe_minus_and_locale(fmt, 0.5) - locale.setlocale(locale.LC_ALL, currentLocale) assert x == '$\\mathdefault{0{,}5}$' + # Do not change , in the format string + fmt = ',$\\mathdefault{,%1.1f},$' + x = ticks._format_maybe_minus_and_locale(fmt, 0.5) + assert x == ',$\\mathdefault{,0{,}5},$' + locale.setlocale(locale.LC_ALL, currentLocale) def test_majformatter_type(): From 8e814e0c43b45eddc90b6d6ab3fd63d09581a63e Mon Sep 17 00:00:00 2001 From: devRD Date: Wed, 22 Feb 2023 11:33:28 +0530 Subject: [PATCH 4/6] Add error handling for locale --- lib/matplotlib/tests/test_ticker.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index e46ca65a508b..6aca33688808 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1449,20 +1449,21 @@ def test_latex(self, is_latex, usetex, expected): assert fmt.format_pct(50, 100) == expected -@pytest.mark.xfail(locale != "fy_DE.UTF-8", - reason="Locale 'fy_DE.UTF-8' is not supported") +@pytest.mark.xfail def test_locale_comma(): currentLocale = locale.getlocale() - locale.setlocale(locale.LC_ALL, 'fy_DE.UTF-8') - ticks = mticker.ScalarFormatter(useMathText=True, useLocale=True) - fmt = '$\\mathdefault{%1.1f}$' - x = ticks._format_maybe_minus_and_locale(fmt, 0.5) - assert x == '$\\mathdefault{0{,}5}$' - # Do not change , in the format string - fmt = ',$\\mathdefault{,%1.1f},$' - x = ticks._format_maybe_minus_and_locale(fmt, 0.5) - assert x == ',$\\mathdefault{,0{,}5},$' - locale.setlocale(locale.LC_ALL, currentLocale) + try: + locale.setlocale(locale.LC_ALL, 'fy_DE.UTF-8') + ticks = mticker.ScalarFormatter(useMathText=True, useLocale=True) + fmt = '$\\mathdefault{%1.1f}$' + x = ticks._format_maybe_minus_and_locale(fmt, 0.5) + assert x == '$\\mathdefault{0{,}5}$' + # Do not change , in the format string + fmt = ',$\\mathdefault{,%1.1f},$' + x = ticks._format_maybe_minus_and_locale(fmt, 0.5) + assert x == ',$\\mathdefault{,0{,}5},$' + finally: + locale.setlocale(locale.LC_ALL, currentLocale) def test_majformatter_type(): From 8d49b92d25fba2e1833732883b98ae67eb6c9f4c Mon Sep 17 00:00:00 2001 From: devRD Date: Sat, 25 Feb 2023 11:10:01 +0530 Subject: [PATCH 5/6] Update pytest to skip when locale unsupported --- lib/matplotlib/tests/test_ticker.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 6aca33688808..eb60ae1fe915 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1449,7 +1449,6 @@ def test_latex(self, is_latex, usetex, expected): assert fmt.format_pct(50, 100) == expected -@pytest.mark.xfail def test_locale_comma(): currentLocale = locale.getlocale() try: @@ -1462,6 +1461,8 @@ def test_locale_comma(): fmt = ',$\\mathdefault{,%1.1f},$' x = ticks._format_maybe_minus_and_locale(fmt, 0.5) assert x == ',$\\mathdefault{,0{,}5},$' + except locale.Error: + pytest.skip("Locale fy_DE.UTF-8 is not supported on this machine") finally: locale.setlocale(locale.LC_ALL, currentLocale) From 8c826b9bbf13a7d74ca64d2f0d3d0fcac93281ba Mon Sep 17 00:00:00 2001 From: devRD Date: Wed, 1 Mar 2023 14:12:45 +0530 Subject: [PATCH 6/6] Update locale setting for coverage --- lib/matplotlib/tests/test_ticker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index eb60ae1fe915..1ebd16d25593 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1452,7 +1452,7 @@ def test_latex(self, is_latex, usetex, expected): def test_locale_comma(): currentLocale = locale.getlocale() try: - locale.setlocale(locale.LC_ALL, 'fy_DE.UTF-8') + locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') ticks = mticker.ScalarFormatter(useMathText=True, useLocale=True) fmt = '$\\mathdefault{%1.1f}$' x = ticks._format_maybe_minus_and_locale(fmt, 0.5) @@ -1462,7 +1462,7 @@ def test_locale_comma(): x = ticks._format_maybe_minus_and_locale(fmt, 0.5) assert x == ',$\\mathdefault{,0{,}5},$' except locale.Error: - pytest.skip("Locale fy_DE.UTF-8 is not supported on this machine") + pytest.skip("Locale de_DE.UTF-8 is not supported on this machine") finally: locale.setlocale(locale.LC_ALL, currentLocale)