diff options
| author | Mike Frysinger <vapier@google.com> | 2019-11-11 05:40:22 -0500 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2019-11-12 03:44:39 +0000 |
| commit | 3164d40e2247d42537aef8e80fa7e048e14bec9f (patch) | |
| tree | 650cc33e3d5c4b39c3cc652e93495e47a170931b /project.py | |
| parent | f4545126197781beb03bb0fd47e7f24ce5af6ca8 (diff) | |
| download | git-repo-3164d40e2247d42537aef8e80fa7e048e14bec9f.tar.gz | |
use open context managers in more places
Use open() as a context manager to simplify the close logic and make
the code easier to read & understand. This is also more Pythonic.
Change-Id: I579d03cca86f99b2c6c6a1f557f6e5704e2515a7
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/244734
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'project.py')
| -rwxr-xr-x | project.py | 17 |
1 files changed, 4 insertions, 13 deletions
| @@ -58,11 +58,8 @@ else: | |||
| 58 | def _lwrite(path, content): | 58 | def _lwrite(path, content): |
| 59 | lock = '%s.lock' % path | 59 | lock = '%s.lock' % path |
| 60 | 60 | ||
| 61 | fd = open(lock, 'w') | 61 | with open(lock, 'w') as fd: |
| 62 | try: | ||
| 63 | fd.write(content) | 62 | fd.write(content) |
| 64 | finally: | ||
| 65 | fd.close() | ||
| 66 | 63 | ||
| 67 | try: | 64 | try: |
| 68 | platform_utils.rename(lock, path) | 65 | platform_utils.rename(lock, path) |
| @@ -1393,12 +1390,9 @@ class Project(object): | |||
| 1393 | if is_new: | 1390 | if is_new: |
| 1394 | alt = os.path.join(self.gitdir, 'objects/info/alternates') | 1391 | alt = os.path.join(self.gitdir, 'objects/info/alternates') |
| 1395 | try: | 1392 | try: |
| 1396 | fd = open(alt) | 1393 | with open(alt) as fd: |
| 1397 | try: | ||
| 1398 | # This works for both absolute and relative alternate directories. | 1394 | # This works for both absolute and relative alternate directories. |
| 1399 | alt_dir = os.path.join(self.objdir, 'objects', fd.readline().rstrip()) | 1395 | alt_dir = os.path.join(self.objdir, 'objects', fd.readline().rstrip()) |
| 1400 | finally: | ||
| 1401 | fd.close() | ||
| 1402 | except IOError: | 1396 | except IOError: |
| 1403 | alt_dir = None | 1397 | alt_dir = None |
| 1404 | else: | 1398 | else: |
| @@ -2893,14 +2887,11 @@ class Project(object): | |||
| 2893 | else: | 2887 | else: |
| 2894 | path = os.path.join(self._project.worktree, '.git', HEAD) | 2888 | path = os.path.join(self._project.worktree, '.git', HEAD) |
| 2895 | try: | 2889 | try: |
| 2896 | fd = open(path) | 2890 | with open(path) as fd: |
| 2891 | line = fd.readline() | ||
| 2897 | except IOError as e: | 2892 | except IOError as e: |
| 2898 | raise NoManifestException(path, str(e)) | 2893 | raise NoManifestException(path, str(e)) |
| 2899 | try: | 2894 | try: |
| 2900 | line = fd.readline() | ||
| 2901 | finally: | ||
| 2902 | fd.close() | ||
| 2903 | try: | ||
| 2904 | line = line.decode() | 2895 | line = line.decode() |
| 2905 | except AttributeError: | 2896 | except AttributeError: |
| 2906 | pass | 2897 | pass |
