MCPcopy Index your code
hub / github.com/python/cpython / pattern

Method pattern

Lib/_strptime.py:453–489  ·  view source on GitHub ↗

Return regex pattern for the format string. Need to make sure that any characters that might be interpreted as regex syntax are escaped.

(self, format)

Source from the content-addressed store, hash-verified

451 return '(?P<%s>%s)' % (directive, regex)
452
453 def pattern(self, format):
454 """Return regex pattern for the format string.
455
456 Need to make sure that any characters that might be interpreted as
457 regex syntax are escaped.
458
459 """
460 # The sub() call escapes all characters that might be misconstrued
461 # as regex syntax. Cannot use re.escape since we have to deal with
462 # format directives (%m, etc.).
463 format = re_sub(r"([\\.^$*+?\(\){}\[\]|])", r"\\\1", format)
464 format = re_sub(r'\s+', r'\\s+', format)
465 format = re_sub(r"'", "['\u02bc]", format) # needed for br_FR
466 year_in_format = False
467 day_of_month_in_format = False
468 def repl(m):
469 directive = m.group()[1:] # exclude `%` symbol
470 match directive:
471 case 'Y' | 'y' | 'G':
472 nonlocal year_in_format
473 year_in_format = True
474 case 'd':
475 nonlocal day_of_month_in_format
476 day_of_month_in_format = True
477 return self[directive]
478 format = re_sub(r'%[-_0^#]*[0-9]*([OE]?[:\\]?.?)', repl, format)
479 if day_of_month_in_format and not year_in_format:
480 import warnings
481 warnings.warn("""\
482Parsing dates involving a day of month without a year specified is ambiguous
483and fails to parse leap day. The default behavior will change in Python 3.15
484to either always raise an exception or to use a different default year (TBD).
485To avoid trouble, add a specific year to the input & format.
486See https://github.com/python/cpython/issues/70647.""",
487 DeprecationWarning,
488 skip_file_prefixes=(os.path.dirname(__file__),))
489 return format
490
491 def compile(self, format):
492 """Return a compiled re object for the format string."""

Callers 6

__init__Method · 0.95
compileMethod · 0.95
test_patternMethod · 0.80
test_pattern_escapingMethod · 0.80
test_blankpatternMethod · 0.80

Calls 2

warnMethod · 0.45
dirnameMethod · 0.45

Tested by 4

test_patternMethod · 0.64
test_pattern_escapingMethod · 0.64
test_blankpatternMethod · 0.64