def get_parent_pb_and_mode(xml_pbtype):
    """
    Returns a parent pb_type and mode element for the given pb_type. If there
    are no modes (implicit default mode) then the mode element returned is
    the same as the pb_type element.
    """
    assert xml_pbtype.tag in ["pb_type", "mode"], xml_pbtype.tag

    # Get immediate parent
    xml_parent = xml_pbtype.getparent()

    # We've hit the end
    if xml_parent is None or xml_parent.tag == "complexblocklist":
        return None, None

    assert xml_parent.tag in ["pb_type", "mode"], xml_parent.tag

    # pb_type parent
    if xml_pbtype.tag == "pb_type":

        if xml_parent.tag == "pb_type":
            return xml_parent, xml_parent

        elif xml_parent.tag == "mode":
            return xml_parent.getparent(), xml_parent

    elif xml_pbtype.tag == "mode":

        return xml_parent.getparent(), xml_pbtype