jenkins-bot merged this change.

View Change

Approvals: Dvorapa: Looks good to me, approved jenkins-bot: Verified
[bugfix] close infile even if the method is leaved by return

- use contextlib.closing to ensure UrlOpener is closed
even if the read_file_content is leaved due to wrong
context_type

Change-Id: I9b512df4ac542467ae3c532206a47d8cbbbce071
---
M pywikibot/specialbots/_upload.py
1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/pywikibot/specialbots/_upload.py b/pywikibot/specialbots/_upload.py
index 3201f70..eec04ad 100644
--- a/pywikibot/specialbots/_upload.py
+++ b/pywikibot/specialbots/_upload.py
@@ -14,6 +14,8 @@
import os
import tempfile

+from contextlib import closing
+
import pywikibot
import pywikibot.data.api

@@ -138,32 +140,31 @@
pywikibot.output('Resume download...')
uo.addheader('Range', 'bytes=%s-' % rlen)

- infile = uo.open(file_url)
- info = infile.info()
+ with closing(uo.open(file_url)) as infile:
+ info = infile.info()

- if PY2:
- info_get = info.getheader
- else:
- info_get = info.get
- content_type = info_get('Content-Type')
- content_len = info_get('Content-Length')
- accept_ranges = info_get('Accept-Ranges')
+ if PY2:
+ info_get = info.getheader
+ else:
+ info_get = info.get
+ content_type = info_get('Content-Type')
+ content_len = info_get('Content-Length')
+ accept_ranges = info_get('Accept-Ranges')

- if 'text/html' in content_type:
- pywikibot.output("Couldn't download the image: "
- 'the requested URL was not found on server.')
- return
+ if 'text/html' in content_type:
+ pywikibot.output(
+ "Couldn't download the image: "
+ 'the requested URL was not found on server.')
+ return

- valid_ranges = accept_ranges == 'bytes'
+ valid_ranges = accept_ranges == 'bytes'

- if resume:
- _contents += infile.read()
- else:
- _contents = infile.read()
+ if resume:
+ _contents += infile.read()
+ else:
+ _contents = infile.read()

- infile.close()
retrieved = True
-
if content_len:
rlen = len(_contents)
content_len = int(content_len)

To view, visit change 605868. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I9b512df4ac542467ae3c532206a47d8cbbbce071
Gerrit-Change-Number: 605868
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Dvorapa <dvorapa@seznam.cz>
Gerrit-Reviewer: jenkins-bot (75)