New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
repr(regex) doesn't include actual regex #57801
Comments
When calling repr() on a compiled regex pattern like this:
you don't get the pattern of the regex out of the compiled form. Also all my research has shown no getter to allow this. I noticed this in my application because I was unable to show good error messages for things involving regexes, which is a shame. So please add the actual regex to the repr() form of the compiled regex, or alternatively provide a getter / property to get at it. |
I'm not sure having the pattern in the repr will make it more readable, since the regex might even be very long. You can use the .pattern attribute if you want to see the pattern. |
Hmm, I think it's a reasonable feature request myself. |
In reply to Ezio, the repr of a large string, list, tuple or dict is also long. The repr of a compiled regex should probably also show the flags, but should it just be the numeric value? |
ISTM that .pattern is the one way to do it. |
To me this is like saying the repr() of functions should not show their |
If you change the repr, it should at least eval-able, so be sure to capture the flags and whatnot. |
Actually, one possibility that occurs to me is to provide the flags within the pattern. The .pattern attribute gives the original pattern, but repr could give the flags in-line at the start of the pattern: >>> # Assuming Python 3.
>>> r = re.compile("a", re.I)
>>> r.flags
34
>>> r.pattern
'a'
>>> repr(r)
"<_sre.SRE_Pattern '(?i)a'>" I'm not sure how to make it eval-able, unless you mean something more like: >>> repr(r)
"re.Regex('(?i)a')" where re.Regex == re.compile, which would be more meaningful than: >>> repr(r)
"re.compile('(?i)a')" |
If an eval-able re.Regex is used, the flags can be showed as second arg, like: The repr can be generated with something like |
That is what I would like to see. |
I'm just adding this to the regex module and I've come up against a possible issue. The regex module supports named lists, which could be very big. Should the entire contents of those lists also be shown in the repr?They would have to be if the repr is to be a eval-able. |
I don't see how eval()able repr is a big deal. Most reprs aren't, and I |
"I don't see how eval()able repr is a big deal. Most reprs aren't, ..." Sometimes, I wonder if we're even talking about the same programming language. Historically, a good deal of effort has gone into creating evalable reprs, if only because they accurately describe an object and because they teach users how to create similar objects. But it only takes one committer who doesn't care about evalable reprs to permanently break the pattern for everyone :-( |
Raymond, Antoine: I don't see your claims as contradictory, it's definitely true that the Python standardlib has historically tried to keep reprs as being eval-able, I think Antoine's correct that the vast majority of 3rd-party code does not keep with that trend. |
So 95% of our datatypes were committed by a single person? :) |
Hey, I started the patch under >>> import re
>>> re.compile("foo")
re.compile("foo", re.UNICODE)
>>> re.compile("foo", re.DOTALL)
re.compile("foo", re.DOTALL|re.UNICODE)
>>> re.compile("foo", re.DOTALL|re.MULTILINE)
re.compile("foo", re.MULTILINE|re.DOTALL|re.UNICODE)
>>> Do you have any comments on it? I want to adapt the patch to make it work with python 2.7 too. Do you think is it worthful? The attached patch was done after commit 3fbfa61634de. |
Python 2.7 is the end of the Python 2 line, and it's closed except for security fixes. |
2.7 is on extended maintenance for normal bugs, but does not get new features/enhancements. It is too late for 3.3 also. |
Thanks for the review ezio.melotti. He has notice a few things in my patch:
And I realized I was not covering the case where no flags are enabled (byte string, for instance). And I have fixed all this issues. And now I think this patch would work against py2x and py3k anyway. Attaching a new patch. |
Changed two test names to avoid misunderstanding. |
Any news about this patch? Is it going to be merged? When is next CPython release? |
See also bpo-17087 which is essentially the same issue but for match objects. |
Here is fixed and simplified patch. |
|
New changeset 8c00677da6c0 by Serhiy Storchaka in branch 'default': |
Thank you Hugo for your contribution. Thank you Thomas and Ezio for your reviews and suggestions. |
New changeset 4ba7a29fe02c by Ezio Melotti in branch 'default': |
Isn't the trucation of long patterns too rough? Currently, repr(re.compile("a" * 1000)) returns something like "re.compile('aaaaaaaaaaaaa)", i.e. no ending quote and no indication that something was truncated (besides the missing quote). It looked like a bug to me at first sight. |
This looks weird, but is not a bug. See bpo-26090. After implementing that feature truncating a pattern would look more explicit. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: