jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/605868 )
Change subject: [bugfix] close infile even if the method is leaved by return ......................................................................
[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(-)
Approvals: Dvorapa: Looks good to me, approved jenkins-bot: Verified
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)