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

Skip to content

Commit 6c5f521

Browse files
committed
#5057: the peepholer no longer optimizes subscription on unicode literals (e.g. u"foo"[0]) in order to produce compatible pyc files between narrow and wide builds.
1 parent 8cd1c76 commit 6c5f521

3 files changed

Lines changed: 18 additions & 24 deletions

File tree

Lib/test/test_peepholer.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,22 @@ def test_folding_of_binops_on_constants(self):
196196
self.assertIn('(1000)', asm)
197197

198198
def test_binary_subscr_on_unicode(self):
199-
# valid code get optimized
199+
# unicode strings don't get optimized
200200
asm = dis_single('"foo"[0]')
201-
self.assertIn("('f')", asm)
202-
self.assertNotIn('BINARY_SUBSCR', asm)
201+
self.assertNotIn("('f')", asm)
202+
self.assertIn('BINARY_SUBSCR', asm)
203203
asm = dis_single('"\u0061\uffff"[1]')
204-
self.assertIn("('\\uffff')", asm)
205-
self.assertNotIn('BINARY_SUBSCR', asm)
204+
self.assertNotIn("('\\uffff')", asm)
205+
self.assertIn('BINARY_SUBSCR', asm)
206206

207-
# invalid code doesn't get optimized
208207
# out of range
209208
asm = dis_single('"fuu"[10]')
210209
self.assertIn('BINARY_SUBSCR', asm)
211210
# non-BMP char (see #5057)
212211
asm = dis_single('"\U00012345"[0]')
213212
self.assertIn('BINARY_SUBSCR', asm)
213+
asm = dis_single('"\U00012345abcdef"[3]')
214+
self.assertIn('BINARY_SUBSCR', asm)
214215

215216

216217
def test_folding_of_unaryops_on_constants(self):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #5057: the peepholer no longer optimizes subscription on unicode
14+
literals (e.g. u'foo'[0]) in order to produce compatible pyc files between
15+
narrow and wide builds.
16+
1317
- Issue #16402: When slicing a range, fix shadowing of exceptions from
1418
__index__.
1519

Python/peephole.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,14 @@ fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
132132
newconst = PyNumber_Subtract(v, w);
133133
break;
134134
case BINARY_SUBSCR:
135-
newconst = PyObject_GetItem(v, w);
136135
/* #5057: if v is unicode, there might be differences between
137-
wide and narrow builds in cases like '\U00012345'[0].
138-
Wide builds will return a non-BMP char, whereas narrow builds
139-
will return a surrogate. In both the cases skip the
140-
optimization in order to produce compatible pycs.
141-
*/
142-
if (newconst != NULL &&
143-
PyUnicode_Check(v) && PyUnicode_Check(newconst)) {
144-
Py_UNICODE ch = PyUnicode_AS_UNICODE(newconst)[0];
145-
#ifdef Py_UNICODE_WIDE
146-
if (ch > 0xFFFF) {
147-
#else
148-
if (ch >= 0xD800 && ch <= 0xDFFF) {
149-
#endif
150-
Py_DECREF(newconst);
151-
return 0;
152-
}
153-
}
136+
wide and narrow builds in cases like '\U00012345'[0] or
137+
'\U00012345abcdef'[3], so it's better to skip the optimization
138+
in order to produce compatible pycs.
139+
*/
140+
if (PyUnicode_Check(v))
141+
return 0;
142+
newconst = PyObject_GetItem(v, w);
154143
break;
155144
case BINARY_LSHIFT:
156145
newconst = PyNumber_Lshift(v, w);

0 commit comments

Comments
 (0)