Compute the effect an op_code and oparg have on the stack.  See python/compile.c.

def _stack_effect(op_code, oparg):
  """Compute the effect an op_code and oparg have on the stack.  See python/compile.c."""
  n_args = lambda o: (o % 256) + 2 * (o // 256)

  if op_code == 'DUP_TOPX':
    return oparg
  elif op_code == 'UNPACK_SEQUENCE':
    return oparg - 1
  elif op_code == 'BUILD_TUPLE':
    return -oparg  # Was 1 - oparg
  elif op_code == 'BUILD_LIST':
    return -oparg  # Was 1 - oparg
  elif op_code == 'BUILD_SET':
    return -oparg  # Was 1 - oparg
  elif op_code == 'RAISE_VARARGS':
    return -oparg
  elif op_code == 'CALL_FUNCTION':
    return -n_args(oparg)  # args + (#kwargs << 8)
  elif op_code == 'CALL_FUNCTION_VAR':
    return -n_args(oparg) - 1  # args + (#kwargs << 8)
  elif op_code == 'CALL_FUNCTION_KW':
    return -n_args(oparg) - 1  # args + (#kwargs << 8)
  elif op_code == 'CALL_FUNCTION_VAR_KW':
    return -n_args(oparg) - 2  # args + (#kwargs << 8)
  elif op_code == 'MAKE_FUNCTION':
    return -oparg
  elif op_code == 'BUILD_SLICE':
    if oparg == 3:
      return -2
    else:
      return -1
  elif op_code == 'MAKE_CLOSURE':
    return -oparg - 1

  return _STACK_EFFECTS[op_code]