122
122
"""
123
123
124
124
import enum
125
- import sre_compile
126
- import sre_parse
125
+ from . import _compiler , _parser
127
126
import functools
128
127
try :
129
128
import _locale
146
145
@enum ._simple_enum (enum .IntFlag , boundary = enum .KEEP )
147
146
class RegexFlag :
148
147
NOFLAG = 0
149
- ASCII = A = sre_compile .SRE_FLAG_ASCII # assume ascii "locale"
150
- IGNORECASE = I = sre_compile .SRE_FLAG_IGNORECASE # ignore case
151
- LOCALE = L = sre_compile .SRE_FLAG_LOCALE # assume current 8-bit locale
152
- UNICODE = U = sre_compile .SRE_FLAG_UNICODE # assume unicode "locale"
153
- MULTILINE = M = sre_compile .SRE_FLAG_MULTILINE # make anchors look for newline
154
- DOTALL = S = sre_compile .SRE_FLAG_DOTALL # make dot match newline
155
- VERBOSE = X = sre_compile .SRE_FLAG_VERBOSE # ignore whitespace and comments
148
+ ASCII = A = _compiler .SRE_FLAG_ASCII # assume ascii "locale"
149
+ IGNORECASE = I = _compiler .SRE_FLAG_IGNORECASE # ignore case
150
+ LOCALE = L = _compiler .SRE_FLAG_LOCALE # assume current 8-bit locale
151
+ UNICODE = U = _compiler .SRE_FLAG_UNICODE # assume unicode "locale"
152
+ MULTILINE = M = _compiler .SRE_FLAG_MULTILINE # make anchors look for newline
153
+ DOTALL = S = _compiler .SRE_FLAG_DOTALL # make dot match newline
154
+ VERBOSE = X = _compiler .SRE_FLAG_VERBOSE # ignore whitespace and comments
156
155
# sre extensions (experimental, don't rely on these)
157
- TEMPLATE = T = sre_compile .SRE_FLAG_TEMPLATE # disable backtracking
158
- DEBUG = sre_compile .SRE_FLAG_DEBUG # dump pattern after compilation
156
+ TEMPLATE = T = _compiler .SRE_FLAG_TEMPLATE # disable backtracking
157
+ DEBUG = _compiler .SRE_FLAG_DEBUG # dump pattern after compilation
159
158
__str__ = object .__str__
160
159
_numeric_repr_ = hex
161
160
162
161
# sre exception
163
- error = sre_compile .error
162
+ error = _compiler .error
164
163
165
164
# --------------------------------------------------------------------
166
165
# public interface
@@ -257,8 +256,8 @@ def escape(pattern):
257
256
pattern = str (pattern , 'latin1' )
258
257
return pattern .translate (_special_chars_map ).encode ('latin1' )
259
258
260
- Pattern = type (sre_compile .compile ('' , 0 ))
261
- Match = type (sre_compile .compile ('' , 0 ).match ('' ))
259
+ Pattern = type (_compiler .compile ('' , 0 ))
260
+ Match = type (_compiler .compile ('' , 0 ).match ('' ))
262
261
263
262
# --------------------------------------------------------------------
264
263
# internals
@@ -279,9 +278,9 @@ def _compile(pattern, flags):
279
278
raise ValueError (
280
279
"cannot process flags argument with a compiled pattern" )
281
280
return pattern
282
- if not sre_compile .isstring (pattern ):
281
+ if not _compiler .isstring (pattern ):
283
282
raise TypeError ("first argument must be string or compiled pattern" )
284
- p = sre_compile .compile (pattern , flags )
283
+ p = _compiler .compile (pattern , flags )
285
284
if not (flags & DEBUG ):
286
285
if len (_cache ) >= _MAXCACHE :
287
286
# Drop the oldest item
@@ -295,12 +294,12 @@ def _compile(pattern, flags):
295
294
@functools .lru_cache (_MAXCACHE )
296
295
def _compile_repl (repl , pattern ):
297
296
# internal: compile replacement pattern
298
- return sre_parse .parse_template (repl , pattern )
297
+ return _parser .parse_template (repl , pattern )
299
298
300
299
def _expand (pattern , match , template ):
301
300
# internal: Match.expand implementation hook
302
- template = sre_parse .parse_template (template , pattern )
303
- return sre_parse .expand_template (template , match )
301
+ template = _parser .parse_template (template , pattern )
302
+ return _parser .expand_template (template , match )
304
303
305
304
def _subx (pattern , template ):
306
305
# internal: Pattern.sub/subn implementation helper
@@ -309,7 +308,7 @@ def _subx(pattern, template):
309
308
# literal replacement
310
309
return template [1 ][0 ]
311
310
def filter (match , template = template ):
312
- return sre_parse .expand_template (template , match )
311
+ return _parser .expand_template (template , match )
313
312
return filter
314
313
315
314
# register myself for pickling
@@ -326,22 +325,22 @@ def _pickle(p):
326
325
327
326
class Scanner :
328
327
def __init__ (self , lexicon , flags = 0 ):
329
- from sre_constants import BRANCH , SUBPATTERN
328
+ from . _constants import BRANCH , SUBPATTERN
330
329
if isinstance (flags , RegexFlag ):
331
330
flags = flags .value
332
331
self .lexicon = lexicon
333
332
# combine phrases into a compound pattern
334
333
p = []
335
- s = sre_parse .State ()
334
+ s = _parser .State ()
336
335
s .flags = flags
337
336
for phrase , action in lexicon :
338
337
gid = s .opengroup ()
339
- p .append (sre_parse .SubPattern (s , [
340
- (SUBPATTERN , (gid , 0 , 0 , sre_parse .parse (phrase , flags ))),
338
+ p .append (_parser .SubPattern (s , [
339
+ (SUBPATTERN , (gid , 0 , 0 , _parser .parse (phrase , flags ))),
341
340
]))
342
341
s .closegroup (gid , p [- 1 ])
343
- p = sre_parse .SubPattern (s , [(BRANCH , (None , p ))])
344
- self .scanner = sre_compile .compile (p )
342
+ p = _parser .SubPattern (s , [(BRANCH , (None , p ))])
343
+ self .scanner = _compiler .compile (p )
345
344
def scan (self , string ):
346
345
result = []
347
346
append = result .append
0 commit comments