3737# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838# SOFTWARE.
3939
40- import polyglot as _interop
41-
4240from mmap import mmap
4341
4442_mappingpoxy = type (type .__dict__ )
@@ -81,6 +79,40 @@ def setup(sre_compiler, error_class, flags_table):
8179 FLAGS = flags_table
8280
8381 def configure_fallback_compiler (mode ):
82+ # wraps a native 're.Pattern' object
83+ class ExecutablePattern :
84+ def __init__ (self , sticky , compiled_pattern ):
85+ self .__sticky__ = sticky
86+ self .__compiled_pattern__ = compiled_pattern
87+
88+ def __call__ (self , * args ):
89+ # deprecated
90+ return self .exec (* args )
91+
92+ def exec (self , * args ):
93+ nargs = len (args )
94+ if nargs == 2 :
95+ # new-style signature
96+ pattern_input , from_index = args
97+ elif nargs == 3 :
98+ # old-style signature; deprecated
99+ _ , pattern_input , from_index = args
100+ else :
101+ raise TypeError ("invalid arguments: " + repr (args ))
102+ if self .__sticky__ :
103+ result = self .__compiled_pattern__ .match (pattern_input , from_index )
104+ else :
105+ result = self .__compiled_pattern__ .search (pattern_input , from_index )
106+ is_match = result is not None
107+ group_count = 1 + self .__compiled_pattern__ .groups
108+ return _RegexResult (
109+ pattern_input = pattern_input ,
110+ isMatch = is_match ,
111+ groupCount = group_count if is_match else 0 ,
112+ start = [result .start (i ) for i in range (group_count )] if is_match else [],
113+ end = [result .end (i ) for i in range (group_count )] if is_match else []
114+ )
115+
84116 def fallback_compiler (pattern , flags ):
85117 sticky = False
86118 bit_flags = 0
@@ -93,53 +125,30 @@ def fallback_compiler(pattern, flags):
93125
94126 compiled_pattern = sre_compiler (pattern if mode == "str" else _str_to_bytes (pattern ), bit_flags )
95127
96- # wraps a native 're.Pattern' object
97- class ExecutablePattern :
98- def __call__ (self , * args ):
99- # deprecated
100- return self .exec (* args )
101-
102- def exec (self , * args ):
103- nargs = len (args )
104- if nargs == 2 :
105- # new-style signature
106- pattern_input , from_index = args
107- elif nargs == 3 :
108- # old-style signature; deprecated
109- _ , pattern_input , from_index = args
110- else :
111- raise TypeError ("invalid arguments: " + repr (args ))
112- search_method = compiled_pattern .match if sticky else compiled_pattern .search
113- result = search_method (pattern_input , from_index )
114- is_match = result is not None
115- group_count = 1 + compiled_pattern .groups
116- return _RegexResult (
117- pattern_input = pattern_input ,
118- isMatch = is_match ,
119- groupCount = group_count if is_match else 0 ,
120- start = [result .start (i ) for i in range (group_count )] if is_match else [],
121- end = [result .end (i ) for i in range (group_count )] if is_match else []
122- )
123-
124- return ExecutablePattern ()
128+ return ExecutablePattern (sticky , compiled_pattern )
125129
126130 return fallback_compiler
127131
128132 engine_builder = _build_regex_engine ("" )
129133
130- global TREGEX_ENGINE_STR
131- global TREGEX_ENGINE_BYTES
132- TREGEX_ENGINE_STR = engine_builder ("Flavor=PythonStr" , configure_fallback_compiler ("str" ))
133- TREGEX_ENGINE_BYTES = engine_builder ("Flavor=PythonBytes" , configure_fallback_compiler ("bytes" ))
134+ if engine_builder :
135+ global TREGEX_ENGINE_STR
136+ global TREGEX_ENGINE_BYTES
137+ TREGEX_ENGINE_STR = engine_builder ("Flavor=PythonStr" , configure_fallback_compiler ("str" ))
138+ TREGEX_ENGINE_BYTES = engine_builder ("Flavor=PythonBytes" , configure_fallback_compiler ("bytes" ))
134139
135- def new_compile (p , flags = 0 ):
136- if isinstance (p , (str , bytes )):
137- return _tcompile (p , flags )
138- else :
140+ def new_compile (p , flags = 0 ):
141+ if isinstance (p , (str , bytes )):
142+ return _tcompile (p , flags )
143+ else :
144+ return sre_compiler (p , flags )
145+ else :
146+ def new_compile (p , flags = 0 ):
139147 return sre_compiler (p , flags )
140148
141149 return new_compile
142150
151+
143152CODESIZE = 4
144153
145154MAGIC = 20171005
@@ -196,8 +205,8 @@ def __group__(self, idx):
196205 def groupdict (self , default = None ):
197206 d = {}
198207 if self .compiled_regex .groups :
199- assert _interop . __has_keys__ (self .compiled_regex .groups )
200- for k in _interop . __keys__ (self .compiled_regex .groups ):
208+ assert dir (self .compiled_regex .groups )
209+ for k in dir (self .compiled_regex .groups ):
201210 idx = self .compiled_regex .groups [k ]
202211 d [k ] = self .__group__ (idx )
203212 return d
@@ -250,7 +259,7 @@ def __init__(self, pattern, flags):
250259 if self .__tregex_compile (self .pattern ).groups is not None :
251260 for group_name in dir (self .__tregex_compile (self .pattern ).groups ):
252261 groups = self .__tregex_compile (self .pattern ).groups
253- self .groups = _interop . __get_size__ ( _interop . __keys__ (groups ))
262+ self .groups = len ( dir (groups ))
254263 groupindex [group_name ] = groups [group_name ]
255264 self .groupindex = _mappingpoxy (groupindex )
256265
0 commit comments