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
Warn about octal escapes > 0o377 in re #66558
Comments
Currently the re module accepts octal escapes from \400 to \777, but ignore highest bit. >>> re.search(r'\542', 'abc')
<_sre.SRE_Match object; span=(1, 2), match='b'> This behavior looks surprising and is inconsistent with the regex module which preserve highest bit. Such escaping is not portable across different regular exception engines. I propose to add a warning when octal escape value is larger than 0o377. Here is preliminary patch which adds UserWarning. Or may be better to emit DeprecationWarning and then replace it by ValueError in future releases? |
I think we should simply raise ValueError in 3.5. There's no reason to accept such invalid escapes. |
Well, here is a patch which makes re raise an exception on suspicious octals. |
re_octal_escape_overflow_raise.patch: you should write a subfunction to not repeat the error message 3 times. + if c > 0o377: Hum, I never use octal. 255 instead of 0o377 would be less surprising :-p By the way, you should also check for negative numbers. >>> -3 & 0xff
253 Before, "& 0xff" also converted negative numbers to positive in range 0..255. |
Not in this case. You can't construct negative number from three octal digits. |
Warning or exception? This is a question. |
Using -Werror, warnings raise exceptions :-) |
This is an error, so it should really be an exception. There's no use case for being lenient, IMO. |
If this is error, should the patch be applied to maintained releases? |
New changeset 3b32f495fb38 by Serhiy Storchaka in branch 'default': |
Thanks Antoine and Victor for the review. |
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: