Merge lp:~mthaddon/codetree/requests-contextlib-1884297 into lp:codetree

Proposed by Tom Haddon
Status: Merged
Approved by: Tom Haddon
Approved revision: 121
Merged at revision: 121
Proposed branch: lp:~mthaddon/codetree/requests-contextlib-1884297
Merge into: lp:codetree
Diff against target: 44 lines (+8/-2)
2 files modified
codetree/handlers/charm.py (+5/-2)
tests/test_charm_handler.py (+3/-0)
To merge this branch: bzr merge lp:~mthaddon/codetree/requests-contextlib-1884297
Reviewer Review Type Date Requested Status
Haw Loeung +1 Approve
Guillermo Gonzalez (community) Approve
Canonical IS Reviewers Pending
Review via email: mp+386189@code.launchpad.net

Commit message

Add a closing wrapper for requests.get per LP:1884297

Description of the change

Add a closing wrapper for requests.get per lp#1884297

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Guillermo Gonzalez (verterok) wrote :

+1

review: Approve
Revision history for this message
Haw Loeung (hloeung) wrote :

LGTM

review: Approve (+1)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 121

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'codetree/handlers/charm.py'
--- codetree/handlers/charm.py 2020-04-08 09:58:38 +0000
+++ codetree/handlers/charm.py 2020-06-22 14:39:30 +0000
@@ -1,6 +1,5 @@
1from __future__ import division, print_function1from __future__ import division, print_function
2import hashlib2import hashlib
3from io import open
4import json3import json
5import logging4import logging
6import os5import os
@@ -9,6 +8,8 @@
9import subprocess8import subprocess
10import tempfile9import tempfile
11import sys10import sys
11from contextlib import closing
12from io import open
1213
13from .basic import HandlerResult, SourceHandler14from .basic import HandlerResult, SourceHandler
14from .exceptions import InvalidOption, NoSuchCharm15from .exceptions import InvalidOption, NoSuchCharm
@@ -160,7 +161,9 @@
160 url = "{0}/{1}/archive".format(self.base_url, self.charmstore_url)161 url = "{0}/{1}/archive".format(self.base_url, self.charmstore_url)
161 zip_path = os.path.join(download_dir, 'charm.zip')162 zip_path = os.path.join(download_dir, 'charm.zip')
162 calculated_hash = hashlib.new("sha384")163 calculated_hash = hashlib.new("sha384")
163 with open(zip_path, 'wb') as charm_file, requests.get(url) as req:164 # Note that we need the `closing` wrapper for requests.get as it isn't
165 # a true context manager in python3.4. See LP#1884297.
166 with open(zip_path, 'wb') as charm_file, closing(requests.get(url)) as req:
164 for chunk in req.iter_content(8192):167 for chunk in req.iter_content(8192):
165 charm_file.write(chunk)168 charm_file.write(chunk)
166 calculated_hash.update(six.ensure_binary(chunk))169 calculated_hash.update(six.ensure_binary(chunk))
167170
=== modified file 'tests/test_charm_handler.py'
--- tests/test_charm_handler.py 2020-04-08 09:27:18 +0000
+++ tests/test_charm_handler.py 2020-06-22 14:39:30 +0000
@@ -58,6 +58,9 @@
58 def __exit__(self, exc_type, exc_val, exc_tb):58 def __exit__(self, exc_type, exc_val, exc_tb):
59 pass59 pass
6060
61 def close(self):
62 pass
63
61 @property64 @property
62 def content(self):65 def content(self):
63 if isinstance(self._content, six.BytesIO):66 if isinstance(self._content, six.BytesIO):

Subscribers

People subscribed via source and target branches