Lazily compile a regex with flags.
(regex, flags=0)
| 341 | |
| 342 | |
| 343 | def _lazy_re_compile(regex, flags=0): |
| 344 | """Lazily compile a regex with flags.""" |
| 345 | |
| 346 | def _compile(): |
| 347 | # Compile the regex if it was not passed pre-compiled. |
| 348 | if isinstance(regex, (str, bytes)): |
| 349 | return re.compile(regex, flags) |
| 350 | else: |
| 351 | assert not flags, "flags must be empty if regex is passed pre-compiled" |
| 352 | return regex |
| 353 | |
| 354 | return SimpleLazyObject(_compile) |
no test coverage detected