[Python-checkins] r42998 - in python/trunk: Lib/test/test_compile.py Python/compile.c

Guido van Rossum guido at python.org
Mon Mar 13 16:14:50 CET 2006


I don't know what Travis expected/wanted, but this changes behavior
with respect to 2.4, and I don't like the change. (Or possibly the
code generated was already incompatible with 2.4 ever since the AST
branch was merged in.)
Previously, the SLICE opcodes would only be generated for slices with
a single colon. That is,
 a[x:y]
would generate a SLICE opcode, but
 a[x:y:]
would generate an extended slice operation. This was intentional! The
new code seems to ignore the second colon if it is present but no
value follows. I'd like this to be changed back to generating code
equivalent to
 a[slice(x,y,None)].
if the second colon is present. Why?
(1) For backwards compatibility; it's always been done this way. It'll
probably break at least some unit tests. It's unlikely but not
impossible that some type interprets these things differently.
(2) Old-fashioned types that support __getslice__ but not calling
__getitem__ with a slice() object should not support indexing with
extended slice syntax at all, even if the step is empty.
--Guido
On 3/13/06, nick.coghlan <python-checkins at python.org> wrote:
> Author: nick.coghlan
> Date: Mon Mar 13 13:31:58 2006
> New Revision: 42998
>> Modified:
> python/trunk/Lib/test/test_compile.py
> python/trunk/Python/compile.c
> Log:
> Fix SF bug #1448804 and ad a test to ensure that all subscript operations continue to be handled correctly
>> Modified: python/trunk/Lib/test/test_compile.py
> ==============================================================================
> --- python/trunk/Lib/test/test_compile.py (original)
> +++ python/trunk/Lib/test/test_compile.py Mon Mar 13 13:31:58 2006
> @@ -284,6 +284,78 @@
> f1, f2 = f()
> self.assertNotEqual(id(f1.func_code), id(f2.func_code))
>> + def test_subscripts(self):
> + # SF bug 1448804
> + # Class to make testing subscript results easy
> + class str_map(object):
> + def __init__(self):
> + self.data = {}
> + def __getitem__(self, key):
> + return self.data[str(key)]
> + def __setitem__(self, key, value):
> + self.data[str(key)] = value
> + def __delitem__(self, key):
> + del self.data[str(key)]
> + def __contains__(self, key):
> + return str(key) in self.data
> + d = str_map()
> + # Index
> + d[1] = 1
> + self.assertEqual(d[1], 1)
> + d[1] += 1
> + self.assertEqual(d[1], 2)
> + del d[1]
> + self.assertEqual(1 in d, False)
> + # Tuple of indices
> + d[1, 1] = 1
> + self.assertEqual(d[1, 1], 1)
> + d[1, 1] += 1
> + self.assertEqual(d[1, 1], 2)
> + del d[1, 1]
> + self.assertEqual((1, 1) in d, False)
> + # Simple slice
> + d[1:2] = 1
> + self.assertEqual(d[1:2], 1)
> + d[1:2] += 1
> + self.assertEqual(d[1:2], 2)
> + del d[1:2]
> + self.assertEqual(slice(1, 2) in d, False)
> + # Tuple of simple slices
> + d[1:2, 1:2] = 1
> + self.assertEqual(d[1:2, 1:2], 1)
> + d[1:2, 1:2] += 1
> + self.assertEqual(d[1:2, 1:2], 2)
> + del d[1:2, 1:2]
> + self.assertEqual((slice(1, 2), slice(1, 2)) in d, False)
> + # Extended slice
> + d[1:2:3] = 1
> + self.assertEqual(d[1:2:3], 1)
> + d[1:2:3] += 1
> + self.assertEqual(d[1:2:3], 2)
> + del d[1:2:3]
> + self.assertEqual(slice(1, 2, 3) in d, False)
> + # Tuple of extended slices
> + d[1:2:3, 1:2:3] = 1
> + self.assertEqual(d[1:2:3, 1:2:3], 1)
> + d[1:2:3, 1:2:3] += 1
> + self.assertEqual(d[1:2:3, 1:2:3], 2)
> + del d[1:2:3, 1:2:3]
> + self.assertEqual((slice(1, 2, 3), slice(1, 2, 3)) in d, False)
> + # Ellipsis
> + d[...] = 1
> + self.assertEqual(d[...], 1)
> + d[...] += 1
> + self.assertEqual(d[...], 2)
> + del d[...]
> + self.assertEqual(Ellipsis in d, False)
> + # Tuple of Ellipses
> + d[..., ...] = 1
> + self.assertEqual(d[..., ...], 1)
> + d[..., ...] += 1
> + self.assertEqual(d[..., ...], 2)
> + del d[..., ...]
> + self.assertEqual((Ellipsis, Ellipsis) in d, False)
> +
> def test_main():
> test_support.run_unittest(TestSpecifics)
>>> Modified: python/trunk/Python/compile.c
> ==============================================================================
> --- python/trunk/Python/compile.c (original)
> +++ python/trunk/Python/compile.c Mon Mar 13 13:31:58 2006
> @@ -3853,42 +3853,47 @@
> static int
> compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
> {
> + char * kindname = NULL;
> switch (s->kind) {
> + case Index_kind:
> + kindname = "index";
> + if (ctx != AugStore) {
> + VISIT(c, expr, s->v.Index.value);
> + }
> + break;
> case Ellipsis_kind:
> - ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
> + kindname = "ellipsis";
> + if (ctx != AugStore) {
> + ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
> + }
> break;
> case Slice_kind:
> + kindname = "slice";
> if (!s->v.Slice.step)
> return compiler_simple_slice(c, s, ctx);
> - if (!compiler_slice(c, s, ctx))
> - return 0;
> - if (ctx == AugLoad) {
> - ADDOP_I(c, DUP_TOPX, 2);
> - }
> - else if (ctx == AugStore) {
> - ADDOP(c, ROT_THREE);
> - }
> - return compiler_handle_subscr(c, "slice", ctx);
> - case ExtSlice_kind: {
> - int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
> - for (i = 0; i < n; i++) {
> - slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
> - if (!compiler_visit_nested_slice(c, sub, ctx))
> + if (ctx != AugStore) {
> + if (!compiler_slice(c, s, ctx))
> return 0;
> }
> - ADDOP_I(c, BUILD_TUPLE, n);
> - return compiler_handle_subscr(c, "extended slice", ctx);
> - }
> - case Index_kind:
> - if (ctx != AugStore)
> - VISIT(c, expr, s->v.Index.value);
> - return compiler_handle_subscr(c, "index", ctx);
> + break;
> + case ExtSlice_kind:
> + kindname = "extended slice";
> + if (ctx != AugStore) {
> + int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
> + for (i = 0; i < n; i++) {
> + slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
> + if (!compiler_visit_nested_slice(c, sub, ctx))
> + return 0;
> + }
> + ADDOP_I(c, BUILD_TUPLE, n);
> + }
> + break;
> default:
> PyErr_Format(PyExc_SystemError,
> - "invalid slice %d", s->kind);
> + "invalid subscript kind %d", s->kind);
> return 0;
> }
> - return 1;
> + return compiler_handle_subscr(c, kindname, ctx);
> }
>> /* do depth-first search of basic block graph, starting with block.
> _______________________________________________
> Python-checkins mailing list
> Python-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-checkins
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /