Skip to content

Commit c46323c

Browse files
gh-153177: Deprecate re.LOCALE
The flag only works with bytes patterns in 8-bit locales and makes matching much slower. Emit a DeprecationWarning when a pattern uses the flag or the inline flag (?L). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent b175621 commit c46323c

7 files changed

Lines changed: 142 additions & 29 deletions

File tree

Doc/deprecations/pending-removal-in-3.18.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Pending removal in Python 3.18
1111
C implementation, has been deprecated since Python 3.13.
1212
(Contributed by Serhiy Storchaka in :gh:`89902`.)
1313

14+
* :mod:`re`:
15+
16+
* The :const:`~re.LOCALE` flag and the inline flag ``(?L)``.
17+
Decode the input and match str patterns instead.
18+
(Contributed by Serhiy Storchaka in :gh:`153177`.)
19+
1420
* Deprecations defined by :pep:`829`:
1521

1622
* ``import`` lines in :file:`{name}.pth` files are silently ignored.

Doc/library/re.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,10 @@ Flags
881881
no longer depend on the locale at compile time.
882882
Only the locale at matching time affects the result of matching.
883883

884+
.. deprecated:: next
885+
The flag and the inline flag ``(?L)`` are deprecated.
886+
Decode the input and match str patterns instead.
887+
884888

885889
.. data:: M
886890
MULTILINE

Doc/whatsnew/3.16.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,12 @@ New deprecations
575575
3.9, now issues a deprecation warning on use. This property is slated for
576576
removal in 3.21. Use ``ast.Tuple.elts`` instead.
577577

578+
* :mod:`re`:
579+
580+
* The :const:`~re.LOCALE` flag and the inline flag ``(?L)`` are
581+
deprecated. Decode the input and match str patterns instead.
582+
(Contributed by Serhiy Storchaka in :gh:`153177`.)
583+
578584
* :mod:`struct`:
579585

580586
* Soft-deprecated since Python 3.15, using ``'F'`` and ``'D'`` type codes are now

Lib/re/_compiler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ def compile(p, flags=0):
396396
else:
397397
pattern = None
398398

399+
if p.state.flags & SRE_FLAG_LOCALE or p.state.locale_used:
400+
import warnings
401+
warnings.warn("re.LOCALE and the inline flag (?L) are deprecated",
402+
DeprecationWarning,
403+
stacklevel=4)
404+
399405
code = _code(p, flags)
400406

401407
if flags & SRE_FLAG_DEBUG:

Lib/re/_parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class State:
7777
# keeps track of state for parsing
7878
def __init__(self):
7979
self.flags = 0
80+
self.locale_used = False
8081
self.groupdict = {}
8182
self.groupwidths = [None] # group 0
8283
self.lookbehindgroups = None
@@ -1097,6 +1098,8 @@ def _parse_flags(source, state, char):
10971098
raise source.error("bad inline flags: cannot turn off global flag", 1)
10981099
if add_flags & del_flags:
10991100
raise source.error("bad inline flags: flag turned on and off", 1)
1101+
if add_flags & SRE_FLAG_LOCALE:
1102+
state.locale_used = True
11001103
return add_flags, del_flags
11011104

11021105
def fix_flags(src, flags):

Lib/test/test_re.py

Lines changed: 114 additions & 29 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Deprecate the :const:`re.LOCALE` flag and the inline flag ``(?L)``.
2+
They only work with bytes patterns in 8-bit locales and make matching
3+
much slower. Decode the input and match str patterns instead.

0 commit comments

Comments
 (0)