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