Skip to content

Commit e0c19dd

Browse files
bpo-34681: Rename class Pattern in sre_parse to State. (GH-9310)
Also rename corresponding attributes, parameters and variables.
1 parent 9c53fa6 commit e0c19dd

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

Lib/re.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def __init__(self, lexicon, flags=0):
334334
self.lexicon = lexicon
335335
# combine phrases into a compound pattern
336336
p = []
337-
s = sre_parse.Pattern()
337+
s = sre_parse.State()
338338
s.flags = flags
339339
for phrase, action in lexicon:
340340
gid = s.opengroup()

Lib/sre_compile.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def isstring(obj):
597597

598598
def _code(p, flags):
599599

600-
flags = p.pattern.flags | flags
600+
flags = p.state.flags | flags
601601
code = []
602602

603603
# compile info block
@@ -772,13 +772,13 @@ def compile(p, flags=0):
772772
dis(code)
773773

774774
# map in either direction
775-
groupindex = p.pattern.groupdict
776-
indexgroup = [None] * p.pattern.groups
775+
groupindex = p.state.groupdict
776+
indexgroup = [None] * p.state.groups
777777
for k, i in groupindex.items():
778778
indexgroup[i] = k
779779

780780
return _sre.compile(
781-
pattern, flags | p.pattern.flags, code,
782-
p.pattern.groups-1,
781+
pattern, flags | p.state.flags, code,
782+
p.state.groups-1,
783783
groupindex, tuple(indexgroup)
784784
)

Lib/sre_parse.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
class Verbose(Exception):
7272
pass
7373

74-
class Pattern:
75-
# main pattern object. keeps track of global attributes
74+
class State:
75+
# keeps track of state for parsing
7676
def __init__(self):
7777
self.flags = 0
7878
self.groupdict = {}
@@ -108,8 +108,8 @@ def checklookbehindgroup(self, gid, source):
108108

109109
class SubPattern:
110110
# a subpattern, in intermediate form
111-
def __init__(self, pattern, data=None):
112-
self.pattern = pattern
111+
def __init__(self, state, data=None):
112+
self.state = state
113113
if data is None:
114114
data = []
115115
self.data = data
@@ -163,7 +163,7 @@ def __delitem__(self, index):
163163
del self.data[index]
164164
def __getitem__(self, index):
165165
if isinstance(index, slice):
166-
return SubPattern(self.pattern, self.data[index])
166+
return SubPattern(self.state, self.data[index])
167167
return self.data[index]
168168
def __setitem__(self, index, code):
169169
self.data[index] = code
@@ -202,7 +202,7 @@ def getwidth(self):
202202
lo = lo + 1
203203
hi = hi + 1
204204
elif op is GROUPREF:
205-
i, j = self.pattern.groupwidths[av]
205+
i, j = self.state.groupwidths[av]
206206
lo = lo + i
207207
hi = hi + j
208208
elif op is GROUPREF_EXISTS:
@@ -940,28 +940,28 @@ def fix_flags(src, flags):
940940
raise ValueError("ASCII and LOCALE flags are incompatible")
941941
return flags
942942

943-
def parse(str, flags=0, pattern=None):
943+
def parse(str, flags=0, state=None):
944944
# parse 're' pattern into list of (opcode, argument) tuples
945945

946946
source = Tokenizer(str)
947947

948-
if pattern is None:
949-
pattern = Pattern()
950-
pattern.flags = flags
951-
pattern.str = str
948+
if state is None:
949+
state = State()
950+
state.flags = flags
951+
state.str = str
952952

953953
try:
954-
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
954+
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
955955
except Verbose:
956956
# the VERBOSE flag was switched on inside the pattern. to be
957957
# on the safe side, we'll parse the whole thing again...
958-
pattern = Pattern()
959-
pattern.flags = flags | SRE_FLAG_VERBOSE
960-
pattern.str = str
958+
state = State()
959+
state.flags = flags | SRE_FLAG_VERBOSE
960+
state.str = str
961961
source.seek(0)
962-
p = _parse_sub(source, pattern, True, 0)
962+
p = _parse_sub(source, state, True, 0)
963963

964-
p.pattern.flags = fix_flags(str, p.pattern.flags)
964+
p.state.flags = fix_flags(str, p.state.flags)
965965

966966
if source.next is not None:
967967
assert source.next == ")"
@@ -972,7 +972,7 @@ def parse(str, flags=0, pattern=None):
972972

973973
return p
974974

975-
def parse_template(source, pattern):
975+
def parse_template(source, state):
976976
# parse 're' replacement string into list of literals and
977977
# group references
978978
s = Tokenizer(source)
@@ -982,14 +982,14 @@ def parse_template(source, pattern):
982982
literal = []
983983
lappend = literal.append
984984
def addgroup(index, pos):
985-
if index > pattern.groups:
985+
if index > state.groups:
986986
raise s.error("invalid group reference %d" % index, pos)
987987
if literal:
988988
literals.append(''.join(literal))
989989
del literal[:]
990990
groups.append((len(literals), index))
991991
literals.append(None)
992-
groupindex = pattern.groupindex
992+
groupindex = state.groupindex
993993
while True:
994994
this = sget()
995995
if this is None:

0 commit comments

Comments
 (0)