Skip to content

urlretrieve regression: first call returns block size as 0 #60613

Closed

Description

@techtonik
mannequin
BPO 16409
Nosy @gpshead, @4kir4
Files
  • test.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/gpshead'
    closed_at = <Date 2012-11-10.21:45:35.627>
    created_at = <Date 2012-11-04.22:27:04.999>
    labels = ['type-bug', 'library']
    title = 'urlretrieve regression: first call returns block size as 0'
    updated_at = <Date 2012-11-10.21:45:35.626>
    user = 'https://bugs.python.org/techtonik'

    bugs.python.org fields:

    activity = <Date 2012-11-10.21:45:35.626>
    actor = 'gregory.p.smith'
    assignee = 'gregory.p.smith'
    closed = True
    closed_date = <Date 2012-11-10.21:45:35.627>
    closer = 'gregory.p.smith'
    components = ['Library (Lib)']
    creation = <Date 2012-11-04.22:27:04.999>
    creator = 'techtonik'
    dependencies = []
    files = ['27886']
    hgrepos = []
    issue_num = 16409
    keywords = []
    message_count = 7.0
    messages = ['174836', '174868', '174875', '174914', '175079', '175084', '175297']
    nosy_count = 4.0
    nosy_names = ['gregory.p.smith', 'techtonik', 'akira', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue16409'
    versions = ['Python 3.3', 'Python 3.4']

    Activity

    1. techtonik commented on Nov 4, 2012

      techtonikmannequin
      MannequinAuthor

      Renamed urllib.urlretrieve changed behaviour in Py3k, which leads to ZeroDivisionErrors in applications that use block_size parameter for calculations. Previously, block size was constant. Now it varies making it impossible to exactly calculate value transferred so far. Test file attached.

      C:\Python33\python.exe test.py
      0 0 59654
      1 8192 59654
      2 8192 59654
      3 8192 59654
      4 8192 59654
      5 8192 59654
      6 8192 59654
      7 8192 59654
      8 2310 59654

      C:\Python27\python.exe test.py
      (0, 8192, 59654)
      (1, 8192, 59654)
      (2, 8192, 59654)
      (3, 8192, 59654)
      (4, 8192, 59654)
      (5, 8192, 59654)
      (6, 8192, 59654)
      (7, 8192, 59654)
      (8, 8192, 59654)

    2. added
      stdlib
      on Nov 4, 2012
    3. 4kir4 commented on Nov 5, 2012

      4kir4mannequin
      Mannequin

      Summary: It is a new behavior. There is no need to change either code or
      docs. Though docs could be clarified to be more explicit.

      The behavior has been introduced only in 3.3 in revision 53715804dc71
      1 from bpo-10050 2

      It knownly breaks backward-compatibility. The docstring says:

      The reporthook argument should be a callable that accepts a block
      number, a read size, and the total file size of the URL target.

      The change was rejected in bpo-849407 3

      Though it is not mentioned in Misc/NEWS and
      Doc/library/urllib.request.rst doesn't mention the change from a static
      block size to a read size explicitly:

      The third argument, if present, is a hook function that will be called
      once on establishment of the network connection and once after each
      block read thereafter. The hook will be passed three arguments; a
      count of blocks transferred so far, a block size in bytes, and the
      total size of the file.

      *after each block read* and *block size in bytes* do not suggest that
      the block size must be constant. This part in the docs could be more
      clear.

      There is related bpo-1490929 4

    4. added
      type-bug
      on Nov 5, 2012
    5. techtonik commented on Nov 5, 2012

      techtonikmannequin
      MannequinAuthor

      I strongly disagree with your summary.

      It's a new behavior for the old renamed module that clearly breaks existing code ported with 2to3.

      The 95% of callable usage is to get the estimated download progress (to draw progress bar or time calculations). This requires calculating value of total blocks like:

          total_blocks = math.ceil(float(total_size) / block_size)

      when block_size allowed to be 0, it immediately spawns ZeroDivisionErrors. This change broke http://pypi.python.org/pypi/wget

      If you make block size in callback parameters varying, the parameter with number of blocks transferred loses any sense.

    6. 4kir4 commented on Nov 5, 2012

      4kir4mannequin
      Mannequin

      The summary assumes that bpo-10050 is valid i.e., urlretrieve is
      reimplemented using new urlopen and 2.x FancyURLopener is deprecated.

      It might not be so 1. In this case the summary is incorrect.

      Old implementation is available as:

        opener = FancyURLopener()
        urlretrieve = opener.retrieve

      btw, the new implementation doesn't prevent you to estimate the
      progress:

        def make_reporthook():
            read = 0  # number of bytes read so far
            def reporthook(blocknum, blocksize, totalsize):
                nonlocal read
                read += blocksize
                if totalsize > 0:
                    percent = read * 1e2 / totalsize
                    print("%5.1f%% %*d / %d" % (
                            percent, len(str(totalsize)), read, totalsize))
                else:
                    print("read %d" % (read,))
            return reporthook
    7. techtonik commented on Nov 7, 2012

      techtonikmannequin
      MannequinAuthor

      I see nothing wrong with reimplementation with different underlying lib as long as behavior stays the same and lib2to3 converts code correctly.

      The API that block size if constant and non-zero is broken with Python 3.3. I thought that API breaks are only allowed for major Python versions?

    8. techtonik commented on Nov 7, 2012

      techtonikmannequin
      MannequinAuthor

      I agree that new implementation doesn't prevent me from estimating the
      progress, but it does make the callback code worse, not better. Either by closure or by global variable, but it makes callback stateful.

      To fix this issue either blocks_number parameter should be removed, or previous behavior restored.

    9. self-assigned this
      on Nov 8, 2012
    10. python-dev commented on Nov 10, 2012

      python-devmannequin
      Mannequin

      New changeset 19e6e32db5ec by Gregory P. Smith in branch '3.3':
      Fixes issue bpo-16409: The reporthook callback made by the legacy
      http://hg.python.org/cpython/rev/19e6e32db5ec

      New changeset 8c48eb0239ca by Gregory P. Smith in branch 'default':
      Fixes issue bpo-16409: The reporthook callback made by the legacy
      http://hg.python.org/cpython/rev/8c48eb0239ca

    11. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions