Return a suitable pickle protocol version for a given target.

    Arguments:
        target: The internals description of the targeted python
            version. If this is ``None`` the specification of the currently
            running python version will be used.
        protocol(None or int): The requested protocol version (or None for the
            default of the target python version).

    Returns:
        int: A suitable pickle protocol version.

def get_protocol_version(protocol=None, target=None):
    """
    Return a suitable pickle protocol version for a given target.

    Arguments:
        target: The internals description of the targeted python
            version. If this is ``None`` the specification of the currently
            running python version will be used.
        protocol(None or int): The requested protocol version (or None for the
            default of the target python version).

    Returns:
        int: A suitable pickle protocol version.
    """

    target = get_py_internals(target)

    if protocol is None:
        protocol = target['pickle_default_protocol']

    if protocol > cPickle.HIGHEST_PROTOCOL:
        warnings.warn('Downgrading pickle protocol, running python supports up to %d.' % cPickle.HIGHEST_PROTOCOL)
        protocol = cPickle.HIGHEST_PROTOCOL

    target_highest_protocol = target['pickle_highest_protocol']
    if protocol > target_highest_protocol:
        warnings.warn('Downgrading pickle protocol, target python supports up to %d.' % target_highest_protocol)
        protocol = target_highest_protocol

    return protocol