From 0127d51c137dbe8063b81012763550faf5111d87 Mon Sep 17 00:00:00 2001 From: Brad Bishop Date: Thu, 28 Jul 2016 13:52:29 -0400 Subject: propertycacher: context manager for file open Change-Id: Ib75f3f5ccbd35d7e70d9c171c0642077930c4741 Signed-off-by: Brad Bishop --- obmc/dbuslib/propertycacher.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'obmc/dbuslib/propertycacher.py') diff --git a/obmc/dbuslib/propertycacher.py b/obmc/dbuslib/propertycacher.py index ce83195..7888b46 100644 --- a/obmc/dbuslib/propertycacher.py +++ b/obmc/dbuslib/propertycacher.py @@ -34,16 +34,14 @@ def save(obj_path, iface_name, properties): try: if not os.path.exists(parent): os.makedirs(parent) - output = open(filename, 'wb') - try: - ## use json module to convert dbus datatypes - props = json.dumps(properties[iface_name]) - prop_obj = json.loads(props) - cPickle.dump(prop_obj, output) - except Exception as e: - print "ERROR: "+str(e) - finally: - output.close() + with open(filename, 'wb') as output: + try: + ## use json module to convert dbus datatypes + props = json.dumps(properties[iface_name]) + prop_obj = json.loads(props) + cPickle.dump(prop_obj, output) + except Exception as e: + print "ERROR: "+str(e) except: print "ERROR opening cache file: "+filename -- cgit v1.2.1