def _get_override(self, ctx, node_state):
        # if called for this context previously return the cached result
        if node_state.override_cache is not None:
            if node_state.override_cache is self:
                return None
            return node_state.override_cache

        # if there's an override set for this node in this context return that
        if node_state.override is not None:
            node_state.override_cache = node_state.override
            return node_state.override

        # find the most shifted context with an override set between ctx
        # and the root context
        parent = cython.declare(MDFContext)
        parent = ctx.get_parent()
        if parent is None:
            node_state.override_cache = self
            return None

        best_match_num_shifts = cython.declare(int)
        best_match_num_shifts = -1

        best_match = cython.declare(MDFContext)
        best_match = None

        shifted_ctx = cython.declare(MDFContext)
        shifted_node_state = cython.declare(NodeState)
        for shifted_ctx in itertools.chain([parent], parent.get_shifted_contexts()):
            try:
                shifted_node_state = self._states[shifted_ctx._id_obj]
            except __HOLE__:
                continue

            if shifted_node_state.override is not None \
            and (shifted_ctx is ctx or ctx.is_shift_of(shifted_ctx)):
                # shifted_ctx is a shifted version of ctx and has an
                # override set. If it's more shifted than any previously
                # encountered use the shift from this context
                num_shifts = len(shifted_ctx.get_shift_set())

                if num_shifts == best_match_num_shifts:
                    # if two equally shifted contexts both have an override set for
                    # this context then there's no way to sensibly decide which
                    # override to use. This won't usually happen as overrides are
                    # either set on the root context or as a result of shifting.
                    raise Exception("Ambiguous override found for %s: %s vs. %s" % (self.name,
                                                                                    best_match,
                                                                                    shifted_ctx))

                if num_shifts > best_match_num_shifts:
                    best_match = shifted_ctx
                    best_match_num_shifts = num_shifts

                    # early out if this context is the main context
                    # since there can't be a candidate that ctx is a
                    # shift of and is more shifted
                    if shifted_ctx is ctx:
                        break

        # if no contexts with an override were found return None
        if best_match is None:
            node_state.override_cache = self
            return None

        shifted_node_state = self._states[best_match._id_obj]
        node_state.override_cache = shifted_node_state.override
        return shifted_node_state.override
dataset/ETHPy150Open manahl/mdf/mdf/nodes.py/MDFNode._get_override