Permalink
Browse files

Dropped warnings in test for Python 3.2

Manage resources more carefully
  • Loading branch information...
dvarrazzo committed Jul 11, 2012
1 parent 8e08a9a commit 75d3ce680fdc8ce54c38900919ac6c4281fb10b4
View
@@ -6,6 +6,8 @@
# This file is part of the PGXN client
+from __future__ import with_statement
+
from urllib import urlencode
from pgxnclient.utils import load_json
@@ -20,35 +22,38 @@ def __init__(self, mirror):
def dist(self, dist, version=''):
try:
- return load_json(self.call(
- version and 'meta' or 'dist',
- {'dist': dist, 'version': version}))
+ with self.call(version and 'meta' or 'dist',
+ {'dist': dist, 'version': version}) as f:
+ return load_json(f)
except ResourceNotFound:
raise NotFound("distribution '%s' not found" % dist)
def ext(self, ext):
try:
- return load_json(self.call('extension', {'extension': ext}))
+ with self.call('extension', {'extension': ext}) as f:
+ return load_json(f)
except ResourceNotFound:
raise NotFound("extension '%s' not found" % ext)
def meta(self, dist, version, as_json=True):
- f = self.call('meta', {'dist': dist, 'version': version})
- if as_json:
- return load_json(f)
- else:
- return f.read().decode('utf-8')
+ with self.call('meta', {'dist': dist, 'version': version}) as f:
+ if as_json:
+ return load_json(f)
+ else:
+ return f.read().decode('utf-8')
def readme(self, dist, version):
- return self.call('readme', {'dist': dist, 'version': version}).read()
+ with self.call('readme', {'dist': dist, 'version': version}) as f:
+ return f.read()
def download(self, dist, version):
dist = dist.lower()
version = version.lower()
return self.call('download', {'dist': dist, 'version': version})
def mirrors(self):
- return load_json(self.call('mirrors'))
+ with self.call('mirrors') as f:
+ return load_json(f)
def search(self, where, query):
"""Search into PGXN.
@@ -60,14 +65,16 @@ def search(self, where, query):
# convert the query list into a string
q = ' '.join([' ' in s and ('"%s"' % s) or s for s in query])
- return load_json(self.call('search', {'in': where},
- query={'q': q}))
+ with self.call('search', {'in': where}, query={'q': q}) as f:
+ return load_json(f)
def stats(self, arg):
- return load_json(self.call('stats', {'stats': arg}))
+ with self.call('stats', {'stats': arg}) as f:
+ return load_json(f)
def user(self, username):
- return load_json(self.call('user', {'user': username}))
+ with self.call('user', {'user': username}) as f:
+ return load_json(f)
def call(self, meth, args=None, query=None):
url = self.get_url(meth, args, query)
@@ -91,7 +98,8 @@ def get_index(self):
if self._api_index is None:
url = self.mirror.rstrip('/') + '/index.json'
try:
- self._api_index = load_json(get_file(url))
+ with get_file(url) as f:
+ self._api_index = load_json(f)
except ResourceNotFound:
raise NetworkError("API index not found at '%s'" % url)
@@ -10,6 +10,8 @@
# This file is part of the PGXN client
+from __future__ import with_statement
+
import os
import sys
import logging
@@ -379,7 +381,8 @@ def get_meta(self, spec):
raise PgxnClientException(
_("file 'META.json' not found in '%s'") % dir)
- return load_json(open(fn))
+ with open(fn) as f:
+ return load_json(f)
elif spec.is_file():
# Get the metadata from a zip file
@@ -6,9 +6,10 @@
# This file is part of the PGXN client
+from __future__ import with_statement
+
import os
import re
-import sys
import shutil
import difflib
import logging
@@ -50,9 +51,9 @@ def run(self):
raise PgxnClientException(
"sha1 missing from the distribution meta")
- fin = self.api.download(data['name'], SemVer(data['version']))
- fn = self._get_local_file_name(fin.url)
- fn = download(fin, fn, rename=True)
+ with self.api.download(data['name'], SemVer(data['version'])) as fin:
+ fn = self._get_local_file_name(fin.url)
+ fn = download(fin, fn, rename=True)
self.verify_checksum(fn, chk)
return fn
@@ -280,9 +281,9 @@ def load_sql(self, filename=None, data=None):
# load via pipe to enable psql commands in the file
if not data:
logger.debug("loading sql from %s", filename)
- fin = open(filename, 'r')
- p = self.popen(cmdline, stdin=fin)
- p.communicate()
+ with open(filename, 'r') as fin:
+ p = self.popen(cmdline, stdin=fin)
+ p.communicate()
else:
if len(data) > 105:
tdata = data[:100] + "..."
@@ -400,7 +401,7 @@ def _get_extensions(self):
# not mangled by the PGXN upload script yet.
name = dist['name']
for ext in self.opts.extensions:
- if ext <> name:
+ if ext != name:
raise PgxnClientException(
"can't find extension '%s' in the distribution '%s'"
% (name, spec))
View
@@ -9,6 +9,7 @@
import os
import urllib2
from itertools import count
+from contextlib import closing
from pgxnclient import __version__
from pgxnclient.i18n import _
@@ -22,7 +23,7 @@ def get_file(url):
opener.addheaders = [('User-agent', 'pgxnclient/%s' % __version__)]
logger.debug('opening url: %s', url)
try:
- return opener.open(url)
+ return closing(opener.open(url))
except urllib2.HTTPError, e:
if e.code == 404:
raise ResourceNotFound(_("resource not found: '%s'") % e.url)
@@ -22,6 +22,13 @@
import unittest
+# fix unittest maintainers stubborness: see Python issue #9424
+if unittest.TestCase.assert_ is not unittest.TestCase.assertTrue:
+ # Vaffanculo, Wolf
+ unittest.TestCase.assert_ = unittest.TestCase.assertTrue
+ unittest.TestCase.assertEquals = unittest.TestCase.assertEqual
+
+
if __name__ == '__main__':
unittest.main()
@@ -15,6 +15,13 @@ def __init__(self, *args):
self._f = open(*args)
self.url = None
+ def __enter__(self):
+ self._f.__enter__()
+ return self
+
+ def __exit__(self, type, value, traceback):
+ self._f.__exit__(type, value, traceback)
+
def __getattr__(self, attr):
return getattr(self._f, attr)

0 comments on commit 75d3ce6

Please sign in to comment.