Skip to content

Commit 9423c7a

Browse files
Merge pull request #377 from andrewnester/369-download-zero-division
ZeroDivisionError when calling 'MediaDownloadProgress.progress'
2 parents aa64d20 + a4a44cf commit 9423c7a

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

googleapiclient/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def progress(self):
203203
the percentage complete as a float, returning 0.0 if the total size of
204204
the upload is unknown.
205205
"""
206-
if self.total_size is not None:
206+
if self.total_size is not None and self.total_size != 0:
207207
return float(self.resumable_progress) / float(self.total_size)
208208
else:
209209
return 0.0
@@ -229,7 +229,7 @@ def progress(self):
229229
the percentage complete as a float, returning 0.0 if the total size of
230230
the download is unknown.
231231
"""
232-
if self.total_size is not None:
232+
if self.total_size is not None and self.total_size != 0:
233233
return float(self.resumable_progress) / float(self.total_size)
234234
else:
235235
return 0.0

tests/test_discovery.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,30 @@ def test_pickle_with_credentials(self):
13891389
new_http = new_zoo._http
13901390
self.assertFalse(hasattr(new_http.request, 'credentials'))
13911391

1392+
def test_resumable_media_upload_no_content(self):
1393+
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
1394+
zoo = build('zoo', 'v1', http=self.http)
1395+
1396+
media_upload = MediaFileUpload(datafile('empty'), resumable=True)
1397+
request = zoo.animals().insert(media_body=media_upload, body=None)
1398+
1399+
self.assertEquals(media_upload, request.resumable)
1400+
self.assertEquals(request.body, None)
1401+
self.assertEquals(request.resumable_uri, None)
1402+
1403+
http = HttpMockSequence([
1404+
({'status': '200',
1405+
'location': 'http://upload.example.com'}, ''),
1406+
({'status': '308',
1407+
'location': 'http://upload.example.com/2',
1408+
'range': '0-0'}, ''),
1409+
])
1410+
1411+
status, body = request.next_chunk(http=http)
1412+
self.assertEquals(None, body)
1413+
self.assertTrue(isinstance(status, MediaUploadProgress))
1414+
self.assertEquals(0, status.progress())
1415+
13921416

13931417
class Next(unittest.TestCase):
13941418

tests/test_http.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,28 @@ def test_media_io_base_download_retries_5xx(self):
550550
self.assertEqual(5, download._progress)
551551
self.assertEqual(5, download._total_size)
552552

553+
def test_media_io_base_download_empty_file(self):
554+
self.request.http = HttpMockSequence([
555+
({'status': '200',
556+
'content-range': '0-0/0'}, b''),
557+
])
558+
559+
download = MediaIoBaseDownload(
560+
fd=self.fd, request=self.request, chunksize=3)
561+
562+
self.assertEqual(self.fd, download._fd)
563+
self.assertEqual(0, download._progress)
564+
self.assertEqual(None, download._total_size)
565+
self.assertEqual(False, download._done)
566+
self.assertEqual(self.request.uri, download._uri)
567+
568+
status, done = download.next_chunk()
569+
570+
self.assertEqual(True, done)
571+
self.assertEqual(0, download._progress)
572+
self.assertEqual(0, download._total_size)
573+
self.assertEqual(0, status.progress())
574+
553575
EXPECTED = """POST /someapi/v1/collection/?foo=bar HTTP/1.1
554576
Content-Type: application/json
555577
MIME-Version: 1.0

0 commit comments

Comments
 (0)