From 76ca9f8145f367f83df19981da4dd934fdda471b Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sat, 18 Apr 2009 14:48:03 -0700 Subject: Make usage of open safer by setting binary mode and closing fds Signed-off-by: Shawn O. Pearce --- git_config.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'git_config.py') diff --git a/git_config.py b/git_config.py index 7aad80d2..7e642a4c 100644 --- a/git_config.py +++ b/git_config.py @@ -219,7 +219,11 @@ class GitConfig(object): return None try: Trace(': unpickle %s', self.file) - return cPickle.load(open(self._pickle, 'r')) + fd = open(self._pickle, 'rb') + try: + return cPickle.load(fd) + finally: + fd.close() except IOError: os.remove(self._pickle) return None @@ -229,9 +233,11 @@ class GitConfig(object): def _SavePickle(self, cache): try: - cPickle.dump(cache, - open(self._pickle, 'w'), - cPickle.HIGHEST_PROTOCOL) + fd = open(self._pickle, 'wb') + try: + cPickle.dump(cache, fd, cPickle.HIGHEST_PROTOCOL) + finally: + fd.close() except IOError: os.remove(self._pickle) except cPickle.PickleError: -- cgit v1.2.3-54-g00ecf