Skip to content

Instantly share code, notes, and snippets.

@isidentical
Created January 5, 2020 15:33
Show Gist options
  • Save isidentical/aefd27950f357acc3bd493086a8e98e5 to your computer and use it in GitHub Desktop.
Save isidentical/aefd27950f357acc3bd493086a8e98e5 to your computer and use it in GitHub Desktop.
#define ENSURE_CONSTANT(NODE) \
if ((NODE) != NULL && (NODE)->kind != Constant_kind) \
return 1;
#define GET_CONSTANT(NODE) (((NODE) != NULL) ? (NODE)->v.Constant.value : NULL)
static int
fold_subscr(expr_ty node, PyArena *arena, int optimize)
{
PyObject *newval, *key;
expr_ty arg;
slice_ty slice;
arg = node->v.Subscript.value;
slice = node->v.Subscript.slice;
if (node->v.Subscript.ctx != Load)
{
return 1;
}
ENSURE_CONSTANT(arg)
switch (slice->kind) {
case Index_kind:
ENSURE_CONSTANT(slice->v.Index.value)
key = GET_CONSTANT(slice->v.Index.value);
break;
case Slice_kind:
ENSURE_CONSTANT(slice->v.Slice.lower)
ENSURE_CONSTANT(slice->v.Slice.upper)
ENSURE_CONSTANT(slice->v.Slice.step)
key = PySlice_New(GET_CONSTANT(slice->v.Slice.lower),
GET_CONSTANT(slice->v.Slice.upper),
GET_CONSTANT(slice->v.Slice.step));
break;
default:
return 1;
}
newval = PyObject_GetItem(GET_CONSTANT(arg), key);
return make_const(node, newval, arena);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment