Bump re to 3.14.2 · RustPython/RustPython@c8b1cb8 · GitHub
Skip to content

Commit c8b1cb8

Browse files
morealyouknowone
authored andcommitted
Bump re to 3.14.2
1 parent f1fd1e9 commit c8b1cb8

5 files changed

Lines changed: 63 additions & 61 deletions

File tree

Lib/re/__init__.py

Lines changed: 1 addition & 1 deletion

Lib/re/_compiler.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
POSSESSIVE_REPEAT: (POSSESSIVE_REPEAT, SUCCESS, POSSESSIVE_REPEAT_ONE),
2929
}
3030

31+
_CHARSET_ALL = [(NEGATE, None)]
32+
3133
def _combine_flags(flags, add_flags, del_flags,
3234
TYPE_FLAGS=_parser.TYPE_FLAGS):
3335
if add_flags & TYPE_FLAGS:
@@ -84,17 +86,22 @@ def _compile(code, pattern, flags):
8486
code[skip] = _len(code) - skip
8587
elif op is IN:
8688
charset, hascased = _optimize_charset(av, iscased, tolower, fixes)
87-
if flags & SRE_FLAG_IGNORECASE and flags & SRE_FLAG_LOCALE:
88-
emit(IN_LOC_IGNORE)
89-
elif not hascased:
90-
emit(IN)
91-
elif not fixes: # ascii
92-
emit(IN_IGNORE)
89+
if not charset:
90+
emit(FAILURE)
91+
elif charset == _CHARSET_ALL:
92+
emit(ANY_ALL)
9393
else:
94-
emit(IN_UNI_IGNORE)
95-
skip = _len(code); emit(0)
96-
_compile_charset(charset, flags, code)
97-
code[skip] = _len(code) - skip
94+
if flags & SRE_FLAG_IGNORECASE and flags & SRE_FLAG_LOCALE:
95+
emit(IN_LOC_IGNORE)
96+
elif not hascased:
97+
emit(IN)
98+
elif not fixes: # ascii
99+
emit(IN_IGNORE)
100+
else:
101+
emit(IN_UNI_IGNORE)
102+
skip = _len(code); emit(0)
103+
_compile_charset(charset, flags, code)
104+
code[skip] = _len(code) - skip
98105
elif op is ANY:
99106
if flags & SRE_FLAG_DOTALL:
100107
emit(ANY_ALL)
@@ -277,6 +284,10 @@ def _optimize_charset(charset, iscased=None, fixup=None, fixes=None):
277284
charmap[i] = 1
278285
elif op is NEGATE:
279286
out.append((op, av))
287+
elif op is CATEGORY and tail and (CATEGORY, CH_NEGATE[av]) in tail:
288+
# Optimize [\s\S] etc.
289+
out = [] if out else _CHARSET_ALL
290+
return out, False
280291
else:
281292
tail.append((op, av))
282293
except IndexError:
@@ -524,13 +535,18 @@ def _compile_info(code, pattern, flags):
524535
# look for a literal prefix
525536
prefix = []
526537
prefix_skip = 0
527-
charset = [] # not used
538+
charset = None # not used
528539
if not (flags & SRE_FLAG_IGNORECASE and flags & SRE_FLAG_LOCALE):
529540
# look for literal prefix
530541
prefix, prefix_skip, got_all = _get_literal_prefix(pattern, flags)
531542
# if no prefix, look for charset prefix
532543
if not prefix:
533544
charset = _get_charset_prefix(pattern, flags)
545+
if charset:
546+
charset, hascased = _optimize_charset(charset)
547+
assert not hascased
548+
if charset == _CHARSET_ALL:
549+
charset = None
534550
## if prefix:
535551
## print("*** PREFIX", prefix, prefix_skip)
536552
## if charset:
@@ -565,8 +581,6 @@ def _compile_info(code, pattern, flags):
565581
# generate overlap table
566582
code.extend(_generate_overlap_table(prefix))
567583
elif charset:
568-
charset, hascased = _optimize_charset(charset)
569-
assert not hascased
570584
_compile_charset(charset, flags, code)
571585
code[skip] = len(code) - skip
572586

Lib/re/_constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
MAGIC = 20230612
1717

18-
from _sre import MAXREPEAT, MAXGROUPS
18+
from _sre import MAXREPEAT, MAXGROUPS # noqa: F401
1919

2020
# SRE standard exception (access as sre.error)
2121
# should this really be here?
@@ -206,6 +206,8 @@ def _makecodes(*names):
206206
CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
207207
}
208208

209+
CH_NEGATE = dict(zip(CHCODES[::2] + CHCODES[1::2], CHCODES[1::2] + CHCODES[::2]))
210+
209211
# flags
210212
SRE_FLAG_IGNORECASE = 2 # case insensitive
211213
SRE_FLAG_LOCALE = 4 # honour system locale

Lib/re/_parser.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
r"\S": (IN, [(CATEGORY, CATEGORY_NOT_SPACE)]),
5050
r"\w": (IN, [(CATEGORY, CATEGORY_WORD)]),
5151
r"\W": (IN, [(CATEGORY, CATEGORY_NOT_WORD)]),
52-
r"\Z": (AT, AT_END_STRING), # end of string
52+
r"\z": (AT, AT_END_STRING), # end of string
53+
r"\Z": (AT, AT_END_STRING), # end of string (obsolete)
5354
}
5455

5556
FLAGS = {
@@ -807,14 +808,6 @@ def _parse(source, state, verbose, nested, first=False):
807808
state.grouprefpos[condgroup] = (
808809
source.tell() - len(condname) - 1
809810
)
810-
if not (condname.isdecimal() and condname.isascii()):
811-
import warnings
812-
warnings.warn(
813-
"bad character in group name %s at position %d" %
814-
(repr(condname) if source.istext else ascii(condname),
815-
source.tell() - len(condname) - 1),
816-
DeprecationWarning, stacklevel=nested + 6
817-
)
818811
state.checklookbehindgroup(condgroup, source)
819812
item_yes = _parse(source, state, verbose, nested + 1)
820813
if source.match("|"):
@@ -1038,14 +1031,6 @@ def addgroup(index, pos):
10381031
if index >= MAXGROUPS:
10391032
raise s.error("invalid group reference %d" % index,
10401033
len(name) + 1)
1041-
if not (name.isdecimal() and name.isascii()):
1042-
import warnings
1043-
warnings.warn(
1044-
"bad character in group name %s at position %d" %
1045-
(repr(name) if s.istext else ascii(name),
1046-
s.tell() - len(name) - 1),
1047-
DeprecationWarning, stacklevel=5
1048-
)
10491034
addgroup(index, len(name) + 1)
10501035
elif c == "0":
10511036
if s.next in OCTDIGITS:

Lib/test/test_re.py

Lines changed: 30 additions & 29 deletions

0 commit comments

Comments
 (0)