diff options
| author | Shawn O. Pearce <sop@google.com> | 2009-04-18 14:48:03 -0700 |
|---|---|---|
| committer | Shawn O. Pearce <sop@google.com> | 2009-04-18 14:48:03 -0700 |
| commit | 76ca9f8145f367f83df19981da4dd934fdda471b (patch) | |
| tree | d6c095928fd2b1131972206b3c3f498843a736a6 | |
| parent | accc56d82b902e7c7a22401db710958fcb1c7b58 (diff) | |
| download | git-repo-76ca9f8145f367f83df19981da4dd934fdda471b.tar.gz | |
Make usage of open safer by setting binary mode and closing fds
Signed-off-by: Shawn O. Pearce <sop@google.com>
| -rw-r--r-- | editor.py | 6 | ||||
| -rw-r--r-- | git_config.py | 14 | ||||
| -rw-r--r-- | git_refs.py | 4 | ||||
| -rw-r--r-- | project.py | 10 |
4 files changed, 23 insertions, 11 deletions
| @@ -78,7 +78,11 @@ least one of these before using this command.""" | |||
| 78 | 78 | ||
| 79 | if subprocess.Popen(editor + [path]).wait() != 0: | 79 | if subprocess.Popen(editor + [path]).wait() != 0: |
| 80 | raise EditorError() | 80 | raise EditorError() |
| 81 | return open(path).read() | 81 | fd = open(path) |
| 82 | try: | ||
| 83 | return read() | ||
| 84 | finally: | ||
| 85 | fd.close() | ||
| 82 | finally: | 86 | finally: |
| 83 | if fd: | 87 | if fd: |
| 84 | os.close(fd) | 88 | os.close(fd) |
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): | |||
| 219 | return None | 219 | return None |
| 220 | try: | 220 | try: |
| 221 | Trace(': unpickle %s', self.file) | 221 | Trace(': unpickle %s', self.file) |
| 222 | return cPickle.load(open(self._pickle, 'r')) | 222 | fd = open(self._pickle, 'rb') |
| 223 | try: | ||
| 224 | return cPickle.load(fd) | ||
| 225 | finally: | ||
| 226 | fd.close() | ||
| 223 | except IOError: | 227 | except IOError: |
| 224 | os.remove(self._pickle) | 228 | os.remove(self._pickle) |
| 225 | return None | 229 | return None |
| @@ -229,9 +233,11 @@ class GitConfig(object): | |||
| 229 | 233 | ||
| 230 | def _SavePickle(self, cache): | 234 | def _SavePickle(self, cache): |
| 231 | try: | 235 | try: |
| 232 | cPickle.dump(cache, | 236 | fd = open(self._pickle, 'wb') |
| 233 | open(self._pickle, 'w'), | 237 | try: |
| 234 | cPickle.HIGHEST_PROTOCOL) | 238 | cPickle.dump(cache, fd, cPickle.HIGHEST_PROTOCOL) |
| 239 | finally: | ||
| 240 | fd.close() | ||
| 235 | except IOError: | 241 | except IOError: |
| 236 | os.remove(self._pickle) | 242 | os.remove(self._pickle) |
| 237 | except cPickle.PickleError: | 243 | except cPickle.PickleError: |
diff --git a/git_refs.py b/git_refs.py index 24760918..ac8ed0c1 100644 --- a/git_refs.py +++ b/git_refs.py | |||
| @@ -101,7 +101,7 @@ class GitRefs(object): | |||
| 101 | def _ReadPackedRefs(self): | 101 | def _ReadPackedRefs(self): |
| 102 | path = os.path.join(self._gitdir, 'packed-refs') | 102 | path = os.path.join(self._gitdir, 'packed-refs') |
| 103 | try: | 103 | try: |
| 104 | fd = open(path, 'r') | 104 | fd = open(path, 'rb') |
| 105 | mtime = os.path.getmtime(path) | 105 | mtime = os.path.getmtime(path) |
| 106 | except IOError: | 106 | except IOError: |
| 107 | return | 107 | return |
| @@ -138,7 +138,7 @@ class GitRefs(object): | |||
| 138 | 138 | ||
| 139 | def _ReadLoose1(self, path, name): | 139 | def _ReadLoose1(self, path, name): |
| 140 | try: | 140 | try: |
| 141 | fd = open(path, 'r') | 141 | fd = open(path, 'rb') |
| 142 | mtime = os.path.getmtime(path) | 142 | mtime = os.path.getmtime(path) |
| 143 | except OSError: | 143 | except OSError: |
| 144 | return | 144 | return |
| @@ -1070,9 +1070,7 @@ class Project(object): | |||
| 1070 | rev = self.GetRemote(self.remote.name).ToLocal(self.revision) | 1070 | rev = self.GetRemote(self.remote.name).ToLocal(self.revision) |
| 1071 | rev = self.bare_git.rev_parse('%s^0' % rev) | 1071 | rev = self.bare_git.rev_parse('%s^0' % rev) |
| 1072 | 1072 | ||
| 1073 | f = open(os.path.join(dotgit, HEAD), 'wb') | 1073 | _lwrite(os.path.join(dotgit, HEAD), '%s\n' % rev) |
| 1074 | f.write("%s\n" % rev) | ||
| 1075 | f.close() | ||
| 1076 | 1074 | ||
| 1077 | cmd = ['read-tree', '--reset', '-u'] | 1075 | cmd = ['read-tree', '--reset', '-u'] |
| 1078 | cmd.append('-v') | 1076 | cmd.append('-v') |
| @@ -1167,7 +1165,11 @@ class Project(object): | |||
| 1167 | path = os.path.join(self._project.gitdir, HEAD) | 1165 | path = os.path.join(self._project.gitdir, HEAD) |
| 1168 | else: | 1166 | else: |
| 1169 | path = os.path.join(self._project.worktree, '.git', HEAD) | 1167 | path = os.path.join(self._project.worktree, '.git', HEAD) |
| 1170 | line = open(path, 'r').read() | 1168 | fd = open(path, 'rb') |
| 1169 | try: | ||
| 1170 | line = fd.read() | ||
| 1171 | finally: | ||
| 1172 | fd.close() | ||
| 1171 | if line.startswith('ref: '): | 1173 | if line.startswith('ref: '): |
| 1172 | return line[5:-1] | 1174 | return line[5:-1] |
| 1173 | return line[:-1] | 1175 | return line[:-1] |
