Skip to content

gh-114014: Update fractions.Fraction()'s rational parsing regex#114015

Merged
mdickinson merged 5 commits into
python:mainfrom
thatbirdguythatuknownot:patch-32
Jan 13, 2024
Merged

gh-114014: Update fractions.Fraction()'s rational parsing regex#114015
mdickinson merged 5 commits into
python:mainfrom
thatbirdguythatuknownot:patch-32

Conversation

@thatbirdguythatuknownot

@thatbirdguythatuknownot thatbirdguythatuknownot commented Jan 13, 2024

Copy link
Copy Markdown
Contributor

Fixes #114014.

The previous regex caused unintentional inconsistency in error messages due to the below:

(?P<decimal>d*|\d+(_\d+)*)
            ^

Which matches the character d instead of the intended digit set \d. Fix it by adding a backslash behind d.

(?P<decimal>\d*|\d+(_\d+)*)
            ^^

Fixes pythongh-114014.

`\d` can easily be mistyped as simply `d`. Add tests for it.
```
(?P<decimal>d*|\d+(_\d+)*)
            ^^
```

@mdickinson mdickinson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR! The fix and the new tests look good to me. There's a PEP8 nit in about comments in the fractions.py file. We also need a news entry - are you in a position to add that? (I can create the news entry if you prefer.)

Comment thread Lib/fractions.py Outdated
@bedevere-app

bedevere-app Bot commented Jan 13, 2024

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@mdickinson

Copy link
Copy Markdown
Member

Random observation: the subpattern \d*|\d+(_\d+)* is slightly inelegant, in that the only case that the first branch of the regex covers that isn't already covered by the second branch is the empty string. (\d+(_\d+)*)? might be slightly better, and involves less backtracking in corner cases involving a large number of digits followed by an underscore (and more digits). That said, I don't think it's worth changing this in this PR. The bugfix backport in particular should be as simple as possible and not risk introducing more errors, so I think the fix in this PR is the right way to go.

@mdickinson mdickinson added needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Jan 13, 2024
Make sure the comments in the regex are 2 spaces away from the content.
@thatbirdguythatuknownot

thatbirdguythatuknownot commented Jan 13, 2024

Copy link
Copy Markdown
Contributor Author

The subpattern \d*|\d+(_\d+)* is slightly inelegant, in that the only case that the first branch of the regex covers that isn't already covered by the second branch is the empty string. (\d+(_\d+)*)? might be slightly better, and involves less backtracking in corner cases involving a large number of digits followed by an underscore (and more digits).

I considered that, but the num group was using the former pattern; I decided to make the decimal group pattern consistent with that, since it seemed to me that the only point of the issue is to fix the decimal pattern and not change anything else.

@mdickinson

Copy link
Copy Markdown
Member

it seemed to me that the only point of the issue is to fix the denom pattern and not change anything else

Yep, agreed - it's good to be conservative with bug fixes, especially those that'll be backported to older Python versions.

@thatbirdguythatuknownot

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

Comment thread Misc/NEWS.d/next/Library/2024-01-13-11-34-29.gh-issue-114014.WRHifN.rst Outdated
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>

@mdickinson mdickinson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mdickinson mdickinson merged commit dd56b57 into python:main Jan 13, 2024
@miss-islington-app

Copy link
Copy Markdown

Thanks @thatbirdguythatuknownot for the PR, and @mdickinson for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 13, 2024
…ex (pythonGH-114015)

Fix a bug in the regex used for parsing a string input to the `fractions.Fraction` constructor. That bug led to an inconsistent exception message being given for some inputs.

---------

(cherry picked from commit dd56b57)

Co-authored-by: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
@miss-islington-app

Copy link
Copy Markdown

Sorry, @thatbirdguythatuknownot and @mdickinson, I could not cleanly backport this to 3.11 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker dd56b5748317c3d504d6a9660d9207620c547f5c 3.11

@bedevere-app

bedevere-app Bot commented Jan 13, 2024

Copy link
Copy Markdown

GH-114023 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Jan 13, 2024
mdickinson added a commit to mdickinson/cpython that referenced this pull request Jan 13, 2024
…ex (python#114015)

Fix a bug in the regex used for parsing a string input to the `fractions.Fraction` constructor. That bug led to an inconsistent exception message being given for some inputs.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
@bedevere-app

bedevere-app Bot commented Jan 13, 2024

Copy link
Copy Markdown

GH-114025 is a backport of this pull request to the 3.11 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.11 only security fixes label Jan 13, 2024
@mdickinson

Copy link
Copy Markdown
Member

I wasn't able to cajole cherry_picker into doing the backport for me for 3.11, so I've backported manually in #114025.

@bedevere-app

bedevere-app Bot commented Jan 13, 2024

Copy link
Copy Markdown

GH-114025 is a backport of this pull request to the 3.11 branch.

terryjreedy pushed a commit that referenced this pull request Jan 13, 2024
…gex (GH-114015) (#114023)

Fix a bug in the regex used for parsing a string input to the `fractions.Fraction` constructor. That bug led to an inconsistent exception message being given for some inputs.

---------

(cherry picked from commit dd56b57)

Co-authored-by: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
terryjreedy pushed a commit that referenced this pull request Jan 13, 2024
…gex (GH-114015) (#114025)

Fix a bug in the regex used for parsing a string input to the `fractions.Fraction` constructor. That bug led to an inconsistent exception message being given for some inputs.

---------

Co-authored-by: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
kulikjak pushed a commit to kulikjak/cpython that referenced this pull request Jan 22, 2024
…ex (python#114015)

Fix a bug in the regex used for parsing a string input to the `fractions.Fraction` constructor. That bug led to an inconsistent exception message being given for some inputs.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…ex (python#114015)

Fix a bug in the regex used for parsing a string input to the `fractions.Fraction` constructor. That bug led to an inconsistent exception message being given for some inputs.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…ex (python#114015)

Fix a bug in the regex used for parsing a string input to the `fractions.Fraction` constructor. That bug led to an inconsistent exception message being given for some inputs.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
scoder added a commit to scoder/quicktions that referenced this pull request Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistency in fractions.Fraction() initialization error with invalid string

2 participants