Get a descriptor out of a file object.

    :param fileobj:
        An integer (existing descriptor) or any object having the `fileno()`
        method.
    :raises ValueError:
        if the descriptor cannot be obtained or if the descriptor is invalid
    :returns:
        file descriptor number

def _get_fd(fileobj):
    """
    Get a descriptor out of a file object.

    :param fileobj:
        An integer (existing descriptor) or any object having the `fileno()`
        method.
    :raises ValueError:
        if the descriptor cannot be obtained or if the descriptor is invalid
    :returns:
        file descriptor number
    """
    if isinstance(fileobj, int):
        fd = fileobj
    else:
        try:
            fd = fileobj.fileno()
        except AttributeError:
            fd = None
    if fd is None or fd < 0:
        raise ValueError("invalid fileobj: {!r}".format(fileobj))
    return fd