47
47
import math # for log
48
48
import os
49
49
import re
50
- import sre_compile
51
50
import string
52
51
import sys
53
52
import unicodedata
54
53
54
+ # sre_compile will be/has been removed in Python 3.13
55
+ # use re._compiler instead
56
+ # Refs: https://github.com/python/cpython/issues/105456
57
+ # Refs: https://github.com/python/cpython/issues/91308
58
+ try :
59
+ srecompile = re ._compiler .compile
60
+ except AttributeError :
61
+ import sre_compile
62
+ srecompile = sre_compile .compile
55
63
56
64
try :
57
65
xrange (0 ,1 )
@@ -506,7 +514,7 @@ def Match(pattern, s):
506
514
# performance reasons; factoring it out into a separate function turns out
507
515
# to be noticeably expensive.
508
516
if pattern not in _regexp_compile_cache :
509
- _regexp_compile_cache [pattern ] = sre_compile . compile (pattern )
517
+ _regexp_compile_cache [pattern ] = srecompile (pattern )
510
518
return _regexp_compile_cache [pattern ].match (s )
511
519
512
520
@@ -524,14 +532,14 @@ def ReplaceAll(pattern, rep, s):
524
532
string with replacements made (or original string if no replacements)
525
533
"""
526
534
if pattern not in _regexp_compile_cache :
527
- _regexp_compile_cache [pattern ] = sre_compile . compile (pattern )
535
+ _regexp_compile_cache [pattern ] = srecompile (pattern )
528
536
return _regexp_compile_cache [pattern ].sub (rep , s )
529
537
530
538
531
539
def Search (pattern , s ):
532
540
"""Searches the string for the pattern, caching the compiled regexp."""
533
541
if pattern not in _regexp_compile_cache :
534
- _regexp_compile_cache [pattern ] = sre_compile . compile (pattern )
542
+ _regexp_compile_cache [pattern ] = srecompile (pattern )
535
543
return _regexp_compile_cache [pattern ].search (s )
536
544
537
545
0 commit comments