def _most_derived_metaclass(meta, bases):
    """Selects the most derived metaclass of all the given metaclasses.

    This will be the same metaclass that is selected by

    .. code-block:: python

        class temporary_class(*bases, metaclass=meta): pass

    or equivalently by

    .. code-block:: python

        types.prepare_class('temporary_class', bases, metaclass=meta)

    "Most derived" means the item in {meta, type(bases[0]), type(bases[1]), ...}
    which is a non-strict subclass of every item in that set.

    If no such item exists, then :exc:`TypeError` is raised.

    :type meta:   `type`
    :type bases:  :class:`Iterable` of `type`
    """
    most_derived_metaclass = meta
    for base_type in map(type, bases):
        if issubclass(base_type, most_derived_metaclass):
            most_derived_metaclass = base_type
        elif not issubclass(most_derived_metaclass, base_type):
            return type.__new__(meta, str('temporary_class'), bases, {})
    return most_derived_metaclass
