Skip to content

Commit 1be3260

Browse files
bpo-47152: Convert the re module into a package (GH-32177)
The sre_* modules are now deprecated.
1 parent 4ed8a9a commit 1be3260

16 files changed

+2235
-2182
lines changed

Doc/library/modulefinder.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ Sample output (may vary depending on the architecture)::
9696
Loaded modules:
9797
_types:
9898
copyreg: _inverted_registry,_slotnames,__all__
99-
sre_compile: isstring,_sre,_optimize_unicode
99+
re._compiler: isstring,_sre,_optimize_unicode
100100
_sre:
101-
sre_constants: REPEAT_ONE,makedict,AT_END_LINE
101+
re._constants: REPEAT_ONE,makedict,AT_END_LINE
102102
sys:
103103
re: __module__,finditer,_expand
104104
itertools:
105105
__main__: re,itertools,baconhameggs
106-
sre_parse: _PATTERNENDERS,SRE_FLAG_UNICODE
106+
re._parser: _PATTERNENDERS,SRE_FLAG_UNICODE
107107
array:
108108
types: __module__,IntType,TypeType
109109
---------------------------------------------------

Doc/library/profile.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ the following::
7373
ncalls tottime percall cumtime percall filename:lineno(function)
7474
1 0.000 0.000 0.002 0.002 {built-in method builtins.exec}
7575
1 0.000 0.000 0.001 0.001 <string>:1(<module>)
76-
1 0.000 0.000 0.001 0.001 re.py:250(compile)
77-
1 0.000 0.000 0.001 0.001 re.py:289(_compile)
78-
1 0.000 0.000 0.000 0.000 sre_compile.py:759(compile)
79-
1 0.000 0.000 0.000 0.000 sre_parse.py:937(parse)
80-
1 0.000 0.000 0.000 0.000 sre_compile.py:598(_code)
81-
1 0.000 0.000 0.000 0.000 sre_parse.py:435(_parse_sub)
76+
1 0.000 0.000 0.001 0.001 __init__.py:250(compile)
77+
1 0.000 0.000 0.001 0.001 __init__.py:289(_compile)
78+
1 0.000 0.000 0.000 0.000 _compiler.py:759(compile)
79+
1 0.000 0.000 0.000 0.000 _parser.py:937(parse)
80+
1 0.000 0.000 0.000 0.000 _compiler.py:598(_code)
81+
1 0.000 0.000 0.000 0.000 _parser.py:435(_parse_sub)
8282

8383
The first line indicates that 214 calls were monitored. Of those calls, 207
8484
were :dfn:`primitive`, meaning that the call was not induced via recursion. The

Doc/whatsnew/3.11.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@ Deprecated
532532
be able to parse Python 3.10 or newer. See the :pep:`617` (New PEG parser for
533533
CPython). (Contributed by Victor Stinner in :issue:`40360`.)
534534

535+
* Undocumented modules ``sre_compile``, ``sre_constants`` and ``sre_parse``
536+
are now deprecated.
537+
(Contributed by Serhiy Storchaka in :issue:`47152`.)
538+
535539
* :class:`webbrowser.MacOSX` is deprecated and will be removed in Python 3.13.
536540
It is untested and undocumented and also not used by webbrowser itself.
537541
(Contributed by Dong-hee Na in :issue:`42255`.)

Lib/re.py renamed to Lib/re/__init__.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@
122122
"""
123123

124124
import enum
125-
import sre_compile
126-
import sre_parse
125+
from . import _compiler, _parser
127126
import functools
128127
try:
129128
import _locale
@@ -146,21 +145,21 @@
146145
@enum._simple_enum(enum.IntFlag, boundary=enum.KEEP)
147146
class 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)
296295
def _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

300299
def _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

305304
def _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

327326
class 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

Comments
 (0)