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

Skip to content

Commit e1adeeb

Browse files
committed
#5057: Merge with 3.2.
2 parents b496159 + 71e84a6 commit e1adeeb

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

Lib/test/test_peepholer.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ def test_folding_of_binops_on_constants(self):
205205
asm = dis_single('a="x"*1000')
206206
self.assertIn('(1000)', asm)
207207

208+
def test_binary_subscr_on_unicode(self):
209+
# valid code get optimized
210+
asm = dis_single('"foo"[0]')
211+
self.assertIn("('f')", asm)
212+
self.assertNotIn('BINARY_SUBSCR', asm)
213+
asm = dis_single('"\u0061\uffff"[1]')
214+
self.assertIn("('\\uffff')", asm)
215+
self.assertNotIn('BINARY_SUBSCR', asm)
216+
217+
# invalid code doesn't get optimized
218+
# out of range
219+
asm = dis_single('"fuu"[10]')
220+
self.assertIn('BINARY_SUBSCR', asm)
221+
# non-BMP char (see #5057)
222+
asm = dis_single('"\U00012345"[0]')
223+
self.assertIn('BINARY_SUBSCR', asm)
224+
225+
208226
def test_folding_of_unaryops_on_constants(self):
209227
for line, elem in (
210228
('-0.5', '(-0.5)'), # unary negative

Misc/NEWS

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #5057: fix a bug in the peepholer that led to non-portable pyc files
14+
between narrow and wide builds while optimizing BINARY_SUBSCR on non-BMP
15+
chars (e.g. "\U00012345"[0]).
16+
1317
- Issue #11845: Fix typo in rangeobject.c that caused a crash in
1418
compute_slice_indices. Patch by Daniel Urban.
1519

@@ -106,7 +110,7 @@ Core and Builtins
106110
Library
107111
-------
108112

109-
- Issue #11467: Fix urlparse behavior when handling urls which contains scheme
113+
- Issue #11467: Fix urlparse behavior when handling urls which contains scheme
110114
specific part only digits. Patch by Santoso Wijaya.
111115

112116
- Issue #11474: Fix the bug with url2pathname() handling of '/C|/' on Windows.

Python/peephole.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ fold_binops_on_constants(unsigned char *codestr, PyObject *consts, PyObject **ob
183183
break;
184184
case BINARY_SUBSCR:
185185
newconst = PyObject_GetItem(v, w);
186+
/* #5057: if v is unicode, there might be differences between
187+
wide and narrow builds in cases like '\U00012345'[0].
188+
Wide builds will return a non-BMP char, whereas narrow builds
189+
will return a surrogate. In both the cases skip the
190+
optimization in order to produce compatible pycs.
191+
*/
192+
if (newconst != NULL &&
193+
PyUnicode_Check(v) && PyUnicode_Check(newconst)) {
194+
Py_UNICODE ch = PyUnicode_AS_UNICODE(newconst)[0];
195+
#ifdef Py_UNICODE_WIDE
196+
if (ch > 0xFFFF) {
197+
#else
198+
if (ch >= 0xD800 && ch <= 0xDFFF) {
199+
#endif
200+
Py_DECREF(newconst);
201+
return 0;
202+
}
203+
}
186204
break;
187205
case BINARY_LSHIFT:
188206
newconst = PyNumber_Lshift(v, w);

0 commit comments

Comments
 (0)