Skip to content

Commit d3dbe6c

Browse files
author
Martin Lambertsen
committed
Fix some resource leaks by open file handles
1 parent 27a283b commit d3dbe6c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

git/repo/base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import re
1010
import shlex
1111
import warnings
12+
13+
from pathlib import Path
14+
1215
from gitdb.db.loose import LooseObjectDB
1316

1417
from gitdb.exc import BadObject
@@ -268,7 +271,7 @@ def __init__(
268271
pass
269272

270273
try:
271-
common_dir = open(osp.join(self.git_dir, "commondir"), "rt").readlines()[0].strip()
274+
common_dir = (Path(self.git_dir) / "commondir").read_text().splitlines()[0].strip()
272275
self._common_dir = osp.join(self.git_dir, common_dir)
273276
except OSError:
274277
self._common_dir = ""
@@ -1385,4 +1388,6 @@ def currently_rebasing_on(self) -> Commit | None:
13851388
rebase_head_file = osp.join(self.git_dir, "REBASE_HEAD")
13861389
if not osp.isfile(rebase_head_file):
13871390
return None
1388-
return self.commit(open(rebase_head_file, "rt").readline().strip())
1391+
with open(rebase_head_file, "rt") as f:
1392+
content = f.readline().strip()
1393+
return self.commit(content)

git/repo/fun.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33
import os
44
import stat
5+
from pathlib import Path
56
from string import digits
67

78
from git.exc import WorkTreeRepositoryUnsupported
@@ -83,7 +84,7 @@ def find_worktree_git_dir(dotgit: "PathLike") -> Optional[str]:
8384
return None
8485

8586
try:
86-
lines = open(dotgit, "r").readlines()
87+
lines = Path(dotgit).read_text().split_lines()
8788
for key, value in [line.strip().split(": ") for line in lines]:
8889
if key == "gitdir":
8990
return value

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy