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

Skip to content

Commit def0a4c

Browse files
Issue #18037: 2to3 now escapes '\u' and '\U' in native strings.
1 parent 2a8b3f2 commit def0a4c

3 files changed

Lines changed: 64 additions & 7 deletions

File tree

Lib/lib2to3/fixes/fix_unicode.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
1-
"""Fixer that changes unicode to str, unichr to chr, and u"..." into "...".
1+
r"""Fixer for unicode.
2+
3+
* Changes unicode to str and unichr to chr.
4+
5+
* If "...\u..." is not unicode literal change it into "...\\u...".
6+
7+
* Change u"..." into "...".
28
39
"""
410

5-
import re
611
from ..pgen2 import token
712
from .. import fixer_base
813

914
_mapping = {"unichr" : "chr", "unicode" : "str"}
10-
_literal_re = re.compile(r"[uU][rR]?[\'\"]")
1115

1216
class FixUnicode(fixer_base.BaseFix):
1317
BM_compatible = True
1418
PATTERN = "STRING | 'unicode' | 'unichr'"
1519

20+
def start_tree(self, tree, filename):
21+
super(FixUnicode, self).start_tree(tree, filename)
22+
self.unicode_literals = 'unicode_literals' in tree.future_features
23+
1624
def transform(self, node, results):
1725
if node.type == token.NAME:
1826
new = node.clone()
1927
new.value = _mapping[node.value]
2028
return new
2129
elif node.type == token.STRING:
22-
if _literal_re.match(node.value):
23-
new = node.clone()
24-
new.value = new.value[1:]
25-
return new
30+
val = node.value
31+
if (not self.unicode_literals and val[0] in 'rR\'"' and
32+
'\\' in val):
33+
val = r'\\'.join([
34+
v.replace('\\u', r'\\u').replace('\\U', r'\\U')
35+
for v in val.split(r'\\')
36+
])
37+
if val[0] in 'uU':
38+
val = val[1:]
39+
if val == node.value:
40+
return node
41+
new = node.clone()
42+
new.value = val
43+
return new

Lib/lib2to3/tests/test_fixers.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,6 +2824,43 @@ def test_unicode_literal_3(self):
28242824
a = """R'''x''' """
28252825
self.check(b, a)
28262826

2827+
def test_native_literal_escape_u(self):
2828+
b = r"""'\\\u20ac\U0001d121\\u20ac'"""
2829+
a = r"""'\\\\u20ac\\U0001d121\\u20ac'"""
2830+
self.check(b, a)
2831+
2832+
b = r"""r'\\\u20ac\U0001d121\\u20ac'"""
2833+
a = r"""r'\\\\u20ac\\U0001d121\\u20ac'"""
2834+
self.check(b, a)
2835+
2836+
def test_bytes_literal_escape_u(self):
2837+
b = r"""b'\\\u20ac\U0001d121\\u20ac'"""
2838+
a = r"""b'\\\u20ac\U0001d121\\u20ac'"""
2839+
self.check(b, a)
2840+
2841+
b = r"""br'\\\u20ac\U0001d121\\u20ac'"""
2842+
a = r"""br'\\\u20ac\U0001d121\\u20ac'"""
2843+
self.check(b, a)
2844+
2845+
def test_unicode_literal_escape_u(self):
2846+
b = r"""u'\\\u20ac\U0001d121\\u20ac'"""
2847+
a = r"""'\\\u20ac\U0001d121\\u20ac'"""
2848+
self.check(b, a)
2849+
2850+
b = r"""ur'\\\u20ac\U0001d121\\u20ac'"""
2851+
a = r"""r'\\\u20ac\U0001d121\\u20ac'"""
2852+
self.check(b, a)
2853+
2854+
def test_native_unicode_literal_escape_u(self):
2855+
f = 'from __future__ import unicode_literals\n'
2856+
b = f + r"""'\\\u20ac\U0001d121\\u20ac'"""
2857+
a = f + r"""'\\\u20ac\U0001d121\\u20ac'"""
2858+
self.check(b, a)
2859+
2860+
b = f + r"""r'\\\u20ac\U0001d121\\u20ac'"""
2861+
a = f + r"""r'\\\u20ac\U0001d121\\u20ac'"""
2862+
self.check(b, a)
2863+
28272864
class Test_callable(FixerTestCase):
28282865
fixer = "callable"
28292866

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Core and Builtins
7171
Library
7272
-------
7373

74+
- Issue #18037: 2to3 now escapes '\u' and '\U' in native strings.
75+
7476
- Issue #19137: The pprint module now correctly formats instances of set and
7577
frozenset subclasses.
7678

0 commit comments

Comments
 (0)