10000 bpo-47152: Convert the re module into a package by serhiy-storchaka · Pull Request #32177 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-47152: Convert the re module into a package #32177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 2, 2022
Prev Previous commit
Next Next commit
Revert "Keep old names of sre_* modules after moving them."
This reverts commit 3da0b4c.
  • Loading branch information
serhiy-storchaka committed Apr 2, 2022
commit d1e520ac0a444afb3ccd04c8475d646aa1e399ed
6 changes: 3 additions & 3 deletions Doc/library/modulefinder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ Sample output (may vary depending on the architecture)::
Loaded modules:
_types:
copyreg: _inverted_registry,_slotnames,__all__
re.sre_compile: isstring,_sre,_optimize_unicode
re._compiler: isstring,_sre,_optimize_unicode
_sre:
re.sre_constants: REPEAT_ONE,makedict,AT_END_LINE
re._constants: REPEAT_ONE,makedict,AT_END_LINE
sys:
re: __module__,finditer,_expand
itertools:
__main__: re,itertools,baconhameggs
re.sre_parse: _PATTERNENDERS,SRE_FLAG_UNICODE
re._parser: _PATTERNENDERS,SRE_FLAG_UNICODE
array:
types: __module__,IntType,TypeType
---------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions Doc/library/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ the following::
1 0.000 0.000 0.001 0.001 <string>:1(<module>)
1 0.000 0.000 0.001 0.001 re.py:212(compile)
1 0.000 0.000 0.001 0.001 re.py:268(_compile)
1 0.000 0.000 0.000 0.000 sre_compile.py:172(_compile_charset)
1 0.000 0.000 0.000 0.000 sre_compile.py:201(_optimize_charset)
4 0.000 0.000 0.000 0.000 sre_compile.py:25(_identityfunction)
3/1 0.000 0.000 0.000 0.000 sre_compile.py:33(_compile)
1 0.000 0.000 0.000 0.000 _compiler.py:172(_compile_charset)
1 0.000 0.000 0.000 0.000 _compiler.py:201(_optimize_charset)
4 0.000 0.000 0.000 0.000 _compiler.py:25(_identityfunction)
3/1 0.000 0.000 0.000 0.000 _compiler.py:33(_compile)

