122122"""
123123
124124import enum
125- import sre_compile
126- import sre_parse
125+ from . import _compiler , _parser
127126import functools
128127try :
129128 import _locale
146145@enum ._simple_enum (enum .IntFlag , boundary = enum .KEEP )
147146class RegexFlag :
148147 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
156155 # 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
159158 __str__ = object .__str__
160159 _numeric_repr_ = hex
161160
162161# sre exception
163- error = sre_compile .error
162+ error = _compiler .error
164163
165164# --------------------------------------------------------------------
166165# public interface
@@ -257,8 +256,8 @@ def escape(pattern):
257256 pattern = str (pattern , 'latin1' )
258257 return pattern .translate (_special_chars_map ).encode ('latin1' )
259258
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 ('' ))
262261
263262# --------------------------------------------------------------------
264263# internals
@@ -279,9 +278,9 @@ def _compile(pattern, flags):
279278 raise ValueError (
280279 "cannot process flags argument with a compiled pattern" )
281280 return pattern
282- if not sre_compile .isstring (pattern ):
281+ if not _compiler .isstring (pattern ):
283282 raise TypeError ("first argument must be string or compiled pattern" )
284- p = sre_compile .compile (pattern , flags )
283+ p = _compiler .compile (pattern , flags )
285284 if not (flags & DEBUG ):
286285 if len (_cache ) >= _MAXCACHE :
287286 # Drop the oldest item
@@ -295,12 +294,12 @@ def _compile(pattern, flags):
295294@functools .lru_cache (_MAXCACHE )
296295def _compile_repl (repl , pattern ):
297296 # internal: compile replacement pattern
298- return sre_parse .parse_template (repl , pattern )
297+ return _parser .parse_template (repl , pattern )
299298
300299def _expand (pattern , match , template ):
301300 # 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 )
304303
305304def _subx (pattern , template ):
306305 # internal: Pattern.sub/subn implementation helper
@@ -309,7 +308,7 @@ def _subx(pattern, template):
309308 # literal replacement
310309 return template [1 ][0 ]
311310 def filter (match , template = template ):
312- return sre_parse .expand_template (template , match )
311+ return _parser .expand_template (template , match )
313312 return filter
314313
315314# register myself for pickling
@@ -326,22 +325,22 @@ def _pickle(p):
326325
327326class Scanner :
328327 def __init__ (self , lexicon , flags = 0 ):
329- from sre_constants import BRANCH , SUBPATTERN
328+ from . _constants import BRANCH , SUBPATTERN
330329 if isinstance (flags , RegexFlag ):
331330 flags = flags .value
332331 self .lexicon = lexicon
333332 # combine phrases into a compound pattern
334333 p = []
335- s = sre_parse .State ()
334+ s = _parser .State ()
336335 s .flags = flags
337336 for phrase , action in lexicon :
338337 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 ))),
341340 ]))
342341 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 )
345344 def scan (self , string ):
346345 result = []
347346 append = result .append
0 commit comments