mirror of
https://github.com/python/cpython.git
synced 2025-08-29 13:15:11 +00:00
gh-91308: Simplify parsing inline flag "x" (verbose) (GH-91855)
This commit is contained in:
parent
6b45076bd6
commit
130a8c386b
1 changed files with 4 additions and 16 deletions
|
@ -67,9 +67,6 @@ FLAGS = {
|
||||||
TYPE_FLAGS = SRE_FLAG_ASCII | SRE_FLAG_LOCALE | SRE_FLAG_UNICODE
|
TYPE_FLAGS = SRE_FLAG_ASCII | SRE_FLAG_LOCALE | SRE_FLAG_UNICODE
|
||||||
GLOBAL_FLAGS = SRE_FLAG_DEBUG
|
GLOBAL_FLAGS = SRE_FLAG_DEBUG
|
||||||
|
|
||||||
class Verbose(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class State:
|
class State:
|
||||||
# keeps track of state for parsing
|
# keeps track of state for parsing
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -448,6 +445,8 @@ def _parse_sub(source, state, verbose, nested):
|
||||||
not nested and not items))
|
not nested and not items))
|
||||||
if not sourcematch("|"):
|
if not sourcematch("|"):
|
||||||
break
|
break
|
||||||
|
if not nested:
|
||||||
|
verbose = state.flags & SRE_FLAG_VERBOSE
|
||||||
|
|
||||||
if len(items) == 1:
|
if len(items) == 1:
|
||||||
return items[0]
|
return items[0]
|
||||||
|
@ -826,8 +825,7 @@ def _parse(source, state, verbose, nested, first=False):
|
||||||
raise source.error('global flags not at the start '
|
raise source.error('global flags not at the start '
|
||||||
'of the expression',
|
'of the expression',
|
||||||
source.tell() - start)
|
source.tell() - start)
|
||||||
if (state.flags & SRE_FLAG_VERBOSE) and not verbose:
|
verbose = state.flags & SRE_FLAG_VERBOSE
|
||||||
raise Verbose
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
add_flags, del_flags = flags
|
add_flags, del_flags = flags
|
||||||
|
@ -963,17 +961,7 @@ def parse(str, flags=0, state=None):
|
||||||
state.flags = flags
|
state.flags = flags
|
||||||
state.str = str
|
state.str = str
|
||||||
|
|
||||||
try:
|
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
|
||||||
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
|
|
||||||
except Verbose:
|
|
||||||
# the VERBOSE flag was switched on inside the pattern. to be
|
|
||||||
# on the safe side, we'll parse the whole thing again...
|
|
||||||
state = State()
|
|
||||||
state.flags = flags | SRE_FLAG_VERBOSE
|
|
||||||
state.str = str
|
|
||||||
source.seek(0)
|
|
||||||
p = _parse_sub(source, state, True, 0)
|
|
||||||
|
|
||||||
p.state.flags = fix_flags(str, p.state.flags)
|
p.state.flags = fix_flags(str, p.state.flags)
|
||||||
|
|
||||||
if source.next is not None:
|
if source.next is not None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue