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

def _stack_effect3(op_code, oparg):
  """Compute the effect an op_code and oparg have on the stack.  See python/compile.c."""
  if op_code == 'UNPACK_SEQUENCE':
    return oparg - 1
  if op_code == 'UNPACK_EX':
    return (oparg & 0xFF) + (oparg >> 8)
  if op_code == 'BUILD_TUPLE':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_LIST':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_SET':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_STRING':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_LIST_UNPACK':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_TUPLE_UNPACK':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_TUPLE_UNPACK_WITH_CALL':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_SET_UNPACK':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_MAP_UNPACK':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_MAP_UNPACK_WITH_CALL':
    return -oparg  # Was 1 - oparg
  if op_code == 'BUILD_MAP':
    return -2 * oparg  # Was 1 - 2 * oparg
  if op_code == 'BUILD_CONST_KEY_MAP':
    return -oparg
  if op_code == 'RAISE_VARARGS':
    return -oparg
  if op_code == 'CALL_FUNCTION':
    return -oparg
  if op_code == 'CALL_FUNCTION_KW':
    return -oparg - 1
  if op_code == 'CALL_FUNCTION_EX':
    return -((oparg & 0x01) != 0) - ((oparg & 0x02) != 0)
  if op_code == 'MAKE_FUNCTION':
    return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0)
  if op_code == 'BUILD_SLICE':
    return -2 if (oparg == 3) else -1
  if op_code == 'FORMAT_VALUE':
    # If there's a fmt_spec on the stack we go from 2->1 else 1->1.
    return -1 if (oparg & FVS_MASK) == FVS_HAVE_SPEC else 0
  if op_code == 'EXTENDED_ARG':
    return 0  # EXTENDED_ARG just builds up a longer argument value for the next instruction (there may be multiple in a row?)

  return _STACK_EFFECTS3[op_code]