[WIP] gh-89621: re.subn?(): raise error when RegexFlag is wrongly used as count argument#91477
Conversation
if count is RegexFlag, raise an error.
|
Another method is implementing def sub(pattern, repl, string, count=0, flags=0):
return _compile(pattern, flags).sub(repl, string, count)
def subn(pattern, repl, string, count=0, flags=0):
return _compile(pattern, flags).subn(repl, string, count) |
| PyErr_SetString(PyExc_ValueError, | ||
| "count arguemnt wrong type."); |
There was a problem hiding this comment.
Just keep the existing error. ValueError is wrong exception for wrong type, and the raised exception can be for example OverflowError.
| "count argument should not be RegexFlag."); | ||
| return NULL; | ||
| } else { | ||
| count_value = PyLong_AsSsize_t(count); |
There was a problem hiding this comment.
Look at the code for the n format unit in getargs.c. You should reproduce it.
| repl: object | ||
| string: object | ||
| count: Py_ssize_t = 0 | ||
| count: object(c_default="NULL") = 0 |
There was a problem hiding this comment.
It could be easier to implement a custom converter (see docs for PyArg_Parse, format unit O&).
count: object(converter="my_converter_for_ssize_t_except_RegexFlag") = 0
|
I have just fallen into this trap. It's very easy to get cocky and forget to pay attention to the Any chance of at least adding type hints to the |
@serhiy-storchaka
In
re.subn?()functions, some users wrongly useRegexFlagas thecountparameter.Many invalid bug reports can be found (#89621, #86639, #85930, #85830, #79724, #75110, #73091, #72038, #70542, #66949, #61863, #59972, #56287, #56166, #56156, #55471). Maybe there are some code still generating incorrect data silently.
This PR is just a draft, not completive. I didn't expect this PR to be merged.
See if you agree with this method. Compared to changing
countto a keyword only parameter, it keeps compatibility, and the overhead is very small.If you want to make a better PR, please make it.
IMO making such PR is faster & easier than reviewing.
Or let me know if you want me to perfect this PR.