def wrap_open_close(function, object, filename, mode='rb', file_handle_name
    ='_file'):
    """Wraps the objects file handle for execution of function.
    
    :param function: Function to be executed during file handle wrap.
    :type function: callable
    
    :param object: Object having the file handle.
    :type object: object
    
    :param filename: Filename opened.
    :type filename: string
    
    :param mode: Opening mode.
    :type mode: string
    
    :param file_handle_name: Name of the instance variable in object.
    :type file_handle_name: string
    
    :return: New function which being run acts as wrapped function call.
    :rtype: function
    
    """
    file_handle = getattr(object, file_handle_name)
    if file_handle is not None and not file_handle.closed:
        function()
        return
    new_file_handle = open(filename, mode)
    setattr(object, file_handle_name, new_file_handle)
    function()
    new_file_handle.close()
