re module defines re.match both as a function and class
There appears to be a conflict in both the documentation and the implementation of the re/ure module.
The documentation for the re module specifies that re.match is a function
.. function:: match(regex_str, string)
Compile *regex_str* and match against *string*. Match always happens
from starting position in a string.
However it also appears to document that re.match is a class, as there are methods documented.
I would expect that the class would be named Match rather then match
Match objects
-------------
Match objects as returned by `match()` and `search()` methods, and passed
to the replacement function in `sub()`.
.. method:: match.group(index)
Return matching (sub)string. *index* is 0 for entire match,
1 and above for each capturing group. Only numeric groups are supported.
.. method:: match.groups()
When checking the implementation ( v1.18) with the below code it appears that there is indeed a class named match returned from the function match.
import re
Substring ='.*Python'
String1 = "MicroPython"
m =re.match(Substring, String1)
print(type(m))
# MicroPython: <class 'match'>
# CPython: <class 're.Match'>
The reason that I noticed this is that I am creating and validating .pyi stubs that are autogenerated from the documentation, and in as part of test and validation noticed this conflict.
Its not to difficult to create another PR to update the documentation, however that would not match the current implementation.
However my main question is: should the implementation be updated to name the class 'Match' ?
Fix unix-ffi/re: Fix OverflowError in re.groups().
Adjust the re.groups() methods to properly handle the PCRE2_UNSET value for unmatched optional groups.
This change prevents OverflowError when calling groups() on a match with no content.
The return matches CPython's.
A test case for an empty string match has been added to verify expected behavior.
Fixes micropython/micropython#18877
Thanks for this, the fix looks good!
Can I suggest adding more tests, ie:
That will test
group()behaviour and default value.Sure, happy to add.
I noticed that the readme mentions 'unsupported' for the unix-ffi folder, and I do not think these modules are included in CI testing either.
So AFIKT the tests need to be run manually, so still useful for validating.
I think it is run under CI, see
tools/ci.sh:ci_package_tests_run.@Josverl are you able to add those few tests I list above?
sorry , got sidetracked.
You are correct that the tests are run in CI.
Added the tests as requested.