The first line indicates that 197 calls were monitored. Of those calls, 192
were :dfn:`primitive`, meaning that the call was not induced via recursion. The
Expand Down
50 changes: 25 additions & 25 deletions Lib/re/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"""

import enum
from . import sre_compile, sre_parse
from . import _compiler, _parser
import functools
try:
import _locale
Expand All @@ -145,21 +145,21 @@
@enum._simple_enum(enum.IntFlag, boundary=enum.KEEP)
class RegexFlag:
NOFLAG = 0
ASCII = A = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
IGNORECASE = I = sre_compile.SRE_FLAG_IGNORECASE # ignore case
LOCALE = L = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
UNICODE = U = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
MULTILINE = M = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
DOTALL = S = sre_compile.SRE_FLAG_DOTALL # make dot match newline
VERBOSE = X = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
ASCII = A = _compiler.SRE_FLAG_ASCII # assume ascii "locale"
IGNORECASE = I = _compiler.SRE_FLAG_IGNORECASE # ignore case
LOCALE = L = _compiler.SRE_FLAG_LOCALE # assume current 8-bit locale
UNICODE = U = _compiler.SRE_FLAG_UNICODE # assume unicode "locale"
MULTILINE = M = _compiler.SRE_FLAG_MULTILINE # make anchors look for newline
DOTALL = S = _compiler.SRE_FLAG_DOTALL # make dot match newline
VERBOSE = X = _compiler.SRE_FLAG_VERBOSE # ignore whitespace and comments
# sre extensions (experimental, don't rely on these)
TEMPLATE = T = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
TEMPLATE = T = _compiler.SRE_FLAG_TEMPLATE # disable backtracking
DEBUG = _compiler.SRE_FLAG_DEBUG # dump pattern after compilation
__str__ = object.__str__
_numeric_repr_ = hex

# sre exception
error = sre_compile.error
error = _compiler.error

# --------------------------------------------------------------------
# public interface
Expand Down Expand Up @@ -256,8 +256,8 @@ def escape(pattern):
pattern = str(pattern, 'latin1')
return pattern.translate(_special_chars_map).encode('latin1')

Pattern = type(sre_compile.compile('', 0))
Match = type(sre_compile.compile('', 0).match(''))
Pattern = type(_compiler.compile('', 0))
Match = type(_compiler.compile('', 0).match(''))

# --------------------------------------------------------------------
# internals
Expand All @@ -278,9 +278,9 @@ def _compile(pattern, flags):
raise ValueError(
"cannot process flags argument with a compiled pattern")
return pattern
if not sre_compile.isstring(pattern):
if not _compiler.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern")
p = sre_compile.compile(pattern, flags)
p = _compiler.compile(pattern, flags)
if not (flags & DEBUG):
if len(_cache) >= _MAXCACHE:
# Drop the oldest item
Expand All @@ -294,12 +294,12 @@ def _compile(pattern, flags):
@functools.lru_cache(_MAXCACHE)
def _compile_repl(repl, pattern):
# internal: compile replacement pattern
return sre_parse.parse_template(repl, pattern)
return _parser.parse_template(repl, pattern)

def _expand(pattern, match, template):
# internal: Match.expand implementation hook
template = sre_parse.parse_template(template, pattern)
return sre_parse.expand_template(template, match)
template = _parser.parse_template(template, pattern)
return _parser.expand_template(template, match)

def _subx(pattern, template):
# internal: Pattern.sub/subn implementation helper
Expand All @@ -308,7 +308,7 @@ def _subx(pattern, template):
# literal replacement
return template[1][0]
def filter(match, template=template):
return sre_parse.expand_template(template, match)
return _parser.expand_template(template, match)
return filter

# register myself for pickling
Expand All @@ -325,22 +325,22 @@ def _pickle(p):

class Scanner:
def __init__(self, lexicon, flags=0):
from .sre_constants import BRANCH, SUBPATTERN
from ._constants import BRANCH, SUBPATTERN
if isinstance(flags, RegexFlag):
flags = flags.value
self.lexicon = lexicon
# combine phrases into a compound pattern
p = []
s = sre_parse.State()
s = _parser.State()
s.flags = flags
for phrase, action in lexicon:
gid = s.opengroup()
p.append(sre_parse.SubPattern(s, [
(SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))),
p.append(_parser.SubPattern(s, [
(SUBPATTERN, (gid, 0, 0, _parser.parse(phrase, flags))),
]))
s.closegroup(gid, p[-1])
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.scanner = sre_compile.compile(p)
p = _parser.SubPattern(s, [(BRANCH, (None, p))])
self.scanner = _compiler.compile(p)
def scan(self, string):
result = []
append = result.append
Expand Down
8 changes: 4 additions & 4 deletions Lib/re/sre_compile.py → Lib/re/_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"""Internal support module for sre"""

import _sre
from . import sre_parse
from .sre_constants import *
from . import _parser
from ._constants import *

assert _sre.MAGIC == MAGIC, "SRE module mismatch"

Expand Down Expand Up @@ -68,7 +68,7 @@
for t in _equivalences for i in t}

def _combine_flags(flags, add_flags, del_flags,
TYPE_FLAGS=sre_parse.TYPE_FLAGS):
TYPE_FLAGS=_parser.TYPE_FLAGS):
if add_flags & TYPE_FLAGS:
flags &= ~TYPE_FLAGS
return (flags | add_flags) & ~del_flags
Expand Down Expand Up @@ -777,7 +777,7 @@ def compile(p, flags=0):

if isstring(p):
pattern = p
p = sre_parse.parse(p, flags)
p = _parser.parse(p, flags)
else:
pattern = None

Expand Down
4 changes: 2 additions & 2 deletions Lib/re/sre_constants.py → Lib/re/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ def dump(f, d, prefix):
*
* regular expression matching engine
*
* NOTE: This file is generated by Lib/re/sre_constants.py. If you need
* to change anything in here, edit Lib/re/sre_constants.py and run it.
* NOTE: This file is generated by Lib/re/_constants.py. If you need
* to change anything in here, edit Lib/re/_constants.py and run it.
*
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
*
Expand Down
2 changes: 1 addition & 1 deletion Lib/re/sre_parse.py → Lib/re/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# XXX: show string offset and offending character for all errors

from .sre_constants import *
from ._constants import *

SPECIAL_CHARS = ".\\[{()*+?^$|"
REPEAT_CHARS = "*+?{"
Expand Down
2 changes: 1 addition & 1 deletion Lib/sre_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
DeprecationWarning,
stacklevel=2)

from re.sre_compile import *
from re._compiler import *
2 changes: 1 addition & 1 deletion Lib/sre_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
DeprecationWarning,
stacklevel=2)

from re.sre_constants import *
from re._constants import *
2 changes: 1 addition & 1 deletion Lib/sre_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
DeprecationWarning,
stacklevel=2)

from re.sre_parse import *
from re._parser import *
2 changes: 1 addition & 1 deletion Lib/test/test_pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_others(self):
cm('cgi', ignore=('log',)) # set with = in module
cm('pickle', ignore=('partial', 'PickleBuffer'))
cm('aifc', ignore=('_aifc_params',)) # set with = in module
cm('re.sre_parse', ignore=('dump', 'groups', 'pos')) # from .sre_constants import *; property
cm('re._parser', ignore=('dump', 'groups', 'pos')) # from ._constants import *; property
cm(
'pdb',
# pyclbr does not handle elegantly `typing` or properties
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def test_re_groupref_exists(self):
'two branches', 10)

def test_re_groupref_overflow(self):
from re.sre_constants import MAXGROUPS
from re._constants import MAXGROUPS
self.checkTemplateError('()', r'\g<%s>' % MAXGROUPS, 'xx',
'invalid group reference %d' % MAXGROUPS, 3)
self.checkPatternError(r'(?P<a>)(?(%d))' % MAXGROUPS,
Expand Down Expand Up @@ -2433,7 +2433,7 @@ def test_immutable(self):
tp.foo = 1

def test_overlap_table(self):
f = re.sre_compile._generate_overlap_table
f = re._compiler._generate_overlap_table
self.assertEqual(f(""), [])
self.assertEqual(f("a"), [0])
self.assertEqual(f("abcd"), [0, 0, 0, 0])
Expand All @@ -2442,8 +2442,8 @@ def test_overlap_table(self):
self.assertEqual(f("abcabdac"), [0, 0, 0, 1, 2, 0, 1, 0])

def test_signedness(self):
self.assertGreaterEqual(re.sre_compile.MAXREPEAT, 0)
self.assertGreaterEqual(re.sre_compile.MAXGROUPS, 0)
self.assertGreaterEqual(re._compiler.MAXREPEAT, 0)
self.assertGreaterEqual(re._compiler.MAXGROUPS, 0)

@cpython_only
def test_disallow_instantiation(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def test_startup_imports(self):
self.assertIn('site', modules)

# http://bugs.python.org/issue19205
re_mods = {'re', '_sre', 're.sre_compile', 're.sre_constants', 're.sre_parse'}
re_mods = {'re', '_sre', 're._compiler', 're._constants', 're._parser'}
self.assertFalse(modules.intersection(re_mods), stderr)

# http://bugs.python.org/issue9548
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Convert the :mod:`re` module into a package. Deprecate undocumented modules
``sre_compile``, ``sre_constants`` and ``sre_parse``.
Convert the :mod:`re` module into a package. Deprecate modules ``sre_compile``,
``sre_constants`` and ``sre_parse``.
4 changes: 2 additions & 2 deletions Modules/sre_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*
* regular expression matching engine
*
* NOTE: This file is generated by Lib/re/sre_constants.py. If you need
* to change anything in here, edit Lib/re/sre_constants.py and run it.
* NOTE: This file is generated by Lib/re/_constants.py. If you need
* to change anything in here, edit Lib/re/_constants.py and run it.
*
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
*
Expand Down
0