def download_fragment(fragment, ctx):
    if (not interrupt_trigger[0]):
        return
    frag_index = ctx['fragment_index'] = fragment['frag_index']
    ctx['last_error'] = None
    headers = info_dict.get('http_headers', {}).copy()
    byte_range = fragment.get('byte_range')
    if byte_range:
        headers['Range'] = ('bytes=%d-%d' % (byte_range['start'], (byte_range['end'] - 1)))
    (fatal, count) = (is_fatal((fragment.get('index') or (frag_index - 1))), 0)
    while (count <= fragment_retries):
        try:
            ctx['fragment_count'] = fragment.get('fragment_count')
            if self._download_fragment(ctx, fragment['url'], info_dict, headers):
                break
            return
        except (compat_urllib_error.HTTPError, http.client.IncompleteRead) as err:
            count += 1
            ctx['last_error'] = err
            if (count <= fragment_retries):
                self.report_retry_fragment(err, frag_index, count, fragment_retries)
        except DownloadError:
            if (not fatal):
                break
            raise
    if ((count > fragment_retries) and fatal):
        ctx['dest_stream'].close()
        self.report_error(('Giving up after %s fragment retries' % fragment_retries))
def download_fragment(fragment, ctx):
    if (not interrupt_trigger[0]):
        return
    frag_index = ctx['fragment_index'] = fragment['frag_index']
    ctx['last_error'] = None
    headers = info_dict.get('http_headers', {}).copy()
    byte_range = fragment.get('byte_range')
    if byte_range:
        headers['Range'] = ('bytes=%d-%d' % (byte_range['start'], (byte_range['end'] - 1)))
    (fatal, count) = (is_fatal((fragment.get('index') or (frag_index - 1))), 0)
    while (count <= fragment_retries):
        try:
            ctx['fragment_count'] = fragment.get('fragment_count')
            if self._download_fragment(ctx, fragment['url'], info_dict, headers):
                break
            return
        except (compat_urllib_error.HTTPError, http.client.IncompleteRead) as err:
            count += 1
            ctx['last_error'] = err
            if (count <= fragment_retries):
                self.report_retry_fragment(err, frag_index, count, fragment_retries)
        except DownloadError:
            if (not fatal):
                break
            raise
    if ((count > fragment_retries) and fatal):
        ctx['dest_stream'].close()
        self.report_error(('Giving up after %s fragment retries' % fragment_retries))