Skip to content

Commit

Permalink
Merge pull request #966 from Lukasa/scop-close
Browse files Browse the repository at this point in the history
Extra with statements.
  • Loading branch information
Lukasa committed Sep 5, 2016
2 parents 4a68c53 + e5fb977 commit 75be152
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions _travis/fetch_gae_sdk.py
Expand Up @@ -97,8 +97,8 @@ def main(argv):
return 1
sdk_contents.seek(0)
try:
zip_contents = zipfile.ZipFile(sdk_contents)
zip_contents.extractall(dest_dir)
with zipfile.ZipFile(sdk_contents) as zip_contents:
zip_contents.extractall(dest_dir)
except:
print('Error extracting SDK contents')
return 1
Expand Down
6 changes: 4 additions & 2 deletions docs/user-guide.rst
Expand Up @@ -162,7 +162,8 @@ For uploading files using ``multipart/form-data`` encoding you can use the same
approach as :ref:`form_data` and specify the file field as a tuple of
``(file_name, file_data)``::

>>> file_data = open('example.txt').read()
>>> with open('example.txt') as fp:
... file_data = fp.read()
>>> r = http.request(
... 'POST',
... 'http://httpbin.org/post',
Expand All @@ -186,7 +187,8 @@ to specify the file's MIME type explicitly::
For sending raw binary data simply specify the ``body`` argument. It's also
recommended to set the ``Content-Type`` header::

>>> binary_data = open('example.jpg', 'rb').read()
>>> with open('example.jpg', 'rb') as fp:
... binary_data = fp.read()
>>> r = http.request(
... 'POST',
... 'http://httpbin.org/post',
Expand Down
6 changes: 3 additions & 3 deletions dummyserver/handlers.py
@@ -1,6 +1,7 @@
from __future__ import print_function

import collections
import contextlib
import gzip
import json
import logging
Expand Down Expand Up @@ -199,9 +200,8 @@ def encodingrequest(self, request):
if encoding == 'gzip':
headers = [('Content-Encoding', 'gzip')]
file_ = BytesIO()
zipfile = gzip.GzipFile('', mode='w', fileobj=file_)
zipfile.write(data)
zipfile.close()
with contextlib.closing(gzip.GzipFile('', mode='w', fileobj=file_)) as zipfile:
zipfile.write(data)
data = file_.getvalue()
elif encoding == 'deflate':
headers = [('Content-Encoding', 'deflate')]
Expand Down
13 changes: 7 additions & 6 deletions setup.py
Expand Up @@ -9,13 +9,14 @@
base_path = os.path.dirname(__file__)

# Get the version (borrowed from SQLAlchemy)
fp = open(os.path.join(base_path, 'urllib3', '__init__.py'))
VERSION = re.compile(r".*__version__ = '(.*?)'",
re.S).match(fp.read()).group(1)
fp.close()
with open(os.path.join(base_path, 'urllib3', '__init__.py')) as fp:
VERSION = re.compile(r".*__version__ = '(.*?)'",
re.S).match(fp.read()).group(1)

readme = codecs.open('README.rst', encoding='utf-8').read()
changes = codecs.open('CHANGES.rst', encoding='utf-8').read()
with codecs.open('README.rst', encoding='utf-8') as fp:
readme = fp.read()
with codecs.open('CHANGES.rst', encoding='utf-8') as fp:
changes = fp.read()
version = VERSION

setup(name='urllib3',
Expand Down

0 comments on commit 75be152

Please sign in to comment.