diff --git a/python/phonenumbers/re_util.py b/python/phonenumbers/re_util.py index 03361a0a..7a3f2d60 100644 --- a/python/phonenumbers/re_util.py +++ b/python/phonenumbers/re_util.py @@ -22,23 +22,35 @@ 1 """ import re +import sys +if sys.version_info >= (3, 4): # pragma no cover -def fullmatch(pattern, string, flags=0): - """Try to apply the pattern at the start of the string, returning a match - object if the whole string matches, or None if no match was found.""" - # Build a version of the pattern with a non-capturing group around it. - # This is needed to get m.end() to correctly report the size of the - # matched expression (as per the final doctest above). - grouped_pattern = re.compile("^(?:%s)$" % pattern.pattern, pattern.flags) - m = grouped_pattern.match(string) - if m and m.end() < len(string): - # Incomplete match (which should never happen because of the $ at the - # end of the regexp), treat as failure. - m = None # pragma no cover - return m + def fullmatch(pattern, string): + """Try to apply the pattern at the start of the string, returning a match + object if the whole string matches, or None if no match was found.""" + return pattern.fullmatch(string) -if __name__ == '__main__': # pragma no cover +else: + + def fullmatch(pattern, string): + """Try to apply the pattern at the start of the string, returning a match + object if the whole string matches, or None if no match was found.""" + + # Build a version of the pattern with a non-capturing group around it. + # This is needed to get m.end() to correctly report the size of the + # matched expression (as per the final doctest above). + grouped_pattern = re.compile("^(?:%s)$" % pattern.pattern, pattern.flags) + m = grouped_pattern.match(string) + if m and m.end() < len(string): + # Incomplete match (which should never happen because of the $ at the + # end of the regexp), treat as failure. + m = None # pragma no cover + return m + + +if __name__ == "__main__": # pragma no cover import doctest + doctest.testmod() diff --git a/python/phonenumbers/re_util.pyi b/python/phonenumbers/re_util.pyi index 3f4efaa6..2eefb861 100644 --- a/python/phonenumbers/re_util.pyi +++ b/python/phonenumbers/re_util.pyi @@ -1,4 +1,4 @@ from re import Match from re import Pattern -def fullmatch(pattern: Pattern[str], string: str, flags: int = ...) -> Match[str] | None: ... +def fullmatch(pattern: Pattern[str], string: str) -> Match[str] | None: ...