Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 143328b

Browse files
author
Fredrik Lundh
committed
-- tightened up parsing of octal numbers
-- improved the SRE test harness: don't use asserts, test a few more things (including more boundary conditions)
1 parent 412f246 commit 143328b

3 files changed

Lines changed: 158 additions & 198 deletions

File tree

Lib/sre_parse.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
MAXREPEAT = 65535
1616

1717
SPECIAL_CHARS = ".\\[{()*+?^$|"
18-
REPEAT_CHARS = "*+?{"
18+
REPEAT_CHARS = "*+?{"
1919

2020
DIGITS = tuple("0123456789")
2121

@@ -259,29 +259,29 @@ def _escape(source, escape, state):
259259
# hexadecimal escape
260260
while source.next in HEXDIGITS and len(escape) < 4:
261261
escape = escape + source.get()
262-
escape = escape[2:]
263-
if len(escape) != 2:
264-
raise error, "bogus escape: %s" % repr("\\" + escape)
265-
return LITERAL, int(escape, 16) & 0xff
262+
if len(escape) != 4:
263+
raise ValueError
264+
return LITERAL, int(escape[2:], 16) & 0xff
266265
elif escape[1:2] == "0":
267266
# octal escape
268-
while source.next in OCTDIGITS and len(escape) < 5:
267+
while source.next in OCTDIGITS and len(escape) < 4:
269268
escape = escape + source.get()
270269
return LITERAL, int(escape[1:], 8) & 0xff
271270
elif escape[1:2] in DIGITS:
272271
# octal escape *or* decimal group reference (sigh)
273272
here = source.tell()
274273
if source.next in DIGITS:
275274
escape = escape + source.get()
276-
if escape[2] in OCTDIGITS and source.next in OCTDIGITS:
275+
if (escape[1] in OCTDIGITS and escape[2] in OCTDIGITS and
276+
source.next in OCTDIGITS):
277277
# got three octal digits; this is an octal escape
278278
escape = escape + source.get()
279279
return LITERAL, int(escape[1:], 8) & 0xff
280280
# got at least one decimal digit; this is a group reference
281281
group = _group(escape, state.groups)
282282
if group:
283283
return GROUPREF, group
284-
raise error, "bogus escape: %s" % repr(escape)
284+
raise ValueError
285285
if len(escape) == 2:
286286
return LITERAL, ord(escape[1])
287287
except ValueError:

Lib/test/output/test_sre

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
test_sre
2-
maximum recursion limit exceeded

0 commit comments

Comments
 (0)