diff -r 1f767f834e67 Lib/test/test_peepholer.py --- a/Lib/test/test_peepholer.py Thu Apr 14 07:51:57 2011 +0300 +++ b/Lib/test/test_peepholer.py Thu Apr 14 15:51:14 2011 +0300 @@ -205,6 +205,24 @@ asm = dis_single('a="x"*1000') self.assertIn('(1000)', asm) + def test_binary_subscr_on_unicode(self): + # valid code get optimized + asm = dis_single('"foo"[0]') + self.assertIn("('f')", asm) + self.assertNotIn('BINARY_SUBSCR', asm) + asm = dis_single('"\u0061\uffff"[1]') + self.assertIn("('\\uffff')", asm) + self.assertNotIn('BINARY_SUBSCR', asm) + + # invalid code doesn't get optimized + # out of range + asm = dis_single('"fuu"[10]') + self.assertIn('BINARY_SUBSCR', asm) + # non-BMP char (see #5057) + asm = dis_single('"\U00012345"[0]') + self.assertIn('BINARY_SUBSCR', asm) + + def test_folding_of_unaryops_on_constants(self): for line, elem in ( ('-0.5', '(-0.5)'), # unary negative diff -r 1f767f834e67 Python/peephole.c --- a/Python/peephole.c Thu Apr 14 07:51:57 2011 +0300 +++ b/Python/peephole.c Thu Apr 14 15:51:14 2011 +0300 @@ -183,6 +183,23 @@ break; case BINARY_SUBSCR: newconst = PyObject_GetItem(v, w); + /* #5057: if v is unicode, there might be differences between + wide and narrow builds in cases like '\U00012345'[0]. + Wide builds will return a non-BMP char, whereas narrow builds + will return a surrogate. In both the cases skip the + optimization in order to produce compatible pycs. + */ + if (newconst != NULL && + PyUnicode_Check(v) && PyUnicode_Check(newconst)) { + Py_UNICODE ch = PyUnicode_AS_UNICODE(newconst)[0]; +#ifdef Py_UNICODE_WIDE + if (ch > 0xFFFF) + return 0; +#else + if (ch >= 0xD800 && ch <= 0xDFFF) + return 0; +#endif + } break; case BINARY_LSHIFT: newconst = PyNumber_Lshift(v, w);