summaryrefslogtreecommitdiffstats
path: root/base/kra/functional
diff options
context:
space:
mode:
authorAbhishek Koneru <akoneru@redhat.com>2013-05-20 16:07:21 -0400
committerAbhishek Koneru <akoneru@redhat.com>2013-06-06 11:37:10 -0400
commite21bed6f6d049e776eded9016aed635945350ce6 (patch)
tree5fc2bf566e02a7867cc70a4cca605b753de39ea3 /base/kra/functional
parentd81c8e94c36a9a32b9233754f11216e97bac9a54 (diff)
downloadpki-e21bed6f6d049e776eded9016aed635945350ce6.tar.gz
pki-e21bed6f6d049e776eded9016aed635945350ce6.tar.xz
pki-e21bed6f6d049e776eded9016aed635945350ce6.zip
Use 'with' construct for file operations.
Replace try-except with with construct in python code in applicable places where there is no exception handling required. Also added finally block to close resources opened in a try except block. Ticket #560
Diffstat (limited to 'base/kra/functional')
-rw-r--r--base/kra/functional/drmclient.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/base/kra/functional/drmclient.py b/base/kra/functional/drmclient.py
index ea9514dce..5dd2336ea 100644
--- a/base/kra/functional/drmclient.py
+++ b/base/kra/functional/drmclient.py
@@ -318,7 +318,8 @@ def https_request(host, port, url, secdir, password, nickname, operation, args,
elif kw != None:
post = urlencode(kw)
request_headers = {"Content-type": "application/x-www-form-urlencoded",
- "Accept": "text/plain"}
+ "Accept": "text/plain"}
+ conn = None
try:
conn = nsslib.NSSConnection(host, port, dbdir=secdir)
conn.set_debuglevel(0)
@@ -338,9 +339,11 @@ def https_request(host, port, url, secdir, password, nickname, operation, args,
http_reason_phrase = unicode(res.reason, 'utf-8')
http_headers = res.msg.dict
http_body = res.read()
- conn.close()
except Exception, e:
raise NetworkError(uri=uri, error=str(e))
+ finally:
+ if conn is not None:
+ conn.close()
return http_status, http_reason_phrase, http_headers, http_body
@@ -380,9 +383,11 @@ def http_request(host, port, url, operation, args):
http_reason_phrase = unicode(res.reason, 'utf-8')
http_headers = res.msg.dict
http_body = res.read()
- conn.close()
except NSPRError, e:
raise NetworkError(uri=uri, error=str(e))
+ finally:
+ if conn is not None:
+ conn.close()
logging.debug('request status %d', http_status)
logging.debug('request reason_phrase %r', http_reason_phrase)
@@ -411,9 +416,8 @@ class kra:
self.transport_cert_nickname = kra_nickname
self.mechanism = nss.CKM_DES3_CBC_PAD
try:
- f = open(self.pwd_file, "r")
- self.password = f.readline().strip()
- f.close()
+ with open(self.pwd_file, "r") as f:
+ self.password = f.readline().strip()
except IOError:
self.password = ''