[Patches] [ python-Patches-810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

SourceForge.net noreply at sourceforge.net
Sun Dec 19 09:51:07 CET 2004


Patches item #810023, was opened at 2003-09-20 22:13
Message generated for change (Comment added) made by titus
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=810023&group_id=5470

Category: Library (Lib)
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: J. Lewis Muir (jlmuir)
Assigned to: Nobody/Anonymous (nobody)
Summary: Fix for off-by-one bug in urllib.URLopener.retrieve

Initial Comment:
This patch fixes an off-by-one bug in the reporthook part of 
urllib.URLopener.retrieve and adds test cases to verify the 
fix.

The retrieve method reports reading one more block than it 
actually does. Here's an example. The file is empty. I expect 
the initial reporthook call on establishment of the network 
connection but not an additional call since the file is empty 
(no block was read):

>>> import urllib
>>> def reporter(count, blockSize, fileSize):
...     print "c: %i, bs: %i, fs: %i" % (count, blockSize, 
fileSize)
...
>>> srcFile = file("/tmp/empty.txt", "wb")
>>> srcFile.close()
>>> urllib.urlretrieve("file:///tmp/empty.txt", "/tmp/new-
empty.txt", reporter)
c: 0, bs: 8192, fs: 0
c: 1, bs: 8192, fs: 0
('/tmp/new-empty.txt', <mimetools.Message instance at 
0x5a4f58>)
>>>

As a second example, if the file contains 1 byte, the retrieve 
method claims it read 2 blocks when it really only read 1:

>>> srcFile = file("/tmp/empty.txt", "wb")
>>> srcFile.write("x")
>>> srcFile.close()
>>> urllib.urlretrieve("file:///tmp/empty.txt", "/tmp/new-
empty.txt", reporter)
c: 0, bs: 8192, fs: 1
c: 1, bs: 8192, fs: 1
c: 2, bs: 8192, fs: 1
('/tmp/new-empty.txt', <mimetools.Message instance at 
0x5a50d0>)
>>>

This patch also includes some changes to 
test_urllib.urlretrieve_FileTests (where I added the test 
cases) to support creating more than one temporary file and 
making sure any created temporary files get deleted when 
the test case fixture is torn down.


----------------------------------------------------------------------

Comment By: Titus Brown (titus)
Date: 2004-12-19 00:51

Message:
Logged In: YES 
user_id=23486

Patch needed slight modification to work against current HEAD; new 
patch at 

http://issola.caltech.edu/~t/transfer/transfer/patch-810023-
reporthook.diff

The new regression tests look reasonable.  I verified that the new 
regression tests FAILED against old urllib.py, and worked against patched 
urllib.py.  Recommend apply.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=810023&group_id=5470


More information about the Patches mailing list