Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/extensions/utils.py

Issue 29372215: Noissue - use with and contextlib.closing for closing urlopen stream (Closed)
Patch Set: Move contextlib.closing into _urlopen Created Jan. 17, 2017, 1:10 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/extensions/utils.py
===================================================================
--- a/sitescripts/extensions/utils.py
+++ b/sitescripts/extensions/utils.py
@@ -9,16 +9,17 @@
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
import codecs
+import contextlib
import os
import json
import re
import subprocess
import traceback
import time
import urlparse
import urllib
@@ -244,29 +245,28 @@
def _urlopen(url, attempts=3):
"""
Tries to open a particular URL, retries on failure.
"""
for i in range(attempts):
try:
- return urllib.urlopen(url)
+ return contextlib.closing(urllib.urlopen(url))
except IOError as e:
error = Exception('Error {0} while opening {1} url'
.format(e, url))
time.sleep(5)
raise error
def _parseXMLDocument(url, attempts=2):
for i in range(attempts):
- page = _urlopen(url)
- content = page.read()
- page.close()
+ with _urlopen(url) as page:
+ content = page.read()
try:
return dom.parseString(content)
except ExpatError as err:
exception = Exception('Error {0} while parsing xml:\n{1}\nfrom {2}'
.format(err, content, url))
raise exception
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld