54
54
import math # for log
55
55
import os
56
56
import re
57
- import sre_compile
58
57
import string
59
58
import sys
60
59
import sysconfig
66
65
67
66
__VERSION__ = '1.6.1'
68
67
68
+ # sre_compile will be/has been removed in Python 3.13
69
+ # use re._compiler instead
70
+ # Refs: https://github.com/python/cpython/issues/105456
71
+ # Refs: https://github.com/python/cpython/issues/91308
72
+ try :
73
+ srecompile = re ._compiler .compile
74
+ except AttributeError :
75
+ import sre_compile
76
+ srecompile = sre_compile .compile
77
+
69
78
try :
70
79
# -- pylint: disable=used-before-assignment
71
80
xrange # Python 2
@@ -1077,7 +1086,7 @@ def Match(pattern, s):
1077
1086
# performance reasons; factoring it out into a separate function turns out
1078
1087
# to be noticeably expensive.
1079
1088
if pattern not in _regexp_compile_cache :
1080
- _regexp_compile_cache [pattern ] = sre_compile . compile (pattern )
1089
+ _regexp_compile_cache [pattern ] = srecompile (pattern )
1081
1090
return _regexp_compile_cache [pattern ].match (s )
1082
1091
1083
1092
@@ -1095,14 +1104,14 @@ def ReplaceAll(pattern, rep, s):
1095
1104
string with replacements made (or original string if no replacements)
1096
1105
"""
1097
1106
if pattern not in _regexp_compile_cache :
1098
- _regexp_compile_cache [pattern ] = sre_compile . compile (pattern )
1107
+ _regexp_compile_cache [pattern ] = srecompile (pattern )
1099
1108
return _regexp_compile_cache [pattern ].sub (rep , s )
1100
1109
1101
1110
1102
1111
def Search (pattern , s ):
1103
1112
"""Searches the string for the pattern, caching the compiled regexp."""
1104
1113
if pattern not in _regexp_compile_cache :
1105
- _regexp_compile_cache [pattern ] = sre_compile . compile (pattern )
1114
+ _regexp_compile_cache [pattern ] = srecompile (pattern )
1106
1115
return _regexp_compile_cache [pattern ].search (s )
1107
1116
1108
1117
0 commit comments