From 14d29b166cdcf77e3a5af2c138ec06704a6cbd0a Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Sat, 13 Jan 2024 13:49:37 +0800 Subject: [PATCH 1/5] Update `Fraction()`'s rational parsing regex --- Lib/fractions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/fractions.py b/Lib/fractions.py index 6532d5d54e3c35..8ef46dcbd68623 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -62,7 +62,7 @@ def _hash_algorithm(numerator, denominator): (?: # followed by (?:\s*/\s*(?P\d+(_\d+)*))? # an optional denominator | # or - (?:\.(?Pd*|\d+(_\d+)*))? # an optional fractional part + (?:\.(?P\d*|\d+(_\d+)*))? # an optional fractional part (?:E(?P[-+]?\d+(_\d+)*))? # and optional exponent ) \s*\Z # and optional whitespace to finish From 4f550ead55699a8c07845b0d7216b8da325203cb Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Sat, 13 Jan 2024 14:00:04 +0800 Subject: [PATCH 2/5] Add tests for invalid strings containing `d` Fixes gh-114014. `\d` can easily be mistyped as simply `d`. Add tests for it. ``` (?Pd*|\d+(_\d+)*) ^^ ``` --- Lib/test/test_fractions.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 84779526ce0eb0..af3cb214ab0ac1 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -261,6 +261,30 @@ def testFromString(self): self.assertRaisesMessage( ValueError, "Invalid literal for Fraction: '1.1e+1__1'", F, "1.1e+1__1") + self.assertRaisesMessage( + ValueError, "Invalid literal for Fraction: '123.dd'", + F, "123.dd") + self.assertRaisesMessage( + ValueError, "Invalid literal for Fraction: '123.5_dd'", + F, "123.5_dd") + self.assertRaisesMessage( + ValueError, "Invalid literal for Fraction: 'dd.5'", + F, "dd.5") + self.assertRaisesMessage( + ValueError, "Invalid literal for Fraction: '7_dd'", + F, "7_dd") + self.assertRaisesMessage( + ValueError, "Invalid literal for Fraction: '1/dd'", + F, "1/dd") + self.assertRaisesMessage( + ValueError, "Invalid literal for Fraction: '1/123_dd'", + F, "1/123_dd") + self.assertRaisesMessage( + ValueError, "Invalid literal for Fraction: '789edd'", + F, "789edd") + self.assertRaisesMessage( + ValueError, "Invalid literal for Fraction: '789e2_dd'", + F, "789e2_dd") # Test catastrophic backtracking. val = "9"*50 + "_" self.assertRaisesMessage( From 35076f2a8ffd976ae618a2e10a248ab0ab871cde Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Sat, 13 Jan 2024 19:28:23 +0800 Subject: [PATCH 3/5] Fix PEP8 nit Make sure the comments in the regex are 2 spaces away from the content. --- Lib/fractions.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Lib/fractions.py b/Lib/fractions.py index 8ef46dcbd68623..389ab386b6a8a4 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -55,17 +55,17 @@ def _hash_algorithm(numerator, denominator): return -2 if result == -1 else result _RATIONAL_FORMAT = re.compile(r""" - \A\s* # optional whitespace at the start, - (?P[-+]?) # an optional sign, then - (?=\d|\.\d) # lookahead for digit or .digit - (?P\d*|\d+(_\d+)*) # numerator (possibly empty) - (?: # followed by - (?:\s*/\s*(?P\d+(_\d+)*))? # an optional denominator - | # or - (?:\.(?P\d*|\d+(_\d+)*))? # an optional fractional part - (?:E(?P[-+]?\d+(_\d+)*))? # and optional exponent + \A\s* # optional whitespace at the start, + (?P[-+]?) # an optional sign, then + (?=\d|\.\d) # lookahead for digit or .digit + (?P\d*|\d+(_\d+)*) # numerator (possibly empty) + (?: # followed by + (?:\s*/\s*(?P\d+(_\d+)*))? # an optional denominator + | # or + (?:\.(?P\d*|\d+(_\d+)*))? # an optional fractional part + (?:E(?P[-+]?\d+(_\d+)*))? # and optional exponent ) - \s*\Z # and optional whitespace to finish + \s*\Z # and optional whitespace to finish """, re.VERBOSE | re.IGNORECASE) From 9be42a0e4dd4cbf6f5b0a7adb1e963e4f92f667f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 13 Jan 2024 11:34:33 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst diff --git a/Misc/NEWS.d/next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst b/Misc/NEWS.d/next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst new file mode 100644 index 00000000000000..6ef422dbcf5827 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst @@ -0,0 +1 @@ +Fixed a bug in `fractions.Fraction()` where an invalid string using `d` in the decimals part creates a different error compared to other invalid letters/characters. Patch by Jeremiah Gabriel Pascual. From 7572189b1317da77f88d3dca48b8ab1b106a382a Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Sat, 13 Jan 2024 19:39:43 +0800 Subject: [PATCH 5/5] Fix markup Co-authored-by: Mark Dickinson --- .../next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst b/Misc/NEWS.d/next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst index 6ef422dbcf5827..a6630d73435467 100644 --- a/Misc/NEWS.d/next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst +++ b/Misc/NEWS.d/next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst @@ -1 +1 @@ -Fixed a bug in `fractions.Fraction()` where an invalid string using `d` in the decimals part creates a different error compared to other invalid letters/characters. Patch by Jeremiah Gabriel Pascual. +Fixed a bug in :class:`fractions.Fraction` where an invalid string using ``d`` in the decimals part creates a different error compared to other invalid letters/characters. Patch by Jeremiah Gabriel Pascual.