Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/deprecations/pending-removal-in-3.18.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Pending removal in Python 3.18
C implementation, has been deprecated since Python 3.13.
(Contributed by Serhiy Storchaka in :gh:`89902`.)

* :mod:`re`:

* The :const:`~re.LOCALE` flag and the inline flag ``(?L)``.
Decode the input and match str patterns instead.
(Contributed by Serhiy Storchaka in :gh:`153177`.)

* Deprecations defined by :pep:`829`:

* ``import`` lines in :file:`{name}.pth` files are silently ignored.
Expand Down
4 changes: 4 additions & 0 deletions Doc/library/re.rst
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,10 @@ Flags
no longer depend on the locale at compile time.
Only the locale at matching time affects the result of matching.

.. deprecated-removed:: next 3.18
The flag and the inline flag ``(?L)`` are deprecated.
Decode the input and match str patterns instead.


.. data:: M
MULTILINE
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,12 @@ New deprecations
3.9, now issues a deprecation warning on use. This property is slated for
removal in 3.21. Use ``ast.Tuple.elts`` instead.

* :mod:`re`:

* The :const:`~re.LOCALE` flag and the inline flag ``(?L)`` are
deprecated. Decode the input and match str patterns instead.
(Contributed by Serhiy Storchaka in :gh:`153177`.)

* :mod:`struct`:

* Soft-deprecated since Python 3.15, using ``'F'`` and ``'D'`` type codes are now
Expand Down
6 changes: 6 additions & 0 deletions Lib/re/_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ def compile(p, flags=0):
else:
pattern = None

if p.state.flags & SRE_FLAG_LOCALE or p.state.locale_used:
import warnings
warnings.warn("re.LOCALE and the inline flag (?L) are deprecated",
DeprecationWarning,
stacklevel=4)

code = _code(p, flags)

if flags & SRE_FLAG_DEBUG:
Expand Down
3 changes: 3 additions & 0 deletions Lib/re/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class State:
# keeps track of state for parsing
def __init__(self):
self.flags = 0
self.locale_used = False
self.groupdict = {}
self.groupwidths = [None] # group 0
self.lookbehindgroups = None
Expand Down Expand Up @@ -1097,6 +1098,8 @@ def _parse_flags(source, state, char):
raise source.error("bad inline flags: cannot turn off global flag", 1)
if add_flags & del_flags:
raise source.error("bad inline flags: flag turned on and off", 1)
if add_flags & SRE_FLAG_LOCALE:
state.locale_used = True
return add_flags, del_flags

def fix_flags(src, flags):
Expand Down
143 changes: 114 additions & 29 deletions Lib/test/test_re.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Deprecate the :const:`re.LOCALE` flag and the inline flag ``(?L)``.
They only work with bytes patterns in 8-bit locales and make matching
much slower. Decode the input and match str patterns instead.
Loading