def _export(obj, fp, extensions_map, extension, overwrite, protocol=None):
    r"""
    The shared export function. This handles the shared logic of ensuring
    that the given ``fp`` is either a ``pathlib.Path`` or a file like
    object. All exporter methods are defined as receiving a buffer object,
    regardless of if a path is provided. If a file-like object is provided
    then the extension mut not be ``None``.

    Parameters
    ----------
    obj : `object`
        The Python object to export.
    fp : `Path` or file-like object
        The path or file buffer to write to.
    extensions_map : `dict` of `str` -> `callable`
        The dictionary mapping extensions to export callables.
    extension : `str`
        User provided extension (required if a file-like ``fp`` is passed).
    overwrite : `bool`
        If ``True``, overwrite any existing files at the given path.
    protocol : `int`, optional
        The output pickle protocol, if necessary.
    """
    if isinstance(fp, basestring):
        fp = Path(fp)  # cheeky conversion to Path to reuse existing code
    if isinstance(fp, Path):
        path_filepath = _validate_filepath(fp, overwrite)
        extension = _parse_and_validate_extension(path_filepath, extension,
                                                  extensions_map)

        export_function = _extension_to_export_function(extension,
                                                        extensions_map)

        with path_filepath.open('wb') as file_handle:
            export_function(obj, file_handle, protocol=protocol,
                            extension=extension)
    else:
        # You MUST provide an extension if a file handle is given
        if extension is None:
            raise ValueError('An export file extension must be provided if a '
                             'file-like object is passed.')
        else:
            extension = _normalise_extension(extension)

        # Apparently in Python 2.x there is no reliable way to detect something
        # that is 'file' like (file handle or a StringIO object or something
        # you can read and write to like a file). Therefore, we are going to
        # just be really Pythonic about it and just assume we were handed
        # a correctly behaving object.
        try:
            # Follow PIL like behaviour. Check the file handle extension
            # and check if matches the given extension
            filepath = Path(fp.name)
            _validate_filepath(filepath, overwrite)
            extension = _parse_and_validate_extension(filepath, extension,
                                                      extensions_map)
        except __HOLE__:
            pass

        export_function = _extension_to_export_function(extension,
                                                        extensions_map)
        export_function(obj, fp, protocol=protocol, extension=extension)
dataset/ETHPy150Open menpo/menpo/menpo/io/output/base.py/_export