From 69d796ac50fe6095ba9451c07a1dce668213cae5 Mon Sep 17 00:00:00 2001 From: Ratnabali Dutta Date: Mon, 14 Aug 2023 14:52:12 +0530 Subject: [PATCH 1/3] Fix mathtext mismatched braces --- lib/matplotlib/_mathtext.py | 3 ++- lib/matplotlib/tests/test_mathtext.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 25a825b7b0d9..4761ea7fc626 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -2030,7 +2030,8 @@ def main(self, s, loc, toks): return [Hlist(toks)] def math_string(self, s, loc, toks): - return self._math_expression.parseString(toks[0][1:-1]) + return self._math_expression.parseString(toks[0][1:-1], + parseAll=True) def math(self, s, loc, toks): hlist = Hlist(toks) diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index d80312495d91..ba30b9be214a 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -320,6 +320,7 @@ def test_fontinfo(): (r'$a^2^2$', r'Double superscript'), (r'$a_2_2$', r'Double subscript'), (r'$a^2_a^2$', r'Double superscript'), + (r'$a = {b$', r'Expected end of text'), ], ids=[ 'hspace without value', @@ -347,7 +348,8 @@ def test_fontinfo(): 'unknown symbol', 'double superscript', 'double subscript', - 'super on sub without braces' + 'super on sub without braces', + 'math string with no closing braces' ] ) def test_mathtext_exceptions(math, msg): From 7f65d6b58e2cef2445f6b8778381dd3a56937dd1 Mon Sep 17 00:00:00 2001 From: Ratnabali Dutta Date: Tue, 15 Aug 2023 15:56:35 +0530 Subject: [PATCH 2/3] Add unclosed group to grammar Co-authored-by: Kyle Sunden --- lib/matplotlib/_mathtext.py | 5 +++++ lib/matplotlib/tests/test_mathtext.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 4761ea7fc626..73cb0ae1189d 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -1894,6 +1894,7 @@ def csnames(group, names): p.function = csnames("name", self._function_names) p.group = p.start_group + ZeroOrMore(p.token)("group") + p.end_group + p.unclosed_group = (p.start_group + ZeroOrMore(p.token)("group") + StringEnd()) p.frac = cmd(r"\frac", p.required_group("num") + p.required_group("den")) p.dfrac = cmd(r"\dfrac", p.required_group("num") + p.required_group("den")) @@ -1942,6 +1943,7 @@ def csnames(group, names): p.token <<= ( p.simple | p.auto_delim + | p.unclosed_group | p.unknown_symbol # Must be last ) @@ -2251,6 +2253,9 @@ def end_group(self, s, loc, toks): self.pop_state() return [] + def unclosed_group(self, s, loc, toks): + raise ParseFatalException(s, len(s), "Expected '}'") + def font(self, s, loc, toks): self.get_state().font = toks["font"] return [] diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index ba30b9be214a..133469b00d8e 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -320,7 +320,7 @@ def test_fontinfo(): (r'$a^2^2$', r'Double superscript'), (r'$a_2_2$', r'Double subscript'), (r'$a^2_a^2$', r'Double superscript'), - (r'$a = {b$', r'Expected end of text'), + (r'$a = {b$', r"Expected '}'"), ], ids=[ 'hspace without value', @@ -349,7 +349,7 @@ def test_fontinfo(): 'double superscript', 'double subscript', 'super on sub without braces', - 'math string with no closing braces' + 'unclosed group' ] ) def test_mathtext_exceptions(math, msg): From 3dc34576a0e70981860146a712bcc0a91812afdf Mon Sep 17 00:00:00 2001 From: Ratnabali Dutta Date: Fri, 18 Aug 2023 15:23:57 +0530 Subject: [PATCH 3/3] update changes --- lib/matplotlib/_mathtext.py | 3 +-- lib/matplotlib/tests/test_mathtext.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 73cb0ae1189d..24aab9046e27 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -2032,8 +2032,7 @@ def main(self, s, loc, toks): return [Hlist(toks)] def math_string(self, s, loc, toks): - return self._math_expression.parseString(toks[0][1:-1], - parseAll=True) + return self._math_expression.parseString(toks[0][1:-1], parseAll=True) def math(self, s, loc, toks): hlist = Hlist(toks) diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index 133469b00d8e..5a9e8a8b9264 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -349,7 +349,7 @@ def test_fontinfo(): 'double superscript', 'double subscript', 'super on sub without braces', - 'unclosed group' + 'unclosed group', ] ) def test_mathtext_exceptions(math, msg):