You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current implementation of re.LOCALE support for Unicode strings is nonsensical. It correctly works only on Latin1 locales (because Unicode string interpreted as Latin1 decoded bytes string. all characters outside UCS1 range considered as non-words), on other locales it got strange and useless results.
Proposed patch fixes re.LOCALE support for Unicode strings. It uses the wide-character equivalents of C characters functions (towlower(), iswalpha(), etc).
The problem is that these functions are not exists in C89, they are introduced only in C99. Gcc understand them, we should check other compilers. However these functions are already used on FreeBSD and MacOS.
I don't think we should fix this in 2.x: some people may rely on the old behaviour, and it will be difficult for them to debug.
In 3.x, I simply propose we deprecate re.LOCALE for unicode strings and make it a no-op.
Yes, one of solution is to deprecate re.LOCALE for unicode strings and then
make it incompatible with unicode strings. But I think it would be good to
implement locale-aware matching.
Example.
>>> for a in'Ii\u0130\u0131':
... for b in'Ii\u0130\u0131':
... if a != b and re.match(a, b, re.I): print(a, '~', b)
...
I ~ i
I ~ İ
i ~ I
i ~ İ
İ ~ I
İ ~ i
This is incorrect result in Turkish. Capital dotless "I" matches capital "İ"
with dot above, and small dotless "ı" doesn't match anything.
Regex produces more relevant output, which includes matches for Turkish and
English:
I ~ i
I ~ ı
i ~ I
i ~ İ
İ ~ i
ı ~ I
With locale tr_TR.utf8 (with the patch):
>>> for a in'Ii\u0130\u0131':
... for b in'Ii\u0130\u0131':
... if a != b and re.match(a, b, re.I|re.L): print(a, '~', b)
...
I ~ ı
i ~ İ
İ ~ i
ı ~ I
Here is simple patch which just deprecate using of the re.LOCALE flag with str patterns. It also deprecates using of the re.LOCALE flag with the re.ASCII flag (with bytes patterns) and adds some re.LOCALE related tests.
Activity
serhiy-storchaka commentedon Sep 14, 2014
Current implementation of re.LOCALE support for Unicode strings is nonsensical. It correctly works only on Latin1 locales (because Unicode string interpreted as Latin1 decoded bytes string. all characters outside UCS1 range considered as non-words), on other locales it got strange and useless results.
Proposed patch fixes re.LOCALE support for Unicode strings. It uses the wide-character equivalents of C characters functions (towlower(), iswalpha(), etc).
The problem is that these functions are not exists in C89, they are introduced only in C99. Gcc understand them, we should check other compilers. However these functions are already used on FreeBSD and MacOS.
pitrou commentedon Sep 16, 2014
I don't think we should fix this in 2.x: some people may rely on the old behaviour, and it will be difficult for them to debug.
In 3.x, I simply propose we deprecate re.LOCALE for unicode strings and make it a no-op.
serhiy-storchaka commentedon Sep 16, 2014
Yes, one of solution is to deprecate re.LOCALE for unicode strings and then
make it incompatible with unicode strings. But I think it would be good to
implement locale-aware matching.
Example.
This is incorrect result in Turkish. Capital dotless "I" matches capital "İ"
with dot above, and small dotless "ı" doesn't match anything.
Regex produces more relevant output, which includes matches for Turkish and
English:
I ~ i
I ~ ı
i ~ I
i ~ İ
İ ~ i
ı ~ I
With locale tr_TR.utf8 (with the patch):
This is correct result in Turkish.
Therefore there is a use case for this feature.
pitrou commentedon Sep 16, 2014
Ha, I always forget about the Turkish locale case...
serhiy-storchaka commentedon Oct 9, 2014
Here is simple patch which just deprecate using of the re.LOCALE flag with str patterns. It also deprecates using of the re.LOCALE flag with the re.ASCII flag (with bytes patterns) and adds some re.LOCALE related tests.
serhiy-storchaka commentedon Nov 11, 2014
If there are no objections I'll commit the re_deprecate_unicode_locale.patch patch. But it would be good if someone will review doc changes.
vadmium commentedon Dec 1, 2014
Looks like revision 561d1d0de518 was to fix this issue, but the NEWS entry has the wrong reference number
serhiy-storchaka commentedon Dec 1, 2014
Indeed. Thank you Martin.
python-dev commentedon Dec 1, 2014
New changeset abc7fe393016 by Serhiy Storchaka in branch 'default':
Fixed issue number in Misc/NEWS for issue bpo-22407.
https://hg.python.org/cpython/rev/abc7fe393016